From 05fc8ae187aca6640f5192e344b3a32b584a8b0b Mon Sep 17 00:00:00 2001 From: rebornix Date: Mon, 21 Nov 2016 10:55:58 -0800 Subject: [PATCH] Move delete all left action to contrib --- src/vs/editor/common/config/config.ts | 10 ----- src/vs/editor/common/controller/cursor.ts | 5 --- .../controller/cursorDeleteOperations.ts | 31 --------------- src/vs/editor/common/editorCommon.ts | 1 - .../linesOperations/common/linesOperations.ts | 38 +++++++++++++++++++ .../test/common/linesOperations.test.ts | 38 +++++++++++++++++++ src/vs/monaco.d.ts | 1 - 7 files changed, 76 insertions(+), 48 deletions(-) create mode 100644 src/vs/editor/contrib/linesOperations/test/common/linesOperations.test.ts diff --git a/src/vs/editor/common/config/config.ts b/src/vs/editor/common/config/config.ts index b4195b23008..4012250ae0a 100644 --- a/src/vs/editor/common/config/config.ts +++ b/src/vs/editor/common/config/config.ts @@ -603,16 +603,6 @@ registerCommand(new CoreCommand({ mac: { primary: KeyCode.Delete, secondary: [KeyMod.WinCtrl | KeyCode.KEY_D, KeyMod.WinCtrl | KeyCode.Delete] } } })); -registerCommand(new CoreCommand({ - id: H.DeleteAllLeft, - precondition: EditorContextKeys.Writable, - kbOpts: { - weight: CORE_WEIGHT, - kbExpr: EditorContextKeys.TextFocus, - primary: null, - mac: { primary: KeyMod.CtrlCmd | KeyCode.Backspace } - } -})); registerCommand(new CoreCommand({ id: H.DeleteAllRight, precondition: EditorContextKeys.Writable, diff --git a/src/vs/editor/common/controller/cursor.ts b/src/vs/editor/common/controller/cursor.ts index aaec879f9cc..dc8ed10c13a 100644 --- a/src/vs/editor/common/controller/cursor.ts +++ b/src/vs/editor/common/controller/cursor.ts @@ -1009,7 +1009,6 @@ export class Cursor extends EventEmitter { this._handlers[H.DeleteWordStartRight] = (ctx) => this._deleteWordRight(false, WordNavigationType.WordStart, ctx); this._handlers[H.DeleteWordEndRight] = (ctx) => this._deleteWordRight(false, WordNavigationType.WordEnd, ctx); - this._handlers[H.DeleteAllLeft] = (ctx) => this._deleteAllLeft(ctx); this._handlers[H.DeleteAllRight] = (ctx) => this._deleteAllRight(ctx); this._handlers[H.Cut] = (ctx) => this._cut(ctx); @@ -1518,10 +1517,6 @@ export class Cursor extends EventEmitter { return this._applyEditForAll(ctx, (cursor) => WordOperations.deleteWordRight(cursor.config, cursor.model, cursor.modelState, whitespaceHeuristics, wordNavigationType)); } - private _deleteAllLeft(ctx: IMultipleCursorOperationContext): boolean { - return this._applyEditForAll(ctx, (cursor) => DeleteOperations.deleteAllLeft(cursor.config, cursor.model, cursor.modelState)); - } - private _deleteAllRight(ctx: IMultipleCursorOperationContext): boolean { return this._applyEditForAll(ctx, (cursor) => DeleteOperations.deleteAllRight(cursor.config, cursor.model, cursor.modelState)); } diff --git a/src/vs/editor/common/controller/cursorDeleteOperations.ts b/src/vs/editor/common/controller/cursorDeleteOperations.ts index 81a52b0e5b9..a7dbe42b678 100644 --- a/src/vs/editor/common/controller/cursorDeleteOperations.ts +++ b/src/vs/editor/common/controller/cursorDeleteOperations.ts @@ -164,37 +164,6 @@ export class DeleteOperations { }); } - public static deleteAllLeft(config: CursorConfiguration, model: ICursorSimpleModel, cursor: SingleCursorState): EditOperationResult { - let r = this.autoClosingPairDelete(config, model, cursor); - if (r) { - // This was a case for an auto-closing pair delete - return r; - } - - let selection = cursor.selection; - - if (selection.isEmpty()) { - let position = cursor.position; - let lineNumber = position.lineNumber; - let column = position.column; - - if (column === 1) { - // Ignore deleting at beginning of line - return null; - } - - let deleteSelection = new Range(lineNumber, 1, lineNumber, column); - if (!deleteSelection.isEmpty()) { - return new EditOperationResult(new ReplaceCommand(deleteSelection, ''), { - shouldPushStackElementBefore: false, - shouldPushStackElementAfter: false - }); - } - } - - return this.deleteLeft(config, model, cursor); - } - public static cut(config: CursorConfiguration, model: ICursorSimpleModel, cursor: SingleCursorState, enableEmptySelectionClipboard: boolean): EditOperationResult { let selection = cursor.selection; diff --git a/src/vs/editor/common/editorCommon.ts b/src/vs/editor/common/editorCommon.ts index 027fc00651b..cc6294e6003 100644 --- a/src/vs/editor/common/editorCommon.ts +++ b/src/vs/editor/common/editorCommon.ts @@ -4583,7 +4583,6 @@ export var Handler = { DeleteWordStartRight: 'deleteWordStartRight', DeleteWordEndRight: 'deleteWordEndRight', - DeleteAllLeft: 'deleteAllLeft', DeleteAllRight: 'deleteAllRight', RemoveSecondaryCursors: 'removeSecondaryCursors', diff --git a/src/vs/editor/contrib/linesOperations/common/linesOperations.ts b/src/vs/editor/contrib/linesOperations/common/linesOperations.ts index 9baa609976d..8325a37262b 100644 --- a/src/vs/editor/contrib/linesOperations/common/linesOperations.ts +++ b/src/vs/editor/contrib/linesOperations/common/linesOperations.ts @@ -9,6 +9,8 @@ import { KeyCode, KeyMod, KeyChord } from 'vs/base/common/keyCodes'; import { SortLinesCommand } from 'vs/editor/contrib/linesOperations/common/sortLinesCommand'; import { TrimTrailingWhitespaceCommand } from 'vs/editor/common/commands/trimTrailingWhitespaceCommand'; import { EditorContextKeys, Handler, ICommand, ICommonCodeEditor } from 'vs/editor/common/editorCommon'; +import { ReplaceCommand } from 'vs/editor/common/commands/replaceCommand'; +import { Range } from 'vs/editor/common/core/range'; import { editorAction, ServicesAccessor, IActionOptions, EditorAction, HandlerEditorAction } from 'vs/editor/common/editorCommonExtensions'; import { CopyLinesCommand } from './copyLinesCommand'; import { DeleteLinesCommand } from './deleteLinesCommand'; @@ -346,3 +348,39 @@ class InsertLineAfterAction extends HandlerEditorAction { }); } } + +@editorAction +export class DeleteAllLeftAction extends EditorAction { + constructor() { + super({ + id: 'deleteAllLeft', + label: nls.localize('lines.deleteAllLeft', "Delete All Left"), + alias: 'Delete All Left', + precondition: EditorContextKeys.Writable, + kbOpts: { + kbExpr: EditorContextKeys.TextFocus, + primary: null, + mac: { primary: KeyMod.CtrlCmd | KeyCode.Backspace } + } + }); + } + + public run(accessor: ServicesAccessor, editor: ICommonCodeEditor): void { + let selections = editor.getSelections(); + + let commands: ICommand[] = []; + + for (let i = 0, len = selections.length; i < len; i++) { + let selection = selections[i]; + + if (selection.isEmpty()) { + console.log(selection.startColumn); + commands[i] = new ReplaceCommand(new Range(selection.startLineNumber, 1, selection.startLineNumber, selection.startColumn), ''); + } else { + commands[i] = new ReplaceCommand(selection, ''); + } + } + + editor.executeCommands(this.id, commands); + } +} diff --git a/src/vs/editor/contrib/linesOperations/test/common/linesOperations.test.ts b/src/vs/editor/contrib/linesOperations/test/common/linesOperations.test.ts new file mode 100644 index 00000000000..c8f9cc5e4e1 --- /dev/null +++ b/src/vs/editor/contrib/linesOperations/test/common/linesOperations.test.ts @@ -0,0 +1,38 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ +'use strict'; + +import * as assert from 'assert'; +import { Selection } from 'vs/editor/common/core/selection'; +import { withMockCodeEditor } from 'vs/editor/test/common/mocks/mockCodeEditor'; +import { DeleteAllLeftAction } from 'vs/editor/contrib/linesOperations/common/linesOperations'; + +suite('Editor Contrib - Line Operations', () => { + test('delete all left', function () { + withMockCodeEditor( + [ + 'one', + 'two', + 'three', + 'one two three four' + ], {}, (editor, cursor) => { + let model = editor.getModel(); + let joinLinesAction = new DeleteAllLeftAction(); + + editor.setSelection(new Selection(1, 2, 1, 2)); + joinLinesAction.run(null, editor); + assert.equal(model.getLineContent(1), 'ne', '001'); + + editor.setSelections([new Selection(2, 2, 2, 2), new Selection(3, 2, 3, 2)]); + joinLinesAction.run(null, editor); + assert.equal(model.getLineContent(2), 'wo', '002'); + assert.equal(model.getLineContent(3), 'hree', '003'); + + editor.setSelections([new Selection(4, 5, 4, 5), new Selection(4, 15, 4, 15)]); + joinLinesAction.run(null, editor); + assert.equal(model.getLineContent(4), 'four', '004'); + }); + }); +}); \ No newline at end of file diff --git a/src/vs/monaco.d.ts b/src/vs/monaco.d.ts index 69a12985a59..c4c392f603c 100644 --- a/src/vs/monaco.d.ts +++ b/src/vs/monaco.d.ts @@ -3406,7 +3406,6 @@ declare module monaco.editor { DeleteWordRight: string; DeleteWordStartRight: string; DeleteWordEndRight: string; - DeleteAllLeft: string; DeleteAllRight: string; RemoveSecondaryCursors: string; CancelSelection: string; -- GitLab