Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code analysis fix formatting. #9661

Merged
merged 5 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,12 @@
},
"scope": "resource"
},
"C_Cpp.codeAnalysis.clangTidy.codeAction.formatFixes": {
"type": "boolean",
"description": "%c_cpp.configuration.codeAnalysis.clangTidy.codeAction.formatFixes.markdownDescription%",
"default": true,
"scope": "resource"
},
"C_Cpp.codeAnalysis.clangTidy.codeAction.showClear": {
"type": "string",
"description": "%c_cpp.configuration.codeAnalysis.clangTidy.codeAction.showClear.description%",
Expand Down
1 change: 1 addition & 0 deletions Extension/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"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).",
"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>)",
"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",
"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." ] },
sean-mcmanus marked this conversation as resolved.
Show resolved Hide resolved
"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." ] },
"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." ] },
"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." ] },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* ------------------------------------------------------------------------------------------ */
import * as vscode from 'vscode';
import { DefaultClient, FormatParams, FormatDocumentRequest } from '../client';
import { CppSettings, getEditorConfigSettings } from '../settings';
import { CppSettings, getEditorConfigSettings, OtherSettings } from '../settings';
import { makeVscodeTextEdits } from '../utils';

export class DocumentFormattingEditProvider implements vscode.DocumentFormattingEditProvider {
Expand All @@ -16,6 +16,34 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
public async provideDocumentFormattingEdits(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken): Promise<vscode.TextEdit[]> {
await this.client.awaitUntilLanguageClientReady();
const filePath: string = document.uri.fsPath;
const onChanges: string | number | boolean = options.onChanges;
if (onChanges) {
let insertSpacesSet: boolean = false;
let tabSizeSet: boolean = false;
for (const editor of vscode.window.visibleTextEditors) {
if (document === editor.document) {
if (editor.options.insertSpaces && typeof editor.options.insertSpaces === "boolean") {
options.insertSpaces = editor.options.insertSpaces;
insertSpacesSet = true;
}
if (editor.options.tabSize && typeof editor.options.tabSize === "number") {
options.tabSize = editor.options.tabSize;
tabSizeSet = true;
}
break;
}
};

if (!insertSpacesSet || !tabSizeSet) {
const settings: OtherSettings = new OtherSettings(this.client.RootUri);
if (!insertSpacesSet) {
options.insertSpaces = settings.editorInsertSpaces ?? true;
}
if (!tabSizeSet) {
options.tabSize = settings.editorTabSize ?? 4;
}
}
}
const settings: CppSettings = new CppSettings(this.client.RootUri);
const useVcFormat: boolean = settings.useVcFormat(document);
const configCallBack = async (editorConfigSettings: any | undefined) => {
Expand All @@ -35,7 +63,8 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
character: 0,
line: 0
}
}
},
onChanges: onChanges === true
};
// We do not currently pass the CancellationToken to sendRequest
// because there is not currently cancellation logic for formatting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export class DocumentRangeFormattingEditProvider implements vscode.DocumentRange
character: range.end.character,
line: range.end.line
}
}
},
onChanges: false
};
// We do not currently pass the CancellationToken to sendRequest
// because there is not currently cancellation logic for formatting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export class OnTypeFormattingEditProvider implements vscode.OnTypeFormattingEdit
character: 0,
line: 0
}
}
},
onChanges: false
};
// We do not currently pass the CancellationToken to sendRequest
// because there is not currently cancellation logic for formatting
Expand Down
18 changes: 18 additions & 0 deletions Extension/src/LanguageServer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ export interface FormatParams {
tabSize: number;
editorConfigSettings: any;
useVcFormat: boolean;
onChanges: boolean;
}

export interface GetFoldingRangesParams {
Expand Down Expand Up @@ -3028,6 +3029,23 @@ export class DefaultClient implements Client {

public async handleFixCodeAnalysisProblems(workspaceEdit: vscode.WorkspaceEdit, refreshSquigglesOnSave: boolean, identifiersAndUris: CodeAnalysisDiagnosticIdentifiersAndUri[]): Promise<void> {
if (await vscode.workspace.applyEdit(workspaceEdit)) {
const settings: CppSettings = new CppSettings(this.RootUri);
if (settings.clangTidyCodeActionFormatFixes) {
const editedFiles: Set<vscode.Uri> = new Set<vscode.Uri>();
for (const entry of workspaceEdit.entries()) {
editedFiles.add(entry[0]);
}
const formatEdits: vscode.WorkspaceEdit = new vscode.WorkspaceEdit();
for (const uri of editedFiles) {
const formatTextEdits: vscode.TextEdit[] | undefined = await vscode.commands.executeCommand<vscode.TextEdit[] | undefined>("vscode.executeFormatDocumentProvider", uri, { onChanges: true });
if (formatTextEdits && formatTextEdits.length > 0) {
formatEdits.set(uri, formatTextEdits);
}
}
if (formatEdits.size > 0) {
await vscode.workspace.applyEdit(formatEdits);
}
}
return this.handleRemoveCodeAnalysisProblems(refreshSquigglesOnSave, identifiersAndUris);
}
}
Expand Down
2 changes: 2 additions & 0 deletions Extension/src/LanguageServer/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export class CppSettings extends Settings {
public get clangTidyCodeActionShowDisable(): boolean | undefined { return super.Section.get<boolean>("codeAnalysis.clangTidy.codeAction.showDisable"); }
public get clangTidyCodeActionShowClear(): string { return super.Section.get<string>("codeAnalysis.clangTidy.codeAction.showClear") ?? "AllAndAllType"; }
public get clangTidyCodeActionShowDocumentation(): boolean | undefined { return super.Section.get<boolean>("codeAnalysis.clangTidy.codeAction.showDocumentation"); }
public get clangTidyCodeActionFormatFixes(): boolean { return super.Section.get<boolean>("codeAnalysis.clangTidy.codeAction.formatFixes") ?? true; }
public addClangTidyChecksDisabled(value: string): void {
const checks: string[] | undefined = this.clangTidyChecksDisabled;
if (checks === undefined) {
Expand Down Expand Up @@ -808,6 +809,7 @@ export class OtherSettings {
}

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