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

improves bracket undo behavior when vscode autocloses brackets #649

Merged
merged 4 commits into from
Aug 27, 2016
Merged
Changes from 1 commit
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
24 changes: 21 additions & 3 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ export class ModeHandler implements vscode.Disposable {
ModeHandler._statusBarItem.show();
}

// Return true if a new undo point should be created based on the keypress
// Return true if a new undo point should be created based on brackets and parenthesis
private createUndoPointForBrackets(vimState: VimState): boolean {
// }])> keys all start a new undo state when directly next to an {[(< opening character
const key = vimState.recordedState.actionKeys[vimState.recordedState.actionKeys.length - 1];
Expand All @@ -1081,6 +1081,7 @@ export class ModeHandler implements vscode.Disposable {
return false;
}

// Check if the keypress is a closing bracket to a corresponding opening bracket right next to it
const letterToTheLeft = TextEditor.getLineAt(vimState.cursorPosition).text[vimState.cursorPosition.character - 2];
switch (key) {
case "}":
Expand All @@ -1095,8 +1096,25 @@ export class ModeHandler implements vscode.Disposable {
case ">":
if (letterToTheLeft === "<") { return true; }
break;
default:
return false;
}

// This section is mostly for vscode auto closing brackets without a user inserting them
const letterToTheRight = TextEditor.getLineAt(vimState.cursorPosition).text[vimState.cursorPosition.character];
if (letterToTheRight !== undefined) {
switch (key) {
case "{":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally when there are giant switch statements that's a sign that you could better represent your stuff as a data structure. In this case, maybe something like this:

const pairings = { 
    "{" : "}",
    "[" : "]",
    "<" : ">",

    /* etc... */
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this idea would also let you DRY up this code a little bit. It's a little repetitive right now.

if (letterToTheRight === "}") { return true; }
break;
case "[":
if (letterToTheRight === "]") { return true; }
break;
case "(":
if (letterToTheRight === ")") { return true; }
break;
case "<":
if (letterToTheRight === ">") { return true; }
break;
}
}
}
return false;
Expand Down