Skip to content

Implements Balanced Bracket Pair Delete #134371

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions src/vs/editor/common/controller/cursorWordOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface DeleteWordContext {
autoClosingQuotes: EditorAutoClosingStrategy;
autoClosingPairs: AutoClosingPairs;
autoClosedCharacters: Range[];
deleteBracketPair: boolean;
}

export class WordOperations {
Expand Down
22 changes: 22 additions & 0 deletions src/vs/editor/contrib/wordOperations/test/wordOperations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import * as assert from 'assert';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { CoreEditingCommands } from 'vs/editor/browser/controller/coreCommands';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { EditorCommand } from 'vs/editor/browser/editorExtensions';
Expand Down Expand Up @@ -871,4 +872,25 @@ suite('WordOperations', () => {
assert.strictEqual(model.getValue(), '');
});
});

test('deleteWordLeft - brackets', () => {
const store = new DisposableStore();
try {
const mode = 'testMode';
store.add(LanguageConfigurationRegistry.register(mode, {
brackets: [
['{', '}'], ['[', ']'], ['(', ')']
]
}));

withTestCodeEditor(null, { model: createTextModel('{ text }', {}, mode) }, (editor, _) => {
const model = editor.getModel()!;
editor.setPosition(new Position(1, 9));
_deleteWordLeft.runEditorCommand(null, editor, { deleteBracketPair: true });
assert.strictEqual(model.getValue(), ' text ');
});
} finally {
store.dispose();
}
});
});
40 changes: 27 additions & 13 deletions src/vs/editor/contrib/wordOperations/wordOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { flatten } from 'vs/base/common/arrays';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { EditorAction, EditorCommand, ICommandOptions, registerEditorAction, registerEditorCommand, ServicesAccessor } from 'vs/editor/browser/editorExtensions';
Expand Down Expand Up @@ -330,7 +331,7 @@ export abstract class DeleteWordCommand extends EditorCommand {
this._wordNavigationType = opts.wordNavigationType;
}

public runEditorCommand(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void {
public runEditorCommand(accessor: ServicesAccessor | null, editor: ICodeEditor, args: any): void {
if (!editor.hasModel()) {
return;
}
Expand All @@ -342,8 +343,8 @@ export abstract class DeleteWordCommand extends EditorCommand {
const autoClosingPairs = LanguageConfigurationRegistry.getAutoClosingPairs(model.getLanguageId());
const viewModel = editor._getViewModel();

const commands = selections.map((sel) => {
const deleteRange = this._delete({
const commands = flatten(selections.map((sel) => {
const deleteRanges = this._delete({
wordSeparators,
model,
selection: sel,
Expand All @@ -352,38 +353,51 @@ export abstract class DeleteWordCommand extends EditorCommand {
autoClosingBrackets,
autoClosingQuotes,
autoClosingPairs,
autoClosedCharacters: viewModel.getCursorAutoClosedCharacters()
autoClosedCharacters: viewModel.getCursorAutoClosedCharacters(),
deleteBracketPair: Boolean(args?.deleteBracketPair)
}, this._wordNavigationType);
return new ReplaceCommand(deleteRange, '');
});
return deleteRanges.map(range => new ReplaceCommand(range, ''));
}));

editor.pushUndoStop();
editor.executeCommands(this.id, commands);
editor.pushUndoStop();
}

protected abstract _delete(ctx: DeleteWordContext, wordNavigationType: WordNavigationType): Range;
protected abstract _delete(ctx: DeleteWordContext, wordNavigationType: WordNavigationType): Range[];
}

export class DeleteWordLeftCommand extends DeleteWordCommand {
protected _delete(ctx: DeleteWordContext, wordNavigationType: WordNavigationType): Range {
protected _delete(ctx: DeleteWordContext, wordNavigationType: WordNavigationType): Range[] {
if (ctx.deleteBracketPair) {
const brackets = ctx.model.bracketPairs.getBracketPairsInRange(Range.fromPositions(ctx.selection.getStartPosition()));
const lastBracket = brackets[brackets.length - 1];
if (lastBracket) {
if (lastBracket.closingBracketRange &&
(lastBracket.closingBracketRange.getEndPosition().equals(ctx.selection.getStartPosition())
|| lastBracket.openingBracketRange.getEndPosition().equals(ctx.selection.getStartPosition()))) {
return [lastBracket.closingBracketRange, lastBracket.openingBracketRange];
}
}
}

let r = WordOperations.deleteWordLeft(ctx, wordNavigationType);
if (r) {
return r;
return [r];
}
return new Range(1, 1, 1, 1);
return [new Range(1, 1, 1, 1)];
}
}

export class DeleteWordRightCommand extends DeleteWordCommand {
protected _delete(ctx: DeleteWordContext, wordNavigationType: WordNavigationType): Range {
protected _delete(ctx: DeleteWordContext, wordNavigationType: WordNavigationType): Range[] {
let r = WordOperations.deleteWordRight(ctx, wordNavigationType);
if (r) {
return r;
return [r];
}
const lineCount = ctx.model.getLineCount();
const maxColumn = ctx.model.getLineMaxColumn(lineCount);
return new Range(lineCount, maxColumn, lineCount, maxColumn);
return [new Range(lineCount, maxColumn, lineCount, maxColumn)];
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/vs/editor/contrib/wordPartOperations/wordPartOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ export class DeleteWordPartLeft extends DeleteWordCommand {
});
}

protected _delete(ctx: DeleteWordContext, wordNavigationType: WordNavigationType): Range {
protected _delete(ctx: DeleteWordContext, wordNavigationType: WordNavigationType): Range[] {
let r = WordPartOperations.deleteWordPartLeft(ctx);
if (r) {
return r;
return [r];
}
return new Range(1, 1, 1, 1);
return [new Range(1, 1, 1, 1)];
}
}

Expand All @@ -56,14 +56,14 @@ export class DeleteWordPartRight extends DeleteWordCommand {
});
}

protected _delete(ctx: DeleteWordContext, wordNavigationType: WordNavigationType): Range {
protected _delete(ctx: DeleteWordContext, wordNavigationType: WordNavigationType): Range[] {
let r = WordPartOperations.deleteWordPartRight(ctx);
if (r) {
return r;
return [r];
}
const lineCount = ctx.model.getLineCount();
const maxColumn = ctx.model.getLineMaxColumn(lineCount);
return new Range(lineCount, maxColumn, lineCount, maxColumn);
return [new Range(lineCount, maxColumn, lineCount, maxColumn)];
}
}

Expand Down