Skip to content

Commit 4cf5e00

Browse files
committed
Refresh diagnostics for open PowerShell docs
Force re-analysis of open PowerShell documents after the language server starts. Adds DidOpenTextDocument and DidCloseTextDocument notification types and a refreshOpenPowerShellDocumentDiagnostics() helper that filters open documents (powershell, file/untitled), logs the action, and sends didClose followed by didOpen (including languageId, version and text) to the language client. The method is invoked after the session reports it has started; it no-ops if the client isn't running or there are no matching open documents.
1 parent f3ee736 commit 4cf5e00

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/session.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ export const PowerShellVersionRequestType = new RequestType0<
9999
void
100100
>("powerShell/getVersion");
101101

102+
const DidOpenTextDocumentNotificationType = new NotificationType<{
103+
textDocument: {
104+
uri: string;
105+
languageId: string;
106+
version: number;
107+
text: string;
108+
};
109+
}>("textDocument/didOpen");
110+
111+
const DidCloseTextDocumentNotificationType = new NotificationType<{
112+
textDocument: {
113+
uri: string;
114+
};
115+
}>("textDocument/didClose");
116+
102117
export class SessionManager implements Middleware {
103118
public HostName: string;
104119
public DisplayName: string;
@@ -295,6 +310,7 @@ export class SessionManager implements Middleware {
295310
`Started PowerShell v${this.versionDetails.version}.`,
296311
);
297312
this.setSessionRunningStatus(); // Yay, we made it!
313+
this.refreshOpenPowerShellDocumentDiagnostics();
298314

299315
await this.writePidIfInDevMode(this.languageServerProcess);
300316

@@ -1167,6 +1183,49 @@ Type 'help' to get help.
11671183
}
11681184
}
11691185

1186+
private refreshOpenPowerShellDocumentDiagnostics(): void {
1187+
if (!this.languageClient?.isRunning()) {
1188+
return;
1189+
}
1190+
1191+
const openPowerShellDocuments = vscode.workspace.textDocuments.filter(
1192+
(document) =>
1193+
document.languageId === "powershell" &&
1194+
(document.uri.scheme === "file" ||
1195+
document.uri.scheme === "untitled"),
1196+
);
1197+
1198+
if (openPowerShellDocuments.length === 0) {
1199+
return;
1200+
}
1201+
1202+
this.logger.writeDebug(
1203+
`Refreshing analysis for ${openPowerShellDocuments.length} open PowerShell document(s).`,
1204+
);
1205+
1206+
for (const document of openPowerShellDocuments) {
1207+
const uri = document.uri.toString();
1208+
this.languageClient.sendNotification(
1209+
DidCloseTextDocumentNotificationType,
1210+
{
1211+
textDocument: { uri },
1212+
},
1213+
);
1214+
1215+
this.languageClient.sendNotification(
1216+
DidOpenTextDocumentNotificationType,
1217+
{
1218+
textDocument: {
1219+
uri,
1220+
languageId: document.languageId,
1221+
version: document.version,
1222+
text: document.getText(),
1223+
},
1224+
},
1225+
);
1226+
}
1227+
}
1228+
11701229
private createStatusBarItem(): vscode.LanguageStatusItem {
11711230
const statusTitle = "Show PowerShell Session Menu";
11721231
const languageStatusItem = vscode.languages.createLanguageStatusItem(

0 commit comments

Comments
 (0)