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

remove leading spaces when <Esc>... is pressed #685 #962

Merged
merged 1 commit into from
Oct 25, 2016
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
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 inserts 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([" "]);
});
});