diff --git a/src/vs/editor/contrib/format/common/format.ts b/src/vs/editor/contrib/format/common/format.ts index 876c06028ad..dd0530ba182 100644 --- a/src/vs/editor/contrib/format/common/format.ts +++ b/src/vs/editor/contrib/format/common/format.ts @@ -9,13 +9,12 @@ import {illegalArgument} from 'vs/base/common/errors'; import URI from 'vs/base/common/uri'; import {TPromise} from 'vs/base/common/winjs.base'; import {Range} from 'vs/editor/common/core/range'; -import {IReadOnlyModel, ISingleEditOperation, IModel, ICommonCodeEditor} from 'vs/editor/common/editorCommon'; +import {IReadOnlyModel, ISingleEditOperation} from 'vs/editor/common/editorCommon'; import {CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions'; import {DocumentFormattingEditProviderRegistry, DocumentRangeFormattingEditProviderRegistry, OnTypeFormattingEditProviderRegistry, FormattingOptions} from 'vs/editor/common/modes'; import {IModelService} from 'vs/editor/common/services/modelService'; import {asWinJsPromise} from 'vs/base/common/async'; import {Position} from 'vs/editor/common/core/position'; -import {EditOperationsCommand} from './formatCommand'; export function getDocumentRangeFormattingEdits(model: IReadOnlyModel, range: Range, options: FormattingOptions): TPromise { const [support] = DocumentRangeFormattingEditProviderRegistry.ordered(model); @@ -86,28 +85,3 @@ CommonEditorRegistry.registerDefaultLanguageCommand('_executeFormatOnTypeProvide } return getOnTypeFormattingEdits(model, position, ch, options); }); - - -export function formatDocument(model: IModel, editor: ICommonCodeEditor) { - - const {tabSize, insertSpaces} = model.getOptions(); - - return getDocumentRangeFormattingEdits(model, model.getFullModelRange(), { tabSize, insertSpaces }).then(edits => { - if (!edits) { - return; - } - - if (!editor) { - - model.applyEdits(edits.map(({text, range}) => ({ - text, - range: Range.lift(range), - identifier: undefined, - forceMoveMarkers: true - }))); - - } else { - editor.executeCommand('format.document', new EditOperationsCommand(edits, editor.getSelection())); - } - }); -}; diff --git a/src/vs/workbench/api/node/mainThreadSaveParticipant.ts b/src/vs/workbench/api/node/mainThreadSaveParticipant.ts index d711d8dcf54..582c5a978d7 100644 --- a/src/vs/workbench/api/node/mainThreadSaveParticipant.ts +++ b/src/vs/workbench/api/node/mainThreadSaveParticipant.ts @@ -12,9 +12,11 @@ import {IThreadService} from 'vs/workbench/services/thread/common/threadService' import {ISaveParticipant, ITextFileEditorModel} from 'vs/workbench/parts/files/common/files'; import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation'; import {IPosition, IModel} from 'vs/editor/common/editorCommon'; +import {Range} from 'vs/editor/common/core/range'; import {Selection} from 'vs/editor/common/core/selection'; import {trimTrailingWhitespace} from 'vs/editor/common/commands/trimTrailingWhitespaceCommand'; -import {formatDocument} from 'vs/editor/contrib/format/common/format'; +import {getDocumentRangeFormattingEdits} from 'vs/editor/contrib/format/common/format'; +import {EditOperationsCommand} from 'vs/editor/contrib/format/common/formatCommand'; import {IConfigurationService} from 'vs/platform/configuration/common/configuration'; import {TextFileEditorModel} from 'vs/workbench/parts/files/common/editors/textFileEditorModel'; import {ExtHostContext, ExtHostDocumentSaveParticipantShape} from './extHost.protocol'; @@ -83,11 +85,33 @@ class FormatOnSaveParticipant implements ISaveParticipant { } participate(editorModel: ITextFileEditorModel, env: { isAutoSaved: boolean }): TPromise { - if (this._configurationService.lookup('files.formatOnSave').value) { - const model: IModel = editorModel.textEditorModel; - const editor = this._findEditor(model); - return formatDocument(model, editor); + + if (!this._configurationService.lookup('files.formatOnSave').value) { + return; } + + const model: IModel = editorModel.textEditorModel; + const editor = this._findEditor(model); + const {tabSize, insertSpaces} = model.getOptions(); + + return getDocumentRangeFormattingEdits(model, model.getFullModelRange(), { tabSize, insertSpaces }).then(edits => { + if (!edits) { + return; + } + + if (!editor) { + + model.applyEdits(edits.map(({text, range}) => ({ + text, + range: Range.lift(range), + identifier: undefined, + forceMoveMarkers: true + }))); + + } else { + editor.executeCommand('format.document', new EditOperationsCommand(edits, editor.getSelection())); + } + }); } private _findEditor(model: IModel) {