keep old setting as it and add dependent setting

This commit is contained in:
Johannes Rieken 2020-08-21 12:39:12 +02:00
parent a87dc68f41
commit 6e4fe5fbb6
2 changed files with 21 additions and 14 deletions

View file

@ -239,19 +239,23 @@ class FormatOnSaveParticipant implements ITextFileSaveParticipant {
)
});
});
const enabled = this.configurationService.getValue<boolean>('editor.formatOnSave', overrides);
if (!enabled) {
return undefined;
}
const editorOrModel = findEditor(textEditorModel, this.codeEditorService) || textEditorModel;
const config = this.configurationService.getValue<boolean | string>('editor.formatOnSave', overrides);
if (config === true || config === 'file') {
// format the whole file
await this.instantiationService.invokeFunction(formatDocumentWithSelectedProvider, editorOrModel, FormattingMode.Silent, nestedProgress, token);
} else if (config === 'modifications') {
const mode = this.configurationService.getValue<'file' | 'modifications'>('editor.formatOnSaveMode', overrides);
if (mode === 'modifications') {
// format modifications
const ranges = await this.instantiationService.invokeFunction(getModifiedRanges, isCodeEditor(editorOrModel) ? editorOrModel.getModel() : editorOrModel);
if (ranges) {
await this.instantiationService.invokeFunction(formatDocumentRangesWithSelectedProvider, editorOrModel, ranges, FormattingMode.Silent, nestedProgress, token);
}
} else {
// format the whole file
await this.instantiationService.invokeFunction(formatDocumentWithSelectedProvider, editorOrModel, FormattingMode.Silent, nestedProgress, token);
}
}
}

View file

@ -360,21 +360,24 @@ configurationRegistry.registerConfiguration({
...editorConfigurationBaseNode,
properties: {
'editor.formatOnSave': {
'type': 'boolean',
'description': nls.localize('formatOnSave', "Format a file on save. A formatter must be available, the file must not be saved after delay, and the editor must not be shutting down."),
'scope': ConfigurationScope.LANGUAGE_OVERRIDABLE,
},
'editor.formatOnSaveMode': {
'type': 'string',
'default': 'off',
'default': 'file',
'enum': [
'off',
'file',
'modifications'
],
'enumDescriptions': [
nls.localize('off', "Disable format on save."),
nls.localize('everything', "Format the whole."),
nls.localize('everything', "Format the whole file."),
nls.localize('modification', "Format modifications (requires source control)."),
],
'description': nls.localize('formatOnSave', "Format a file on save. A formatter must be available, the file must not be saved after delay, and the editor must not be shutting down."),
scope: ConfigurationScope.LANGUAGE_OVERRIDABLE,
}
'markdownDescription': nls.localize('formatOnSaveMode', "Controls if format on save formats the whole file or only modifications. Only applies when `#editor.formatOnSave#` is `true`."),
'scope': ConfigurationScope.LANGUAGE_OVERRIDABLE,
},
}
});