/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { EditOperation } from '../../../common/core/editOperation.js'; import { Range } from '../../../common/core/range.js'; export class FormattingEdit { static _handleEolEdits(editor, edits) { let newEol = undefined; const singleEdits = []; for (const edit of edits) { if (typeof edit.eol === 'number') { newEol = edit.eol; } if (edit.range && typeof edit.text === 'string') { singleEdits.push(edit); } } if (typeof newEol === 'number') { if (editor.hasModel()) { editor.getModel().pushEOL(newEol); } } return singleEdits; } static _isFullModelReplaceEdit(editor, edit) { if (!editor.hasModel()) { return false; } const model = editor.getModel(); const editRange = model.validateRange(edit.range); const fullModelRange = model.getFullModelRange(); return fullModelRange.equalsRange(editRange); } static execute(editor, _edits, addUndoStops) { if (addUndoStops) { editor.pushUndoStop(); } const edits = FormattingEdit._handleEolEdits(editor, _edits); if (edits.length === 1 && FormattingEdit._isFullModelReplaceEdit(editor, edits[0])) { // We use replace semantics and hope that markers stay put... editor.executeEdits('formatEditsCommand', edits.map(edit => EditOperation.replace(Range.lift(edit.range), edit.text))); } else { editor.executeEdits('formatEditsCommand', edits.map(edit => EditOperation.replaceMove(Range.lift(edit.range), edit.text))); } if (addUndoStops) { editor.pushUndoStop(); } } }