From a44d73cc29932a6b38e92b9e6aab10db6f3bb08c Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Mon, 22 Nov 2021 22:57:34 +0100 Subject: [PATCH] Extract `LineSelectionAction` to its own file --- .../editor/browser/controller/coreCommands.ts | 32 -------- .../contrib/lineSelection/lineSelection.ts | 46 ++++++++++++ .../lineSelection/test/lineSelection.test.ts | 74 +++++++++++++++++++ src/vs/editor/editor.all.ts | 1 + .../test/browser/controller/cursor.test.ts | 41 ---------- 5 files changed, 121 insertions(+), 73 deletions(-) create mode 100644 src/vs/editor/contrib/lineSelection/lineSelection.ts create mode 100644 src/vs/editor/contrib/lineSelection/test/lineSelection.test.ts diff --git a/src/vs/editor/browser/controller/coreCommands.ts b/src/vs/editor/browser/controller/coreCommands.ts index a9b05a78192..b5a9ad8ad9a 100644 --- a/src/vs/editor/browser/controller/coreCommands.ts +++ b/src/vs/editor/browser/controller/coreCommands.ts @@ -22,7 +22,6 @@ import { Range } from 'vs/editor/common/core/range'; import { Handler, ScrollType } from 'vs/editor/common/editorCommon'; import { EditorContextKeys } from 'vs/editor/common/editorContextKeys'; import { VerticalRevealType } from 'vs/editor/common/view/viewEvents'; -import { MenuId } from 'vs/platform/actions/common/actions'; import { ICommandHandlerDescription } from 'vs/platform/commands/common/commands'; import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; @@ -1519,37 +1518,6 @@ export namespace CoreNavigationCommands { precondition: undefined })); - export const ExpandLineSelection: CoreEditorCommand = registerEditorCommand(new class extends CoreEditorCommand { - constructor() { - super({ - id: 'expandLineSelection', - precondition: undefined, - kbOpts: { - weight: CORE_WEIGHT, - kbExpr: EditorContextKeys.textInputFocus, - primary: KeyMod.CtrlCmd | KeyCode.KeyL - }, - menuOpts: [{ - menuId: MenuId.CommandPalette, - group: '', - title: nls.localize('expandLineSelection', "Expand Line Selection"), - order: 1 - }] - }); - } - - public runCoreEditorCommand(viewModel: IViewModel, args: any): void { - viewModel.model.pushStackElement(); - viewModel.setCursorStates( - args.source, - CursorChangeReason.Explicit, - CursorMoveCommands.expandLineSelection(viewModel, viewModel.getCursorStates()) - ); - viewModel.revealPrimaryCursor(args.source, true); - } - - }); - export const CancelSelection: CoreEditorCommand = registerEditorCommand(new class extends CoreEditorCommand { constructor() { super({ diff --git a/src/vs/editor/contrib/lineSelection/lineSelection.ts b/src/vs/editor/contrib/lineSelection/lineSelection.ts new file mode 100644 index 00000000000..f261fb7b6c9 --- /dev/null +++ b/src/vs/editor/contrib/lineSelection/lineSelection.ts @@ -0,0 +1,46 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { KeyCode, KeyMod } from 'vs/base/common/keyCodes'; +import { ICodeEditor } from 'vs/editor/browser/editorBrowser'; +import { EditorAction, registerEditorAction, ServicesAccessor } from 'vs/editor/browser/editorExtensions'; +import { CursorChangeReason } from 'vs/editor/common/controller/cursorEvents'; +import { CursorMoveCommands } from 'vs/editor/common/controller/cursorMoveCommands'; +import { EditorContextKeys } from 'vs/editor/common/editorContextKeys'; +import * as nls from 'vs/nls'; +import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry'; + +export class ExpandLineSelectionAction extends EditorAction { + constructor() { + super({ + id: 'expandLineSelection', + label: nls.localize('expandLineSelection', "Expand Line Selection"), + alias: 'Expand Line Selection', + precondition: undefined, + kbOpts: { + weight: KeybindingWeight.EditorCore, + kbExpr: EditorContextKeys.textInputFocus, + primary: KeyMod.CtrlCmd | KeyCode.KeyL + }, + }); + } + + public run(_accessor: ServicesAccessor, editor: ICodeEditor, args: any): void { + args = args || {}; + if (!editor.hasModel()) { + return; + } + const viewModel = editor._getViewModel(); + viewModel.model.pushStackElement(); + viewModel.setCursorStates( + args.source, + CursorChangeReason.Explicit, + CursorMoveCommands.expandLineSelection(viewModel, viewModel.getCursorStates()) + ); + viewModel.revealPrimaryCursor(args.source, true); + } +} + +registerEditorAction(ExpandLineSelectionAction); diff --git a/src/vs/editor/contrib/lineSelection/test/lineSelection.test.ts b/src/vs/editor/contrib/lineSelection/test/lineSelection.test.ts new file mode 100644 index 00000000000..1e475a8fe41 --- /dev/null +++ b/src/vs/editor/contrib/lineSelection/test/lineSelection.test.ts @@ -0,0 +1,74 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import * as assert from 'assert'; +import type { ICodeEditor } from 'vs/editor/browser/editorBrowser'; +import { EditorAction } from 'vs/editor/browser/editorExtensions'; +import { Position } from 'vs/editor/common/core/position'; +import { Selection } from 'vs/editor/common/core/selection'; +import { ExpandLineSelectionAction } from 'vs/editor/contrib/lineSelection/lineSelection'; +import { withTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor'; + +function executeAction(action: EditorAction, editor: ICodeEditor): void { + action.run(null!, editor, undefined); +} + +suite('LineSelection', () => { + test('', () => { + const LINE1 = ' \tMy First Line\t '; + const LINE2 = '\tMy Second Line'; + const LINE3 = ' Third Line🐶'; + const LINE4 = ''; + const LINE5 = '1'; + + const TEXT = + LINE1 + '\r\n' + + LINE2 + '\n' + + LINE3 + '\n' + + LINE4 + '\r\n' + + LINE5; + + withTestCodeEditor(TEXT, {}, (editor, viewModel) => { + const action = new ExpandLineSelectionAction(); + + // 0 1 2 + // 01234 56789012345678 0 + // let LINE1 = ' \tMy First Line\t '; + editor.setPosition(new Position(1, 1)); + executeAction(action, editor); + assert.deepStrictEqual(editor.getSelection(), new Selection(1, 1, 2, 1)); + + editor.setPosition(new Position(1, 2)); + executeAction(action, editor); + assert.deepStrictEqual(editor.getSelection(), new Selection(1, 1, 2, 1)); + + editor.setPosition(new Position(1, 5)); + executeAction(action, editor); + assert.deepStrictEqual(editor.getSelection(), new Selection(1, 1, 2, 1)); + + editor.setPosition(new Position(1, 19)); + executeAction(action, editor); + assert.deepStrictEqual(editor.getSelection(), new Selection(1, 1, 2, 1)); + + editor.setPosition(new Position(1, 20)); + executeAction(action, editor); + assert.deepStrictEqual(editor.getSelection(), new Selection(1, 1, 2, 1)); + + editor.setPosition(new Position(1, 21)); + executeAction(action, editor); + assert.deepStrictEqual(editor.getSelection(), new Selection(1, 1, 2, 1)); + executeAction(action, editor); + assert.deepStrictEqual(editor.getSelection(), new Selection(1, 1, 3, 1)); + executeAction(action, editor); + assert.deepStrictEqual(editor.getSelection(), new Selection(1, 1, 4, 1)); + executeAction(action, editor); + assert.deepStrictEqual(editor.getSelection(), new Selection(1, 1, 5, 1)); + executeAction(action, editor); + assert.deepStrictEqual(editor.getSelection(), new Selection(1, 1, 5, LINE5.length + 1)); + executeAction(action, editor); + assert.deepStrictEqual(editor.getSelection(), new Selection(1, 1, 5, LINE5.length + 1)); + }); + }); +}); diff --git a/src/vs/editor/editor.all.ts b/src/vs/editor/editor.all.ts index c0b706b55bd..24db1953ce0 100644 --- a/src/vs/editor/editor.all.ts +++ b/src/vs/editor/editor.all.ts @@ -32,6 +32,7 @@ import 'vs/editor/contrib/hover/hover'; import 'vs/editor/contrib/indentation/indentation'; import 'vs/editor/contrib/inlayHints/inlayHintsController'; import 'vs/editor/contrib/inPlaceReplace/inPlaceReplace'; +import 'vs/editor/contrib/lineSelection/lineSelection'; import 'vs/editor/contrib/linesOperations/linesOperations'; import 'vs/editor/contrib/linkedEditing/linkedEditing'; import 'vs/editor/contrib/links/links'; diff --git a/src/vs/editor/test/browser/controller/cursor.test.ts b/src/vs/editor/test/browser/controller/cursor.test.ts index 8a1dc5e697c..d98fa99b351 100644 --- a/src/vs/editor/test/browser/controller/cursor.test.ts +++ b/src/vs/editor/test/browser/controller/cursor.test.ts @@ -739,47 +739,6 @@ suite('Editor Controller - Cursor', () => { }); }); - test('expandLineSelection', () => { - runTest((editor, viewModel) => { - // 0 1 2 - // 01234 56789012345678 0 - // let LINE1 = ' \tMy First Line\t '; - moveTo(editor, viewModel, 1, 1); - CoreNavigationCommands.ExpandLineSelection.runCoreEditorCommand(viewModel, {}); - assertCursor(viewModel, new Selection(1, 1, 2, 1)); - - moveTo(editor, viewModel, 1, 2); - CoreNavigationCommands.ExpandLineSelection.runCoreEditorCommand(viewModel, {}); - assertCursor(viewModel, new Selection(1, 1, 2, 1)); - - moveTo(editor, viewModel, 1, 5); - CoreNavigationCommands.ExpandLineSelection.runCoreEditorCommand(viewModel, {}); - assertCursor(viewModel, new Selection(1, 1, 2, 1)); - - moveTo(editor, viewModel, 1, 19); - CoreNavigationCommands.ExpandLineSelection.runCoreEditorCommand(viewModel, {}); - assertCursor(viewModel, new Selection(1, 1, 2, 1)); - - moveTo(editor, viewModel, 1, 20); - CoreNavigationCommands.ExpandLineSelection.runCoreEditorCommand(viewModel, {}); - assertCursor(viewModel, new Selection(1, 1, 2, 1)); - - moveTo(editor, viewModel, 1, 21); - CoreNavigationCommands.ExpandLineSelection.runCoreEditorCommand(viewModel, {}); - assertCursor(viewModel, new Selection(1, 1, 2, 1)); - CoreNavigationCommands.ExpandLineSelection.runCoreEditorCommand(viewModel, {}); - assertCursor(viewModel, new Selection(1, 1, 3, 1)); - CoreNavigationCommands.ExpandLineSelection.runCoreEditorCommand(viewModel, {}); - assertCursor(viewModel, new Selection(1, 1, 4, 1)); - CoreNavigationCommands.ExpandLineSelection.runCoreEditorCommand(viewModel, {}); - assertCursor(viewModel, new Selection(1, 1, 5, 1)); - CoreNavigationCommands.ExpandLineSelection.runCoreEditorCommand(viewModel, {}); - assertCursor(viewModel, new Selection(1, 1, 5, LINE5.length + 1)); - CoreNavigationCommands.ExpandLineSelection.runCoreEditorCommand(viewModel, {}); - assertCursor(viewModel, new Selection(1, 1, 5, LINE5.length + 1)); - }); - }); - // --------- eventing test('no move doesn\'t trigger event', () => {