Path: blob/main/src/vs/editor/common/commands/surroundSelectionCommand.ts
3294 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Range } from '../core/range.js';6import { Position } from '../core/position.js';7import { Selection } from '../core/selection.js';8import { ICommand, ICursorStateComputerData, IEditOperationBuilder } from '../editorCommon.js';9import { ITextModel } from '../model.js';1011export class SurroundSelectionCommand implements ICommand {12private readonly _range: Selection;13private readonly _charBeforeSelection: string;14private readonly _charAfterSelection: string;1516constructor(range: Selection, charBeforeSelection: string, charAfterSelection: string) {17this._range = range;18this._charBeforeSelection = charBeforeSelection;19this._charAfterSelection = charAfterSelection;20}2122public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {23builder.addTrackedEditOperation(new Range(24this._range.startLineNumber,25this._range.startColumn,26this._range.startLineNumber,27this._range.startColumn28), this._charBeforeSelection);2930builder.addTrackedEditOperation(new Range(31this._range.endLineNumber,32this._range.endColumn,33this._range.endLineNumber,34this._range.endColumn35), this._charAfterSelection || null); // addTrackedEditOperation() ignores us if the text == ''. Causing a chain of errors in computeCursorState()36}3738public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {39const inverseEditOperations = helper.getInverseEditOperations();40const firstOperationRange = inverseEditOperations[0].range;41const secondOperationRange = inverseEditOperations[1].range;4243return new Selection(44firstOperationRange.endLineNumber,45firstOperationRange.endColumn,46secondOperationRange.endLineNumber,47secondOperationRange.endColumn - this._charAfterSelection.length48);49}50}5152/**53* A surround selection command that runs after composition finished.54*/55export class CompositionSurroundSelectionCommand implements ICommand {5657constructor(58private readonly _position: Position,59private readonly _text: string,60private readonly _charAfter: string61) { }6263public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {64builder.addTrackedEditOperation(new Range(65this._position.lineNumber,66this._position.column,67this._position.lineNumber,68this._position.column69), this._text + this._charAfter);70}7172public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {73const inverseEditOperations = helper.getInverseEditOperations();74const opRange = inverseEditOperations[0].range;7576return new Selection(77opRange.endLineNumber,78opRange.startColumn,79opRange.endLineNumber,80opRange.endColumn - this._charAfter.length81);82}83}848586