Skip to content

Commit 32ac8ae

Browse files
Restart Language Server for active file (#8128)
1 parent 2f78e86 commit 32ac8ae

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

Extension/package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,11 @@
13601360
"title": "%c_cpp.command.buildAndDebugActiveFile.title%",
13611361
"category": "C/C++"
13621362
},
1363+
{
1364+
"command": "C_Cpp.RestartIntelliSenseForFile",
1365+
"title": "%c_cpp.command.restartIntelliSenseForFile.title%",
1366+
"category": "C/C++"
1367+
},
13631368
{
13641369
"command": "C_Cpp.LogDiagnostics",
13651370
"title": "%c_cpp.command.logDiagnostics.title%",
@@ -2542,7 +2547,12 @@
25422547
{
25432548
"when": "editorLangId == 'c' || editorLangId == 'cpp' || editorLangId == 'cuda-cpp'",
25442549
"command": "C_Cpp.BuildAndDebugActiveFile",
2545-
"group": "other2_debug@1"
2550+
"group": "other2@1"
2551+
},
2552+
{
2553+
"when": "editorLangId == 'c' || editorLangId == 'cpp' || editorLangId == 'cuda-cpp'",
2554+
"command": "C_Cpp.RestartIntelliSenseForFile",
2555+
"group": "other2@2"
25462556
}
25472557
],
25482558
"commandPalette": [

Extension/package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"c_cpp.command.resetDatabase.title": "Reset IntelliSense Database",
1313
"c_cpp.command.takeSurvey.title": "Take Survey",
1414
"c_cpp.command.buildAndDebugActiveFile.title": "Build and Debug Active File",
15+
"c_cpp.command.restartIntelliSenseForFile.title": "Restart IntelliSense for Active File",
1516
"c_cpp.command.logDiagnostics.title": "Log Diagnostics",
1617
"c_cpp.command.referencesViewGroupByType.title": "Group by Reference Type",
1718
"c_cpp.command.referencesViewUngroupByType.title": "Ungroup by Reference Type",

Extension/src/LanguageServer/client.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ const PauseAnalysisNotification: NotificationType<void, void> = new Notification
484484
const ResumeAnalysisNotification: NotificationType<void, void> = new NotificationType<void, void>('cpptools/resumeAnalysis');
485485
const CancelAnalysisNotification: NotificationType<void, void> = new NotificationType<void, void>('cpptools/cancelAnalysis');
486486
const ActiveDocumentChangeNotification: NotificationType<TextDocumentIdentifier, void> = new NotificationType<TextDocumentIdentifier, void>('cpptools/activeDocumentChange');
487+
const RestartIntelliSenseForFileNotification: NotificationType<TextDocumentIdentifier, void> = new NotificationType<TextDocumentIdentifier, void>('cpptools/restartIntelliSenseForFile');
487488
const TextEditorSelectionChangeNotification: NotificationType<Range, void> = new NotificationType<Range, void>('cpptools/textEditorSelectionChange');
488489
const ChangeCppPropertiesNotification: NotificationType<CppPropertiesParams, void> = new NotificationType<CppPropertiesParams, void>('cpptools/didChangeCppProperties');
489490
const ChangeCompileCommandsNotification: NotificationType<FileChangedParams, void> = new NotificationType<FileChangedParams, void>('cpptools/didChangeCompileCommands');
@@ -630,6 +631,7 @@ export interface Client {
630631
awaitUntilLanguageClientReady(): void;
631632
requestSwitchHeaderSource(rootPath: string, fileName: string): Thenable<string>;
632633
activeDocumentChanged(document: vscode.TextDocument): Promise<void>;
634+
restartIntelliSenseForFile(document: vscode.TextDocument): Promise<void>;
633635
activate(): void;
634636
selectionChanged(selection: Range): void;
635637
resetDatabase(): void;
@@ -2439,9 +2441,16 @@ export class DefaultClient implements Client {
24392441
*/
24402442
public async activeDocumentChanged(document: vscode.TextDocument): Promise<void> {
24412443
await this.updateActiveDocumentTextOptions();
2442-
this.notifyWhenLanguageClientReady(() => {
2443-
this.languageClient.sendNotification(ActiveDocumentChangeNotification, this.languageClient.code2ProtocolConverter.asTextDocumentIdentifier(document));
2444-
});
2444+
await this.awaitUntilLanguageClientReady();
2445+
this.languageClient.sendNotification(ActiveDocumentChangeNotification, this.languageClient.code2ProtocolConverter.asTextDocumentIdentifier(document));
2446+
}
2447+
2448+
/**
2449+
* send notifications to the language server to restart IntelliSense for the selected file.
2450+
*/
2451+
public async restartIntelliSenseForFile(document: vscode.TextDocument): Promise<void> {
2452+
await this.awaitUntilLanguageClientReady();
2453+
this.languageClient.sendNotification(RestartIntelliSenseForFileNotification, this.languageClient.code2ProtocolConverter.asTextDocumentIdentifier(document));
24452454
}
24462455

24472456
/**
@@ -3011,6 +3020,7 @@ class NullClient implements Client {
30113020
awaitUntilLanguageClientReady(): void { }
30123021
requestSwitchHeaderSource(rootPath: string, fileName: string): Thenable<string> { return Promise.resolve(""); }
30133022
activeDocumentChanged(document: vscode.TextDocument): Promise<void> { return Promise.resolve(); }
3023+
restartIntelliSenseForFile(document: vscode.TextDocument): Promise<void> { return Promise.resolve(); }
30143024
activate(): void { }
30153025
selectionChanged(selection: Range): void { }
30163026
resetDatabase(): void { }

Extension/src/LanguageServer/extension.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,9 +799,21 @@ export function registerCommands(): void {
799799
disposables.push(vscode.commands.registerCommand('cpptools.activeConfigName', onGetActiveConfigName));
800800
disposables.push(vscode.commands.registerCommand('cpptools.activeConfigCustomVariable', onGetActiveConfigCustomVariable));
801801
disposables.push(vscode.commands.registerCommand('cpptools.setActiveConfigName', onSetActiveConfigName));
802+
disposables.push(vscode.commands.registerCommand('C_Cpp.RestartIntelliSenseForFile', onRestartIntelliSenseForFile));
803+
802804
getTemporaryCommandRegistrarInstance().executeDelayedCommands();
803805
}
804806

807+
function onRestartIntelliSenseForFile(): void {
808+
onActivationEvent();
809+
const activeEditor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
810+
if (!activeEditor || !activeEditor.document || activeEditor.document.uri.scheme !== "file" ||
811+
(activeEditor.document.languageId !== "c" && activeEditor.document.languageId !== "cpp" && activeEditor.document.languageId !== "cuda-cpp")) {
812+
return;
813+
}
814+
clients.ActiveClient.restartIntelliSenseForFile(activeEditor.document);
815+
}
816+
805817
async function onSwitchHeaderSource(): Promise<void> {
806818
onActivationEvent();
807819
const activeEditor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;

Extension/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ function rewriteManifest(): Promise<void> {
425425
"onCommand:extension.pickNativeProcess",
426426
"onCommand:extension.pickRemoteNativeProcess",
427427
"onCommand:C_Cpp.BuildAndDebugActiveFile",
428+
"onCommand:C_Cpp.RestartIntelliSenseForFile",
428429
"onCommand:C_Cpp.ConfigurationEditJSON",
429430
"onCommand:C_Cpp.ConfigurationEditUI",
430431
"onCommand:C_Cpp.ConfigurationSelect",

0 commit comments

Comments
 (0)