Slight cleanup in easymotion

This commit is contained in:
Jason Fields 2020-07-05 15:42:01 -04:00
parent 2a06c7c0cb
commit ca753611fa
3 changed files with 25 additions and 54 deletions

View File

@ -12,6 +12,7 @@ import {
} from './types';
import { globalState } from '../../../state/globalState';
import { TextEditor } from '../../../textEditor';
import { MarkerGenerator } from './markerGenerator';
export interface EasymotionTrigger {
key: string;
@ -47,7 +48,7 @@ abstract class BaseEasyMotionCommand extends BaseCommand {
vimState.easyMotion.clearMarkers();
let index = 0;
const markerGenerator = EasyMotion.createMarkerGenerator(matches.length);
const markerGenerator = new MarkerGenerator(matches.length);
for (const match of matches) {
const matchPosition = this.resolveMatchPosition(match);
// Skip if the match position equals to cursor position

View File

@ -58,10 +58,6 @@ export class EasyMotion {
this.decorations = [];
}
public static createMarkerGenerator(matchesCount: number): MarkerGenerator {
return new MarkerGenerator(matchesCount);
}
/**
* Create and cache decoration types for different marker lengths
*/
@ -326,10 +322,10 @@ export class EasyMotion {
dimmingZones.push(new vscode.Range(0, 0, pos.line, dimPos));
} else {
const prevDimPos = dimmingZones[dimmingZones.length - 1];
const ln = prevDimPos.end.line;
const ps = prevDimPos.end.character;
dimmingZones.push(new vscode.Range(ln, ps, pos.line, dimPos));
dimmingZones.push(
new vscode.Range(prevDimPos.end.line, prevDimPos.end.character, pos.line, dimPos)
);
}
}
@ -369,49 +365,20 @@ export class EasyMotion {
}
export namespace EasyMotion {
export class Marker {
private _name: string;
private _position: Position;
constructor(name: string, position: Position) {
this._name = name;
this._position = position;
}
public get name(): string {
return this._name;
}
public get position(): Position {
return this._position;
}
export interface Marker {
name: string;
position: Position;
}
export class Match {
private _position: Position;
private _text: string;
private _index: number;
public position: Position;
public readonly text: string;
public readonly index: number;
constructor(position: Position, text: string, index: number) {
this._position = position;
this._text = text;
this._index = index;
}
public get position(): Position {
return this._position;
}
public get text(): string {
return this._text;
}
public get index(): number {
return this._index;
}
public set position(position: Position) {
this._position = position;
this.position = position;
this.text = text;
this.index = index;
}
public toRange(): vscode.Range {

View File

@ -14,8 +14,7 @@ export class MarkerGenerator {
}
public generateMarker(index: number, markerPosition: Position): EasyMotion.Marker | null {
const keyTable = this.keyTable;
const prefixKeyTable = this.prefixKeyTable;
const { keyTable, prefixKeyTable } = this;
if (index >= keyTable.length - prefixKeyTable.length) {
const remainder = index - (keyTable.length - prefixKeyTable.length);
@ -25,18 +24,22 @@ export class MarkerGenerator {
} else {
const prefix = prefixKeyTable[currentStep - 1];
const label = keyTable[remainder % keyTable.length];
return new EasyMotion.Marker(prefix + label, markerPosition);
return {
name: prefix + label,
position: markerPosition,
};
}
} else {
const label = keyTable[index];
return new EasyMotion.Marker(label, markerPosition);
return {
name: keyTable[index],
position: markerPosition,
};
}
}
private createPrefixKeyTable(): string[] {
const keyTable = this.keyTable;
const totalRemainder = Math.max(this.matchesCount - keyTable.length, 0);
const totalSteps = Math.ceil(totalRemainder / keyTable.length);
const totalRemainder = Math.max(this.matchesCount - this.keyTable.length, 0);
const totalSteps = Math.ceil(totalRemainder / this.keyTable.length);
const reversed = this.keyTable.slice().reverse();
const count = Math.min(totalSteps, reversed.length);
return reversed.slice(0, count);