Path: blob/main/src/vs/editor/contrib/inPlaceReplace/browser/inPlaceReplaceCommand.ts
4779 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 '../../../common/core/range.js';6import { Selection } from '../../../common/core/selection.js';7import { ICommand, ICursorStateComputerData, IEditOperationBuilder } from '../../../common/editorCommon.js';8import { ITextModel } from '../../../common/model.js';910export class InPlaceReplaceCommand implements ICommand {1112private readonly _editRange: Range;13private readonly _originalSelection: Selection;14private readonly _text: string;1516constructor(editRange: Range, originalSelection: Selection, text: string) {17this._editRange = editRange;18this._originalSelection = originalSelection;19this._text = text;20}2122public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {23builder.addTrackedEditOperation(this._editRange, this._text);24}2526public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {27const inverseEditOperations = helper.getInverseEditOperations();28const srcRange = inverseEditOperations[0].range;2930if (!this._originalSelection.isEmpty()) {31// Preserve selection and extends to typed text32return new Selection(33srcRange.endLineNumber,34srcRange.endColumn - this._text.length,35srcRange.endLineNumber,36srcRange.endColumn37);38}3940return new Selection(41srcRange.endLineNumber,42Math.min(this._originalSelection.positionColumn, srcRange.endColumn),43srcRange.endLineNumber,44Math.min(this._originalSelection.positionColumn, srcRange.endColumn)45);46}47}484950