Skip to content

Commit 853a4f3

Browse files
authored
Code analysis fix formatting. (#9661)
* Code analysis fix formatting.
1 parent 6ee27f5 commit 853a4f3

File tree

7 files changed

+58
-4
lines changed

7 files changed

+58
-4
lines changed

Extension/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,12 @@
419419
},
420420
"scope": "resource"
421421
},
422+
"C_Cpp.codeAnalysis.clangTidy.codeAction.formatFixes": {
423+
"type": "boolean",
424+
"description": "%c_cpp.configuration.codeAnalysis.clangTidy.codeAction.formatFixes.markdownDescription%",
425+
"default": true,
426+
"scope": "resource"
427+
},
422428
"C_Cpp.codeAnalysis.clangTidy.codeAction.showClear": {
423429
"type": "string",
424430
"description": "%c_cpp.configuration.codeAnalysis.clangTidy.codeAction.showClear.description%",

Extension/package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"c_cpp.configuration.codeAnalysis.clangTidy.codeAction.showClear.AllOnly.description": "Show only the 'Clear all' code action (or 'Clear all <type>' if there is only one type or 'Clear this' if there is only one problem).",
5151
"c_cpp.configuration.codeAnalysis.clangTidy.codeAction.showClear.AllAndAllType.description": "Show the 'Clear all' code action (if there are multiple problem types) and the 'Clear all <type>' code action (or 'Clear this' if there is only one problem for the <type>)",
5252
"c_cpp.configuration.codeAnalysis.clangTidy.codeAction.showClear.AllAndAllTypeAndThis.description": "Show the 'Clear all' (if there are multiple problem types), 'Clear all <type>' (if there are multiple problems for the <type>), and 'Clear this' code actions",
53+
"c_cpp.configuration.codeAnalysis.clangTidy.codeAction.formatFixes.markdownDescription": { "message": "If `true`, formatting will be run on the lines changed by 'Fix' code actions.", "comment": [ "Markdown text between `` should not be translated or localized (they represent literal text) and the capitalization, spacing, and punctuation (including the ``) should not be altered." ] },
5354
"c_cpp.configuration.codeAnalysis.clangTidy.enabled.markdownDescription": { "message": "If `true`, code analysis using `clang-tidy` will be enabled and run automatically if `#C_Cpp.codeAnalysis.runAutomatically#` is `true` (the default).", "comment": [ "Markdown text between `` should not be translated or localized (they represent literal text) and the capitalization, spacing, and punctuation (including the ``) should not be altered." ] },
5455
"c_cpp.configuration.codeAnalysis.clangTidy.path.markdownDescription": { "message": "The full path of the `clang-tidy` executable. If not specified, and `clang-tidy` is available in the environment path, that is used. If not found in the environment path, the `clang-tidy` bundled with the extension will be used.", "comment": [ "Markdown text between `` should not be translated or localized (they represent literal text) and the capitalization, spacing, and punctuation (including the ``) should not be altered." ] },
5556
"c_cpp.configuration.codeAnalysis.clangTidy.config.markdownDescription": { "message": "Specifies a `clang-tidy` configuration in YAML/JSON format: `{Checks: '-*,clang-analyzer-*', CheckOptions: [{key: x, value: y}]}`. When the value is empty, `clang-tidy` will attempt to find a file named `.clang-tidy` for each source file in its parent directories.", "comment": [ "Words 'key' and 'value' in '{key: value, ...}' should be translated, but all other markdown text between `` should not be translated or localized (they represent literal text) and the capitalization, spacing, and punctuation (including the ``) should not be altered." ] },

Extension/src/LanguageServer/Providers/documentFormattingEditProvider.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* ------------------------------------------------------------------------------------------ */
55
import * as vscode from 'vscode';
66
import { DefaultClient, FormatParams, FormatDocumentRequest } from '../client';
7-
import { CppSettings, getEditorConfigSettings } from '../settings';
7+
import { CppSettings, getEditorConfigSettings, OtherSettings } from '../settings';
88
import { makeVscodeTextEdits } from '../utils';
99

1010
export class DocumentFormattingEditProvider implements vscode.DocumentFormattingEditProvider {
@@ -16,6 +16,30 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
1616
public async provideDocumentFormattingEdits(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken): Promise<vscode.TextEdit[]> {
1717
await this.client.awaitUntilLanguageClientReady();
1818
const filePath: string = document.uri.fsPath;
19+
const onChanges: string | number | boolean = options.onChanges;
20+
if (onChanges) {
21+
let insertSpacesSet: boolean = false;
22+
let tabSizeSet: boolean = false;
23+
const editor: vscode.TextEditor = await vscode.window.showTextDocument(document, undefined, true);
24+
if (editor.options.insertSpaces && typeof editor.options.insertSpaces === "boolean") {
25+
options.insertSpaces = editor.options.insertSpaces;
26+
insertSpacesSet = true;
27+
}
28+
if (editor.options.tabSize && typeof editor.options.tabSize === "number") {
29+
options.tabSize = editor.options.tabSize;
30+
tabSizeSet = true;
31+
}
32+
33+
if (!insertSpacesSet || !tabSizeSet) {
34+
const settings: OtherSettings = new OtherSettings(this.client.RootUri);
35+
if (!insertSpacesSet) {
36+
options.insertSpaces = settings.editorInsertSpaces ?? true;
37+
}
38+
if (!tabSizeSet) {
39+
options.tabSize = settings.editorTabSize ?? 4;
40+
}
41+
}
42+
}
1943
const settings: CppSettings = new CppSettings(this.client.RootUri);
2044
const useVcFormat: boolean = settings.useVcFormat(document);
2145
const configCallBack = async (editorConfigSettings: any | undefined) => {
@@ -35,7 +59,8 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
3559
character: 0,
3660
line: 0
3761
}
38-
}
62+
},
63+
onChanges: onChanges === true
3964
};
4065
// We do not currently pass the CancellationToken to sendRequest
4166
// because there is not currently cancellation logic for formatting

