From 47f69deed8f3401e7d9f909d93c89f84d7da9f42 Mon Sep 17 00:00:00 2001 From: Richard Natal Date: Sat, 19 Dec 2015 13:42:57 -0200 Subject: [PATCH] Implementation of editor.action.insertCursorAsEndOfEachSelectedLine - inspired by issue #950 and pull #961 - the same behaviour of Sublime Text ctrl+shift+l command... Signed-off-by: Richard Natal --- .../contrib/multicursor/common/multicursor.ts | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/vs/editor/contrib/multicursor/common/multicursor.ts b/src/vs/editor/contrib/multicursor/common/multicursor.ts index cc257a8daeba3..c62cf631f2c41 100644 --- a/src/vs/editor/contrib/multicursor/common/multicursor.ts +++ b/src/vs/editor/contrib/multicursor/common/multicursor.ts @@ -6,7 +6,8 @@ import nls = require('vs/nls'); import {CommonEditorRegistry, ContextKey, EditorActionDescriptor} from 'vs/editor/common/editorCommonExtensions'; -import {HandlerEditorAction} from 'vs/editor/common/editorAction'; +import {TPromise} from 'vs/base/common/winjs.base'; +import {EditorAction, HandlerEditorAction} from 'vs/editor/common/editorAction'; import EditorCommon = require('vs/editor/common/editorCommon'); import {INullService} from 'vs/platform/instantiation/common/instantiation'; import {KeyMod, KeyCode} from 'vs/base/common/keyCodes'; @@ -27,6 +28,44 @@ class InsertCursorBelow extends HandlerEditorAction { } } +class InsertCursorsFromLineSelection extends EditorAction { + static ID = 'editor.action.insertCursorAtEndOfEachLineSelected'; + + constructor(descriptor:EditorCommon.IEditorActionDescriptorData, editor:EditorCommon.ICommonCodeEditor, @INullService ns) { + super(descriptor, editor); + } + + public run(): TPromise { + let sel = this.editor.getSelection(); + if(!sel.isEmpty()) { + let model = this.editor.getModel(); + let sels = new Array(); + let sStart = sel.getStartPosition(); + let sEnd = sel.getEndPosition(); + for (var i = sStart.lineNumber; i <= sEnd.lineNumber; i++) { + if(i !== sEnd.lineNumber) { + let lEnd = model.getLineMaxColumn(i); + sels.push({ + selectionStartLineNumber: i, + selectionStartColumn: lEnd, + positionLineNumber: i, + positionColumn: lEnd + }); + } else if( sEnd.column > 0 ) { + sels.push({ + selectionStartLineNumber: sEnd.lineNumber, + selectionStartColumn: sEnd.column, + positionLineNumber: sEnd.lineNumber, + positionColumn: sEnd.column + }); + } + } + this.editor.setSelections(sels); + } + return TPromise.as(true); + } +} + // register actions CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(InsertCursorAbove, InsertCursorAbove.ID, nls.localize('mutlicursor.insertAbove', "Add Cursor Above"), { @@ -45,3 +84,7 @@ CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(InsertCurso secondary: [KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.DownArrow] } })); +CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(InsertCursorsFromLineSelection, InsertCursorsFromLineSelection.ID, nls.localize('mutlicursor.insertAtEndOfEachLineSelected', "Create multiple cursors from selected lines"), { + context: ContextKey.EditorTextFocus, + primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_I +}));