Skip to content

Commit

Permalink
Fixes #8574: Implement transpose letters command
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima authored and VS Code committed Jan 23, 2017
1 parent 72ee065 commit c907112
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/vs/editor/browser/editor.all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down
68 changes: 68 additions & 0 deletions src/vs/editor/contrib/caretOperations/common/transpose.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit c907112

Please sign in to comment.