move things back to participant for better control

This commit is contained in:
Johannes Rieken
2016-09-22 15:42:24 +02:00
parent 818f5deef0
commit 07aeea6f46
2 changed files with 30 additions and 32 deletions
+1 -27
View File
@@ -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<ISingleEditOperation[]> {
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()));
}
});
};
@@ -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<any> {
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) {