Skip to content

Commit 45addfd

Browse files
authored
Add Missing Include Code Action (#11488)
* Add loc string and Add Missing Include handler * Add AddMissingInclude Command and resolve lint * Refactor codeActionProvider * resolve formatting and append newline to includes * Only apply new line to Add Missing Include * Resolve lint issue * Add new line specific to OS * Resolve newline
1 parent 6f80226 commit 45addfd

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

Extension/src/LanguageServer/Providers/codeActionProvider.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,16 @@ export class CodeActionProvider implements vscode.CodeActionProvider {
9797
let wsEdit: vscode.WorkspaceEdit | undefined;
9898
let codeActionKind: vscode.CodeActionKind = vscode.CodeActionKind.QuickFix;
9999
if (command.edit) {
100-
// Inline macro feature.
101-
codeActionKind = CodeActionProvider.inlineMacroKind;
102100
wsEdit = new vscode.WorkspaceEdit();
101+
if (command.command === 'C_Cpp.AddMissingInclude') {
102+
command.edit.newText += "\n";
103+
}
103104
wsEdit.replace(document.uri, makeVscodeRange(command.edit.range), command.edit.newText);
104-
hasInlineMacro = true;
105+
if (command.command === "edit") {
106+
// Inline macro feature.
107+
codeActionKind = CodeActionProvider.inlineMacroKind;
108+
hasInlineMacro = true;
109+
}
105110
} else if (command.command === "C_Cpp.RemoveAllCodeAnalysisProblems" && command.uri !== undefined) {
106111
// The "RemoveAll" message is sent for all code analysis squiggles.
107112
const vsCodeRange: vscode.Range = makeVscodeRange(r);

Extension/src/LanguageServer/extension.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ export function registerCommands(enabled: boolean): void {
438438
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.CreateDeclarationOrDefinition', enabled ? onCreateDeclarationOrDefinition : onDisabledCommand));
439439
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.CopyDeclarationOrDefinition', enabled ? onCopyDeclarationOrDefinition : onDisabledCommand));
440440
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.RescanCompilers', enabled ? onRescanCompilers : onDisabledCommand));
441+
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.AddMissingInclude', enabled ? onAddMissingInclude : onDisabledCommand));
441442
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.ExtractToFunction', enabled ? () => onExtractToFunction(false, false) : onDisabledCommand));
442443
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.ExtractToFreeFunction', enabled ? () => onExtractToFunction(true, false) : onDisabledCommand));
443444
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.ExtractToMemberFunction', enabled ? () => onExtractToFunction(false, true) : onDisabledCommand));
@@ -537,6 +538,10 @@ async function onRescanCompilers(sender?: any): Promise<void> {
537538
return clients.ActiveClient.rescanCompilers(sender);
538539
}
539540

541+
async function onAddMissingInclude(): Promise<void> {
542+
telemetry.logLanguageServerEvent('AddMissingInclude');
543+
}
544+
540545
async function selectIntelliSenseConfiguration(sender?: any): Promise<void> {
541546
await clients.ActiveClient.ready;
542547
return clients.ActiveClient.promptSelectIntelliSenseConfiguration(sender);

Extension/src/nativeStrings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"include_not_found_in_browse_path": "Include file not found in browse.path.",
44
"update_browse_path": "Edit \"browse.path\" setting",
55
"add_to_include_path": "Add to \"includePath\": {0}",
6+
"add_missing_include_path": {
7+
"text": "Add '{0}'",
8+
"hint": "{0} is C++ code to add, such as '#include <string>'"
9+
},
610
"edit_include_path": "Edit \"includePath\" setting",
711
"disable_error_squiggles": "Disable error squiggles",
812
"enable_error_squiggles": "Enable all error squiggles",

0 commit comments

Comments
 (0)