prototype
This commit is contained in:
parent
44435f33e1
commit
690891b660
|
@ -57,6 +57,7 @@ export interface DeleteWordContext {
|
|||
autoClosingQuotes: EditorAutoClosingStrategy;
|
||||
autoClosingPairs: AutoClosingPairs;
|
||||
autoClosedCharacters: Range[];
|
||||
deleteBracketPair: boolean;
|
||||
}
|
||||
|
||||
export class WordOperations {
|
||||
|
|
|
@ -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';
|
||||
|
@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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';
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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)];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue