inPlaceReplaceCommand.js 1.4 KB

123456789101112131415161718192021222324
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { Selection } from '../../common/core/selection.js';
  6. export class InPlaceReplaceCommand {
  7. constructor(editRange, originalSelection, text) {
  8. this._editRange = editRange;
  9. this._originalSelection = originalSelection;
  10. this._text = text;
  11. }
  12. getEditOperations(model, builder) {
  13. builder.addTrackedEditOperation(this._editRange, this._text);
  14. }
  15. computeCursorState(model, helper) {
  16. const inverseEditOperations = helper.getInverseEditOperations();
  17. const srcRange = inverseEditOperations[0].range;
  18. if (!this._originalSelection.isEmpty()) {
  19. // Preserve selection and extends to typed text
  20. return new Selection(srcRange.endLineNumber, srcRange.endColumn - this._text.length, srcRange.endLineNumber, srcRange.endColumn);
  21. }
  22. return new Selection(srcRange.endLineNumber, Math.min(this._originalSelection.positionColumn, srcRange.endColumn), srcRange.endLineNumber, Math.min(this._originalSelection.positionColumn, srcRange.endColumn));
  23. }
  24. }