Skip to content

Commit

Permalink
Respect editor.autoClosingBrackets and editor.autoClosingQuotes w…
Browse files Browse the repository at this point in the history
…hen deleting a bracket/quote (#3941)

When we delete a "pairable" character like (, we look to the right to see if there's the matching ) and delete it is well. We should not do that if auto-matching is disabled in settings. This behavior regressed because the type of editor.autoClosingBrackets was changed from a bool to a more fine-grained enum. There's also editor.autoClosingQuotes now, so we should respect that too.
Fixes #3936
  • Loading branch information
J-Fields authored Aug 28, 2019
1 parent 60cf40d commit dca512c
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/common/matching/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 =
Expand Down

0 comments on commit dca512c

Please sign in to comment.