Skip to content

Commit

Permalink
remove leading spaces when <Esc>... is pressed #685
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzzen authored and pengzhenqing committed Oct 25, 2016
1 parent 55450f4 commit 948a1ea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,21 @@ class CommandEscInsertMode extends BaseCommand {

public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.cursorPosition = position.getLeft();

// only remove leading spaces inserted by vscode.
// vscode only insert them when user enter a new line,
// ie, o/O in Normal mode or \n in Insert mode.
const lastActionBeforeEsc = vimState.currentFullAction[vimState.currentFullAction.length - 2];
if (['o', 'O', '\n'].indexOf(lastActionBeforeEsc) > -1 &&
vscode.window.activeTextEditor.document.languageId !== 'plaintext' &&
/^\s+$/.test(TextEditor.getLineAt(position).text)) {
vimState.recordedState.transformations.push({
type: "deleteRange",
range: new Range(position.getLineBegin(), position.getLineEnd())
});
vimState.cursorPosition = position.getLineBegin();
}

vimState.currentMode = ModeName.Normal;

if (vimState.historyTracker.currentContentChanges.length > 0) {
Expand Down
10 changes: 10 additions & 0 deletions test/mode/modeInsert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,14 @@ suite("Mode Insert", () => {

assertEqual(TextEditor.getSelection().start.character, 3, "<BS> moved cursor to correct position");
});

test("will not remove leading spaces input by user", async() => {
await modeHandler.handleMultipleKeyEvents([
'i',
' ', ' ',
'<Esc>'
]);

assertEqualLines([" "]);
});
});

0 comments on commit 948a1ea

Please sign in to comment.