mirror of
https://github.com/microsoft/vscode.git
synced 2026-08-21 00:42:58 +01:00
Extract LineSelectionAction to its own file
This commit is contained in:
@@ -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({
|
||||
|
||||
@@ -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);
|
||||
@@ -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));
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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';
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
Reference in New Issue
Block a user