Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure cursor is in bounds. closes #3441 #3442

Merged
merged 3 commits into from
Feb 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
214 changes: 107 additions & 107 deletions src/actions/commands/actions.ts

Large diffs are not rendered by default.

42 changes: 21 additions & 21 deletions src/actions/commands/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,30 @@ class CommandEscInsertMode extends BaseCommand {
}

public async exec(position: Position, vimState: VimState): Promise<VimState> {
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()
);
}
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -374,7 +374,7 @@ class CommandCtrlW extends BaseCommand {

await TextEditor.delete(new vscode.Range(wordBegin, position));

vimState.cursorPosition = wordBegin;
vimState.cursorStopPosition = wordBegin;

return vimState;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion src/actions/motion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 22 additions & 22 deletions src/actions/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);

Expand All @@ -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;
}
Expand All @@ -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);

Expand All @@ -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;
}
Expand All @@ -492,7 +492,7 @@ class IndentOperatorInVisualModesIsAWeirdSpecialCase extends BaseOperator {
public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
// 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;
Expand All @@ -509,7 +509,7 @@ class IndentOperatorInVisualModesIsAWeirdSpecialCase extends BaseOperator {
}

await vimState.setCurrentMode(ModeName.Normal);
vimState.cursorPosition = start.getFirstLineNonBlankChar();
vimState.cursorStopPosition = start.getFirstLineNonBlankChar();

return vimState;
}
Expand All @@ -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;
}
Expand All @@ -542,7 +542,7 @@ class OutdentOperatorInVisualModesIsAWeirdSpecialCase extends BaseOperator {
public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
// 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;
Expand All @@ -559,7 +559,7 @@ class OutdentOperatorInVisualModesIsAWeirdSpecialCase extends BaseOperator {
}

await vimState.setCurrentMode(ModeName.Normal);
vimState.cursorPosition = start.getFirstLineNonBlankChar();
vimState.cursorStopPosition = start.getFirstLineNonBlankChar();

return vimState;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/actions/plugins/easymotion/easymotion.cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
Loading