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
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
4 changes: 4 additions & 0 deletions src/matching/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export class PairMatcher {
*/
const toFind = this.pairings[charToMatch];

if (toFind === undefined) {
return undefined;
}

let stackHeight = closed ? 0 : 1;
let matchedPosition: Position | undefined = undefined;

Expand Down
38 changes: 18 additions & 20 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Position } from './../motion/position';
import { RegisterMode } from './../register/register';
import { showCmdLine } from '../../src/cmd_line/main';
import { Configuration } from '../../src/configuration/configuration';
import { PairMatcher } from './../matching/matcher';

export enum VimSpecialCommands {
Nothing,
Expand Down Expand Up @@ -1086,35 +1087,32 @@ 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];

if (vimState.currentMode === ModeName.Insert) {
if (key === undefined) {
return false;
}

if (TextEditor.getLineAt(vimState.cursorPosition).text.length <= 1) {
return false;
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, false);
if (result !== undefined) {
if (vimState.cursorPosition.compareTo(result) === 0) {
return true;
}
}

const letterToTheLeft = TextEditor.getLineAt(vimState.cursorPosition).text[vimState.cursorPosition.character - 2];
switch (key) {
case "}":
if (letterToTheLeft === "{") { return true; }
break;
case "]":
if (letterToTheLeft === "[") { return true; }
break;
case ")":
if (letterToTheLeft === "(") { return true; }
break;
case ">":
if (letterToTheLeft === "<") { return true; }
break;
default:
return false;
result = PairMatcher.nextPairedChar(vimState.cursorPosition.getLeft(), key, true);
if (result !== undefined) {
if (vimState.cursorPosition.getLeftByCount(2).compareTo(result) === 0) {
return true;
}
}
}

return false;
}

Expand Down
18 changes: 17 additions & 1 deletion src/motion/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export class Position extends vscode.Position {
}

/**
* Get the position *count* lines up from this position, but not lower
* Get the position *count* lines up from this position, but not higher
* than the end of the document.
*/
public getUpByCount(count = 0): Position {
Expand All @@ -266,6 +266,22 @@ export class Position extends vscode.Position {
);
}

/**
* Get the position *count* lines left from this position, but not farther
* than the beginning of the line
*/
public getLeftByCount(count = 0): Position {
return new Position(this.line, Math.max(0, this.character - count));
}

/**
* Get the position *count* lines right from this position, but not farther
* than the end of the line
*/
public getRightByCount(count = 0): Position {
return new Position(this.line, Math.min(TextEditor.getLineAt(this).text.length - 1, this.character + count));
}

/**
* Inclusive is true if we consider the current position a valid result, false otherwise.
*/
Expand Down