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

Respect editor.autoClosingBrackets and editor.autoClosingQuotes when deleting a bracket/quote #3941

Merged
merged 2 commits into from
Aug 28, 2019
Merged
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
26 changes: 20 additions & 6 deletions src/common/matching/matcher.ts
Original file line number Diff line number Diff line change
@@ -158,16 +158,30 @@ export class PairMatcher {
return undefined;
}

static shouldDeleteMatchingBracket(type: 'bracket' | 'quote') {
// Don't delete bracket unless autoClosingBrackets is set.
const cfgKey = type === 'bracket' ? 'editor.autoClosingBrackets' : 'editor.autoClosingQuotes';
const cfgValue = configuration.getConfiguration().get(cfgKey);

if (cfgValue === 'never') {
return false;
} else if (cfgValue === 'languageDefined') {
// TODO: if possible, we should look up and use the current language's configuration
return true;
}

return true;
}

/**
* 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.
* This is intended for the deletion of such pairs, so it respects `editor.autoClosingBrackets`.
*/
static immediateMatchingBracket(currentPosition: Position): vscode.Range | undefined {
// Don't delete bracket unless autoClosingBrackets is set
if (!configuration.getConfiguration().get('editor.autoClosingBrackets')) {
return undefined;
}

const charactersToMatch =
(this.shouldDeleteMatchingBracket('bracket') ? '{[(' : '') +
(this.shouldDeleteMatchingBracket('quote') ? '"\'`' : '');
const deleteRange = new vscode.Range(
currentPosition,
currentPosition.getLeftThroughLineBreaks()
@@ -176,7 +190,7 @@ export class PairMatcher {
let matchRange: vscode.Range | undefined;
let isNextMatch = false;

if ('{[("\'`'.indexOf(deleteText) > -1) {
if (charactersToMatch.indexOf(deleteText) > -1) {
const matchPosition = currentPosition.add(new PositionDiff(0, 1));
matchRange = new vscode.Range(matchPosition, matchPosition.getLeftThroughLineBreaks());
isNextMatch =