提交 6e1f737a 编写于 作者: A Alex Dima

Simplify FormatCommand (#44870)

上级 1b032162
......@@ -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 => {
......
......@@ -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);
static _handleEolEdits(editor: ICodeEditor, edits: TextEdit[]): ISingleEditOperation[] {
let newEol: EndOfLineSequence = undefined;
let singleEdits: ISingleEditOperation[] = [];
for (let edit of edits) {
if (typeof edit.eol === 'number') {
newEol = edit.eol;
}
if (edit.range && typeof edit.text === 'string') {
singleEdits.push(edit);
}
}
editor.pushUndoStop();
if (!asCommand) {
editor.executeEdits('formatEditsCommand', cmd._edits.map(edit => EditOperation.replace(Range.lift(edit.range), edit.text)));
} else {
editor.executeCommand('formatEditsCommand', cmd);
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();
}
private _edits: TextEdit[];
private _newEol: EndOfLineSequence;
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: TextEdit[], initialSelection: Selection) {
constructor(edits: ISingleEditOperation[], initialSelection: Selection) {
this._initialSelection = initialSelection;
this._edits = [];
this._newEol = undefined;
for (let edit of edits) {
if (typeof edit.eol === 'number') {
this._newEol = edit.eol;
}
if (edit.range && typeof edit.text === 'string') {
this._edits.push(edit);
}
}
this._edits = edits;
}
public getEditOperations(model: ITextModel, builder: editorCommon.IEditOperationBuilder): void {
......
......@@ -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 {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册