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

Add Missing Include Code Action #11488

Merged
merged 15 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
16 changes: 13 additions & 3 deletions Extension/src/LanguageServer/Providers/codeActionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import * as os from 'os';
import * as vscode from 'vscode';
import { Position, Range, RequestType, TextEdit } from 'vscode-languageclient';
import * as nls from 'vscode-nls';
Expand Down Expand Up @@ -97,11 +98,20 @@ export class CodeActionProvider implements vscode.CodeActionProvider {
let wsEdit: vscode.WorkspaceEdit | undefined;
let codeActionKind: vscode.CodeActionKind = vscode.CodeActionKind.QuickFix;
if (command.edit) {
// Inline macro feature.
codeActionKind = CodeActionProvider.inlineMacroKind;
wsEdit = new vscode.WorkspaceEdit();
browntarik marked this conversation as resolved.
Show resolved Hide resolved
if (command.command === 'C_Cpp.AddMissingInclude') {
if (os.platform() === 'win32') {
command.edit.newText = command.edit.newText + "\r\n";
browntarik marked this conversation as resolved.
Show resolved Hide resolved
} else {
command.edit.newText = command.edit.newText + "\n";
browntarik marked this conversation as resolved.
Show resolved Hide resolved
}
}
wsEdit.replace(document.uri, makeVscodeRange(command.edit.range), command.edit.newText);
hasInlineMacro = true;
if (command.command === "edit") {
// Inline macro feature.
codeActionKind = CodeActionProvider.inlineMacroKind;
hasInlineMacro = true;
}
} else if (command.command === "C_Cpp.RemoveAllCodeAnalysisProblems" && command.uri !== undefined) {
// The "RemoveAll" message is sent for all code analysis squiggles.
const vsCodeRange: vscode.Range = makeVscodeRange(r);
Expand Down
5 changes: 5 additions & 0 deletions Extension/src/LanguageServer/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ export function registerCommands(enabled: boolean): void {
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.CreateDeclarationOrDefinition', enabled ? onCreateDeclarationOrDefinition : onDisabledCommand));
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.CopyDeclarationOrDefinition', enabled ? onCopyDeclarationOrDefinition : onDisabledCommand));
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.RescanCompilers', enabled ? onRescanCompilers : onDisabledCommand));
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.AddMissingInclude', enabled ? onAddMissingInclude : onDisabledCommand));
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.ExtractToFunction', enabled ? () => onExtractToFunction(false, false) : onDisabledCommand));
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.ExtractToFreeFunction', enabled ? () => onExtractToFunction(true, false) : onDisabledCommand));
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.ExtractToMemberFunction', enabled ? () => onExtractToFunction(false, true) : onDisabledCommand));
Expand Down Expand Up @@ -536,6 +537,10 @@ async function onRescanCompilers(sender?: any): Promise<void> {
return clients.ActiveClient.rescanCompilers(sender);
}

async function onAddMissingInclude(): Promise<void> {
telemetry.logLanguageServerEvent('AddMissingInclude');
}

async function selectIntelliSenseConfiguration(sender?: any): Promise<void> {
await clients.ActiveClient.ready;
return clients.ActiveClient.promptSelectIntelliSenseConfiguration(sender);
Expand Down
4 changes: 4 additions & 0 deletions Extension/src/nativeStrings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"include_not_found_in_browse_path": "Include file not found in browse.path.",
"update_browse_path": "Edit \"browse.path\" setting",
"add_to_include_path": "Add to \"includePath\": {0}",
"add_missing_include_path": {
"text": "Add '{0}'",
"hint": "{0} is C++ code to add, such as '#include <string>'"
},
"edit_include_path": "Edit \"includePath\" setting",
"disable_error_squiggles": "Disable error squiggles",
"enable_error_squiggles": "Enable all error squiggles",
Expand Down