未验证 提交 c3144e0a 编写于 作者: J Johannes Rieken 提交者: GitHub

Merge pull request #127849 from kilbouri/main

Commit message
......@@ -247,16 +247,25 @@ class FormatOnSaveParticipant implements ITextFileSaveParticipant {
}
const editorOrModel = findEditor(textEditorModel, this.codeEditorService) || textEditorModel;
const mode = this.configurationService.getValue<'file' | 'modifications'>('editor.formatOnSaveMode', overrides);
if (mode === 'modifications') {
// format modifications
const mode = this.configurationService.getValue<'file' | 'modifications' | 'modificationsIfAvailable'>('editor.formatOnSaveMode', overrides);
// keeping things DRY :)
const formatWholeFile = async () => {
await this.instantiationService.invokeFunction(formatDocumentWithSelectedProvider, editorOrModel, FormattingMode.Silent, nestedProgress, token);
}
if (mode === 'modifications' || mode === 'modificationsIfAvailable') {
// try formatting modifications
const ranges = await this.instantiationService.invokeFunction(getModifiedRanges, isCodeEditor(editorOrModel) ? editorOrModel.getModel() : editorOrModel);
if (ranges) {
// version control reports changes
await this.instantiationService.invokeFunction(formatDocumentRangesWithSelectedProvider, editorOrModel, ranges, FormattingMode.Silent, nestedProgress, token);
} else if (ranges === null) {
// version control not found
formatWholeFile();
}
} else {
// format the whole file
await this.instantiationService.invokeFunction(formatDocumentWithSelectedProvider, editorOrModel, FormattingMode.Silent, nestedProgress, token);
formatWholeFile();
}
}
}
......
......@@ -294,11 +294,13 @@ configurationRegistry.registerConfiguration({
'default': 'file',
'enum': [
'file',
'modifications'
'modifications',
'modificationsIfAvailable'
],
'enumDescriptions': [
nls.localize({ key: 'everything', comment: ['This is the description of an option'] }, "Format the whole file."),
nls.localize({ key: 'modification', comment: ['This is the description of an option'] }, "Format modifications (requires source control)."),
nls.localize({ key: 'modificationIfAvailable', comment: ['This is the description of an option'] }, "Will attempt to format modifications only (requires source control). If source control can't be used, then the whole file will be formatted."),
],
'markdownDescription': nls.localize('formatOnSaveMode', "Controls if format on save formats the whole file or only modifications. Only applies when `#editor.formatOnSave#` is enabled."),
'scope': ConfigurationScope.LANGUAGE_OVERRIDABLE,
......
......@@ -49,14 +49,14 @@ registerEditorAction(class FormatModifiedAction extends EditorAction {
});
export async function getModifiedRanges(accessor: ServicesAccessor, modified: ITextModel): Promise<Range[] | undefined> {
export async function getModifiedRanges(accessor: ServicesAccessor, modified: ITextModel): Promise<Range[] | undefined | null> {
const scmService = accessor.get(ISCMService);
const workerService = accessor.get(IEditorWorkerService);
const modelService = accessor.get(ITextModelService);
const original = await getOriginalResource(scmService, modified.uri);
if (!original) {
return undefined;
return null; // let undefined signify no changes, null represents no source control (there's probably a better way, but I can't think of one rn)
}
const ranges: Range[] = [];
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册