From 6e1f737afce787669c5895747b7d9c8d4ed524fc Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Tue, 27 Mar 2018 18:17:57 +0200 Subject: [PATCH] Simplify FormatCommand (#44870) --- src/vs/editor/contrib/format/formatActions.ts | 6 +- src/vs/editor/contrib/format/formatCommand.ts | 61 +++++++++++-------- .../mainThreadSaveParticipant.ts | 2 +- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/vs/editor/contrib/format/formatActions.ts b/src/vs/editor/contrib/format/formatActions.ts index b7801c7b2c2..70145acddb3 100644 --- a/src/vs/editor/contrib/format/formatActions.ts +++ b/src/vs/editor/contrib/format/formatActions.ts @@ -161,7 +161,7 @@ class FormatOnType implements editorCommon.IEditorContribution { return; } - EditOperationsCommand.execute(this.editor, edits, true); + EditOperationsCommand.executeAsCommand(this.editor, edits); alertFormattingEdits(edits); }, (err) => { @@ -244,7 +244,7 @@ class FormatOnPaste implements editorCommon.IEditorContribution { if (!state.validate(this.editor) || isFalsyOrEmpty(edits)) { return; } - EditOperationsCommand.execute(this.editor, edits, false); + EditOperationsCommand.execute(this.editor, edits); alertFormattingEdits(edits); }); } @@ -280,7 +280,7 @@ export abstract class AbstractFormatAction extends EditorAction { return; } - EditOperationsCommand.execute(editor, edits, false); + EditOperationsCommand.execute(editor, edits); alertFormattingEdits(edits); editor.focus(); }, err => { diff --git a/src/vs/editor/contrib/format/formatCommand.ts b/src/vs/editor/contrib/format/formatCommand.ts index 42f320e538f..0c8cf905e03 100644 --- a/src/vs/editor/contrib/format/formatCommand.ts +++ b/src/vs/editor/contrib/format/formatCommand.ts @@ -15,39 +15,48 @@ import { EditOperation } from 'vs/editor/common/core/editOperation'; export class EditOperationsCommand implements editorCommon.ICommand { - static execute(editor: ICodeEditor, edits: TextEdit[], asCommand: boolean) { - const cmd = new EditOperationsCommand(edits, editor.getSelection()); - if (typeof cmd._newEol === 'number') { - editor.getModel().setEOL(cmd._newEol); - } - editor.pushUndoStop(); - if (!asCommand) { - editor.executeEdits('formatEditsCommand', cmd._edits.map(edit => EditOperation.replace(Range.lift(edit.range), edit.text))); - } else { - editor.executeCommand('formatEditsCommand', cmd); - } - editor.pushUndoStop(); - } - - private _edits: TextEdit[]; - private _newEol: EndOfLineSequence; - - private _initialSelection: Selection; - private _selectionId: string; - - constructor(edits: TextEdit[], initialSelection: Selection) { - this._initialSelection = initialSelection; - this._edits = []; - this._newEol = undefined; + static _handleEolEdits(editor: ICodeEditor, edits: TextEdit[]): ISingleEditOperation[] { + let newEol: EndOfLineSequence = undefined; + let singleEdits: ISingleEditOperation[] = []; for (let edit of edits) { if (typeof edit.eol === 'number') { - this._newEol = edit.eol; + newEol = edit.eol; } if (edit.range && typeof edit.text === 'string') { - this._edits.push(edit); + singleEdits.push(edit); } } + + if (typeof newEol === 'number') { + editor.getModel().setEOL(newEol); + } + + return singleEdits; + } + + static executeAsCommand(editor: ICodeEditor, _edits: TextEdit[]) { + let edits = this._handleEolEdits(editor, _edits); + const cmd = new EditOperationsCommand(edits, editor.getSelection()); + editor.pushUndoStop(); + editor.executeCommand('formatEditsCommand', cmd); + editor.pushUndoStop(); + } + + static execute(editor: ICodeEditor, _edits: TextEdit[]) { + let edits = this._handleEolEdits(editor, _edits); + editor.pushUndoStop(); + editor.executeEdits('formatEditsCommand', edits.map(edit => EditOperation.replace(Range.lift(edit.range), edit.text))); + editor.pushUndoStop(); + } + + private _edits: ISingleEditOperation[]; + private _initialSelection: Selection; + private _selectionId: string; + + constructor(edits: ISingleEditOperation[], initialSelection: Selection) { + this._initialSelection = initialSelection; + this._edits = edits; } public getEditOperations(model: ITextModel, builder: editorCommon.IEditOperationBuilder): void { diff --git a/src/vs/workbench/api/electron-browser/mainThreadSaveParticipant.ts b/src/vs/workbench/api/electron-browser/mainThreadSaveParticipant.ts index 2dde56fd80b..631e2d1483a 100644 --- a/src/vs/workbench/api/electron-browser/mainThreadSaveParticipant.ts +++ b/src/vs/workbench/api/electron-browser/mainThreadSaveParticipant.ts @@ -225,7 +225,7 @@ class FormatOnSaveParticipant implements ISaveParticipantParticipant { } private _editsWithEditor(editor: ICodeEditor, edits: ISingleEditOperation[]): void { - EditOperationsCommand.execute(editor, edits, false); + EditOperationsCommand.execute(editor, edits); } private _editWithModel(model: ITextModel, edits: ISingleEditOperation[]): void {