From 5a40238c40874c2ab3711e0cf5bbb2d71574b015 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Thu, 18 May 2017 21:46:23 +0200 Subject: [PATCH] Move LineInsertBefore out of the cursor --- src/vs/editor/browser/editor.all.ts | 1 + src/vs/editor/common/commonCodeEditor.ts | 6 ++- .../editor/common/controller/coreCommands.ts | 5 +- src/vs/editor/common/controller/cursor.ts | 5 -- .../common/controller/cursorTypeOperations.ts | 9 ++-- src/vs/editor/common/core/selection.ts | 9 +++- src/vs/editor/common/editorCommon.ts | 8 +++- .../linesOperations/common/linesOperations.ts | 9 +++- .../test/common/linesOperations.test.ts | 47 ++++++++++++++++++- .../test/common/controller/cursor.test.ts | 42 ----------------- src/vs/monaco.d.ts | 4 ++ 11 files changed, 83 insertions(+), 62 deletions(-) diff --git a/src/vs/editor/browser/editor.all.ts b/src/vs/editor/browser/editor.all.ts index 19ead3cbcaf..cd56e43c2fa 100644 --- a/src/vs/editor/browser/editor.all.ts +++ b/src/vs/editor/browser/editor.all.ts @@ -5,6 +5,7 @@ 'use strict'; +import 'vs/editor/common/controller/coreCommands'; import 'vs/editor/browser/widget/codeEditorWidget'; import 'vs/editor/browser/widget/diffEditorWidget'; diff --git a/src/vs/editor/common/commonCodeEditor.ts b/src/vs/editor/common/commonCodeEditor.ts index 2fef9136e64..3c8ecce71b1 100644 --- a/src/vs/editor/common/commonCodeEditor.ts +++ b/src/vs/editor/common/commonCodeEditor.ts @@ -13,7 +13,7 @@ import { ServiceCollection } from 'vs/platform/instantiation/common/serviceColle import { IContextKey, IContextKeyServiceTarget, IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; import { CommonEditorConfiguration } from 'vs/editor/common/config/commonEditorConfig'; import { Cursor } from 'vs/editor/common/controller/cursor'; -import { CursorColumns, IViewModelHelper, ICursors } from 'vs/editor/common/controller/cursorCommon'; +import { CursorColumns, IViewModelHelper, ICursors, CursorConfiguration } from 'vs/editor/common/controller/cursorCommon'; import { Position, IPosition } from 'vs/editor/common/core/position'; import { Range, IRange } from 'vs/editor/common/core/range'; import { Selection, ISelection } from 'vs/editor/common/core/selection'; @@ -698,6 +698,10 @@ export abstract class CommonCodeEditor extends Disposable implements editorCommo return this.cursor; } + public _getCursorConfiguration(): CursorConfiguration { + return this.cursor.context.config; + } + public pushUndoStop(): boolean { if (!this.model) { return false; diff --git a/src/vs/editor/common/controller/coreCommands.ts b/src/vs/editor/common/controller/coreCommands.ts index 6ad405c37a1..ae8a6e85e1a 100644 --- a/src/vs/editor/common/controller/coreCommands.ts +++ b/src/vs/editor/common/controller/coreCommands.ts @@ -1395,7 +1395,10 @@ export namespace CoreNavigationCommands { ); } }); -}; +} + +export namespace CoreEditingCommands { +} namespace Config { diff --git a/src/vs/editor/common/controller/cursor.ts b/src/vs/editor/common/controller/cursor.ts index aec77790274..42b712abd33 100644 --- a/src/vs/editor/common/controller/cursor.ts +++ b/src/vs/editor/common/controller/cursor.ts @@ -504,7 +504,6 @@ export class Cursor extends Disposable implements ICursors { private _registerHandlers(): void { let H = editorCommon.Handler; - this._handlers[H.LineInsertBefore] = (args) => this._lineInsertBefore(args); this._handlers[H.LineInsertAfter] = (args) => this._lineInsertAfter(args); this._handlers[H.LineBreakInsert] = (args) => this._lineBreakInsert(args); @@ -559,10 +558,6 @@ export class Cursor extends Disposable implements ICursors { return r; } - private _lineInsertBefore(args: CursorOperationArgs): EditOperationResult { - return TypeOperations.lineInsertBefore(this.context.config, this.context.model, this._getAllCursorsModelState()); - } - private _lineInsertAfter(args: CursorOperationArgs): EditOperationResult { return TypeOperations.lineInsertAfter(this.context.config, this.context.model, this._getAllCursorsModelState()); } diff --git a/src/vs/editor/common/controller/cursorTypeOperations.ts b/src/vs/editor/common/controller/cursorTypeOperations.ts index b231d46d1e4..7d9346cf06d 100644 --- a/src/vs/editor/common/controller/cursorTypeOperations.ts +++ b/src/vs/editor/common/controller/cursorTypeOperations.ts @@ -582,11 +582,11 @@ export class TypeOperations { }); } - public static lineInsertBefore(config: CursorConfiguration, model: ITokenizedModel, cursors: SingleCursorState[]): EditOperationResult { + public static lineInsertBefore(config: CursorConfiguration, model: ITokenizedModel, cursors: Selection[]): ICommand[] { let commands: ICommand[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; - let lineNumber = cursor.position.lineNumber; + let lineNumber = cursor.positionLineNumber; if (lineNumber === 1) { commands[i] = new ReplaceCommandWithoutChangingPosition(new Range(1, 1, 1, 1), '\n'); @@ -597,10 +597,7 @@ export class TypeOperations { commands[i] = this._enter(config, model, false, new Range(lineNumber, column, lineNumber, column)); } } - return new EditOperationResult(commands, { - shouldPushStackElementBefore: true, - shouldPushStackElementAfter: false, - }); + return commands; } public static lineInsertAfter(config: CursorConfiguration, model: ITokenizedModel, cursors: SingleCursorState[]): EditOperationResult { diff --git a/src/vs/editor/common/core/selection.ts b/src/vs/editor/common/core/selection.ts index 21f2066d982..10705ad4fe8 100644 --- a/src/vs/editor/common/core/selection.ts +++ b/src/vs/editor/common/core/selection.ts @@ -5,7 +5,7 @@ 'use strict'; import { Range } from 'vs/editor/common/core/range'; -import { IPosition } from 'vs/editor/common/core/position'; +import { Position, IPosition } from 'vs/editor/common/core/position'; /** * A selection in the editor. @@ -129,6 +129,13 @@ export class Selection extends Range { return new Selection(endLineNumber, endColumn, this.startLineNumber, this.startColumn); } + /** + * Get the position at `positionLineNumber` and `positionColumn`. + */ + public getPosition(): Position { + return new Position(this.positionLineNumber, this.positionColumn); + } + /** * Create a new selection with a different `selectionStartLineNumber` and `selectionStartColumn`. */ diff --git a/src/vs/editor/common/editorCommon.ts b/src/vs/editor/common/editorCommon.ts index 268c48f5d11..bca8965eafb 100644 --- a/src/vs/editor/common/editorCommon.ts +++ b/src/vs/editor/common/editorCommon.ts @@ -23,7 +23,7 @@ import { } from 'vs/editor/common/model/textModelEvents'; import * as editorOptions from 'vs/editor/common/config/editorOptions'; import { ICursorPositionChangedEvent, ICursorSelectionChangedEvent } from 'vs/editor/common/controller/cursorEvents'; -import { ICursors } from 'vs/editor/common/controller/cursorCommon'; +import { ICursors, CursorConfiguration } from 'vs/editor/common/controller/cursorCommon'; /** * Vertical Lane in the overview ruler of the editor. @@ -1918,6 +1918,11 @@ export interface ICommonCodeEditor extends IEditor { */ _getCursors(): ICursors; + /** + * @internal + */ + _getCursorConfiguration(): CursorConfiguration; + /** * Get all the decorations on a line (filtering out decorations from other editors). */ @@ -2078,7 +2083,6 @@ export var Handler = { Undo: 'undo', Redo: 'redo', - LineInsertBefore: 'lineInsertBefore', LineInsertAfter: 'lineInsertAfter', LineBreakInsert: 'lineBreakInsert', }; diff --git a/src/vs/editor/contrib/linesOperations/common/linesOperations.ts b/src/vs/editor/contrib/linesOperations/common/linesOperations.ts index 59acf7fcd23..0cb23b50393 100644 --- a/src/vs/editor/contrib/linesOperations/common/linesOperations.ts +++ b/src/vs/editor/contrib/linesOperations/common/linesOperations.ts @@ -18,6 +18,7 @@ import { editorAction, ServicesAccessor, IActionOptions, EditorAction, HandlerEd import { CopyLinesCommand } from './copyLinesCommand'; import { DeleteLinesCommand } from './deleteLinesCommand'; import { MoveLinesCommand } from './moveLinesCommand'; +import { TypeOperations } from 'vs/editor/common/controller/cursorTypeOperations'; // copy lines @@ -329,20 +330,24 @@ class OutdentLinesAction extends HandlerEditorAction { } @editorAction -class InsertLineBeforeAction extends HandlerEditorAction { +export class InsertLineBeforeAction extends EditorAction { constructor() { super({ id: 'editor.action.insertLineBefore', label: nls.localize('lines.insertBefore', "Insert Line Above"), alias: 'Insert Line Above', precondition: EditorContextKeys.writable, - handlerId: Handler.LineInsertBefore, kbOpts: { kbExpr: EditorContextKeys.textFocus, primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.Enter } }); } + + public run(accessor: ServicesAccessor, editor: ICommonCodeEditor): void { + editor.pushUndoStop(); + editor.executeCommands(this.id, TypeOperations.lineInsertBefore(editor._getCursorConfiguration(), editor.getModel(), editor.getSelections())); + } } @editorAction diff --git a/src/vs/editor/contrib/linesOperations/test/common/linesOperations.test.ts b/src/vs/editor/contrib/linesOperations/test/common/linesOperations.test.ts index 0239d6ee087..f32dd7ee822 100644 --- a/src/vs/editor/contrib/linesOperations/test/common/linesOperations.test.ts +++ b/src/vs/editor/contrib/linesOperations/test/common/linesOperations.test.ts @@ -6,9 +6,11 @@ import * as assert from 'assert'; import { Selection } from 'vs/editor/common/core/selection'; -import { Handler } from 'vs/editor/common/editorCommon'; +import { Position } from 'vs/editor/common/core/position'; +import { Handler, IModel } from 'vs/editor/common/editorCommon'; import { withMockCodeEditor } from 'vs/editor/test/common/mocks/mockCodeEditor'; -import { DeleteAllLeftAction, JoinLinesAction, TransposeAction, UpperCaseAction, LowerCaseAction, DeleteAllRightAction } from 'vs/editor/contrib/linesOperations/common/linesOperations'; +import { DeleteAllLeftAction, JoinLinesAction, TransposeAction, UpperCaseAction, LowerCaseAction, DeleteAllRightAction, InsertLineBeforeAction } from 'vs/editor/contrib/linesOperations/common/linesOperations'; +import { Cursor } from "vs/editor/common/controller/cursor"; suite('Editor Contrib - Line Operations', () => { suite('DeleteAllLeftAction', () => { @@ -488,4 +490,45 @@ suite('Editor Contrib - Line Operations', () => { }); }); }); + + test('InsertLineBeforeAction', function () { + function testInsertLineBefore(lineNumber: number, column: number, callback: (model: IModel, cursor: Cursor) => void): void { + const TEXT = [ + 'First line', + 'Second line', + 'Third line' + ]; + withMockCodeEditor(TEXT, {}, (editor, cursor) => { + editor.setPosition(new Position(lineNumber, column)); + let insertLineBeforeAction = new InsertLineBeforeAction(); + + insertLineBeforeAction.run(null, editor); + callback(editor.getModel(), cursor); + }); + } + + testInsertLineBefore(1, 3, (model, cursor) => { + assert.deepEqual(cursor.getSelection(), new Selection(1, 1, 1, 1)); + assert.equal(model.getLineContent(1), ''); + assert.equal(model.getLineContent(2), 'First line'); + assert.equal(model.getLineContent(3), 'Second line'); + assert.equal(model.getLineContent(4), 'Third line'); + }); + + testInsertLineBefore(2, 3, (model, cursor) => { + assert.deepEqual(cursor.getSelection(), new Selection(2, 1, 2, 1)); + assert.equal(model.getLineContent(1), 'First line'); + assert.equal(model.getLineContent(2), ''); + assert.equal(model.getLineContent(3), 'Second line'); + assert.equal(model.getLineContent(4), 'Third line'); + }); + + testInsertLineBefore(3, 3, (model, cursor) => { + assert.deepEqual(cursor.getSelection(), new Selection(3, 1, 3, 1)); + assert.equal(model.getLineContent(1), 'First line'); + assert.equal(model.getLineContent(2), 'Second line'); + assert.equal(model.getLineContent(3), ''); + assert.equal(model.getLineContent(4), 'Third line'); + }); + }); }); \ No newline at end of file diff --git a/src/vs/editor/test/common/controller/cursor.test.ts b/src/vs/editor/test/common/controller/cursor.test.ts index 134f327f9bf..8a505460120 100644 --- a/src/vs/editor/test/common/controller/cursor.test.ts +++ b/src/vs/editor/test/common/controller/cursor.test.ts @@ -1930,48 +1930,6 @@ suite('Editor Controller - Cursor Configuration', () => { mode.dispose(); }); - test('Insert line before', () => { - let testInsertLineBefore = (lineNumber: number, column: number, callback: (model: Model, cursor: Cursor) => void) => { - usingCursor({ - text: [ - 'First line', - 'Second line', - 'Third line' - ], - }, (model, cursor) => { - moveTo(cursor, lineNumber, column, false); - assertCursor(cursor, new Position(lineNumber, column)); - - cursorCommand(cursor, H.LineInsertBefore, null, 'keyboard'); - callback(model, cursor); - }); - }; - - testInsertLineBefore(1, 3, (model, cursor) => { - assertCursor(cursor, new Selection(1, 1, 1, 1)); - assert.equal(model.getLineContent(1), ''); - assert.equal(model.getLineContent(2), 'First line'); - assert.equal(model.getLineContent(3), 'Second line'); - assert.equal(model.getLineContent(4), 'Third line'); - }); - - testInsertLineBefore(2, 3, (model, cursor) => { - assertCursor(cursor, new Selection(2, 1, 2, 1)); - assert.equal(model.getLineContent(1), 'First line'); - assert.equal(model.getLineContent(2), ''); - assert.equal(model.getLineContent(3), 'Second line'); - assert.equal(model.getLineContent(4), 'Third line'); - }); - - testInsertLineBefore(3, 3, (model, cursor) => { - assertCursor(cursor, new Selection(3, 1, 3, 1)); - assert.equal(model.getLineContent(1), 'First line'); - assert.equal(model.getLineContent(2), 'Second line'); - assert.equal(model.getLineContent(3), ''); - assert.equal(model.getLineContent(4), 'Third line'); - }); - }); - test('Insert line after', () => { let testInsertLineAfter = (lineNumber: number, column: number, callback: (model: Model, cursor: Cursor) => void) => { usingCursor({ diff --git a/src/vs/monaco.d.ts b/src/vs/monaco.d.ts index 334b54b64b1..e12595a783f 100644 --- a/src/vs/monaco.d.ts +++ b/src/vs/monaco.d.ts @@ -724,6 +724,10 @@ declare module monaco { * Create a new selection with a different `positionLineNumber` and `positionColumn`. */ setEndPosition(endLineNumber: number, endColumn: number): Selection; + /** + * Get the position at `positionLineNumber` and `positionColumn`. + */ + getPosition(): Position; /** * Create a new selection with a different `selectionStartLineNumber` and `selectionStartColumn`. */ -- GitLab