Extension/src/LanguageServer/Providers/documentRangeFormattingEditProvider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export class DocumentRangeFormattingEditProvider implements vscode.DocumentRange
3535
character: range.end.character,
3636
line: range.end.line
3737
}
38-
}
38+
},
39+
onChanges: false
3940
};
4041
// We do not currently pass the CancellationToken to sendRequest
4142
// because there is not currently cancellation logic for formatting

Extension/src/LanguageServer/Providers/onTypeFormattingEditProvider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export class OnTypeFormattingEditProvider implements vscode.OnTypeFormattingEdit
3535
character: 0,
3636
line: 0
3737
}
38-
}
38+
},
39+
onChanges: false
3940
};
4041
// We do not currently pass the CancellationToken to sendRequest
4142
// because there is not currently cancellation logic for formatting

Extension/src/LanguageServer/client.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ export interface FormatParams {
312312
tabSize: number;
313313
editorConfigSettings: any;
314314
useVcFormat: boolean;
315+
onChanges: boolean;
315316
}
316317

317318
export interface GetFoldingRangesParams {
@@ -3028,6 +3029,23 @@ export class DefaultClient implements Client {
30283029

30293030
public async handleFixCodeAnalysisProblems(workspaceEdit: vscode.WorkspaceEdit, refreshSquigglesOnSave: boolean, identifiersAndUris: CodeAnalysisDiagnosticIdentifiersAndUri[]): Promise<void> {
30303031
if (await vscode.workspace.applyEdit(workspaceEdit)) {
3032+
const settings: CppSettings = new CppSettings(this.RootUri);
3033+
if (settings.clangTidyCodeActionFormatFixes) {
3034+
const editedFiles: Set<vscode.Uri> = new Set<vscode.Uri>();
3035+
for (const entry of workspaceEdit.entries()) {
3036+
editedFiles.add(entry[0]);
3037+
}
3038+
const formatEdits: vscode.WorkspaceEdit = new vscode.WorkspaceEdit();
3039+
for (const uri of editedFiles) {
3040+
const formatTextEdits: vscode.TextEdit[] | undefined = await vscode.commands.executeCommand<vscode.TextEdit[] | undefined>("vscode.executeFormatDocumentProvider", uri, { onChanges: true });
3041+
if (formatTextEdits && formatTextEdits.length > 0) {
3042+
formatEdits.set(uri, formatTextEdits);
3043+
}
3044+
}
3045+
if (formatEdits.size > 0) {
3046+
await vscode.workspace.applyEdit(formatEdits);
3047+
}
3048+
}
30313049
return this.handleRemoveCodeAnalysisProblems(refreshSquigglesOnSave, identifiersAndUris);
30323050
}
30333051
}

Extension/src/LanguageServer/settings.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ export class CppSettings extends Settings {
189189
public get clangTidyCodeActionShowDisable(): boolean | undefined { return super.Section.get<boolean>("codeAnalysis.clangTidy.codeAction.showDisable"); }
190190
public get clangTidyCodeActionShowClear(): string { return super.Section.get<string>("codeAnalysis.clangTidy.codeAction.showClear") ?? "AllAndAllType"; }
191191
public get clangTidyCodeActionShowDocumentation(): boolean | undefined { return super.Section.get<boolean>("codeAnalysis.clangTidy.codeAction.showDocumentation"); }
192+
public get clangTidyCodeActionFormatFixes(): boolean { return super.Section.get<boolean>("codeAnalysis.clangTidy.codeAction.formatFixes") ?? true; }
192193
public addClangTidyChecksDisabled(value: string): void {
193194
const checks: string[] | undefined = this.clangTidyChecksDisabled;
194195
if (checks === undefined) {
@@ -808,6 +809,7 @@ export class OtherSettings {
808809
}
809810

810811
public get editorTabSize(): number | undefined { return vscode.workspace.getConfiguration("editor", this.resource).get<number>("tabSize"); }
812+
public get editorInsertSpaces(): boolean | undefined { return vscode.workspace.getConfiguration("editor", this.resource).get<boolean>("insertSpaces"); }
811813
public get editorAutoClosingBrackets(): string | undefined { return vscode.workspace.getConfiguration("editor", this.resource).get<string>("autoClosingBrackets"); }
812814
public get filesEncoding(): string | undefined { return vscode.workspace.getConfiguration("files", { uri: this.resource, languageId: "cpp" }).get<string>("encoding"); }
813815
public get filesAssociations(): any { return vscode.workspace.getConfiguration("files").get("associations"); }

0 commit comments

Comments
 (0)