fde9a95072b623599040aa44afcc452e6800f78cf45ab91fb060b7c44317c507e9df6bca7f527fd6236ec9dd3b58a3d064aaeed8c19e95596bbd784c205763 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 { EditOperation } from '../../../common/core/editOperation.js';
  6. import { Range } from '../../../common/core/range.js';
  7. export class FormattingEdit {
  8. static _handleEolEdits(editor, edits) {
  9. let newEol = undefined;
  10. const singleEdits = [];
  11. for (const edit of edits) {
  12. if (typeof edit.eol === 'number') {
  13. newEol = edit.eol;
  14. }
  15. if (edit.range && typeof edit.text === 'string') {
  16. singleEdits.push(edit);
  17. }
  18. }
  19. if (typeof newEol === 'number') {
  20. if (editor.hasModel()) {
  21. editor.getModel().pushEOL(newEol);
  22. }
  23. }
  24. return singleEdits;
  25. }
  26. static _isFullModelReplaceEdit(editor, edit) {
  27. if (!editor.hasModel()) {
  28. return false;
  29. }
  30. const model = editor.getModel();
  31. const editRange = model.validateRange(edit.range);
  32. const fullModelRange = model.getFullModelRange();
  33. return fullModelRange.equalsRange(editRange);
  34. }
  35. static execute(editor, _edits, addUndoStops) {
  36. if (addUndoStops) {
  37. editor.pushUndoStop();
  38. }
  39. const edits = FormattingEdit._handleEolEdits(editor, _edits);
  40. if (edits.length === 1 && FormattingEdit._isFullModelReplaceEdit(editor, edits[0])) {
  41. // We use replace semantics and hope that markers stay put...
  42. editor.executeEdits('formatEditsCommand', edits.map(edit => EditOperation.replace(Range.lift(edit.range), edit.text)));
  43. }
  44. else {
  45. editor.executeEdits('formatEditsCommand', edits.map(edit => EditOperation.replaceMove(Range.lift(edit.range), edit.text)));
  46. }
  47. if (addUndoStops) {
  48. editor.pushUndoStop();
  49. }
  50. }
  51. }