123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993 |
- /*---------------------------------------------------------------------------------------------
- * Copyright (c) Microsoft Corporation. All rights reserved.
- * Licensed under the MIT License. See License.txt in the project root for license information.
- *--------------------------------------------------------------------------------------------*/
- import { KeyChord } from '../../../base/common/keyCodes.js';
- import { CoreEditingCommands } from '../../browser/controller/coreCommands.js';
- import { EditorAction, registerEditorAction } from '../../browser/editorExtensions.js';
- import { ReplaceCommand, ReplaceCommandThatPreservesSelection, ReplaceCommandThatSelectsText } from '../../common/commands/replaceCommand.js';
- import { TrimTrailingWhitespaceCommand } from '../../common/commands/trimTrailingWhitespaceCommand.js';
- import { TypeOperations } from '../../common/controller/cursorTypeOperations.js';
- import { EditOperation } from '../../common/core/editOperation.js';
- import { Position } from '../../common/core/position.js';
- import { Range } from '../../common/core/range.js';
- import { Selection } from '../../common/core/selection.js';
- import { EditorContextKeys } from '../../common/editorContextKeys.js';
- import { CopyLinesCommand } from './copyLinesCommand.js';
- import { MoveLinesCommand } from './moveLinesCommand.js';
- import { SortLinesCommand } from './sortLinesCommand.js';
- import * as nls from '../../../nls.js';
- import { MenuId } from '../../../platform/actions/common/actions.js';
- // copy lines
- class AbstractCopyLinesAction extends EditorAction {
- constructor(down, opts) {
- super(opts);
- this.down = down;
- }
- run(_accessor, editor) {
- if (!editor.hasModel()) {
- return;
- }
- const selections = editor.getSelections().map((selection, index) => ({ selection, index, ignore: false }));
- selections.sort((a, b) => Range.compareRangesUsingStarts(a.selection, b.selection));
- // Remove selections that would result in copying the same line
- let prev = selections[0];
- for (let i = 1; i < selections.length; i++) {
- const curr = selections[i];
- if (prev.selection.endLineNumber === curr.selection.startLineNumber) {
- // these two selections would copy the same line
- if (prev.index < curr.index) {
- // prev wins
- curr.ignore = true;
- }
- else {
- // curr wins
- prev.ignore = true;
- prev = curr;
- }
- }
- }
- const commands = [];
- for (const selection of selections) {
- commands.push(new CopyLinesCommand(selection.selection, this.down, selection.ignore));
- }
- editor.pushUndoStop();
- editor.executeCommands(this.id, commands);
- editor.pushUndoStop();
- }
- }
- class CopyLinesUpAction extends AbstractCopyLinesAction {
- constructor() {
- super(false, {
- id: 'editor.action.copyLinesUpAction',
- label: nls.localize('lines.copyUp', "Copy Line Up"),
- alias: 'Copy Line Up',
- precondition: EditorContextKeys.writable,
- kbOpts: {
- kbExpr: EditorContextKeys.editorTextFocus,
- primary: 512 /* Alt */ | 1024 /* Shift */ | 16 /* UpArrow */,
- linux: { primary: 2048 /* CtrlCmd */ | 512 /* Alt */ | 1024 /* Shift */ | 16 /* UpArrow */ },
- weight: 100 /* EditorContrib */
- },
- menuOpts: {
- menuId: MenuId.MenubarSelectionMenu,
- group: '2_line',
- title: nls.localize({ key: 'miCopyLinesUp', comment: ['&& denotes a mnemonic'] }, "&&Copy Line Up"),
- order: 1
- }
- });
- }
- }
- class CopyLinesDownAction extends AbstractCopyLinesAction {
- constructor() {
- super(true, {
- id: 'editor.action.copyLinesDownAction',
- label: nls.localize('lines.copyDown', "Copy Line Down"),
- alias: 'Copy Line Down',
- precondition: EditorContextKeys.writable,
- kbOpts: {
- kbExpr: EditorContextKeys.editorTextFocus,
- primary: 512 /* Alt */ | 1024 /* Shift */ | 18 /* DownArrow */,
- linux: { primary: 2048 /* CtrlCmd */ | 512 /* Alt */ | 1024 /* Shift */ | 18 /* DownArrow */ },
- weight: 100 /* EditorContrib */
- },
- menuOpts: {
- menuId: MenuId.MenubarSelectionMenu,
- group: '2_line',
- title: nls.localize({ key: 'miCopyLinesDown', comment: ['&& denotes a mnemonic'] }, "Co&&py Line Down"),
- order: 2
- }
- });
- }
- }
- export class DuplicateSelectionAction extends EditorAction {
- constructor() {
- super({
- id: 'editor.action.duplicateSelection',
- label: nls.localize('duplicateSelection', "Duplicate Selection"),
- alias: 'Duplicate Selection',
- precondition: EditorContextKeys.writable,
- menuOpts: {
- menuId: MenuId.MenubarSelectionMenu,
- group: '2_line',
- title: nls.localize({ key: 'miDuplicateSelection', comment: ['&& denotes a mnemonic'] }, "&&Duplicate Selection"),
- order: 5
- }
- });
- }
- run(accessor, editor, args) {
- if (!editor.hasModel()) {
- return;
- }
- const commands = [];
- const selections = editor.getSelections();
- const model = editor.getModel();
- for (const selection of selections) {
- if (selection.isEmpty()) {
- commands.push(new CopyLinesCommand(selection, true));
- }
- else {
- const insertSelection = new Selection(selection.endLineNumber, selection.endColumn, selection.endLineNumber, selection.endColumn);
- commands.push(new ReplaceCommandThatSelectsText(insertSelection, model.getValueInRange(selection)));
- }
- }
- editor.pushUndoStop();
- editor.executeCommands(this.id, commands);
- editor.pushUndoStop();
- }
- }
- // move lines
- class AbstractMoveLinesAction extends EditorAction {
- constructor(down, opts) {
- super(opts);
- this.down = down;
- }
- run(_accessor, editor) {
- let commands = [];
- let selections = editor.getSelections() || [];
- const autoIndent = editor.getOption(9 /* autoIndent */);
- for (const selection of selections) {
- commands.push(new MoveLinesCommand(selection, this.down, autoIndent));
- }
- editor.pushUndoStop();
- editor.executeCommands(this.id, commands);
- editor.pushUndoStop();
- }
- }
- class MoveLinesUpAction extends AbstractMoveLinesAction {
- constructor() {
- super(false, {
- id: 'editor.action.moveLinesUpAction',
- label: nls.localize('lines.moveUp', "Move Line Up"),
- alias: 'Move Line Up',
- precondition: EditorContextKeys.writable,
- kbOpts: {
- kbExpr: EditorContextKeys.editorTextFocus,
- primary: 512 /* Alt */ | 16 /* UpArrow */,
- linux: { primary: 512 /* Alt */ | 16 /* UpArrow */ },
- weight: 100 /* EditorContrib */
- },
- menuOpts: {
- menuId: MenuId.MenubarSelectionMenu,
- group: '2_line',
- title: nls.localize({ key: 'miMoveLinesUp', comment: ['&& denotes a mnemonic'] }, "Mo&&ve Line Up"),
- order: 3
- }
- });
- }
- }
- class MoveLinesDownAction extends AbstractMoveLinesAction {
- constructor() {
- super(true, {
- id: 'editor.action.moveLinesDownAction',
- label: nls.localize('lines.moveDown', "Move Line Down"),
- alias: 'Move Line Down',
- precondition: EditorContextKeys.writable,
- kbOpts: {
- kbExpr: EditorContextKeys.editorTextFocus,
- primary: 512 /* Alt */ | 18 /* DownArrow */,
- linux: { primary: 512 /* Alt */ | 18 /* DownArrow */ },
- weight: 100 /* EditorContrib */
- },
- menuOpts: {
- menuId: MenuId.MenubarSelectionMenu,
- group: '2_line',
- title: nls.localize({ key: 'miMoveLinesDown', comment: ['&& denotes a mnemonic'] }, "Move &&Line Down"),
- order: 4
- }
- });
- }
- }
- export class AbstractSortLinesAction extends EditorAction {
- constructor(descending, opts) {
- super(opts);
- this.descending = descending;
- }
- run(_accessor, editor) {
- const selections = editor.getSelections() || [];
- for (const selection of selections) {
- if (!SortLinesCommand.canRun(editor.getModel(), selection, this.descending)) {
- return;
- }
- }
- let commands = [];
- for (let i = 0, len = selections.length; i < len; i++) {
- commands[i] = new SortLinesCommand(selections[i], this.descending);
- }
- editor.pushUndoStop();
- editor.executeCommands(this.id, commands);
- editor.pushUndoStop();
- }
- }
- export class SortLinesAscendingAction extends AbstractSortLinesAction {
- constructor() {
- super(false, {
- id: 'editor.action.sortLinesAscending',
- label: nls.localize('lines.sortAscending', "Sort Lines Ascending"),
- alias: 'Sort Lines Ascending',
- precondition: EditorContextKeys.writable
- });
- }
- }
- export class SortLinesDescendingAction extends AbstractSortLinesAction {
- constructor() {
- super(true, {
- id: 'editor.action.sortLinesDescending',
- label: nls.localize('lines.sortDescending', "Sort Lines Descending"),
- alias: 'Sort Lines Descending',
- precondition: EditorContextKeys.writable
- });
- }
- }
- export class DeleteDuplicateLinesAction extends EditorAction {
- constructor() {
- super({
- id: 'editor.action.removeDuplicateLines',
- label: nls.localize('lines.deleteDuplicates', "Delete Duplicate Lines"),
- alias: 'Delete Duplicate Lines',
- precondition: EditorContextKeys.writable
- });
- }
- run(_accessor, editor) {
- if (!editor.hasModel()) {
- return;
- }
- let model = editor.getModel();
- if (model.getLineCount() === 1 && model.getLineMaxColumn(1) === 1) {
- return;
- }
- let edits = [];
- let endCursorState = [];
- let linesDeleted = 0;
- for (let selection of editor.getSelections()) {
- let uniqueLines = new Set();
- let lines = [];
- for (let i = selection.startLineNumber; i <= selection.endLineNumber; i++) {
- let line = model.getLineContent(i);
- if (uniqueLines.has(line)) {
- continue;
- }
- lines.push(line);
- uniqueLines.add(line);
- }
- let selectionToReplace = new Selection(selection.startLineNumber, 1, selection.endLineNumber, model.getLineMaxColumn(selection.endLineNumber));
- let adjustedSelectionStart = selection.startLineNumber - linesDeleted;
- let finalSelection = new Selection(adjustedSelectionStart, 1, adjustedSelectionStart + lines.length - 1, lines[lines.length - 1].length);
- edits.push(EditOperation.replace(selectionToReplace, lines.join('\n')));
- endCursorState.push(finalSelection);
- linesDeleted += (selection.endLineNumber - selection.startLineNumber + 1) - lines.length;
- }
- editor.pushUndoStop();
- editor.executeEdits(this.id, edits, endCursorState);
- editor.pushUndoStop();
- }
- }
- export class TrimTrailingWhitespaceAction extends EditorAction {
- constructor() {
- super({
- id: TrimTrailingWhitespaceAction.ID,
- label: nls.localize('lines.trimTrailingWhitespace', "Trim Trailing Whitespace"),
- alias: 'Trim Trailing Whitespace',
- precondition: EditorContextKeys.writable,
- kbOpts: {
- kbExpr: EditorContextKeys.editorTextFocus,
- primary: KeyChord(2048 /* CtrlCmd */ | 41 /* KeyK */, 2048 /* CtrlCmd */ | 54 /* KeyX */),
- weight: 100 /* EditorContrib */
- }
- });
- }
- run(_accessor, editor, args) {
- let cursors = [];
- if (args.reason === 'auto-save') {
- // See https://github.com/editorconfig/editorconfig-vscode/issues/47
- // It is very convenient for the editor config extension to invoke this action.
- // So, if we get a reason:'auto-save' passed in, let's preserve cursor positions.
- cursors = (editor.getSelections() || []).map(s => new Position(s.positionLineNumber, s.positionColumn));
- }
- let selection = editor.getSelection();
- if (selection === null) {
- return;
- }
- let command = new TrimTrailingWhitespaceCommand(selection, cursors);
- editor.pushUndoStop();
- editor.executeCommands(this.id, [command]);
- editor.pushUndoStop();
- }
- }
- TrimTrailingWhitespaceAction.ID = 'editor.action.trimTrailingWhitespace';
- export class DeleteLinesAction extends EditorAction {
- constructor() {
- super({
- id: 'editor.action.deleteLines',
- label: nls.localize('lines.delete', "Delete Line"),
- alias: 'Delete Line',
- precondition: EditorContextKeys.writable,
- kbOpts: {
- kbExpr: EditorContextKeys.textInputFocus,
- primary: 2048 /* CtrlCmd */ | 1024 /* Shift */ | 41 /* KeyK */,
- weight: 100 /* EditorContrib */
- }
- });
- }
- run(_accessor, editor) {
- if (!editor.hasModel()) {
- return;
- }
- let ops = this._getLinesToRemove(editor);
- let model = editor.getModel();
- if (model.getLineCount() === 1 && model.getLineMaxColumn(1) === 1) {
- // Model is empty
- return;
- }
- let linesDeleted = 0;
- let edits = [];
- let cursorState = [];
- for (let i = 0, len = ops.length; i < len; i++) {
- const op = ops[i];
- let startLineNumber = op.startLineNumber;
- let endLineNumber = op.endLineNumber;
- let startColumn = 1;
- let endColumn = model.getLineMaxColumn(endLineNumber);
- if (endLineNumber < model.getLineCount()) {
- endLineNumber += 1;
- endColumn = 1;
- }
- else if (startLineNumber > 1) {
- startLineNumber -= 1;
- startColumn = model.getLineMaxColumn(startLineNumber);
- }
- edits.push(EditOperation.replace(new Selection(startLineNumber, startColumn, endLineNumber, endColumn), ''));
- cursorState.push(new Selection(startLineNumber - linesDeleted, op.positionColumn, startLineNumber - linesDeleted, op.positionColumn));
- linesDeleted += (op.endLineNumber - op.startLineNumber + 1);
- }
- editor.pushUndoStop();
- editor.executeEdits(this.id, edits, cursorState);
- editor.pushUndoStop();
- }
- _getLinesToRemove(editor) {
- // Construct delete operations
- let operations = editor.getSelections().map((s) => {
- let endLineNumber = s.endLineNumber;
- if (s.startLineNumber < s.endLineNumber && s.endColumn === 1) {
- endLineNumber -= 1;
- }
- return {
- startLineNumber: s.startLineNumber,
- selectionStartColumn: s.selectionStartColumn,
- endLineNumber: endLineNumber,
- positionColumn: s.positionColumn
- };
- });
- // Sort delete operations
- operations.sort((a, b) => {
- if (a.startLineNumber === b.startLineNumber) {
- return a.endLineNumber - b.endLineNumber;
- }
- return a.startLineNumber - b.startLineNumber;
- });
- // Merge delete operations which are adjacent or overlapping
- let mergedOperations = [];
- let previousOperation = operations[0];
- for (let i = 1; i < operations.length; i++) {
- if (previousOperation.endLineNumber + 1 >= operations[i].startLineNumber) {
- // Merge current operations into the previous one
- previousOperation.endLineNumber = operations[i].endLineNumber;
- }
- else {
- // Push previous operation
- mergedOperations.push(previousOperation);
- previousOperation = operations[i];
- }
- }
- // Push the last operation
- mergedOperations.push(previousOperation);
- return mergedOperations;
- }
- }
- export class IndentLinesAction extends EditorAction {
- constructor() {
- super({
- id: 'editor.action.indentLines',
- label: nls.localize('lines.indent', "Indent Line"),
- alias: 'Indent Line',
- precondition: EditorContextKeys.writable,
- kbOpts: {
- kbExpr: EditorContextKeys.editorTextFocus,
- primary: 2048 /* CtrlCmd */ | 89 /* BracketRight */,
- weight: 100 /* EditorContrib */
- }
- });
- }
- run(_accessor, editor) {
- const viewModel = editor._getViewModel();
- if (!viewModel) {
- return;
- }
- editor.pushUndoStop();
- editor.executeCommands(this.id, TypeOperations.indent(viewModel.cursorConfig, editor.getModel(), editor.getSelections()));
- editor.pushUndoStop();
- }
- }
- class OutdentLinesAction extends EditorAction {
- constructor() {
- super({
- id: 'editor.action.outdentLines',
- label: nls.localize('lines.outdent', "Outdent Line"),
- alias: 'Outdent Line',
- precondition: EditorContextKeys.writable,
- kbOpts: {
- kbExpr: EditorContextKeys.editorTextFocus,
- primary: 2048 /* CtrlCmd */ | 87 /* BracketLeft */,
- weight: 100 /* EditorContrib */
- }
- });
- }
- run(_accessor, editor) {
- CoreEditingCommands.Outdent.runEditorCommand(_accessor, editor, null);
- }
- }
- 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,
- kbOpts: {
- kbExpr: EditorContextKeys.editorTextFocus,
- primary: 2048 /* CtrlCmd */ | 1024 /* Shift */ | 3 /* Enter */,
- weight: 100 /* EditorContrib */
- }
- });
- }
- run(_accessor, editor) {
- const viewModel = editor._getViewModel();
- if (!viewModel) {
- return;
- }
- editor.pushUndoStop();
- editor.executeCommands(this.id, TypeOperations.lineInsertBefore(viewModel.cursorConfig, editor.getModel(), editor.getSelections()));
- }
- }
- export class InsertLineAfterAction extends EditorAction {
- constructor() {
- super({
- id: 'editor.action.insertLineAfter',
- label: nls.localize('lines.insertAfter', "Insert Line Below"),
- alias: 'Insert Line Below',
- precondition: EditorContextKeys.writable,
- kbOpts: {
- kbExpr: EditorContextKeys.editorTextFocus,
- primary: 2048 /* CtrlCmd */ | 3 /* Enter */,
- weight: 100 /* EditorContrib */
- }
- });
- }
- run(_accessor, editor) {
- const viewModel = editor._getViewModel();
- if (!viewModel) {
- return;
- }
- editor.pushUndoStop();
- editor.executeCommands(this.id, TypeOperations.lineInsertAfter(viewModel.cursorConfig, editor.getModel(), editor.getSelections()));
- }
- }
- export class AbstractDeleteAllToBoundaryAction extends EditorAction {
- run(_accessor, editor) {
- if (!editor.hasModel()) {
- return;
- }
- const primaryCursor = editor.getSelection();
- let rangesToDelete = this._getRangesToDelete(editor);
- // merge overlapping selections
- let effectiveRanges = [];
- for (let i = 0, count = rangesToDelete.length - 1; i < count; i++) {
- let range = rangesToDelete[i];
- let nextRange = rangesToDelete[i + 1];
- if (Range.intersectRanges(range, nextRange) === null) {
- effectiveRanges.push(range);
- }
- else {
- rangesToDelete[i + 1] = Range.plusRange(range, nextRange);
- }
- }
- effectiveRanges.push(rangesToDelete[rangesToDelete.length - 1]);
- let endCursorState = this._getEndCursorState(primaryCursor, effectiveRanges);
- let edits = effectiveRanges.map(range => {
- return EditOperation.replace(range, '');
- });
- editor.pushUndoStop();
- editor.executeEdits(this.id, edits, endCursorState);
- editor.pushUndoStop();
- }
- }
- export class DeleteAllLeftAction extends AbstractDeleteAllToBoundaryAction {
- constructor() {
- super({
- id: 'deleteAllLeft',
- label: nls.localize('lines.deleteAllLeft', "Delete All Left"),
- alias: 'Delete All Left',
- precondition: EditorContextKeys.writable,
- kbOpts: {
- kbExpr: EditorContextKeys.textInputFocus,
- primary: 0,
- mac: { primary: 2048 /* CtrlCmd */ | 1 /* Backspace */ },
- weight: 100 /* EditorContrib */
- }
- });
- }
- _getEndCursorState(primaryCursor, rangesToDelete) {
- let endPrimaryCursor = null;
- let endCursorState = [];
- let deletedLines = 0;
- rangesToDelete.forEach(range => {
- let endCursor;
- if (range.endColumn === 1 && deletedLines > 0) {
- let newStartLine = range.startLineNumber - deletedLines;
- endCursor = new Selection(newStartLine, range.startColumn, newStartLine, range.startColumn);
- }
- else {
- endCursor = new Selection(range.startLineNumber, range.startColumn, range.startLineNumber, range.startColumn);
- }
- deletedLines += range.endLineNumber - range.startLineNumber;
- if (range.intersectRanges(primaryCursor)) {
- endPrimaryCursor = endCursor;
- }
- else {
- endCursorState.push(endCursor);
- }
- });
- if (endPrimaryCursor) {
- endCursorState.unshift(endPrimaryCursor);
- }
- return endCursorState;
- }
- _getRangesToDelete(editor) {
- let selections = editor.getSelections();
- if (selections === null) {
- return [];
- }
- let rangesToDelete = selections;
- let model = editor.getModel();
- if (model === null) {
- return [];
- }
- rangesToDelete.sort(Range.compareRangesUsingStarts);
- rangesToDelete = rangesToDelete.map(selection => {
- if (selection.isEmpty()) {
- if (selection.startColumn === 1) {
- let deleteFromLine = Math.max(1, selection.startLineNumber - 1);
- let deleteFromColumn = selection.startLineNumber === 1 ? 1 : model.getLineContent(deleteFromLine).length + 1;
- return new Range(deleteFromLine, deleteFromColumn, selection.startLineNumber, 1);
- }
- else {
- return new Range(selection.startLineNumber, 1, selection.startLineNumber, selection.startColumn);
- }
- }
- else {
- return new Range(selection.startLineNumber, 1, selection.endLineNumber, selection.endColumn);
- }
- });
- return rangesToDelete;
- }
- }
- export class DeleteAllRightAction extends AbstractDeleteAllToBoundaryAction {
- constructor() {
- super({
- id: 'deleteAllRight',
- label: nls.localize('lines.deleteAllRight', "Delete All Right"),
- alias: 'Delete All Right',
- precondition: EditorContextKeys.writable,
- kbOpts: {
- kbExpr: EditorContextKeys.textInputFocus,
- primary: 0,
- mac: { primary: 256 /* WinCtrl */ | 41 /* KeyK */, secondary: [2048 /* CtrlCmd */ | 20 /* Delete */] },
- weight: 100 /* EditorContrib */
- }
- });
- }
- _getEndCursorState(primaryCursor, rangesToDelete) {
- let endPrimaryCursor = null;
- let endCursorState = [];
- for (let i = 0, len = rangesToDelete.length, offset = 0; i < len; i++) {
- let range = rangesToDelete[i];
- let endCursor = new Selection(range.startLineNumber - offset, range.startColumn, range.startLineNumber - offset, range.startColumn);
- if (range.intersectRanges(primaryCursor)) {
- endPrimaryCursor = endCursor;
- }
- else {
- endCursorState.push(endCursor);
- }
- }
- if (endPrimaryCursor) {
- endCursorState.unshift(endPrimaryCursor);
- }
- return endCursorState;
- }
- _getRangesToDelete(editor) {
- let model = editor.getModel();
- if (model === null) {
- return [];
- }
- let selections = editor.getSelections();
- if (selections === null) {
- return [];
- }
- let rangesToDelete = selections.map((sel) => {
- if (sel.isEmpty()) {
- const maxColumn = model.getLineMaxColumn(sel.startLineNumber);
- if (sel.startColumn === maxColumn) {
- return new Range(sel.startLineNumber, sel.startColumn, sel.startLineNumber + 1, 1);
- }
- else {
- return new Range(sel.startLineNumber, sel.startColumn, sel.startLineNumber, maxColumn);
- }
- }
- return sel;
- });
- rangesToDelete.sort(Range.compareRangesUsingStarts);
- return rangesToDelete;
- }
- }
- export class JoinLinesAction extends EditorAction {
- constructor() {
- super({
- id: 'editor.action.joinLines',
- label: nls.localize('lines.joinLines', "Join Lines"),
- alias: 'Join Lines',
- precondition: EditorContextKeys.writable,
- kbOpts: {
- kbExpr: EditorContextKeys.editorTextFocus,
- primary: 0,
- mac: { primary: 256 /* WinCtrl */ | 40 /* KeyJ */ },
- weight: 100 /* EditorContrib */
- }
- });
- }
- run(_accessor, editor) {
- let selections = editor.getSelections();
- if (selections === null) {
- return;
- }
- let primaryCursor = editor.getSelection();
- if (primaryCursor === null) {
- return;
- }
- selections.sort(Range.compareRangesUsingStarts);
- let reducedSelections = [];
- let lastSelection = selections.reduce((previousValue, currentValue) => {
- if (previousValue.isEmpty()) {
- if (previousValue.endLineNumber === currentValue.startLineNumber) {
- if (primaryCursor.equalsSelection(previousValue)) {
- primaryCursor = currentValue;
- }
- return currentValue;
- }
- if (currentValue.startLineNumber > previousValue.endLineNumber + 1) {
- reducedSelections.push(previousValue);
- return currentValue;
- }
- else {
- return new Selection(previousValue.startLineNumber, previousValue.startColumn, currentValue.endLineNumber, currentValue.endColumn);
- }
- }
- else {
- if (currentValue.startLineNumber > previousValue.endLineNumber) {
- reducedSelections.push(previousValue);
- return currentValue;
- }
- else {
- return new Selection(previousValue.startLineNumber, previousValue.startColumn, currentValue.endLineNumber, currentValue.endColumn);
- }
- }
- });
- reducedSelections.push(lastSelection);
- let model = editor.getModel();
- if (model === null) {
- return;
- }
- let edits = [];
- let endCursorState = [];
- let endPrimaryCursor = primaryCursor;
- let lineOffset = 0;
- for (let i = 0, len = reducedSelections.length; i < len; i++) {
- let selection = reducedSelections[i];
- let startLineNumber = selection.startLineNumber;
- let startColumn = 1;
- let columnDeltaOffset = 0;
- let endLineNumber, endColumn;
- let selectionEndPositionOffset = model.getLineContent(selection.endLineNumber).length - selection.endColumn;
- if (selection.isEmpty() || selection.startLineNumber === selection.endLineNumber) {
- let position = selection.getStartPosition();
- if (position.lineNumber < model.getLineCount()) {
- endLineNumber = startLineNumber + 1;
- endColumn = model.getLineMaxColumn(endLineNumber);
- }
- else {
- endLineNumber = position.lineNumber;
- endColumn = model.getLineMaxColumn(position.lineNumber);
- }
- }
- else {
- endLineNumber = selection.endLineNumber;
- endColumn = model.getLineMaxColumn(endLineNumber);
- }
- let trimmedLinesContent = model.getLineContent(startLineNumber);
- for (let i = startLineNumber + 1; i <= endLineNumber; i++) {
- let lineText = model.getLineContent(i);
- let firstNonWhitespaceIdx = model.getLineFirstNonWhitespaceColumn(i);
- if (firstNonWhitespaceIdx >= 1) {
- let insertSpace = true;
- if (trimmedLinesContent === '') {
- insertSpace = false;
- }
- if (insertSpace && (trimmedLinesContent.charAt(trimmedLinesContent.length - 1) === ' ' ||
- trimmedLinesContent.charAt(trimmedLinesContent.length - 1) === '\t')) {
- insertSpace = false;
- trimmedLinesContent = trimmedLinesContent.replace(/[\s\uFEFF\xA0]+$/g, ' ');
- }
- let lineTextWithoutIndent = lineText.substr(firstNonWhitespaceIdx - 1);
- trimmedLinesContent += (insertSpace ? ' ' : '') + lineTextWithoutIndent;
- if (insertSpace) {
- columnDeltaOffset = lineTextWithoutIndent.length + 1;
- }
- else {
- columnDeltaOffset = lineTextWithoutIndent.length;
- }
- }
- else {
- columnDeltaOffset = 0;
- }
- }
- let deleteSelection = new Range(startLineNumber, startColumn, endLineNumber, endColumn);
- if (!deleteSelection.isEmpty()) {
- let resultSelection;
- if (selection.isEmpty()) {
- edits.push(EditOperation.replace(deleteSelection, trimmedLinesContent));
- resultSelection = new Selection(deleteSelection.startLineNumber - lineOffset, trimmedLinesContent.length - columnDeltaOffset + 1, startLineNumber - lineOffset, trimmedLinesContent.length - columnDeltaOffset + 1);
- }
- else {
- if (selection.startLineNumber === selection.endLineNumber) {
- edits.push(EditOperation.replace(deleteSelection, trimmedLinesContent));
- resultSelection = new Selection(selection.startLineNumber - lineOffset, selection.startColumn, selection.endLineNumber - lineOffset, selection.endColumn);
- }
- else {
- edits.push(EditOperation.replace(deleteSelection, trimmedLinesContent));
- resultSelection = new Selection(selection.startLineNumber - lineOffset, selection.startColumn, selection.startLineNumber - lineOffset, trimmedLinesContent.length - selectionEndPositionOffset);
- }
- }
- if (Range.intersectRanges(deleteSelection, primaryCursor) !== null) {
- endPrimaryCursor = resultSelection;
- }
- else {
- endCursorState.push(resultSelection);
- }
- }
- lineOffset += deleteSelection.endLineNumber - deleteSelection.startLineNumber;
- }
- endCursorState.unshift(endPrimaryCursor);
- editor.pushUndoStop();
- editor.executeEdits(this.id, edits, endCursorState);
- editor.pushUndoStop();
- }
- }
- export class TransposeAction extends EditorAction {
- constructor() {
- super({
- id: 'editor.action.transpose',
- label: nls.localize('editor.transpose', "Transpose characters around the cursor"),
- alias: 'Transpose characters around the cursor',
- precondition: EditorContextKeys.writable
- });
- }
- run(_accessor, editor) {
- let selections = editor.getSelections();
- if (selections === null) {
- return;
- }
- let model = editor.getModel();
- if (model === null) {
- return;
- }
- let commands = [];
- for (let i = 0, len = selections.length; i < len; i++) {
- let selection = selections[i];
- if (!selection.isEmpty()) {
- continue;
- }
- let cursor = selection.getStartPosition();
- let maxColumn = model.getLineMaxColumn(cursor.lineNumber);
- if (cursor.column >= maxColumn) {
- if (cursor.lineNumber === model.getLineCount()) {
- continue;
- }
- // The cursor is at the end of current line and current line is not empty
- // then we transpose the character before the cursor and the line break if there is any following line.
- let deleteSelection = new Range(cursor.lineNumber, Math.max(1, cursor.column - 1), cursor.lineNumber + 1, 1);
- let chars = model.getValueInRange(deleteSelection).split('').reverse().join('');
- commands.push(new ReplaceCommand(new Selection(cursor.lineNumber, Math.max(1, cursor.column - 1), cursor.lineNumber + 1, 1), chars));
- }
- else {
- let deleteSelection = new Range(cursor.lineNumber, Math.max(1, cursor.column - 1), cursor.lineNumber, cursor.column + 1);
- let chars = model.getValueInRange(deleteSelection).split('').reverse().join('');
- commands.push(new ReplaceCommandThatPreservesSelection(deleteSelection, chars, new Selection(cursor.lineNumber, cursor.column + 1, cursor.lineNumber, cursor.column + 1)));
- }
- }
- editor.pushUndoStop();
- editor.executeCommands(this.id, commands);
- editor.pushUndoStop();
- }
- }
- export class AbstractCaseAction extends EditorAction {
- run(_accessor, editor) {
- const selections = editor.getSelections();
- if (selections === null) {
- return;
- }
- const model = editor.getModel();
- if (model === null) {
- return;
- }
- const wordSeparators = editor.getOption(116 /* wordSeparators */);
- const textEdits = [];
- for (const selection of selections) {
- if (selection.isEmpty()) {
- const cursor = selection.getStartPosition();
- const word = editor.getConfiguredWordAtPosition(cursor);
- if (!word) {
- continue;
- }
- const wordRange = new Range(cursor.lineNumber, word.startColumn, cursor.lineNumber, word.endColumn);
- const text = model.getValueInRange(wordRange);
- textEdits.push(EditOperation.replace(wordRange, this._modifyText(text, wordSeparators)));
- }
- else {
- const text = model.getValueInRange(selection);
- textEdits.push(EditOperation.replace(selection, this._modifyText(text, wordSeparators)));
- }
- }
- editor.pushUndoStop();
- editor.executeEdits(this.id, textEdits);
- editor.pushUndoStop();
- }
- }
- export class UpperCaseAction extends AbstractCaseAction {
- constructor() {
- super({
- id: 'editor.action.transformToUppercase',
- label: nls.localize('editor.transformToUppercase', "Transform to Uppercase"),
- alias: 'Transform to Uppercase',
- precondition: EditorContextKeys.writable
- });
- }
- _modifyText(text, wordSeparators) {
- return text.toLocaleUpperCase();
- }
- }
- export class LowerCaseAction extends AbstractCaseAction {
- constructor() {
- super({
- id: 'editor.action.transformToLowercase',
- label: nls.localize('editor.transformToLowercase', "Transform to Lowercase"),
- alias: 'Transform to Lowercase',
- precondition: EditorContextKeys.writable
- });
- }
- _modifyText(text, wordSeparators) {
- return text.toLocaleLowerCase();
- }
- }
- class BackwardsCompatibleRegExp {
- constructor(_pattern, _flags) {
- this._pattern = _pattern;
- this._flags = _flags;
- this._actual = null;
- this._evaluated = false;
- }
- get() {
- if (!this._evaluated) {
- this._evaluated = true;
- try {
- this._actual = new RegExp(this._pattern, this._flags);
- }
- catch (err) {
- // this browser does not support this regular expression
- }
- }
- return this._actual;
- }
- isSupported() {
- return (this.get() !== null);
- }
- }
- export class TitleCaseAction extends AbstractCaseAction {
- constructor() {
- super({
- id: 'editor.action.transformToTitlecase',
- label: nls.localize('editor.transformToTitlecase', "Transform to Title Case"),
- alias: 'Transform to Title Case',
- precondition: EditorContextKeys.writable
- });
- }
- _modifyText(text, wordSeparators) {
- const titleBoundary = TitleCaseAction.titleBoundary.get();
- if (!titleBoundary) {
- // cannot support this
- return text;
- }
- return text
- .toLocaleLowerCase()
- .replace(titleBoundary, (b) => b.toLocaleUpperCase());
- }
- }
- TitleCaseAction.titleBoundary = new BackwardsCompatibleRegExp('(^|[^\\p{L}\\p{N}\']|((^|\\P{L})\'))\\p{L}', 'gmu');
- export class SnakeCaseAction extends AbstractCaseAction {
- constructor() {
- super({
- id: 'editor.action.transformToSnakecase',
- label: nls.localize('editor.transformToSnakecase', "Transform to Snake Case"),
- alias: 'Transform to Snake Case',
- precondition: EditorContextKeys.writable
- });
- }
- _modifyText(text, wordSeparators) {
- const caseBoundary = SnakeCaseAction.caseBoundary.get();
- const singleLetters = SnakeCaseAction.singleLetters.get();
- if (!caseBoundary || !singleLetters) {
- // cannot support this
- return text;
- }
- return (text
- .replace(caseBoundary, '$1_$2')
- .replace(singleLetters, '$1_$2$3')
- .toLocaleLowerCase());
- }
- }
- SnakeCaseAction.caseBoundary = new BackwardsCompatibleRegExp('(\\p{Ll})(\\p{Lu})', 'gmu');
- SnakeCaseAction.singleLetters = new BackwardsCompatibleRegExp('(\\p{Lu}|\\p{N})(\\p{Lu})(\\p{Ll})', 'gmu');
- registerEditorAction(CopyLinesUpAction);
- registerEditorAction(CopyLinesDownAction);
- registerEditorAction(DuplicateSelectionAction);
- registerEditorAction(MoveLinesUpAction);
- registerEditorAction(MoveLinesDownAction);
- registerEditorAction(SortLinesAscendingAction);
- registerEditorAction(SortLinesDescendingAction);
- registerEditorAction(DeleteDuplicateLinesAction);
- registerEditorAction(TrimTrailingWhitespaceAction);
- registerEditorAction(DeleteLinesAction);
- registerEditorAction(IndentLinesAction);
- registerEditorAction(OutdentLinesAction);
- registerEditorAction(InsertLineBeforeAction);
- registerEditorAction(InsertLineAfterAction);
- registerEditorAction(DeleteAllLeftAction);
- registerEditorAction(DeleteAllRightAction);
- registerEditorAction(JoinLinesAction);
- registerEditorAction(TransposeAction);
- registerEditorAction(UpperCaseAction);
- registerEditorAction(LowerCaseAction);
- if (SnakeCaseAction.caseBoundary.isSupported() && SnakeCaseAction.singleLetters.isSupported()) {
- registerEditorAction(SnakeCaseAction);
- }
- if (TitleCaseAction.titleBoundary.isSupported()) {
- registerEditorAction(TitleCaseAction);
- }
|