Skip to content

Commit 1cbc960

Browse files
committed
Add VimState.cursor, equivalent to VimState.cursors[0]
1 parent cc316a1 commit 1cbc960

File tree

11 files changed

+36
-21
lines changed

11 files changed

+36
-21
lines changed

src/actions/commands/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class CommandEsc extends BaseCommand {
159159
vimState.surround = undefined;
160160

161161
if (vimState.isMultiCursor) {
162-
vimState.cursors = [vimState.cursors[0]];
162+
vimState.cursors = [vimState.cursor];
163163
} else {
164164
// If there's nothing to do on the vim side, we might as well call some
165165
// of vscode's default "close notification" actions. I think we should

src/actions/commands/insert.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ class InsertAbove extends BaseCommand {
134134
}
135135

136136
vimState.cursors = getCursorsAfterSync(vimState.editor);
137-
const endPos = vimState.cursors[0].start.character;
137+
const endPos = vimState.cursor.start.character;
138138
const indentAmt = charPos - endPos;
139139

140140
for (let i = 0; i < count; i++) {
141-
const newPos = new Position(vimState.cursors[0].start.line + i, charPos);
141+
const newPos = new Position(vimState.cursor.start.line + i, charPos);
142142
if (i === 0) {
143-
vimState.cursors[0] = Cursor.atPosition(newPos);
143+
vimState.cursor = Cursor.atPosition(newPos);
144144
} else {
145145
vimState.cursors.push(Cursor.atPosition(newPos));
146146
}
@@ -286,7 +286,7 @@ export class ExitInsertMode extends BaseCommand {
286286
vimState.historyTracker.currentContentChanges = [];
287287

288288
if (vimState.isFakeMultiCursor) {
289-
vimState.cursors = [vimState.cursors[0]];
289+
vimState.cursors = [vimState.cursor];
290290
vimState.isFakeMultiCursor = false;
291291
}
292292
}

src/actions/commands/put.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ abstract class BasePutCommand extends BaseCommand {
5050
vimState.currentMode === Mode.CommandlineInProgress ? Mode.Normal : vimState.currentMode;
5151
const registerMode = this.getRegisterMode(register);
5252

53-
const replaceRange = this.getReplaceRange(mode, vimState.cursors[0], registerMode);
53+
const replaceRange = this.getReplaceRange(mode, vimState.cursor, registerMode);
5454

5555
let text = this.getRegisterText(mode, register, count);
5656
if (this.shouldAdjustIndent(mode, registerMode)) {

src/cmd_line/commands/copy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class CopyCommand extends ExCommand {
6161
}
6262

6363
public async execute(vimState: VimState): Promise<void> {
64-
const line = vimState.cursors[0].stop.line;
64+
const line = vimState.cursor.stop.line;
6565
this.copyLines(vimState, line, line);
6666
}
6767

src/cmd_line/commands/fileInfo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ export class FileInfoCommand extends ExCommand {
2121

2222
async execute(vimState: VimState): Promise<void> {
2323
// TODO: Use `this.args`
24-
reportFileInfo(vimState.cursors[0].start, vimState);
24+
reportFileInfo(vimState.cursor.start, vimState);
2525
}
2626
}

src/cmd_line/commands/move.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class MoveCommand extends ExCommand {
9191
}
9292

9393
public async execute(vimState: VimState): Promise<void> {
94-
const line = vimState.cursors[0].stop.line;
94+
const line = vimState.cursor.stop.line;
9595
this.moveLines(vimState, line, line);
9696
}
9797

src/mode/modeHandler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export class ModeHandler implements vscode.Disposable, IModeHandler {
247247
// like 'editor.action.smartSelect.grow' are handled.
248248
if (this.vimState.currentMode === Mode.Visual) {
249249
Logger.trace('Updating Visual Selection!');
250-
this.vimState.cursors[0] = Cursor.fromSelection(selection);
250+
this.vimState.cursor = Cursor.fromSelection(selection);
251251
this.updateView({ drawSelection: false, revealRange: false });
252252

253253
// Store selection for commands like gv
@@ -259,7 +259,7 @@ export class ModeHandler implements vscode.Disposable, IModeHandler {
259259
return;
260260
} else if (!selection.active.isEqual(selection.anchor)) {
261261
Logger.trace('Creating Visual Selection from command!');
262-
this.vimState.cursors[0] = Cursor.fromSelection(selection);
262+
this.vimState.cursor = Cursor.fromSelection(selection);
263263
await this.setCurrentMode(Mode.Visual);
264264
this.updateView({ drawSelection: false, revealRange: false });
265265

@@ -322,7 +322,7 @@ export class ModeHandler implements vscode.Disposable, IModeHandler {
322322
selection.active
323323
}`,
324324
);
325-
this.vimState.cursors[0] = Cursor.fromSelection(selection);
325+
this.vimState.cursor = Cursor.fromSelection(selection);
326326
this.vimState.desiredColumn = selection.active.character;
327327
this.updateView({ drawSelection: false, revealRange: false });
328328
}
@@ -1434,7 +1434,7 @@ export class ModeHandler implements vscode.Disposable, IModeHandler {
14341434
if (isLastCursorTracked) {
14351435
cursorToTrack = this.vimState.cursors.at(-1)!;
14361436
} else {
1437-
cursorToTrack = this.vimState.cursors[0];
1437+
cursorToTrack = this.vimState.cursor;
14381438
}
14391439

14401440
const isCursorAboveRange = (visibleRange: vscode.Range): boolean =>

src/state/vimState.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,33 +125,48 @@ export class VimState implements vscode.Disposable {
125125
public postponedCodeViewChanges: ViewChange[] = [];
126126

127127
/**
128-
* The cursor position (start, stop) when this action finishes.
128+
* @deprecated Use cursor.start instead
129129
*/
130130
public get cursorStartPosition(): Position {
131-
return this.cursors[0].start;
131+
return this.cursor.start;
132132
}
133133
public set cursorStartPosition(value: Position) {
134134
if (!value.isValid(this.editor)) {
135135
Logger.warn(`invalid cursor start position. ${value.toString()}.`);
136136
}
137-
this.cursors[0] = this.cursors[0].withNewStart(value);
137+
this.cursor = this.cursor.withNewStart(value);
138138
}
139139

140+
/**
141+
* @deprecated Use cursor.stop instead
142+
*/
140143
public get cursorStopPosition(): Position {
141-
return this.cursors[0].stop;
144+
return this.cursor.stop;
142145
}
143146
public set cursorStopPosition(value: Position) {
144147
if (!value.isValid(this.editor)) {
145148
Logger.warn(`invalid cursor stop position. ${value.toString()}.`);
146149
}
147-
this.cursors[0] = this.cursors[0].withNewStop(value);
150+
this.cursor = this.cursor.withNewStop(value);
148151
}
149152

150153
/**
151154
* The position of every cursor. Will never be empty.
152155
*/
153156
private _cursors: Cursor[] = [Cursor.atPosition(new Position(0, 0))];
154157

158+
/**
159+
* The 'primary' or 'active' cursor.
160+
* What this means is somewhat context dependent, but generally it's safe to
161+
* use this when you only care about a single cursor.
162+
*/
163+
public get cursor(): Cursor {
164+
return this._cursors[0];
165+
}
166+
public set cursor(value: Cursor) {
167+
this._cursors[0] = value;
168+
}
169+
155170
public get cursors(): Cursor[] {
156171
return this._cursors;
157172
}

src/textEditor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export class TextEditor {
213213
): Iterable<{ line: string; start: Position; end: Position }> {
214214
const { reverse } = options;
215215

216-
cursor ??= vimState.cursors[0];
216+
cursor ??= vimState.cursor;
217217

218218
const topLeft = visualBlockGetTopLeftPosition(cursor.start, cursor.stop);
219219
const bottomRight = visualBlockGetBottomRightPosition(cursor.start, cursor.stop);

src/transformations/execute.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export async function executeTransformations(
153153
switch (transformation.type) {
154154
case 'insertTextVSCode':
155155
await TextEditor.insert(vimState.editor, transformation.text);
156-
vimState.cursors[0] = Cursor.fromSelection(vimState.editor.selection);
156+
vimState.cursor = Cursor.fromSelection(vimState.editor.selection);
157157
break;
158158

159159
case 'replayRecordedState':

0 commit comments

Comments
 (0)