From c907112fd35f567d8aca67f69da09369cfc2629b Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Mon, 23 Jan 2017 16:26:58 +0100 Subject: [PATCH] Fixes #8574: Implement transpose letters command --- src/vs/editor/browser/editor.all.ts | 3 +- .../caretOperations/common/transpose.ts | 68 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/vs/editor/contrib/caretOperations/common/transpose.ts diff --git a/src/vs/editor/browser/editor.all.ts b/src/vs/editor/browser/editor.all.ts index c8a312373584d..9438ae2ad3a4b 100644 --- a/src/vs/editor/browser/editor.all.ts +++ b/src/vs/editor/browser/editor.all.ts @@ -10,6 +10,8 @@ import 'vs/editor/browser/widget/diffEditorWidget'; import 'vs/editor/contrib/bracketMatching/common/bracketMatching'; import 'vs/css!vs/editor/contrib/bracketMatching/browser/bracketMatching'; +import 'vs/editor/contrib/caretOperations/common/caretOperations'; +import 'vs/editor/contrib/caretOperations/common/transpose'; import 'vs/editor/contrib/clipboard/browser/clipboard'; import 'vs/editor/contrib/codelens/browser/codelens'; import 'vs/editor/contrib/comment/common/comment'; @@ -24,7 +26,6 @@ import 'vs/css!vs/editor/contrib/inPlaceReplace/browser/inPlaceReplace'; import 'vs/editor/contrib/inPlaceReplace/common/inPlaceReplace'; import 'vs/editor/contrib/iPadShowKeyboard/browser/iPadShowKeyboard'; import 'vs/editor/contrib/linesOperations/common/linesOperations'; -import 'vs/editor/contrib/caretOperations/common/caretOperations'; import 'vs/editor/contrib/links/browser/links'; import 'vs/editor/contrib/multicursor/common/multicursor'; import 'vs/editor/contrib/multicursor/browser/menuPreventer'; diff --git a/src/vs/editor/contrib/caretOperations/common/transpose.ts b/src/vs/editor/contrib/caretOperations/common/transpose.ts new file mode 100644 index 0000000000000..e4f9eab5c3725 --- /dev/null +++ b/src/vs/editor/contrib/caretOperations/common/transpose.ts @@ -0,0 +1,68 @@ +/*--------------------------------------------------------------------------------------------- + * 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 nls from 'vs/nls'; +import { KeyCode, KeyMod } from 'vs/base/common/keyCodes'; +import { Range } from 'vs/editor/common/core/range'; +import { ICommand, ICommonCodeEditor, EditorContextKeys } from 'vs/editor/common/editorCommon'; +import { editorAction, EditorAction, ServicesAccessor } from 'vs/editor/common/editorCommonExtensions'; +import { ReplaceCommand } from 'vs/editor/common/commands/replaceCommand'; + +@editorAction +class TransposeLettersAction extends EditorAction { + + constructor() { + super({ + id: 'editor.action.transposeLetters', + label: nls.localize('transposeLetters.label', "Transpose Letters"), + alias: 'Transpose Letters', + precondition: EditorContextKeys.Writable, + kbOpts: { + kbExpr: EditorContextKeys.TextFocus, + primary: 0, + mac: { + primary: KeyMod.WinCtrl | KeyCode.KEY_T + } + } + }); + } + + public run(accessor: ServicesAccessor, editor: ICommonCodeEditor): void { + let model = editor.getModel(); + let commands: ICommand[] = []; + let selections = editor.getSelections(); + + for (let i = 0; i < selections.length; i++) { + let selection = selections[i]; + if (!selection.isEmpty()) { + continue; + } + let lineNumber = selection.startLineNumber; + let column = selection.startColumn; + if (column === 1) { + // at the beginning of line + continue; + } + let maxColumn = model.getLineMaxColumn(lineNumber); + if (column === maxColumn) { + // at the end of line + continue; + } + + let lineContent = model.getLineContent(lineNumber); + let charToTheLeft = lineContent.charAt(column - 2); + let charToTheRight = lineContent.charAt(column - 1); + + let replaceRange = new Range(lineNumber, column - 1, lineNumber, column + 1); + + commands.push(new ReplaceCommand(replaceRange, charToTheRight + charToTheLeft)); + } + + if (commands.length > 0) { + editor.executeCommands(this.id, commands); + } + } +}