diff --git a/src/vs/workbench/api/node/mainThreadSaveParticipant.ts b/src/vs/workbench/api/node/mainThreadSaveParticipant.ts index 2a59a90558e610d4b561ecc7844f3f9f24d1a16a..161a59cc558f8687f3057e26a442fc1224fe9009 100644 --- a/src/vs/workbench/api/node/mainThreadSaveParticipant.ts +++ b/src/vs/workbench/api/node/mainThreadSaveParticipant.ts @@ -70,23 +70,21 @@ class TrimWhitespaceParticipant implements INamedSaveParticpant { } function findEditor(model: IModel, codeEditorService: ICodeEditorService): ICommonCodeEditor { - if (model.isAttachedToEditor()) { - const allEditors = codeEditorService.listCodeEditors(); - for (let i = 0, len = allEditors.length; i < len; i++) { - const editor = allEditors[i]; - const editorModel = editor.getModel(); + let candidate: ICommonCodeEditor = null; - if (!editorModel) { - continue; // empty editor - } + if (model.isAttachedToEditor()) { + for (const editor of codeEditorService.listCodeEditors()) { + if (editor.getModel() === model) { + if (editor.isFocused()) { + return editor; // favour focussed editor if there are multiple + } - if (model === editorModel) { - return editor; + candidate = editor; } } } - return null; + return candidate; } export class FinalNewLineParticipant implements INamedSaveParticpant { @@ -158,7 +156,7 @@ class FormatOnSaveParticipant implements INamedSaveParticpant { }).then(edits => { if (edits && versionNow === model.getVersionId()) { - const editor = this._findEditor(model); + const editor = findEditor(model, this._editorService); if (editor) { this._editsWithEditor(editor, edits); } else { @@ -194,24 +192,6 @@ class FormatOnSaveParticipant implements INamedSaveParticpant { forceMoveMarkers: true }; } - - private _findEditor(model: IModel) { - if (!model.isAttachedToEditor()) { - return; - } - - let candidate: ICommonCodeEditor; - for (const editor of this._editorService.listCodeEditors()) { - if (editor.getModel() === model) { - if (editor.isFocused()) { - return editor; - } else { - candidate = editor; - } - } - } - return candidate; - } } class ExtHostSaveParticipant implements INamedSaveParticpant {