From fe707b41f0a8ff33d9cad215cadc21f4cc3cae0c Mon Sep 17 00:00:00 2001 From: Jason Poon Date: Fri, 1 Feb 2019 20:38:05 -0800 Subject: [PATCH 1/3] fix: ensure cursor is in bounds. closes #3441 --- src/common/motion/range.ts | 4 ++++ src/mode/modeHandler.ts | 38 ++++++++++++++++++++++++-------------- src/state/vimState.ts | 11 +++++------ 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/common/motion/range.ts b/src/common/motion/range.ts index adf239d4fa2..a5f60ff3244 100644 --- a/src/common/motion/range.ts +++ b/src/common/motion/range.ts @@ -20,6 +20,10 @@ export class Range { this._stop = stop; } + public isValid(textEditor: vscode.TextEditor) { + return this._start.isValid(textEditor) && this._stop.isValid(textEditor); + } + /** * Create a range from a VSCode selection. */ diff --git a/src/mode/modeHandler.ts b/src/mode/modeHandler.ts index c67520362bf..3a536fae41d 100644 --- a/src/mode/modeHandler.ts +++ b/src/mode/modeHandler.ts @@ -606,25 +606,35 @@ export class ModeHandler implements vscode.Disposable { // Ensure cursor is within bounds if (!vimState.editor.document.isClosed && vimState.editor === vscode.window.activeTextEditor) { - for (const { stop, i } of Range.IterateRanges(vimState.allCursors)) { - if (stop.line >= TextEditor.getLineCount()) { - vimState.allCursors[i] = vimState.allCursors[i].withNewStop( - vimState.cursorPosition.getDocumentEnd(vimState.editor) - ); + const cursors = new Array(); + for (let { range } of Range.IterateRanges(vimState.allCursors)) { + // adjust start/stop + const documentEndPosition = vimState.cursorPosition.getDocumentEnd(vimState.editor); + const documentLineCount = TextEditor.getLineCount(vimState.editor); + if (range.start.line >= documentLineCount) { + range = range.withNewStart(documentEndPosition); + } + if (range.stop.line >= documentLineCount) { + range = range.withNewStop(documentEndPosition); } - const currentLineLength = TextEditor.getLineAt(stop).text.length; + // adjust column + if (vimState.currentMode === ModeName.Normal) { + const currentLineLength = TextEditor.getLineAt(range.stop).text.length; + if (currentLineLength > 0) { + const lineEndPosition = range.start.getLineEnd().getLeftThroughLineBreaks(true); + if (range.start.character >= currentLineLength) { + range = range.withNewStart(lineEndPosition); + } - if ( - vimState.currentMode === ModeName.Normal && - stop.character >= currentLineLength && - currentLineLength > 0 - ) { - vimState.allCursors[i] = vimState.allCursors[i].withNewStop( - stop.getLineEnd().getLeftThroughLineBreaks(true) - ); + if (range.stop.character >= currentLineLength) { + range = range.withNewStop(lineEndPosition); + } + } } + cursors.push(range); } + vimState.allCursors = cursors; } // Update the current history step to have the latest cursor position diff --git a/src/state/vimState.ts b/src/state/vimState.ts index 09468361728..0cc3a8246fc 100644 --- a/src/state/vimState.ts +++ b/src/state/vimState.ts @@ -150,15 +150,14 @@ export class VimState implements vscode.Disposable { return this._allCursors; } - public set allCursors(value: Range[]) { - for (const cursor of value) { - if (!cursor.start.isValid(this.editor) || !cursor.stop.isValid(this.editor)) { - this.logger.debug('invalid value for set cursor position.'); + public set allCursors(cursors: Range[]) { + for (const cursor of cursors) { + if (!cursor.isValid(this.editor)) { + this.logger.warn(`invalid cursor position. ${cursor.toString()}.`); } } - this._allCursors = value; - + this._allCursors = cursors; this.isMultiCursor = this._allCursors.length > 1; } From 4d63c47f1011c94abb66c9821aa97bf73f2518fe Mon Sep 17 00:00:00 2001 From: Jason Poon Date: Fri, 1 Feb 2019 21:01:19 -0800 Subject: [PATCH 2/3] refactor: rename the cursor properties to something more sensible --- extension.ts | 2 +- src/actions/commands/actions.ts | 214 +++++++++--------- src/actions/commands/insert.ts | 42 ++-- src/actions/motion.ts | 2 +- src/actions/operator.ts | 44 ++-- .../plugins/easymotion/easymotion.cmd.ts | 8 +- src/actions/plugins/surround.ts | 20 +- src/actions/textobject.ts | 38 ++-- src/cmd_line/commands/substitute.ts | 2 +- src/cmd_line/node.ts | 4 +- src/common/matching/matcher.ts | 2 +- src/common/motion/position.ts | 2 +- src/configuration/remapper.ts | 4 +- src/jumps/jump.ts | 2 +- src/jumps/jumpTracker.ts | 4 +- src/mode/modeHandler.ts | 120 +++++----- src/neovim/neovim.ts | 6 +- src/register/register.ts | 10 +- src/state/vimState.ts | 27 ++- test/completion/lineCompletion.test.ts | 8 +- test/multicursor.test.ts | 4 +- 21 files changed, 282 insertions(+), 283 deletions(-) diff --git a/extension.ts b/extension.ts index ef7857bbb1b..78544f627c6 100644 --- a/extension.ts +++ b/extension.ts @@ -289,7 +289,7 @@ export async function activate(context: vscode.ExtensionContext) { text: args.text, replaceCharCnt: args.replaceCharCnt, }); - mh.vimState.cursorPosition = Position.FromVSCodePosition( + mh.vimState.cursorStopPosition = Position.FromVSCodePosition( mh.vimState.editor.selection.start ); mh.vimState.cursorStartPosition = Position.FromVSCodePosition( diff --git a/src/actions/commands/actions.ts b/src/actions/commands/actions.ts index 566cdaf1bce..368f3c627c0 100644 --- a/src/actions/commands/actions.ts +++ b/src/actions/commands/actions.ts @@ -137,7 +137,7 @@ export class DocumentContentChangeAction extends BaseAction { vimState.cursorStartPosition = Position.FromVSCodePosition(newStart) .advancePositionByText(last.textDiff.text) .add(last.positionDiff); - vimState.cursorPosition = Position.FromVSCodePosition(newEnd) + vimState.cursorStopPosition = Position.FromVSCodePosition(newEnd) .advancePositionByText(last.textDiff.text) .add(last.positionDiff); } @@ -210,7 +210,7 @@ export abstract class BaseCommand extends BaseAction { let resultingCursors: Range[] = []; - const cursorsToIterateOver = vimState.allCursors + const cursorsToIterateOver = vimState.cursors .map(x => new Range(x.start, x.stop)) .sort((a, b) => a.start.line > b.start.line || @@ -223,14 +223,14 @@ export abstract class BaseCommand extends BaseAction { for (const { start, stop } of cursorsToIterateOver) { this.multicursorIndex = cursorIndex++; - vimState.cursorPosition = stop; + vimState.cursorStopPosition = stop; vimState.cursorStartPosition = start; for (let j = 0; j < timesToRepeat; j++) { vimState = await this.exec(stop, vimState); } - resultingCursors.push(new Range(vimState.cursorStartPosition, vimState.cursorPosition)); + resultingCursors.push(new Range(vimState.cursorStartPosition, vimState.cursorStopPosition)); for (const transformation of vimState.recordedState.transformations) { if (isTextTransformation(transformation) && transformation.cursorIndex === undefined) { @@ -239,7 +239,7 @@ export abstract class BaseCommand extends BaseAction { } } - vimState.allCursors = resultingCursors; + vimState.cursors = resultingCursors; return vimState; } @@ -533,12 +533,12 @@ class CommandEsc extends BaseCommand { // as that is handled for you by the state machine. ESC is // a special case since runsOnceForEveryCursor is false. - vimState.allCursors = vimState.allCursors.map(x => x.withNewStop(x.stop.getLeft())); + vimState.cursors = vimState.cursors.map(x => x.withNewStop(x.stop.getLeft())); } if (vimState.currentMode === ModeName.SearchInProgressMode) { if (vimState.globalState.searchState) { - vimState.cursorPosition = vimState.globalState.searchState.searchCursorStartPosition; + vimState.cursorStopPosition = vimState.globalState.searchState.searchCursorStartPosition; } } @@ -560,7 +560,7 @@ class CommandEsc extends BaseCommand { await vimState.setCurrentMode(ModeName.Normal); if (!vimState.isMultiCursor) { - vimState.allCursors = [vimState.allCursors[0]]; + vimState.cursors = [vimState.cursors[0]]; } return vimState; @@ -670,7 +670,7 @@ class CommandMoveHalfPageDown extends CommandEditorScroll { let editor = vscode.window.activeTextEditor!; let startColumn = vimState.cursorStartPosition.character; let firstLine = editor.visibleRanges[0].start.line; - let currentSelectionLine = vimState.cursorPosition.line; + let currentSelectionLine = vimState.cursorStopPosition.line; lineOffset = currentSelectionLine - firstLine; let timesToRepeat = vimState.recordedState.count || 1; @@ -698,7 +698,7 @@ class CommandMoveHalfPageDown extends CommandEditorScroll { } if (newPosition.isValid()) { - vimState.cursorPosition = newPosition; + vimState.cursorStopPosition = newPosition; } return vimState; @@ -717,7 +717,7 @@ class CommandMoveHalfPageUp extends CommandEditorScroll { let startColumn = vimState.cursorStartPosition.character; let firstLine = editor.visibleRanges[0].start.line; - let currentSelectionLine = vimState.cursorPosition.line; + let currentSelectionLine = vimState.cursorStopPosition.line; lineOffset = currentSelectionLine - firstLine; let timesToRepeat = vimState.recordedState.count || 1; @@ -734,7 +734,7 @@ class CommandMoveHalfPageUp extends CommandEditorScroll { let newFirstLine = editor.visibleRanges[0].start.line; let newPosition = new Position(newFirstLine + lineOffset, startColumn); - vimState.cursorPosition = newPosition; + vimState.cursorStopPosition = newPosition; return vimState; } } @@ -812,7 +812,7 @@ class CommandReplaceInReplaceMode extends BaseCommand { // If you backspace before the beginning of where you started to replace, // just move the cursor back. - vimState.cursorPosition = position.getLeft(); + vimState.cursorStopPosition = position.getLeft(); vimState.cursorStartPosition = position.getLeft(); } else if ( position.line > replaceState.replaceCursorStartPosition.line || @@ -909,7 +909,7 @@ class CommandInsertInSearchMode extends BaseCommand { vimState.globalState.searchStateIndex = vimState.globalState.searchStatePrevious.length - 1; // Move cursor to next match - vimState.cursorPosition = searchState.getNextSearchMatchPosition(vimState.cursorPosition).pos; + vimState.cursorStopPosition = searchState.getNextSearchMatchPosition(vimState.cursorStopPosition).pos; return vimState; } else if (key === '') { @@ -1022,7 +1022,7 @@ class CommandOverrideCopy extends BaseCommand { let text = ''; if (vimState.currentMode === ModeName.Visual || vimState.currentMode === ModeName.Normal) { - text = vimState.allCursors + text = vimState.cursors .map(range => { const start = Position.EarlierOf(range.start, range.stop); const stop = Position.LaterOf(range.start, range.stop); @@ -1030,7 +1030,7 @@ class CommandOverrideCopy extends BaseCommand { }) .join('\n'); } else if (vimState.currentMode === ModeName.VisualLine) { - text = vimState.allCursors + text = vimState.cursors .map(range => { return vimState.editor.document.getText( new vscode.Range( @@ -1067,7 +1067,7 @@ class CommandCmdA extends BaseCommand { public async exec(position: Position, vimState: VimState): Promise { vimState.cursorStartPosition = new Position(0, vimState.desiredColumn); - vimState.cursorPosition = new Position(TextEditor.getLineCount() - 1, vimState.desiredColumn); + vimState.cursorStopPosition = new Position(TextEditor.getLineCount() - 1, vimState.desiredColumn); await vimState.setCurrentMode(ModeName.VisualLine); return vimState; @@ -1087,8 +1087,8 @@ function searchCurrentWord( // outside of the currently selected word. const searchStartCursorPosition = direction === SearchDirection.Backward - ? vimState.cursorPosition.getWordLeft(true) - : vimState.cursorPosition; + ? vimState.cursorStopPosition.getWordLeft(true) + : vimState.cursorStopPosition; return createSearchStateAndMoveToMatch({ needle: currentWord, @@ -1142,13 +1142,13 @@ function createSearchStateAndMoveToMatch(args: { // Start a search for the given term. vimState.globalState.searchState = new SearchState( args.direction, - vimState.cursorPosition, + vimState.cursorStopPosition, searchString, { isRegex: isExact }, vimState.currentMode ); - vimState.cursorPosition = vimState.globalState.searchState.getNextSearchMatchPosition( + vimState.cursorStopPosition = vimState.globalState.searchState.getNextSearchMatchPosition( args.searchStartCursorPosition ).pos; @@ -1254,7 +1254,7 @@ export class CommandSearchForwards extends BaseCommand { public async exec(position: Position, vimState: VimState): Promise { vimState.globalState.searchState = new SearchState( SearchDirection.Forward, - vimState.cursorPosition, + vimState.cursorStopPosition, '', { isRegex: true }, vimState.currentMode @@ -1280,7 +1280,7 @@ export class CommandSearchBackwards extends BaseCommand { public async exec(position: Position, vimState: VimState): Promise { vimState.globalState.searchState = new SearchState( SearchDirection.Backward, - vimState.cursorPosition, + vimState.cursorStopPosition, '', { isRegex: true }, vimState.currentMode @@ -1376,7 +1376,7 @@ export class PutCommand extends BaseCommand { let diff = new PositionDiff(0, 0); const noPrevLine = vimState.cursorStartPosition.isAtDocumentBegin(); - const noNextLine = vimState.cursorPosition.isAtDocumentEnd(); + const noNextLine = vimState.cursorStopPosition.isAtDocumentEnd(); if (register.registerMode === RegisterMode.CharacterWise) { textToAdd = text; @@ -1662,7 +1662,7 @@ export class PutCommandVisual extends BaseCommand { after: boolean = false ): Promise { let start = vimState.cursorStartPosition; - let end = vimState.cursorPosition; + let end = vimState.cursorStopPosition; const isLineWise = vimState.currentMode === ModeName.VisualLine; if (start.isAfter(end)) { [start, end] = [end, start]; @@ -1788,7 +1788,7 @@ export class PutBeforeWithIndentCommand extends BaseCommand { const result = await new PutCommand().exec(position, vimState, true, true); if (vimState.effectiveRegisterMode === RegisterMode.LineWise) { - result.cursorPosition = result.cursorPosition + result.cursorStopPosition = result.cursorStopPosition .getPreviousLineBegin() .getFirstLineNonBlankChar(); } @@ -2031,7 +2031,7 @@ class CommandCloseFold extends CommandFold { public async exec(position: Position, vimState: VimState): Promise { let timesToRepeat = vimState.recordedState.count || 1; await vscode.commands.executeCommand('editor.fold', { levels: timesToRepeat, direction: 'up' }); - vimState.allCursors = await getCursorsAfterSync(); + vimState.cursors = await getCursorsAfterSync(); return vimState; } } @@ -2094,7 +2094,7 @@ class CommandCenterScroll extends BaseCommand { public async exec(position: Position, vimState: VimState): Promise { // In these modes you want to center on the cursor position vimState.editor.revealRange( - new vscode.Range(vimState.cursorPosition, vimState.cursorPosition), + new vscode.Range(vimState.cursorStopPosition, vimState.cursorStopPosition), vscode.TextEditorRevealType.InCenter ); @@ -2118,12 +2118,12 @@ class CommandCenterScrollFirstChar extends BaseCommand { // In these modes you want to center on the cursor position // This particular one moves cursor to first non blank char though vimState.editor.revealRange( - new vscode.Range(vimState.cursorPosition, vimState.cursorPosition), + new vscode.Range(vimState.cursorStopPosition, vimState.cursorStopPosition), vscode.TextEditorRevealType.InCenter ); // Move cursor to first char of line - vimState.cursorPosition = vimState.cursorPosition.getFirstLineNonBlankChar(); + vimState.cursorStopPosition = vimState.cursorStopPosition.getFirstLineNonBlankChar(); return vimState; } @@ -2177,7 +2177,7 @@ class CommandTopScrollFirstChar extends BaseCommand { }); // Move cursor to first char of line - vimState.cursorPosition = vimState.cursorPosition.getFirstLineNonBlankChar(); + vimState.cursorStopPosition = vimState.cursorStopPosition.getFirstLineNonBlankChar(); return vimState; } @@ -2231,7 +2231,7 @@ class CommandBottomScrollFirstChar extends BaseCommand { }); // Move cursor to first char of line - vimState.cursorPosition = vimState.cursorPosition.getFirstLineNonBlankChar(); + vimState.cursorStopPosition = vimState.cursorStopPosition.getFirstLineNonBlankChar(); return vimState; } @@ -2244,8 +2244,8 @@ class CommandGoToOtherEndOfHighlightedText extends BaseCommand { isJump = true; public async exec(position: Position, vimState: VimState): Promise { - [vimState.cursorStartPosition, vimState.cursorPosition] = [ - vimState.cursorPosition, + [vimState.cursorStartPosition, vimState.cursorStopPosition] = [ + vimState.cursorStopPosition, vimState.cursorStartPosition, ]; @@ -2266,7 +2266,7 @@ class CommandUndo extends BaseCommand { const newPositions = await vimState.historyTracker.goBackHistoryStep(); if (newPositions !== undefined) { - vimState.allCursors = newPositions.map(x => new Range(x, x)); + vimState.cursors = newPositions.map(x => new Range(x, x)); } vimState.alteredHistory = true; @@ -2290,7 +2290,7 @@ class CommandUndoOnLine extends BaseCommand { const newPositions = await vimState.historyTracker.goBackHistoryStepsOnLine(); if (newPositions !== undefined) { - vimState.allCursors = newPositions.map(x => new Range(x, x)); + vimState.cursors = newPositions.map(x => new Range(x, x)); } vimState.alteredHistory = true; @@ -2310,7 +2310,7 @@ class CommandRedo extends BaseCommand { const newPositions = await vimState.historyTracker.goForwardHistoryStep(); if (newPositions !== undefined) { - vimState.allCursors = newPositions.map(x => new Range(x, x)); + vimState.cursors = newPositions.map(x => new Range(x, x)); } vimState.alteredHistory = true; @@ -2441,7 +2441,7 @@ class CommandReselectVisual extends BaseCommand { if (vimState.lastVisualSelectionEnd.line <= TextEditor.getLineCount() - 1) { await vimState.setCurrentMode(vimState.lastVisualMode); vimState.cursorStartPosition = vimState.lastVisualSelectionStart; - vimState.cursorPosition = vimState.lastVisualSelectionEnd.getLeft(); + vimState.cursorStopPosition = vimState.lastVisualSelectionEnd.getLeft(); } } return vimState; @@ -2456,45 +2456,45 @@ async function selectLastSearchWord(vimState: VimState, direction: SearchDirecti const newSearchState = new SearchState( direction, - vimState.cursorPosition, + vimState.cursorStopPosition, searchState!.searchString, { isRegex: true }, vimState.currentMode ); let result = { - start: vimState.cursorPosition, - end: vimState.cursorPosition, + start: vimState.cursorStopPosition, + end: vimState.cursorStopPosition, match: false, }; // At first, try to search for current word, and stop searching if matched. // Try to search for the next word if not matched or // if the cursor is at the end of a match string in visual-mode. - result = newSearchState.getSearchMatchRangeOf(vimState.cursorPosition); + result = newSearchState.getSearchMatchRangeOf(vimState.cursorStopPosition); if ( vimState.currentMode === ModeName.Visual && - vimState.cursorPosition.isEqual(result.end.getLeftThroughLineBreaks()) + vimState.cursorStopPosition.isEqual(result.end.getLeftThroughLineBreaks()) ) { result.match = false; } if (!result.match) { // Try to search for the next word - result = newSearchState.getNextSearchMatchRange(vimState.cursorPosition, 1); + result = newSearchState.getNextSearchMatchRange(vimState.cursorStopPosition, 1); if (!result.match) { return vimState; // no match... } } vimState.cursorStartPosition = - vimState.currentMode === ModeName.Normal ? result.start : vimState.cursorPosition; - vimState.cursorPosition = result.end.getLeftThroughLineBreaks(); // end is exclusive + vimState.currentMode === ModeName.Normal ? result.start : vimState.cursorStopPosition; + vimState.cursorStopPosition = result.end.getLeftThroughLineBreaks(); // end is exclusive // Move the cursor, this is a bit hacky... vscode.window.activeTextEditor!.selection = new vscode.Selection( vimState.cursorStartPosition, - vimState.cursorPosition + vimState.cursorStopPosition ); await vimState.setCurrentMode(ModeName.Visual); @@ -2621,7 +2621,7 @@ class CommandGoToDefinition extends BaseCommand { // https://github.com/VSCodeVim/Vim/issues/3277 await waitForCursorSync(1000); if (oldActiveEditor === vimState.editor) { - vimState.cursorPosition = Position.FromVSCodePosition(vimState.editor.selection.start); + vimState.cursorStopPosition = Position.FromVSCodePosition(vimState.editor.selection.start); } return vimState; @@ -2640,10 +2640,10 @@ class CommandGoBackInChangelist extends BaseCommand { const currPos = vimState.historyTracker.getChangePositionAtindex(originalIndex); if (prevPos !== undefined) { - vimState.cursorPosition = prevPos[0]; + vimState.cursorStopPosition = prevPos[0]; vimState.historyTracker.changelistIndex = originalIndex - 1; } else if (currPos !== undefined) { - vimState.cursorPosition = currPos[0]; + vimState.cursorStopPosition = currPos[0]; } return vimState; @@ -2662,10 +2662,10 @@ class CommandGoForwardInChangelist extends BaseCommand { const currPos = vimState.historyTracker.getChangePositionAtindex(originalIndex); if (nextPos !== undefined) { - vimState.cursorPosition = nextPos[0]; + vimState.cursorStopPosition = nextPos[0]; vimState.historyTracker.changelistIndex = originalIndex + 1; } else if (currPos !== undefined) { - vimState.cursorPosition = currPos[0]; + vimState.cursorStopPosition = currPos[0]; } return vimState; @@ -2682,7 +2682,7 @@ class CommandGoLastChange extends BaseCommand { const lastPos = vimState.historyTracker.getLastHistoryStartPosition(); if (lastPos !== undefined) { - vimState.cursorPosition = lastPos[0]; + vimState.cursorStopPosition = lastPos[0]; } return vimState; @@ -2698,7 +2698,7 @@ class CommandInsertAtLastChange extends BaseCommand { const lastPos = vimState.historyTracker.getLastChangeEndPosition(); if (lastPos !== undefined) { - vimState.cursorPosition = lastPos; + vimState.cursorStopPosition = lastPos; await vimState.setCurrentMode(ModeName.Insert); } @@ -2713,7 +2713,7 @@ export class CommandInsertAtFirstCharacter extends BaseCommand { public async exec(position: Position, vimState: VimState): Promise { await vimState.setCurrentMode(ModeName.Insert); - vimState.cursorPosition = position.getFirstLineNonBlankChar(); + vimState.cursorStopPosition = position.getFirstLineNonBlankChar(); return vimState; } @@ -2727,7 +2727,7 @@ class CommandInsertAtLineBegin extends BaseCommand { public async exec(position: Position, vimState: VimState): Promise { await vimState.setCurrentMode(ModeName.Insert); - vimState.cursorPosition = position.getLineBegin(); + vimState.cursorStopPosition = position.getLineBegin(); return vimState; } @@ -2740,7 +2740,7 @@ export class CommandInsertAfterCursor extends BaseCommand { public async exec(position: Position, vimState: VimState): Promise { await vimState.setCurrentMode(ModeName.Insert); - vimState.cursorPosition = position.getRight(); + vimState.cursorStopPosition = position.getRight(); return vimState; } @@ -2770,7 +2770,7 @@ export class CommandInsertAtLineEnd extends BaseCommand { public async exec(position: Position, vimState: VimState): Promise { await vimState.setCurrentMode(ModeName.Insert); - vimState.cursorPosition = position.getLineEnd(); + vimState.cursorStopPosition = position.getLineEnd(); return vimState; } @@ -2800,15 +2800,15 @@ class CommandInsertNewLineAbove extends BaseCommand { await vscode.commands.executeCommand('editor.action.insertLineBefore'); } - vimState.allCursors = await getCursorsAfterSync(); + vimState.cursors = await getCursorsAfterSync(); for (let i = 0; i < count; i++) { const newPos = new Position( - vimState.allCursors[0].start.line + i, - vimState.allCursors[0].start.character + vimState.cursors[0].start.line + i, + vimState.cursors[0].start.character ); - vimState.allCursors.push(new Range(newPos, newPos)); + vimState.cursors.push(new Range(newPos, newPos)); } - vimState.allCursors = vimState.allCursors.reverse(); + vimState.cursors = vimState.cursors.reverse(); vimState.isFakeMultiCursor = true; vimState.isMultiCursor = true; return vimState; @@ -2830,13 +2830,13 @@ class CommandInsertNewLineBefore extends BaseCommand { for (let i = 0; i < count; i++) { await vscode.commands.executeCommand('editor.action.insertLineAfter'); } - vimState.allCursors = await getCursorsAfterSync(); + vimState.cursors = await getCursorsAfterSync(); for (let i = 1; i < count; i++) { const newPos = new Position( - vimState.allCursors[0].start.line - i, - vimState.allCursors[0].start.character + vimState.cursors[0].start.line - i, + vimState.cursors[0].start.character ); - vimState.allCursors.push(new Range(newPos, newPos)); + vimState.cursors.push(new Range(newPos, newPos)); // Ahhhhhh. We have to manually set cursor position here as we need text // transformations AND to set multiple cursors. @@ -2848,7 +2848,7 @@ class CommandInsertNewLineBefore extends BaseCommand { manuallySetCursorPositions: true, }); } - vimState.allCursors = vimState.allCursors.reverse(); + vimState.cursors = vimState.cursors.reverse(); vimState.isFakeMultiCursor = true; vimState.isMultiCursor = true; return vimState; @@ -2917,7 +2917,7 @@ class CommandNavigateLastBOL extends BaseCommand { position: lastJump.position.getLineBegin(), }); vimState.globalState.jumpTracker.recordJump(Jump.fromStateNow(vimState), jump); - vimState.cursorPosition = jump.position; + vimState.cursorStopPosition = jump.position; return vimState; } } @@ -3290,11 +3290,11 @@ class ActionJoin extends BaseCommand { manuallySetCursorPositions: true, }); - vimState.cursorPosition = new Position( + vimState.cursorStopPosition = new Position( startPosition.line, trimmedLinesContent.length - columnDeltaOffset ); - vimState.cursorStartPosition = vimState.cursorPosition; + vimState.cursorStartPosition = vimState.cursorStopPosition; await vimState.setCurrentMode(ModeName.Normal); } } @@ -3307,7 +3307,7 @@ class ActionJoin extends BaseCommand { let resultingCursors: Range[] = []; let i = 0; - const cursorsToIterateOver = vimState.allCursors + const cursorsToIterateOver = vimState.cursors .map(x => new Range(x.start, x.stop)) .sort((a, b) => a.start.line > b.start.line || @@ -3319,12 +3319,12 @@ class ActionJoin extends BaseCommand { for (const { start, stop } of cursorsToIterateOver) { this.multicursorIndex = i++; - vimState.cursorPosition = stop; + vimState.cursorStopPosition = stop; vimState.cursorStartPosition = start; vimState = await this.execJoinLines(start, stop, vimState, timesToRepeat); - resultingCursors.push(new Range(vimState.cursorStartPosition, vimState.cursorPosition)); + resultingCursors.push(new Range(vimState.cursorStartPosition, vimState.cursorStopPosition)); for (const transformation of vimState.recordedState.transformations) { if (isTextTransformation(transformation) && transformation.cursorIndex === undefined) { @@ -3333,7 +3333,7 @@ class ActionJoin extends BaseCommand { } } - vimState.allCursors = resultingCursors; + vimState.cursors = resultingCursors; return vimState; } @@ -3402,7 +3402,7 @@ class ActionJoinNoWhitespace extends BaseCommand { diff: new PositionDiff(0, -lineTwo.length), }); - newState.cursorPosition = new Position(position.line, lineOne.length); + newState.cursorStopPosition = new Position(position.line, lineOne.length); return newState; } @@ -3511,7 +3511,7 @@ class ActionReplaceCharacterVisual extends BaseCommand { let visualSelectionOffset = 1; let start = vimState.cursorStartPosition; - let end = vimState.cursorPosition; + let end = vimState.cursorStopPosition; // If selection is reversed, reorganize it so that the text replace logic always works if (end.isBeforeOrEqual(start)) { @@ -3569,7 +3569,7 @@ class ActionReplaceCharacterVisual extends BaseCommand { } } - vimState.cursorPosition = start; + vimState.cursorStopPosition = start; vimState.cursorStartPosition = start; await vimState.setCurrentMode(ModeName.Normal); return vimState; @@ -3602,10 +3602,10 @@ class ActionReplaceCharacterVisualBlock extends BaseCommand { } const topLeft = VisualBlockMode.getTopLeftPosition( - vimState.cursorPosition, + vimState.cursorStopPosition, vimState.cursorStartPosition ); - vimState.allCursors = [new Range(topLeft, topLeft)]; + vimState.cursors = [new Range(topLeft, topLeft)]; await vimState.setCurrentMode(ModeName.Normal); return vimState; @@ -3638,11 +3638,11 @@ class ActionXVisualBlock extends BaseCommand { Register.put(text, vimState, this.multicursorIndex); const topLeft = VisualBlockMode.getTopLeftPosition( - vimState.cursorPosition, + vimState.cursorStopPosition, vimState.cursorStartPosition ); - vimState.allCursors = [new Range(topLeft, topLeft)]; + vimState.cursors = [new Range(topLeft, topLeft)]; await vimState.setCurrentMode(ModeName.Normal); return vimState; @@ -3678,11 +3678,11 @@ class ActionShiftDVisualBlock extends BaseCommand { } const topLeft = VisualBlockMode.getTopLeftPosition( - vimState.cursorPosition, + vimState.cursorStopPosition, vimState.cursorStartPosition ); - vimState.allCursors = [new Range(topLeft, topLeft)]; + vimState.cursors = [new Range(topLeft, topLeft)]; await vimState.setCurrentMode(ModeName.Normal); return vimState; @@ -3706,9 +3706,9 @@ class ActionGoToInsertVisualBlockMode extends BaseCommand { if (line === '' && start.character !== 0) { continue; } - vimState.allCursors.push(new Range(start, start)); + vimState.cursors.push(new Range(start, start)); } - vimState.allCursors = vimState.allCursors.slice(1); + vimState.cursors = vimState.cursors.slice(1); return vimState; } } @@ -3735,9 +3735,9 @@ class ActionChangeInVisualBlockMode extends BaseCommand { vimState.isFakeMultiCursor = true; for (const { start } of Position.IterateLine(vimState)) { - vimState.allCursors.push(new Range(start, start)); + vimState.cursors.push(new Range(start, start)); } - vimState.allCursors = vimState.allCursors.slice(1); + vimState.cursors = vimState.cursors.slice(1); return vimState; } @@ -3765,9 +3765,9 @@ class ActionChangeToEOLInVisualBlockMode extends BaseCommand { vimState.isFakeMultiCursor = true; for (const { end } of Position.IterateLine(vimState)) { - vimState.allCursors.push(new Range(end, end)); + vimState.cursors.push(new Range(end, end)); } - vimState.allCursors = vimState.allCursors.slice(1); + vimState.cursors = vimState.cursors.slice(1); return vimState; } @@ -3796,12 +3796,12 @@ abstract class ActionGoToInsertVisualLineModeCommand extends BaseCommand { [start, end] = [end, start]; } - vimState.allCursors = []; + vimState.cursors = []; for (let i = start.line; i <= end.line; i++) { const line = TextEditor.getLineAt(new Position(i, 0)); if (line.text.trim() !== '') { - vimState.allCursors.push(this.getCursorRangeForLine(line, start, end)); + vimState.cursors.push(this.getCursorRangeForLine(line, start, end)); } } return vimState; @@ -3891,9 +3891,9 @@ class ActionGoToInsertVisualBlockModeAppend extends BaseCommand { end: new Position(end.line, end.character), }); } - vimState.allCursors.push(new Range(end, end)); + vimState.cursors.push(new Range(end, end)); } - vimState.allCursors = vimState.allCursors.slice(1); + vimState.cursors = vimState.cursors.slice(1); return vimState; } } @@ -3908,7 +3908,7 @@ class ActionDeleteLineVisualMode extends BaseCommand { return new operator.DeleteOperator(this.multicursorIndex).run( vimState, vimState.cursorStartPosition.getLineBegin(), - vimState.cursorPosition.getLineEnd() + vimState.cursorStopPosition.getLineEnd() ); } else { return new operator.DeleteOperator(this.multicursorIndex).run( @@ -3929,7 +3929,7 @@ class ActionChangeLineVisualMode extends BaseCommand { return new operator.DeleteOperator(this.multicursorIndex).run( vimState, vimState.cursorStartPosition.getLineBegin(), - vimState.cursorPosition.getLineEndIncludingEOL() + vimState.cursorStopPosition.getLineEndIncludingEOL() ); } } @@ -3943,7 +3943,7 @@ class ActionRemoveLineVisualMode extends BaseCommand { return new operator.DeleteOperator(this.multicursorIndex).run( vimState, vimState.cursorStartPosition.getLineBegin(), - vimState.cursorPosition.getLineEndIncludingEOL() + vimState.cursorStopPosition.getLineEndIncludingEOL() ); } } @@ -3990,11 +3990,11 @@ class ToggleCaseAndMoveForward extends BaseCommand { public async exec(position: Position, vimState: VimState): Promise { await new operator.ToggleCaseOperator().run( vimState, - vimState.cursorPosition, - vimState.cursorPosition + vimState.cursorStopPosition, + vimState.cursorStopPosition ); - vimState.cursorPosition = vimState.cursorPosition.getRight(); + vimState.cursorStopPosition = vimState.cursorStopPosition.getRight(); return vimState; } } @@ -4033,13 +4033,13 @@ abstract class IncrementDecrementNumberAction extends BaseCommand { num !== null && position.character < start.character + num.prefix.length + num.value.toString().length ) { - vimState.cursorPosition = await this.replaceNum( + vimState.cursorStopPosition = await this.replaceNum( num, this.offset * (vimState.recordedState.count || 1), start, end ); - vimState.cursorPosition = vimState.cursorPosition.getLeftByCount(num.suffix.length); + vimState.cursorStopPosition = vimState.cursorStopPosition.getLeftByCount(num.suffix.length); return vimState; } else if (num !== null) { word = word.slice(num.prefix.length + num.value.toString().length); @@ -4131,11 +4131,11 @@ class ActionOverrideCmdD extends BaseCommand { public async exec(position: Position, vimState: VimState): Promise { await vscode.commands.executeCommand('editor.action.addSelectionToNextFindMatch'); - vimState.allCursors = await getCursorsAfterSync(); + vimState.cursors = await getCursorsAfterSync(); // If this is the first cursor, select 1 character less // so that only the word is selected, no extra character - vimState.allCursors = vimState.allCursors.map(x => x.withNewStop(x.stop.getLeft())); + vimState.cursors = vimState.cursors.map(x => x.withNewStop(x.stop.getLeft())); await vimState.setCurrentMode(ModeName.Visual); @@ -4184,7 +4184,7 @@ class ActionOverrideCmdDInsert extends BaseCommand { } ); await vscode.commands.executeCommand('editor.action.addSelectionToNextFindMatch'); - vimState.allCursors = await getCursorsAfterSync(); + vimState.cursors = await getCursorsAfterSync(); return vimState; } } @@ -4203,7 +4203,7 @@ class ActionOverrideCmdAltDown extends BaseCommand { public async exec(position: Position, vimState: VimState): Promise { await vscode.commands.executeCommand('editor.action.insertCursorBelow'); - vimState.allCursors = await getCursorsAfterSync(); + vimState.cursors = await getCursorsAfterSync(); return vimState; } @@ -4223,7 +4223,7 @@ class ActionOverrideCmdAltUp extends BaseCommand { public async exec(position: Position, vimState: VimState): Promise { await vscode.commands.executeCommand('editor.action.insertCursorAbove'); - vimState.allCursors = await getCursorsAfterSync(); + vimState.cursors = await getCursorsAfterSync(); return vimState; } diff --git a/src/actions/commands/insert.ts b/src/actions/commands/insert.ts index de7b2c27d0e..3ed6104ce31 100644 --- a/src/actions/commands/insert.ts +++ b/src/actions/commands/insert.ts @@ -31,30 +31,30 @@ class CommandEscInsertMode extends BaseCommand { } public async exec(position: Position, vimState: VimState): Promise { - vimState.allCursors = vimState.allCursors.map(x => x.withNewStop(x.stop.getLeft())); + vimState.cursors = vimState.cursors.map(x => x.withNewStop(x.stop.getLeft())); if (vimState.returnToInsertAfterCommand && position.character !== 0) { - vimState.allCursors = vimState.allCursors.map(x => x.withNewStop(x.stop.getRight())); + vimState.cursors = vimState.cursors.map(x => x.withNewStop(x.stop.getRight())); } // only remove leading spaces inserted by vscode. // vscode only inserts them when user enter a new line, // ie, o/O in Normal mode or \n in Insert mode. - for (let i = 0; i < vimState.allCursors.length; i++) { + for (let i = 0; i < vimState.cursors.length; i++) { const lastActionBeforeEsc = vimState.keyHistory[vimState.keyHistory.length - 2]; if ( ['o', 'O', '\n'].indexOf(lastActionBeforeEsc) > -1 && vimState.editor.document.languageId !== 'plaintext' && - /^\s+$/.test(TextEditor.getLineAt(vimState.allCursors[i].stop).text) + /^\s+$/.test(TextEditor.getLineAt(vimState.cursors[i].stop).text) ) { vimState.recordedState.transformations.push({ type: 'deleteRange', range: new Range( - vimState.allCursors[i].stop.getLineBegin(), - vimState.allCursors[i].stop.getLineEnd() + vimState.cursors[i].stop.getLineBegin(), + vimState.cursors[i].stop.getLineEnd() ), }); - vimState.allCursors[i] = vimState.allCursors[i].withNewStop( - vimState.allCursors[i].stop.getLineBegin() + vimState.cursors[i] = vimState.cursors[i].withNewStop( + vimState.cursors[i].stop.getLineBegin() ); } } @@ -105,7 +105,7 @@ class CommandEscInsertMode extends BaseCommand { } if (vimState.isFakeMultiCursor) { - vimState.allCursors = [vimState.allCursors[0]]; + vimState.cursors = [vimState.cursors[0]]; vimState.isMultiCursor = false; vimState.isFakeMultiCursor = false; } @@ -133,15 +133,15 @@ export class CommandInsertPreviousText extends BaseCommand { for (let action of actions) { if (action instanceof BaseCommand) { - vimState = await action.execCount(vimState.cursorPosition, vimState); + vimState = await action.execCount(vimState.cursorStopPosition, vimState); } if (action instanceof DocumentContentChangeAction) { - vimState = await action.exec(vimState.cursorPosition, vimState); + vimState = await action.exec(vimState.cursorStopPosition, vimState); } } - vimState.cursorPosition = Position.FromVSCodePosition(vimState.editor.selection.end); + vimState.cursorStopPosition = Position.FromVSCodePosition(vimState.editor.selection.end); vimState.cursorStartPosition = Position.FromVSCodePosition(vimState.editor.selection.start); await vimState.setCurrentMode(ModeName.Insert); return vimState; @@ -182,7 +182,7 @@ class CommandInsertBelowChar extends BaseCommand { await TextEditor.insert(char, position); vimState.cursorStartPosition = Position.FromVSCodePosition(vimState.editor.selection.start); - vimState.cursorPosition = Position.FromVSCodePosition(vimState.editor.selection.start); + vimState.cursorStopPosition = Position.FromVSCodePosition(vimState.editor.selection.start); return vimState; } @@ -272,14 +272,14 @@ export class CommandInsertInInsertMode extends BaseCommand { } } - vimState.cursorPosition = vimState.cursorPosition.getLeft(); + vimState.cursorStopPosition = vimState.cursorStopPosition.getLeft(); vimState.cursorStartPosition = vimState.cursorStartPosition.getLeft(); } else { if (vimState.isMultiCursor) { vimState.recordedState.transformations.push({ type: 'insertText', text: char, - position: vimState.cursorPosition, + position: vimState.cursorStopPosition, }); } else { vimState.recordedState.transformations.push({ @@ -329,7 +329,7 @@ class CommandInsertRegisterContent extends BaseCommand { await TextEditor.insertAt(text, position); await vimState.setCurrentMode(ModeName.Insert); vimState.cursorStartPosition = Position.FromVSCodePosition(vimState.editor.selection.start); - vimState.cursorPosition = Position.FromVSCodePosition(vimState.editor.selection.start); + vimState.cursorStopPosition = Position.FromVSCodePosition(vimState.editor.selection.start); return vimState; } @@ -374,7 +374,7 @@ class CommandCtrlW extends BaseCommand { await TextEditor.delete(new vscode.Range(wordBegin, position)); - vimState.cursorPosition = wordBegin; + vimState.cursorStopPosition = wordBegin; return vimState; } @@ -410,7 +410,7 @@ class CommandDeleteIndentInCurrentLine extends BaseCommand { position.character + (newIndentationWidth - indentationWidth) / tabSize ) ); - vimState.cursorPosition = cursorPosition; + vimState.cursorStopPosition = cursorPosition; vimState.cursorStartPosition = cursorPosition; await vimState.setCurrentMode(ModeName.Insert); return vimState; @@ -439,7 +439,7 @@ class CommandInsertAboveChar extends BaseCommand { await TextEditor.insert(char, position); vimState.cursorStartPosition = Position.FromVSCodePosition(vimState.editor.selection.start); - vimState.cursorPosition = Position.FromVSCodePosition(vimState.editor.selection.start); + vimState.cursorStopPosition = Position.FromVSCodePosition(vimState.editor.selection.start); return vimState; } @@ -470,7 +470,7 @@ class CommandCtrlUInInsertMode extends BaseCommand { ? position.getLineBegin() : position.getLineBeginRespectingIndent(); await TextEditor.delete(new vscode.Range(start, position)); - vimState.cursorPosition = start; + vimState.cursorStopPosition = start; vimState.cursorStartPosition = start; return vimState; } @@ -544,7 +544,7 @@ class CommandCtrlVInInsertMode extends BaseCommand { vimState.recordedState.transformations.push({ type: 'insertText', text: textFromClipboard, - position: vimState.cursorPosition, + position: vimState.cursorStopPosition, }); } else { vimState.recordedState.transformations.push({ diff --git a/src/actions/motion.ts b/src/actions/motion.ts index 068ceaa7381..4204fe1b9ed 100644 --- a/src/actions/motion.ts +++ b/src/actions/motion.ts @@ -1493,7 +1493,7 @@ export abstract class MoveInsideCharacter extends ExpandingSelection { cursorStartPos = adjacentPosLeft; vimState.cursorStartPosition = adjacentPosLeft; position = adjacentPosRight; - vimState.cursorPosition = adjacentPosRight; + vimState.cursorStopPosition = adjacentPosRight; } } // First, search backwards for the opening character of the sequence diff --git a/src/actions/operator.ts b/src/actions/operator.ts index 2c3de5198ac..fd7dca36396 100644 --- a/src/actions/operator.ts +++ b/src/actions/operator.ts @@ -250,7 +250,7 @@ export class YankOperator extends BaseOperator { if (vimState.surround) { vimState.surround.range = new Range(start, end); await vimState.setCurrentMode(ModeName.SurroundInputMode); - vimState.cursorPosition = start; + vimState.cursorStopPosition = start; vimState.cursorStartPosition = start; return vimState; @@ -292,11 +292,11 @@ export class YankOperator extends BaseOperator { } if (originalMode === ModeName.Normal && !moveCursor) { - vimState.allCursors = vimState.cursorPositionJustBeforeAnythingHappened.map( + vimState.cursors = vimState.cursorPositionJustBeforeAnythingHappened.map( x => new Range(x, x) ); } else { - vimState.cursorPosition = start; + vimState.cursorStopPosition = start; } const numLinesYanked = text.split('\n').length; @@ -356,12 +356,12 @@ export class FormatOperator extends BaseOperator { await vscode.commands.executeCommand('editor.action.formatSelection'); let line = vimState.cursorStartPosition.line; - if (vimState.cursorStartPosition.isAfter(vimState.cursorPosition)) { - line = vimState.cursorPosition.line; + if (vimState.cursorStartPosition.isAfter(vimState.cursorStopPosition)) { + line = vimState.cursorStopPosition.line; } let newCursorPosition = new Position(line, 0).getFirstLineNonBlankChar(); - vimState.cursorPosition = newCursorPosition; + vimState.cursorStopPosition = newCursorPosition; vimState.cursorStartPosition = newCursorPosition; await vimState.setCurrentMode(ModeName.Normal); return vimState; @@ -380,7 +380,7 @@ export class UpperCaseOperator extends BaseOperator { await TextEditor.replace(range, text.toUpperCase()); await vimState.setCurrentMode(ModeName.Normal); - vimState.cursorPosition = start; + vimState.cursorStopPosition = start; return vimState; } @@ -405,7 +405,7 @@ class UpperCaseVisualBlockOperator extends BaseOperator { } const cursorPosition = startPos.isBefore(endPos) ? startPos : endPos; - vimState.cursorPosition = cursorPosition; + vimState.cursorStopPosition = cursorPosition; vimState.cursorStartPosition = cursorPosition; await vimState.setCurrentMode(ModeName.Normal); @@ -425,7 +425,7 @@ export class LowerCaseOperator extends BaseOperator { await TextEditor.replace(range, text.toLowerCase()); await vimState.setCurrentMode(ModeName.Normal); - vimState.cursorPosition = start; + vimState.cursorStopPosition = start; return vimState; } @@ -450,7 +450,7 @@ class LowerCaseVisualBlockOperator extends BaseOperator { } const cursorPosition = startPos.isBefore(endPos) ? startPos : endPos; - vimState.cursorPosition = cursorPosition; + vimState.cursorStopPosition = cursorPosition; vimState.cursorStartPosition = cursorPosition; await vimState.setCurrentMode(ModeName.Normal); @@ -469,7 +469,7 @@ class IndentOperator extends BaseOperator { await vscode.commands.executeCommand('editor.action.indentLines'); await vimState.setCurrentMode(ModeName.Normal); - vimState.cursorPosition = start.getFirstLineNonBlankChar(); + vimState.cursorStopPosition = start.getFirstLineNonBlankChar(); return vimState; } @@ -492,7 +492,7 @@ class IndentOperatorInVisualModesIsAWeirdSpecialCase extends BaseOperator { public async run(vimState: VimState, start: Position, end: Position): Promise { // Repeating this command with dot should apply the indent to the previous selection if (vimState.isRunningDotCommand && vimState.dotCommandPreviousVisualSelection) { - if (vimState.cursorStartPosition.isAfter(vimState.cursorPosition)) { + if (vimState.cursorStartPosition.isAfter(vimState.cursorStopPosition)) { const shiftSelectionByNum = vimState.dotCommandPreviousVisualSelection.end.line - vimState.dotCommandPreviousVisualSelection.start.line; @@ -509,7 +509,7 @@ class IndentOperatorInVisualModesIsAWeirdSpecialCase extends BaseOperator { } await vimState.setCurrentMode(ModeName.Normal); - vimState.cursorPosition = start.getFirstLineNonBlankChar(); + vimState.cursorStopPosition = start.getFirstLineNonBlankChar(); return vimState; } @@ -525,7 +525,7 @@ class OutdentOperator extends BaseOperator { await vscode.commands.executeCommand('editor.action.outdentLines'); await vimState.setCurrentMode(ModeName.Normal); - vimState.cursorPosition = start.getFirstLineNonBlankChar(); + vimState.cursorStopPosition = start.getFirstLineNonBlankChar(); return vimState; } @@ -542,7 +542,7 @@ class OutdentOperatorInVisualModesIsAWeirdSpecialCase extends BaseOperator { public async run(vimState: VimState, start: Position, end: Position): Promise { // Repeating this command with dot should apply the indent to the previous selection if (vimState.isRunningDotCommand && vimState.dotCommandPreviousVisualSelection) { - if (vimState.cursorStartPosition.isAfter(vimState.cursorPosition)) { + if (vimState.cursorStartPosition.isAfter(vimState.cursorStopPosition)) { const shiftSelectionByNum = vimState.dotCommandPreviousVisualSelection.end.line - vimState.dotCommandPreviousVisualSelection.start.line; @@ -559,7 +559,7 @@ class OutdentOperatorInVisualModesIsAWeirdSpecialCase extends BaseOperator { } await vimState.setCurrentMode(ModeName.Normal); - vimState.cursorPosition = start.getFirstLineNonBlankChar(); + vimState.cursorStopPosition = start.getFirstLineNonBlankChar(); return vimState; } @@ -596,7 +596,7 @@ export class ChangeOperator extends BaseOperator { await vimState.setCurrentMode(ModeName.Insert); if (isEndOfLine) { - vimState.cursorPosition = end.getRight(); + vimState.cursorStopPosition = end.getRight(); } return vimState; @@ -666,7 +666,7 @@ export class YankVisualBlockMode extends BaseOperator { ReportLinesYanked(numLinesYanked, vimState); await vimState.setCurrentMode(ModeName.Normal); - vimState.cursorPosition = start; + vimState.cursorStopPosition = start; return vimState; } } @@ -682,7 +682,7 @@ export class ToggleCaseOperator extends BaseOperator { await ToggleCaseOperator.toggleCase(range); const cursorPosition = start.isBefore(end) ? start : end; - vimState.cursorPosition = cursorPosition; + vimState.cursorStopPosition = cursorPosition; vimState.cursorStartPosition = cursorPosition; await vimState.setCurrentMode(ModeName.Normal); @@ -719,7 +719,7 @@ class ToggleCaseVisualBlockOperator extends BaseOperator { } const cursorPosition = startPos.isBefore(endPos) ? startPos : endPos; - vimState.cursorPosition = cursorPosition; + vimState.cursorStopPosition = cursorPosition; vimState.cursorStartPosition = cursorPosition; await vimState.setCurrentMode(ModeName.Normal); @@ -742,7 +742,7 @@ export class CommentOperator extends BaseOperator { vimState.editor.selection = new vscode.Selection(start.getLineBegin(), end.getLineEnd()); await vscode.commands.executeCommand('editor.action.commentLine'); - vimState.cursorPosition = new Position(start.line, 0); + vimState.cursorStopPosition = new Position(start.line, 0); await vimState.setCurrentMode(ModeName.Normal); return vimState; @@ -759,7 +759,7 @@ export class CommentBlockOperator extends BaseOperator { vimState.editor.selection = new vscode.Selection(start, endPosition); await vscode.commands.executeCommand('editor.action.blockComment'); - vimState.cursorPosition = start; + vimState.cursorStopPosition = start; await vimState.setCurrentMode(ModeName.Normal); return vimState; diff --git a/src/actions/plugins/easymotion/easymotion.cmd.ts b/src/actions/plugins/easymotion/easymotion.cmd.ts index 8d6b42b9c1d..897a03027c3 100644 --- a/src/actions/plugins/easymotion/easymotion.cmd.ts +++ b/src/actions/plugins/easymotion/easymotion.cmd.ts @@ -88,7 +88,7 @@ abstract class BaseEasyMotionCommand extends BaseCommand { // Only one found, navigate to it const marker = vimState.easyMotion.markers[0]; // Set cursor position based on marker entered - vimState.cursorPosition = marker.position; + vimState.cursorStopPosition = marker.position; vimState.easyMotion.clearDecorations(); return vimState; } else { @@ -351,7 +351,7 @@ class EasyMotionCharInputMode extends BaseCommand { if (action.shouldFire()) { // Skip Easymotion input mode to make sure not to back to it await vimState.setCurrentMode(vimState.easyMotion.previousMode); - const state = await action.fire(vimState.cursorPosition, vimState); + const state = await action.fire(vimState.cursorStopPosition, vimState); return state; } return vimState; @@ -393,7 +393,7 @@ class MoveEasyMotion extends BaseCommand { vimState.easyMotion.previousMode === ModeName.VisualBlock ) { vimState.cursorStartPosition = vimState.lastVisualSelectionStart; - vimState.cursorPosition = vimState.lastVisualSelectionEnd; + vimState.cursorStopPosition = vimState.lastVisualSelectionEnd; } if (markers.length === 1) { @@ -405,7 +405,7 @@ class MoveEasyMotion extends BaseCommand { await vimState.setCurrentMode(vimState.easyMotion.previousMode); // Set cursor position based on marker entered - vimState.cursorPosition = marker.position; + vimState.cursorStopPosition = marker.position; return vimState; } else { diff --git a/src/actions/plugins/surround.ts b/src/actions/plugins/surround.ts index 10c67cd06c3..5862b4deda4 100644 --- a/src/actions/plugins/surround.ts +++ b/src/actions/plugins/surround.ts @@ -235,22 +235,22 @@ class CommandSurroundModeStartVisual extends BaseCommand { vimState.recordedState.surroundKeyIndexStart = vimState.keyHistory.length; // Make sure cursor positions are ordered correctly for top->down or down->top selection - if (vimState.cursorStartPosition.line > vimState.cursorPosition.line) { - [vimState.cursorPosition, vimState.cursorStartPosition] = [ + if (vimState.cursorStartPosition.line > vimState.cursorStopPosition.line) { + [vimState.cursorStopPosition, vimState.cursorStartPosition] = [ vimState.cursorStartPosition, - vimState.cursorPosition, + vimState.cursorStopPosition, ]; } // Make sure start/end cursor positions are in order if ( - vimState.cursorPosition.line < vimState.cursorPosition.line || - (vimState.cursorPosition.line === vimState.cursorStartPosition.line && - vimState.cursorPosition.character < vimState.cursorStartPosition.character) + vimState.cursorStopPosition.line < vimState.cursorStopPosition.line || + (vimState.cursorStopPosition.line === vimState.cursorStartPosition.line && + vimState.cursorStopPosition.character < vimState.cursorStartPosition.character) ) { - [vimState.cursorPosition, vimState.cursorStartPosition] = [ + [vimState.cursorStopPosition, vimState.cursorStartPosition] = [ vimState.cursorStartPosition, - vimState.cursorPosition, + vimState.cursorStopPosition, ]; } @@ -259,7 +259,7 @@ class CommandSurroundModeStartVisual extends BaseCommand { target: undefined, operator: 'yank', replacement: undefined, - range: new Range(vimState.cursorStartPosition, vimState.cursorPosition), + range: new Range(vimState.cursorStartPosition, vimState.cursorStopPosition), isVisualLine: false, }; @@ -268,7 +268,7 @@ class CommandSurroundModeStartVisual extends BaseCommand { } await vimState.setCurrentMode(ModeName.SurroundInputMode); - vimState.cursorPosition = vimState.cursorStartPosition; + vimState.cursorStopPosition = vimState.cursorStartPosition; return vimState; } diff --git a/src/actions/textobject.ts b/src/actions/textobject.ts index 96e919aad02..1b2306a9e9d 100644 --- a/src/actions/textobject.ts +++ b/src/actions/textobject.ts @@ -71,11 +71,11 @@ export class SelectWord extends TextObjectMovement { if ( vimState.currentMode === ModeName.Visual && - !vimState.cursorPosition.isEqual(vimState.cursorStartPosition) + !vimState.cursorStopPosition.isEqual(vimState.cursorStartPosition) ) { start = vimState.cursorStartPosition; - if (vimState.cursorPosition.isBefore(vimState.cursorStartPosition)) { + if (vimState.cursorStopPosition.isBefore(vimState.cursorStartPosition)) { // If current cursor postion is before cursor start position, we are selecting words in reverser order. if (/\s/.test(currentChar)) { stop = position.getWordLeft(true); @@ -131,11 +131,11 @@ export class SelectABigWord extends TextObjectMovement { } if ( vimState.currentMode === ModeName.Visual && - !vimState.cursorPosition.isEqual(vimState.cursorStartPosition) + !vimState.cursorStopPosition.isEqual(vimState.cursorStartPosition) ) { start = vimState.cursorStartPosition; - if (vimState.cursorPosition.isBefore(vimState.cursorStartPosition)) { + if (vimState.cursorStopPosition.isBefore(vimState.cursorStartPosition)) { // If current cursor postion is before cursor start position, we are selecting words in reverser order. if (/\s/.test(currentChar)) { stop = position.getBigWordLeft(); @@ -193,7 +193,7 @@ export class SelectAnExpandingBlock extends ExpandingSelection { let smallestRange: Range | undefined = undefined; for (const iMotion of ranges) { - const currentSelectedRange = new Range(vimState.cursorStartPosition, vimState.cursorPosition); + const currentSelectedRange = new Range(vimState.cursorStartPosition, vimState.cursorStopPosition); if (iMotion.failed) { continue; } @@ -216,10 +216,10 @@ export class SelectAnExpandingBlock extends ExpandingSelection { if (contender) { const areTheyEqual = - contender.equals(new Range(vimState.cursorStartPosition, vimState.cursorPosition)) || + contender.equals(new Range(vimState.cursorStartPosition, vimState.cursorStopPosition)) || (vimState.currentMode === ModeName.VisualLine && contender.start.line === vimState.cursorStartPosition.line && - contender.stop.line === vimState.cursorPosition.line); + contender.stop.line === vimState.cursorStopPosition.line); if (!areTheyEqual) { smallestRange = contender; @@ -229,7 +229,7 @@ export class SelectAnExpandingBlock extends ExpandingSelection { if (!smallestRange) { return { start: vimState.cursorStartPosition, - stop: vimState.cursorPosition, + stop: vimState.cursorStopPosition, }; } else { // revert relevant state changes @@ -237,7 +237,7 @@ export class SelectAnExpandingBlock extends ExpandingSelection { smallestRange.start.line, smallestRange.start.character ); - vimState.cursorPosition = new Position(smallestRange.stop.line, smallestRange.stop.character); + vimState.cursorStopPosition = new Position(smallestRange.stop.line, smallestRange.stop.character); vimState.recordedState.operatorPositionDiff = undefined; return { start: smallestRange.start, @@ -267,11 +267,11 @@ export class SelectInnerWord extends TextObjectMovement { if ( vimState.currentMode === ModeName.Visual && - !vimState.cursorPosition.isEqual(vimState.cursorStartPosition) + !vimState.cursorStopPosition.isEqual(vimState.cursorStartPosition) ) { start = vimState.cursorStartPosition; - if (vimState.cursorPosition.isBefore(vimState.cursorStartPosition)) { + if (vimState.cursorStopPosition.isBefore(vimState.cursorStartPosition)) { // If current cursor postion is before cursor start position, we are selecting words in reverser order. if (/\s/.test(currentChar)) { stop = position.getLastWordEnd().getRight(); @@ -308,11 +308,11 @@ export class SelectInnerBigWord extends TextObjectMovement { if ( vimState.currentMode === ModeName.Visual && - !vimState.cursorPosition.isEqual(vimState.cursorStartPosition) + !vimState.cursorStopPosition.isEqual(vimState.cursorStartPosition) ) { start = vimState.cursorStartPosition; - if (vimState.cursorPosition.isBefore(vimState.cursorStartPosition)) { + if (vimState.cursorStopPosition.isBefore(vimState.cursorStartPosition)) { // If current cursor postion is before cursor start position, we are selecting words in reverser order. if (/\s/.test(currentChar)) { stop = position.getLastBigWordEnd().getRight(); @@ -362,13 +362,13 @@ export class SelectSentence extends TextObjectMovement { if ( vimState.currentMode === ModeName.Visual && - !vimState.cursorPosition.isEqual(vimState.cursorStartPosition) + !vimState.cursorStopPosition.isEqual(vimState.cursorStartPosition) ) { start = vimState.cursorStartPosition; - if (vimState.cursorPosition.isBefore(vimState.cursorStartPosition)) { + if (vimState.cursorStopPosition.isBefore(vimState.cursorStartPosition)) { // If current cursor postion is before cursor start position, we are selecting sentences in reverser order. - if (currentSentenceNonWhitespaceEnd.isAfter(vimState.cursorPosition)) { + if (currentSentenceNonWhitespaceEnd.isAfter(vimState.cursorStopPosition)) { stop = currentSentenceBegin .getSentenceBegin({ forward: false }) .getCurrentSentenceEnd() @@ -408,13 +408,13 @@ export class SelectInnerSentence extends TextObjectMovement { if ( vimState.currentMode === ModeName.Visual && - !vimState.cursorPosition.isEqual(vimState.cursorStartPosition) + !vimState.cursorStopPosition.isEqual(vimState.cursorStartPosition) ) { start = vimState.cursorStartPosition; - if (vimState.cursorPosition.isBefore(vimState.cursorStartPosition)) { + if (vimState.cursorStopPosition.isBefore(vimState.cursorStartPosition)) { // If current cursor postion is before cursor start position, we are selecting sentences in reverser order. - if (currentSentenceNonWhitespaceEnd.isAfter(vimState.cursorPosition)) { + if (currentSentenceNonWhitespaceEnd.isAfter(vimState.cursorStopPosition)) { stop = currentSentenceBegin; } else { stop = currentSentenceNonWhitespaceEnd.getRight(); diff --git a/src/cmd_line/commands/substitute.ts b/src/cmd_line/commands/substitute.ts index cc994309e99..6e8d6a7767b 100644 --- a/src/cmd_line/commands/substitute.ts +++ b/src/cmd_line/commands/substitute.ts @@ -141,7 +141,7 @@ export class SubstituteCommand extends node.CommandBase { vimState.globalState.substituteState = new SubstituteState(args.pattern, args.replace); vimState.globalState.searchState = new SearchState( SearchDirection.Forward, - vimState.cursorPosition, + vimState.cursorStopPosition, args.pattern, { isRegex: true }, vimState.currentMode diff --git a/src/cmd_line/node.ts b/src/cmd_line/node.ts index abf7bb0daca..b87fb1e4950 100644 --- a/src/cmd_line/node.ts +++ b/src/cmd_line/node.ts @@ -62,8 +62,8 @@ export class LineRange { } var lineRef = this.right.length === 0 ? this.left : this.right; var pos = this.lineRefToPosition(document, lineRef, vimState); - vimState.cursorPosition = vimState.cursorPosition.setLocation(pos.line, pos.character); - vimState.cursorStartPosition = vimState.cursorPosition; + vimState.cursorStopPosition = vimState.cursorStopPosition.setLocation(pos.line, pos.character); + vimState.cursorStartPosition = vimState.cursorStopPosition; } lineRefToPosition( diff --git a/src/common/matching/matcher.ts b/src/common/matching/matcher.ts index ce2389253af..d9feb1af32c 100644 --- a/src/common/matching/matcher.ts +++ b/src/common/matching/matcher.ts @@ -50,7 +50,7 @@ export class PairMatcher { let cursorChar = TextEditor.getCharAt(position); if (vimState) { let startPos = vimState.cursorStartPosition; - let endPos = vimState.cursorPosition; + let endPos = vimState.cursorStopPosition; if (startPos.isEqual(endPos) && cursorChar === charToFind) { return position; } diff --git a/src/common/motion/position.ts b/src/common/motion/position.ts index 8062ee318ef..5daa158a99c 100644 --- a/src/common/motion/position.ts +++ b/src/common/motion/position.ts @@ -254,7 +254,7 @@ export class Position extends vscode.Position { ): Iterable<{ line: string; start: Position; end: Position }> { const { reverse } = options; const start = vimState.cursorStartPosition; - const stop = vimState.cursorPosition; + const stop = vimState.cursorStopPosition; const topLeft = VisualBlockMode.getTopLeftPosition(start, stop); const bottomRight = VisualBlockMode.getBottomRightPosition(start, stop); diff --git a/src/configuration/remapper.ts b/src/configuration/remapper.ts index f26d805a148..c29d22d6c60 100644 --- a/src/configuration/remapper.ts +++ b/src/configuration/remapper.ts @@ -135,9 +135,9 @@ export class Remapper implements IRemapper { // Revert every single inserted character. // We subtract 1 because we haven't actually applied the last key. await vimState.historyTracker.undoAndRemoveChanges( - Math.max(0, numCharsToRemove * vimState.allCursors.length) + Math.max(0, numCharsToRemove * vimState.cursors.length) ); - vimState.allCursors = vimState.allCursors.map(x => + vimState.cursors = vimState.cursors.map(x => x.withNewStop(x.stop.getLeft(numCharsToRemove)) ); } diff --git a/src/jumps/jump.ts b/src/jumps/jump.ts index 4cdb16f0442..48023180dc0 100644 --- a/src/jumps/jump.ts +++ b/src/jumps/jump.ts @@ -42,7 +42,7 @@ export class Jump { return new Jump({ editor: vimState.editor, fileName: vimState.editor.document.fileName, - position: vimState.cursorPosition, + position: vimState.cursorStopPosition, }); } diff --git a/src/jumps/jumpTracker.ts b/src/jumps/jumpTracker.ts index 90005120c1b..f2f83774555 100644 --- a/src/jumps/jumpTracker.ts +++ b/src/jumps/jumpTracker.ts @@ -184,9 +184,9 @@ export class JumpTracker { if (jumpedFiles) { await this.performFileJump(jump, vimState); - vimState.allCursors = await getCursorsAfterSync(); + vimState.cursors = await getCursorsAfterSync(); } else { - vimState.cursorPosition = jump.position; + vimState.cursorStopPosition = jump.position; } return vimState; diff --git a/src/mode/modeHandler.ts b/src/mode/modeHandler.ts index 3a536fae41d..d6f96adde98 100644 --- a/src/mode/modeHandler.ts +++ b/src/mode/modeHandler.ts @@ -83,10 +83,10 @@ export class ModeHandler implements vscode.Disposable { this.vimState.cursorStartPosition = Position.FromVSCodePosition( this.vimState.editor.selection.start ); - this.vimState.cursorPosition = Position.FromVSCodePosition( + this.vimState.cursorStopPosition = Position.FromVSCodePosition( this.vimState.editor.selection.start ); - this.vimState.desiredColumn = this.vimState.cursorPosition.character; + this.vimState.desiredColumn = this.vimState.cursorStopPosition.character; } }, 0); } @@ -109,11 +109,11 @@ export class ModeHandler implements vscode.Disposable { public async handleSelectionChange(e: vscode.TextEditorSelectionChangeEvent): Promise { let selection = e.selections[0]; if ( - (e.selections.length !== this.vimState.allCursors.length || this.vimState.isMultiCursor) && + (e.selections.length !== this.vimState.cursors.length || this.vimState.isMultiCursor) && this.vimState.currentMode !== ModeName.VisualBlock ) { // Number of selections changed, make sure we know about all of them still - this.vimState.allCursors = e.textEditor.selections.map( + this.vimState.cursors = e.textEditor.selections.map( sel => new Range( // Adjust the cursor positions because cursors & selections don't match exactly @@ -141,7 +141,7 @@ export class ModeHandler implements vscode.Disposable { return; } - this.vimState.cursorPosition = Position.FromVSCodePosition(selection.active); + this.vimState.cursorStopPosition = Position.FromVSCodePosition(selection.active); this.vimState.cursorStartPosition = Position.FromVSCodePosition(selection.start); } return; @@ -183,7 +183,7 @@ export class ModeHandler implements vscode.Disposable { this.vimState.lastClickWasPastEol = false; } - this.vimState.cursorPosition = newPosition; + this.vimState.cursorStopPosition = newPosition; this.vimState.cursorStartPosition = newPosition; this.vimState.desiredColumn = newPosition.character; @@ -263,7 +263,7 @@ export class ModeHandler implements vscode.Disposable { key = ''; } - this.vimState.cursorPositionJustBeforeAnythingHappened = this.vimState.allCursors.map( + this.vimState.cursorPositionJustBeforeAnythingHappened = this.vimState.cursors.map( x => x.stop ); this.vimState.recordedState.commandList.push(key); @@ -440,7 +440,7 @@ export class ModeHandler implements vscode.Disposable { */ if (vimState.currentMode === ModeName.Visual) { - vimState.allCursors = vimState.allCursors.map(x => + vimState.cursors = vimState.cursors.map(x => x.start.isEarlierThan(x.stop) ? x.withNewStop(x.stop.getLeftThroughLineBreaks(true)) : x ); } @@ -451,7 +451,7 @@ export class ModeHandler implements vscode.Disposable { } if (action instanceof BaseCommand) { - vimState = await action.execCount(vimState.cursorPosition, vimState); + vimState = await action.execCount(vimState.cursorStopPosition, vimState); await this.executeCommand(vimState); @@ -465,7 +465,7 @@ export class ModeHandler implements vscode.Disposable { } if (action instanceof DocumentContentChangeAction) { - vimState = await action.exec(vimState.cursorPosition, vimState); + vimState = await action.exec(vimState.cursorStopPosition, vimState); } // Update mode (note the ordering allows you to go into search mode, @@ -510,7 +510,7 @@ export class ModeHandler implements vscode.Disposable { } if (vimState.currentMode === ModeName.Visual) { - vimState.allCursors = vimState.allCursors.map(x => + vimState.cursors = vimState.cursors.map(x => x.start.isEarlierThan(x.stop) ? x.withNewStop( x.stop.isLineEnd() ? x.stop.getRightThroughLineBreaks() : x.stop.getRight() @@ -559,7 +559,7 @@ export class ModeHandler implements vscode.Disposable { if (movement && movement.setsDesiredColumnToEOL && !recordedState.operator) { vimState.desiredColumn = Number.POSITIVE_INFINITY; } else { - vimState.desiredColumn = vimState.cursorPosition.character; + vimState.desiredColumn = vimState.cursorStopPosition.character; } } @@ -601,15 +601,15 @@ export class ModeHandler implements vscode.Disposable { vimState.currentRegisterMode = RegisterMode.AscertainFromCurrentMode; if (this.currentMode.name === ModeName.Normal) { - vimState.cursorStartPosition = vimState.cursorPosition; + vimState.cursorStartPosition = vimState.cursorStopPosition; } // Ensure cursor is within bounds if (!vimState.editor.document.isClosed && vimState.editor === vscode.window.activeTextEditor) { const cursors = new Array(); - for (let { range } of Range.IterateRanges(vimState.allCursors)) { + for (let { range } of Range.IterateRanges(vimState.cursors)) { // adjust start/stop - const documentEndPosition = vimState.cursorPosition.getDocumentEnd(vimState.editor); + const documentEndPosition = vimState.cursorStopPosition.getDocumentEnd(vimState.editor); const documentLineCount = TextEditor.getLineCount(vimState.editor); if (range.start.line >= documentLineCount) { range = range.withNewStart(documentEndPosition); @@ -634,17 +634,17 @@ export class ModeHandler implements vscode.Disposable { } cursors.push(range); } - vimState.allCursors = cursors; + vimState.cursors = cursors; } // Update the current history step to have the latest cursor position - vimState.historyTracker.setLastHistoryEndPosition(vimState.allCursors.map(x => x.stop)); + vimState.historyTracker.setLastHistoryEndPosition(vimState.cursors.map(x => x.stop)); if (this.currentMode.isVisualMode && !this.vimState.isRunningDotCommand) { // Store selection for commands like gv this.vimState.lastVisualMode = this.vimState.currentMode; this.vimState.lastVisualSelectionStart = this.vimState.cursorStartPosition; - this.vimState.lastVisualSelectionEnd = this.vimState.cursorPosition; + this.vimState.lastVisualSelectionEnd = this.vimState.cursorStopPosition; } // Make sure no two cursors are at the same location. @@ -654,7 +654,7 @@ export class ModeHandler implements vscode.Disposable { const resultingList: Range[] = []; - for (const cursor of vimState.allCursors) { + for (const cursor of vimState.cursors) { let shouldAddToList = true; for (const alreadyAddedCursor of resultingList) { @@ -669,7 +669,7 @@ export class ModeHandler implements vscode.Disposable { } } - vimState.allCursors = resultingList; + vimState.cursors = resultingList; return vimState; } @@ -681,7 +681,7 @@ export class ModeHandler implements vscode.Disposable { vimState.lastMovementFailed = false; let recordedState = vimState.recordedState; - for (let i = 0; i < vimState.allCursors.length; i++) { + for (let i = 0; i < vimState.cursors.length; i++) { /** * Essentially what we're doing here is pretending like the * current VimState only has one cursor (the cursor that we just @@ -694,22 +694,22 @@ export class ModeHandler implements vscode.Disposable { * Action definitions without having to think about multiple * cursors in almost all cases. */ - let cursorPosition = vimState.allCursors[i].stop; - const old = vimState.cursorPosition; + let cursorPosition = vimState.cursors[i].stop; + const old = vimState.cursorStopPosition; - vimState.cursorPosition = cursorPosition; + vimState.cursorStopPosition = cursorPosition; const result = await movement.execActionWithCount( cursorPosition, vimState, recordedState.count ); - vimState.cursorPosition = old; + vimState.cursorStopPosition = old; if (result instanceof Position) { - vimState.allCursors[i] = vimState.allCursors[i].withNewStop(result); + vimState.cursors[i] = vimState.cursors[i].withNewStop(result); if (!this.currentMode.isVisualMode && !vimState.recordedState.operator) { - vimState.allCursors[i] = vimState.allCursors[i].withNewStart(result); + vimState.cursors[i] = vimState.cursors[i].withNewStart(result); } } else if (isIMovement(result)) { if (result.failed) { @@ -717,7 +717,7 @@ export class ModeHandler implements vscode.Disposable { vimState.lastMovementFailed = true; } - vimState.allCursors[i] = Range.FromIMovement(result); + vimState.cursors[i] = Range.FromIMovement(result); if (result.registerMode) { vimState.currentRegisterMode = result.registerMode; @@ -729,14 +729,14 @@ export class ModeHandler implements vscode.Disposable { // Keep the cursor within bounds if (vimState.currentMode !== ModeName.Normal || recordedState.operator) { - let stop = vimState.cursorPosition; + let stop = vimState.cursorStopPosition; // Vim does this weird thing where it allows you to select and delete // the newline character, which it places 1 past the last character // in the line. This is why we use > instead of >=. if (stop.character > Position.getLineLength(stop.line)) { - vimState.cursorPosition = stop.getLineEnd(); + vimState.cursorStopPosition = stop.getLineEnd(); } } @@ -762,7 +762,7 @@ export class ModeHandler implements vscode.Disposable { let startingModeName = vimState.currentMode; - for (let { start, stop } of vimState.allCursors) { + for (let { start, stop } of vimState.cursors) { if (start.compareTo(stop) > 0) { [start, stop] = [stop, start]; } @@ -805,7 +805,7 @@ export class ModeHandler implements vscode.Disposable { let resultingRange = new Range( resultVimState.cursorStartPosition, - resultVimState.cursorPosition + resultVimState.cursorStopPosition ); resultingCursors.push(resultingRange); @@ -815,8 +815,8 @@ export class ModeHandler implements vscode.Disposable { await this.executeCommand(vimState); } else { // Keep track of all cursors (in the case of multi-cursor). - resultVimState.allCursors = resultingCursors; - this.vimState.editor.selections = vimState.allCursors.map( + resultVimState.cursors = resultingCursors; + this.vimState.editor.selections = vimState.cursors.map( cursor => new vscode.Selection(cursor.start, cursor.stop) ); } @@ -915,7 +915,7 @@ export class ModeHandler implements vscode.Disposable { vimState.cursorStartPosition = Position.FromVSCodePosition( this.vimState.editor.selection.start ); - vimState.cursorPosition = Position.FromVSCodePosition(this.vimState.editor.selection.end); + vimState.cursorStopPosition = Position.FromVSCodePosition(this.vimState.editor.selection.end); break; case 'showCommandHistory': @@ -967,11 +967,11 @@ export class ModeHandler implements vscode.Disposable { case 'contentChange': for (const change of command.changes) { await TextEditor.insert(change.text); - vimState.cursorPosition = Position.FromVSCodePosition( + vimState.cursorStopPosition = Position.FromVSCodePosition( this.vimState.editor.selection.start ); } - const newPos = vimState.cursorPosition.add(command.diff); + const newPos = vimState.cursorStopPosition.add(command.diff); this.vimState.editor.selection = new vscode.Selection(newPos, newPos); break; case 'tab': @@ -1026,7 +1026,7 @@ export class ModeHandler implements vscode.Disposable { // We handle multiple cursors in a different way in visual block mode, unfortunately. // TODO - refactor that out! if (vimState.currentMode !== ModeName.VisualBlock && !manuallySetCursorPositions) { - vimState.allCursors = []; + vimState.cursors = []; const resultingCursors: Range[] = []; @@ -1059,11 +1059,11 @@ export class ModeHandler implements vscode.Disposable { vimState.recordedState.operatorPositionDiff = undefined; - vimState.allCursors = resultingCursors; + vimState.cursors = resultingCursors; } else { if (accumulatedPositionDifferences[0] !== undefined) { if (accumulatedPositionDifferences[0].length > 0) { - vimState.cursorPosition = vimState.cursorPosition.add( + vimState.cursorStopPosition = vimState.cursorStopPosition.add( accumulatedPositionDifferences[0][0] ); vimState.cursorStartPosition = vimState.cursorStartPosition.add( @@ -1081,8 +1081,8 @@ export class ModeHandler implements vscode.Disposable { */ if (firstTransformation.type === 'deleteRange') { if (firstTransformation.collapseRange) { - vimState.cursorPosition = new Position( - vimState.cursorPosition.line, + vimState.cursorStopPosition = new Position( + vimState.cursorStopPosition.line, vimState.cursorStartPosition.character ); } @@ -1168,7 +1168,7 @@ export class ModeHandler implements vscode.Disposable { } vimState.isRunningDotCommand = false; - vimState.cursorPositionJustBeforeAnythingHappened = vimState.allCursors.map(x => x.stop); + vimState.cursorPositionJustBeforeAnythingHappened = vimState.cursors.map(x => x.stop); return vimState; } @@ -1194,7 +1194,7 @@ export class ModeHandler implements vscode.Disposable { if (!vimState.isMultiCursor) { let start = vimState.cursorStartPosition; - let stop = vimState.cursorPosition; + let stop = vimState.cursorStopPosition; if (selectionMode === ModeName.Visual) { /** @@ -1225,17 +1225,17 @@ export class ModeHandler implements vscode.Disposable { // Maintain cursor position based on which direction the selection is going if (start.line <= stop.line) { vimState.cursorStartPosition = selections[0].start as Position; - vimState.cursorPosition = selections[0].end as Position; + vimState.cursorStopPosition = selections[0].end as Position; } else { vimState.cursorStartPosition = selections[0].end as Position; - vimState.cursorPosition = selections[0].start as Position; + vimState.cursorStopPosition = selections[0].start as Position; } // Adjust the selection so that active and anchor are correct, this // makes relative line numbers display correctly if ( selections[0].start.line <= selections[0].end.line && - vimState.cursorPosition.line <= vimState.cursorStartPosition.line + vimState.cursorStopPosition.line <= vimState.cursorStartPosition.line ) { selections = [new vscode.Selection(selections[0].end, selections[0].start)]; } @@ -1253,7 +1253,7 @@ export class ModeHandler implements vscode.Disposable { selections = []; switch (selectionMode) { case ModeName.Visual: { - for (let { start: cursorStart, stop: cursorStop } of vimState.allCursors) { + for (let { start: cursorStart, stop: cursorStop } of vimState.cursors) { if (cursorStart.compareTo(cursorStop) > 0) { cursorStart = cursorStart.getRight(); } @@ -1264,7 +1264,7 @@ export class ModeHandler implements vscode.Disposable { } case ModeName.Normal: case ModeName.Insert: { - for (const { stop: cursorStop } of vimState.allCursors) { + for (const { stop: cursorStop } of vimState.cursors) { selections.push(new vscode.Selection(cursorStop, cursorStop)); } break; @@ -1287,14 +1287,14 @@ export class ModeHandler implements vscode.Disposable { // Scroll to position of cursor if (this.vimState.currentMode === ModeName.SearchInProgressMode) { const nextMatch = vimState.globalState.searchState!.getNextSearchMatchPosition( - vimState.cursorPosition + vimState.cursorStopPosition ).pos; this.vimState.editor.revealRange(new vscode.Range(nextMatch, nextMatch)); } else { if (args.revealRange) { this.vimState.editor.revealRange( - new vscode.Range(vimState.cursorPosition, vimState.cursorPosition) + new vscode.Range(vimState.cursorStopPosition, vimState.cursorStopPosition) ); } } @@ -1323,7 +1323,7 @@ export class ModeHandler implements vscode.Disposable { // Fake block cursor with text decoration. Unfortunately we can't have a cursor // in the middle of a selection natively, which is what we need for Visual Mode. if (this.currentMode.name === ModeName.Visual) { - for (const { start: cursorStart, stop: cursorStop } of vimState.allCursors) { + for (const { start: cursorStart, stop: cursorStop } of vimState.cursors) { if (cursorStart.isEarlierThan(cursorStop)) { cursorRange.push(new vscode.Range(cursorStop.getLeft(), cursorStop)); } else { @@ -1331,7 +1331,7 @@ export class ModeHandler implements vscode.Disposable { } } } else { - for (const { stop: cursorStop } of vimState.allCursors) { + for (const { stop: cursorStop } of vimState.cursors) { cursorRange.push(new vscode.Range(cursorStop, cursorStop.getRight())); } } @@ -1360,7 +1360,7 @@ export class ModeHandler implements vscode.Disposable { searchRanges.push.apply(searchRanges, searchState.matchRanges); - const { start, end, match } = searchState.getNextSearchMatchRange(vimState.cursorPosition); + const { start, end, match } = searchState.getNextSearchMatchRange(vimState.cursorStopPosition); if (match) { searchRanges.push(new vscode.Range(start, end)); @@ -1371,7 +1371,7 @@ export class ModeHandler implements vscode.Disposable { const easyMotionHighlightRanges = this.currentMode.name === ModeName.EasyMotionInputMode ? vimState.easyMotion.searchAction - .getMatches(vimState.cursorPosition, vimState) + .getMatches(vimState.cursorStopPosition, vimState) .map(x => x.toRange()) : []; this.vimState.editor.setDecorations(Decoration.EasyMotion, easyMotionHighlightRanges); @@ -1379,7 +1379,7 @@ export class ModeHandler implements vscode.Disposable { for (let i = 0; i < this.vimState.postponedCodeViewChanges.length; i++) { let viewChange = this.vimState.postponedCodeViewChanges[i]; await vscode.commands.executeCommand(viewChange.command, viewChange.args); - vimState.allCursors = await getCursorsAfterSync(); + vimState.cursors = await getCursorsAfterSync(); } this.vimState.postponedCodeViewChanges = []; @@ -1439,16 +1439,16 @@ export class ModeHandler implements vscode.Disposable { if (vimState.currentMode === ModeName.Insert) { // Check if the keypress is a closing bracket to a corresponding opening bracket right next to it - let result = PairMatcher.nextPairedChar(vimState.cursorPosition, key); + let result = PairMatcher.nextPairedChar(vimState.cursorStopPosition, key); if (result !== undefined) { - if (vimState.cursorPosition.compareTo(result) === 0) { + if (vimState.cursorStopPosition.compareTo(result) === 0) { return true; } } - result = PairMatcher.nextPairedChar(vimState.cursorPosition.getLeft(), key); + result = PairMatcher.nextPairedChar(vimState.cursorStopPosition.getLeft(), key); if (result !== undefined) { - if (vimState.cursorPosition.getLeftByCount(2).compareTo(result) === 0) { + if (vimState.cursorStopPosition.getLeftByCount(2).compareTo(result) === 0) { return true; } } diff --git a/src/neovim/neovim.ts b/src/neovim/neovim.ts index 1cc0275a47d..6d21c1dacc0 100644 --- a/src/neovim/neovim.ts +++ b/src/neovim/neovim.ts @@ -112,12 +112,12 @@ export class NeovimWrapper implements vscode.Disposable { }); const [rangeStart, rangeEnd] = [ - Position.EarlierOf(vimState.cursorPosition, vimState.cursorStartPosition), - Position.LaterOf(vimState.cursorPosition, vimState.cursorStartPosition), + Position.EarlierOf(vimState.cursorStopPosition, vimState.cursorStartPosition), + Position.LaterOf(vimState.cursorStopPosition, vimState.cursorStartPosition), ]; await this.nvim.callFunction('setpos', [ '.', - [0, vimState.cursorPosition.line + 1, vimState.cursorPosition.character, false], + [0, vimState.cursorStopPosition.line + 1, vimState.cursorStopPosition.character, false], ]); await this.nvim.callFunction('setpos', [ "'<", diff --git a/src/register/register.ts b/src/register/register.ts index ecfbaa5e92d..0b257b35f96 100644 --- a/src/register/register.ts +++ b/src/register/register.ts @@ -144,7 +144,7 @@ export class Register { (registerContent.text as string[]).push(content as string); - if (multicursorIndex === vimState.allCursors.length - 1) { + if (multicursorIndex === vimState.cursors.length - 1) { if (registerContent.isClipboardRegister) { let clipboardText: string = ''; @@ -181,14 +181,14 @@ export class Register { if (typeof appendToRegister.text === 'string') { createEmptyRegister = true; } else { - if ((appendToRegister.text as string[]).length !== vimState.allCursors.length) { + if ((appendToRegister.text as string[]).length !== vimState.cursors.length) { createEmptyRegister = true; } } if (createEmptyRegister) { Register.registers[register.toLowerCase()] = { - text: Array(vimState.allCursors.length).fill(''), + text: Array(vimState.cursors.length).fill(''), registerMode: vimState.effectiveRegisterMode, isClipboardRegister: Register.isClipboardRegister(register), }; @@ -367,7 +367,7 @@ export class Register { let registerText: string | string[]; if (vimState && vimState.isMultiCursor) { registerText = text.split('\n'); - if (registerText.length !== vimState.allCursors.length) { + if (registerText.length !== vimState.cursors.length) { registerText = text; } } else { @@ -384,7 +384,7 @@ export class Register { registerText = text; } else { if (vimState && vimState.isMultiCursor && typeof text === 'object') { - if ((text as string[]).length === vimState.allCursors.length) { + if ((text as string[]).length === vimState.cursors.length) { registerText = text; } else { registerText = (text as string[]).join('\n'); diff --git a/src/state/vimState.ts b/src/state/vimState.ts index 0cc3a8246fc..4ecb25dac5a 100644 --- a/src/state/vimState.ts +++ b/src/state/vimState.ts @@ -122,11 +122,11 @@ export class VimState implements vscode.Disposable { /** * The position the cursor will be when this action finishes. */ - public get cursorPosition(): Position { - return this.allCursors[0].stop; + public get cursorStopPosition(): Position { + return this.cursors[0].stop; } - public set cursorPosition(value: Position) { - this.allCursors[0] = this.allCursors[0].withNewStop(value); + public set cursorStopPosition(value: Position) { + this.cursors[0] = this.cursors[0].withNewStop(value); } /** @@ -135,30 +135,29 @@ export class VimState implements vscode.Disposable { * actually starts e.g. if you use the "aw" text motion in the middle of a word. */ public get cursorStartPosition(): Position { - return this.allCursors[0].start; + return this.cursors[0].start; } public set cursorStartPosition(value: Position) { - this.allCursors[0] = this.allCursors[0].withNewStart(value); + this.cursors[0] = this.cursors[0].withNewStart(value); } /** - * In Multi Cursor Mode, the position of every cursor. + * The position of every cursor. */ - private _allCursors: Range[] = [new Range(new Position(0, 0), new Position(0, 0))]; + private _cursors: Range[] = [new Range(new Position(0, 0), new Position(0, 0))]; - public get allCursors(): Range[] { - return this._allCursors; + public get cursors(): Range[] { + return this._cursors; } - - public set allCursors(cursors: Range[]) { + public set cursors(cursors: Range[]) { for (const cursor of cursors) { if (!cursor.isValid(this.editor)) { this.logger.warn(`invalid cursor position. ${cursor.toString()}.`); } } - this._allCursors = cursors; - this.isMultiCursor = this._allCursors.length > 1; + this._cursors = cursors; + this.isMultiCursor = this._cursors.length > 1; } public cursorPositionJustBeforeAnythingHappened = [new Position(0, 0)]; diff --git a/test/completion/lineCompletion.test.ts b/test/completion/lineCompletion.test.ts index 7e0b06361e6..d54645a7fa7 100644 --- a/test/completion/lineCompletion.test.ts +++ b/test/completion/lineCompletion.test.ts @@ -21,7 +21,7 @@ suite('Provide line completions', () => { teardown(cleanUpWorkspace); const setupTestWithLines = async lines => { - vimState.cursorPosition = new Position(0, 0); + vimState.cursorStopPosition = new Position(0, 0); await modeHandler.handleKeyEvent(''); await vimState.editor.edit(builder => { @@ -38,7 +38,7 @@ suite('Provide line completions', () => { await setupTestWithLines(lines); const expectedCompletions = ['a2', 'a1', 'a3', 'a4']; const topCompletions = getCompletionsForCurrentLine( - vimState.cursorPosition, + vimState.cursorStopPosition, vimState.editor.document )!.slice(0, expectedCompletions.length); @@ -50,7 +50,7 @@ suite('Provide line completions', () => { await setupTestWithLines(lines); const expectedCompletions = ['a 2', 'a1', 'a3 ', 'a4']; const topCompletions = getCompletionsForCurrentLine( - vimState.cursorPosition, + vimState.cursorStopPosition, vimState.editor.document )!.slice(0, expectedCompletions.length); @@ -62,7 +62,7 @@ suite('Provide line completions', () => { await setupTestWithLines(lines); const expectedCompletions = []; const completions = getCompletionsForCurrentLine( - vimState.cursorPosition, + vimState.cursorStopPosition, vimState.editor.document )!.slice(0, expectedCompletions.length); diff --git a/test/multicursor.test.ts b/test/multicursor.test.ts index 413f4a6ee32..e6a633584cc 100644 --- a/test/multicursor.test.ts +++ b/test/multicursor.test.ts @@ -23,7 +23,7 @@ suite('Multicursor', () => { await modeHandler.handleMultipleKeyEvents(['']); } - assertEqual(modeHandler.vimState.allCursors.length, 2, 'Cursor succesfully created.'); + assertEqual(modeHandler.vimState.cursors.length, 2, 'Cursor succesfully created.'); await modeHandler.handleMultipleKeyEvents(['c', 'w', '3', '3', '']); assertEqualLines(['33', '33']); }); @@ -39,7 +39,7 @@ suite('Multicursor', () => { await modeHandler.handleMultipleKeyEvents(['', '']); } - assertEqual(modeHandler.vimState.allCursors.length, 3, 'Cursor succesfully created.'); + assertEqual(modeHandler.vimState.cursors.length, 3, 'Cursor succesfully created.'); await modeHandler.handleMultipleKeyEvents(['c', 'w', '4', '4', '']); assertEqualLines(['44', '44', '44']); }); From 8ff4bad127534f71b7c5abf58039a2b66f790d55 Mon Sep 17 00:00:00 2001 From: Jason Poon Date: Fri, 1 Feb 2019 22:08:16 -0800 Subject: [PATCH 3/3] fix: validate cursor position on setters --- src/state/vimState.ts | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/state/vimState.ts b/src/state/vimState.ts index 4ecb25dac5a..62c0bdfd80c 100644 --- a/src/state/vimState.ts +++ b/src/state/vimState.ts @@ -120,26 +120,29 @@ export class VimState implements vscode.Disposable { public globalState = globalState; /** - * The position the cursor will be when this action finishes. + * The cursor position (start, stop) when this action finishes. */ + public get cursorStartPosition(): Position { + return this.cursors[0].start; + } + public set cursorStartPosition(value: Position) { + if (!value.isValid(this.editor)) { + this.logger.warn(`invalid cursor start position. ${value.toString()}.`); + } + this.cursors[0] = this.cursors[0].withNewStart(value); + } + public get cursorStopPosition(): Position { return this.cursors[0].stop; } public set cursorStopPosition(value: Position) { + if (!value.isValid(this.editor)) { + this.logger.warn(`invalid cursor stop position. ${value.toString()}.`); + } this.cursors[0] = this.cursors[0].withNewStop(value); } - /** - * The effective starting position of the movement, used along with cursorPosition to determine - * the range over which to run an Operator. May rarely be different than where the cursor - * actually starts e.g. if you use the "aw" text motion in the middle of a word. - */ - public get cursorStartPosition(): Position { - return this.cursors[0].start; - } - public set cursorStartPosition(value: Position) { - this.cursors[0] = this.cursors[0].withNewStart(value); - } + /** * The position of every cursor. @@ -149,14 +152,14 @@ export class VimState implements vscode.Disposable { public get cursors(): Range[] { return this._cursors; } - public set cursors(cursors: Range[]) { - for (const cursor of cursors) { + public set cursors(value: Range[]) { + for (const cursor of value) { if (!cursor.isValid(this.editor)) { this.logger.warn(`invalid cursor position. ${cursor.toString()}.`); } } - this._cursors = cursors; + this._cursors = value; this.isMultiCursor = this._cursors.length > 1; }