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

Delete matching bracket upon backspace #1267

Merged
merged 7 commits into from
Feb 7, 2017
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
Prev Previous commit
Next Next commit
delete matching brackets when the opening bracket is deleted
Ryan Breen committed Feb 3, 2017
commit 794ad41ddbda6ccfdc447a49522c65eab171fdd9
28 changes: 27 additions & 1 deletion src/matching/matcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Position } from './../motion/position';
import { Position, PositionDiff } from './../motion/position';
import * as vscode from 'vscode';

/**
* PairMatcher finds the position matching the given character, respecting nested
@@ -62,4 +63,29 @@ export class PairMatcher {
// TODO(bell)
return undefined;
}

/**
* Given a current position, find an immediate following bracket and return the range. If
* no matching bracket is found immediately following the opening bracket, return undefined.
*/
static immediateMatchingBracket(currentPosition: Position): vscode.Range | undefined {
let deleteRange = new vscode.Range(currentPosition, currentPosition.getLeftThroughLineBreaks());
Copy link
Member

Choose a reason for hiding this comment

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

You can make all these declarations const.

let deleteText = vscode.window.activeTextEditor.document.getText(deleteRange);
let matchRange: vscode.Range | undefined;
let isNextMatch = false;

if ("{[(<".indexOf(deleteText) > -1) {
let matchPosition = currentPosition.add(new PositionDiff(0, 1));
if (matchPosition) {
Copy link
Member

Choose a reason for hiding this comment

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

Do you need this if statement?

matchRange = new vscode.Range(matchPosition, matchPosition.getLeftThroughLineBreaks());
isNextMatch = vscode.window.activeTextEditor.document.getText(matchRange) === PairMatcher.pairings[deleteText].match;
}
}

if (isNextMatch && matchRange) {
return matchRange;
}

return undefined;
}
}
2 changes: 2 additions & 0 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
@@ -1291,6 +1291,8 @@ export class ModeHandler implements vscode.Disposable {
break;

case "deleteText":
let matchRange = PairMatcher.immediateMatchingBracket(command.position);
if (matchRange) { edit.delete(matchRange); }
edit.delete(new vscode.Range(command.position, command.position.getLeftThroughLineBreaks()));
break;

18 changes: 18 additions & 0 deletions test/mode/modeInsert.test.ts
Original file line number Diff line number Diff line change
@@ -174,6 +174,24 @@ suite("Mode Insert", () => {
assertEqualLines([" "]);
});

test("will remove closing bracket", async() => {
await modeHandler.handleMultipleKeyEvents([
'i',
'(',
'<Esc>'
]);

assertEqualLines(["()"]);

await modeHandler.handleMultipleKeyEvents([
'a',
'<BS>',
'<Esc>'
]);

assertEqualLines([""]);
});

newTest({
title: "Can perform <ctrl+o> to exit and perform one command in normal",
start: ['testtest|'],