monaco-editor-core
Version:
A browser based code editor
25 lines (24 loc) • 1.4 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Selection } from '../../../common/core/selection.js';
export class InPlaceReplaceCommand {
constructor(editRange, originalSelection, text) {
this._editRange = editRange;
this._originalSelection = originalSelection;
this._text = text;
}
getEditOperations(model, builder) {
builder.addTrackedEditOperation(this._editRange, this._text);
}
computeCursorState(model, helper) {
const inverseEditOperations = helper.getInverseEditOperations();
const srcRange = inverseEditOperations[0].range;
if (!this._originalSelection.isEmpty()) {
// Preserve selection and extends to typed text
return new Selection(srcRange.endLineNumber, srcRange.endColumn - this._text.length, srcRange.endLineNumber, srcRange.endColumn);
}
return new Selection(srcRange.endLineNumber, Math.min(this._originalSelection.positionColumn, srcRange.endColumn), srcRange.endLineNumber, Math.min(this._originalSelection.positionColumn, srcRange.endColumn));
}
}