Skip to content

Commit 783bfb8

Browse files
authored
made suggested change from LSP (#19216)
1 parent 41a1137 commit 783bfb8

2 files changed

Lines changed: 41 additions & 32 deletions

File tree

src/client/activation/jedi/languageServerProxy.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,37 +60,40 @@ export class JediLanguageServerProxy implements ILanguageServerProxy {
6060
(options.middleware ? (<JediLanguageClientMiddleware>options.middleware).serverVersion : undefined) ??
6161
'0.19.3';
6262

63-
this.languageClient = await this.factory.createLanguageClient(resource, interpreter, options);
64-
this.registerHandlers();
63+
const client = await this.factory.createLanguageClient(resource, interpreter, options);
64+
this.registerHandlers(client);
6565

66-
await this.languageClient.start();
66+
await client.start();
67+
68+
this.languageClient = client;
6769
}
6870

6971
@traceDecoratorVerbose('Stopping language server')
7072
public async stop(): Promise<void> {
73+
while (this.disposables.length > 0) {
74+
const d = this.disposables.shift()!;
75+
d.dispose();
76+
}
77+
7178
if (this.languageClient) {
79+
const client = this.languageClient;
80+
this.languageClient = undefined;
81+
7282
// eslint-disable-next-line @typescript-eslint/no-explicit-any
73-
const pid: number | undefined = ((this.languageClient as any)._serverProcess as ChildProcess)?.pid;
83+
const pid: number | undefined = ((client as any)._serverProcess as ChildProcess)?.pid;
7484
const killServer = () => {
7585
if (pid) {
7686
killPid(pid);
7787
}
7888
};
7989

8090
try {
81-
await this.languageClient.stop();
91+
await client.stop();
8292
killServer();
8393
} catch (ex) {
8494
traceError('Stopping language client failed', ex);
8595
killServer();
8696
}
87-
88-
this.languageClient = undefined;
89-
}
90-
91-
while (this.disposables.length > 0) {
92-
const d = this.disposables.shift()!;
93-
d.dispose();
9497
}
9598
}
9699

@@ -106,8 +109,8 @@ export class JediLanguageServerProxy implements ILanguageServerProxy {
106109
undefined,
107110
JediLanguageServerProxy.versionTelemetryProps,
108111
)
109-
private registerHandlers() {
110-
const progressReporting = new ProgressReporting(this.languageClient!);
112+
private registerHandlers(client: LanguageClient) {
113+
const progressReporting = new ProgressReporting(client);
111114
this.disposables.push(progressReporting);
112115

113116
this.disposables.push(
@@ -116,7 +119,7 @@ export class JediLanguageServerProxy implements ILanguageServerProxy {
116119
// the workspace configurations (to then pick up pythonPath set in the middleware).
117120
// This is needed as interpreter changes via the interpreter path service happen
118121
// outside of VS Code's settings (which would mean VS Code sends the config updates itself).
119-
this.languageClient!.sendNotification(DidChangeConfigurationNotification.type, {
122+
client.sendNotification(DidChangeConfigurationNotification.type, {
120123
settings: null,
121124
});
122125
}),

src/client/activation/node/languageServerProxy.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,36 +95,42 @@ export class NodeLanguageServerProxy implements ILanguageServerProxy {
9595
this.cancellationStrategy = new FileBasedCancellationStrategy();
9696
options.connectionOptions = { cancellationStrategy: this.cancellationStrategy };
9797

98-
this.languageClient = await this.factory.createLanguageClient(resource, interpreter, options);
99-
this.registerHandlers(resource);
98+
const client = await this.factory.createLanguageClient(resource, interpreter, options);
99+
this.registerHandlers(client, resource);
100100

101101
this.disposables.push(
102102
this.workspace.onDidGrantWorkspaceTrust(() => {
103-
this.languageClient!.sendNotification('python/workspaceTrusted', { isTrusted: true });
103+
client.sendNotification('python/workspaceTrusted', { isTrusted: true });
104104
}),
105105
);
106106

107-
await this.languageClient.start();
107+
await client.start();
108+
109+
this.languageClient = client;
108110
}
109111

110112
@traceDecoratorVerbose('Disposing language server')
111113
public async stop(): Promise<void> {
114+
while (this.disposables.length > 0) {
115+
const d = this.disposables.shift()!;
116+
d.dispose();
117+
}
118+
112119
if (this.languageClient) {
120+
const client = this.languageClient;
121+
this.languageClient = undefined;
122+
113123
try {
114-
await this.languageClient.stop();
124+
await client.stop();
115125
} catch (ex) {
116126
traceError('Stopping language client failed', ex);
117127
}
118-
this.languageClient = undefined;
119128
}
129+
120130
if (this.cancellationStrategy) {
121131
this.cancellationStrategy.dispose();
122132
this.cancellationStrategy = undefined;
123133
}
124-
while (this.disposables.length > 0) {
125-
const d = this.disposables.shift()!;
126-
d.dispose();
127-
}
128134
}
129135

130136
// eslint-disable-next-line class-methods-use-this
@@ -139,8 +145,8 @@ export class NodeLanguageServerProxy implements ILanguageServerProxy {
139145
undefined,
140146
NodeLanguageServerProxy.versionTelemetryProps,
141147
)
142-
private registerHandlers(_resource: Resource) {
143-
const progressReporting = new ProgressReporting(this.languageClient!);
148+
private registerHandlers(client: LanguageClient, _resource: Resource) {
149+
const progressReporting = new ProgressReporting(client);
144150
this.disposables.push(progressReporting);
145151

146152
this.disposables.push(
@@ -149,28 +155,28 @@ export class NodeLanguageServerProxy implements ILanguageServerProxy {
149155
// the workspace configurations (to then pick up pythonPath set in the middleware).
150156
// This is needed as interpreter changes via the interpreter path service happen
151157
// outside of VS Code's settings (which would mean VS Code sends the config updates itself).
152-
this.languageClient!.sendNotification(DidChangeConfigurationNotification.type, {
158+
client.sendNotification(DidChangeConfigurationNotification.type, {
153159
settings: null,
154160
});
155161
}),
156162
);
157163
this.disposables.push(
158164
this.environmentService.onDidEnvironmentVariablesChange(() => {
159-
this.languageClient!.sendNotification(DidChangeConfigurationNotification.type, {
165+
client.sendNotification(DidChangeConfigurationNotification.type, {
160166
settings: null,
161167
});
162168
}),
163169
);
164170

165-
this.languageClient!.onRequest(
171+
client.onRequest(
166172
InExperiment.Method,
167173
async (params: InExperiment.IRequest): Promise<InExperiment.IResponse> => {
168174
const inExperiment = await this.experimentService.inExperiment(params.experimentName);
169175
return { inExperiment };
170176
},
171177
);
172178

173-
this.languageClient!.onRequest(
179+
client.onRequest(
174180
GetExperimentValue.Method,
175181
async <T extends boolean | number | string>(
176182
params: GetExperimentValue.IRequest,
@@ -181,7 +187,7 @@ export class NodeLanguageServerProxy implements ILanguageServerProxy {
181187
);
182188

183189
this.disposables.push(
184-
this.languageClient!.onRequest('python/isTrustedWorkspace', async () => ({
190+
client.onRequest('python/isTrustedWorkspace', async () => ({
185191
isTrusted: this.workspace.isTrusted,
186192
})),
187193
);

0 commit comments

Comments
 (0)