Skip to content

Commit 9f4c349

Browse files
Jonas(Jiajie) JingJonas(Jiajie) Jing
authored andcommitted
fix: doc sync & signature help
1 parent b577b96 commit 9f4c349

File tree

2 files changed

+48
-49
lines changed

2 files changed

+48
-49
lines changed

server/src/python/PyrightLanguageProvider.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import { CodeZoneManager } from "../sas/CodeZoneManager";
5858
import { LanguageServiceProvider } from "../sas/LanguageServiceProvider";
5959

6060
export class PyrightLanguageProvider extends PyrightServer {
61-
protected docChangeRecords: Record<string, TextDocument>;
6261
protected sasLspProvider: (uri: string) => LanguageServiceProvider;
6362

6463
constructor(
@@ -70,30 +69,12 @@ export class PyrightLanguageProvider extends PyrightServer {
7069
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
7170
super({ ...connection, listen() {} } as LSPAny, maxWorkers, realFileSystem);
7271
this.sasLspProvider = sasLspProvider;
73-
this.docChangeRecords = {};
74-
this.startDocSynchronizer();
7572
}
7673

7774
public getClientCapabilities(): ClientCapabilities {
7875
return this.client;
7976
}
8077

81-
protected startDocSynchronizer() {
82-
setInterval(() => {
83-
const changes = this.docChangeRecords;
84-
this.docChangeRecords = {};
85-
for (const uri in changes) {
86-
const doc = changes[uri];
87-
const pythonDoc = this.extractPythonCodes(doc);
88-
89-
this.onDidChangeTextDocument({
90-
textDocument: doc,
91-
contentChanges: [{ text: pythonDoc }],
92-
});
93-
}
94-
}, 1000);
95-
}
96-
9778
protected extractPythonCodes(doc: TextDocument): string {
9879
const uri = doc.uri;
9980
const languageService = this.sasLspProvider(uri);
@@ -218,7 +199,12 @@ export class PyrightLanguageProvider extends PyrightServer {
218199
}
219200

220201
public addContentChange(doc: TextDocument): void {
221-
this.docChangeRecords[doc.uri] = doc;
202+
const pythonDoc = this.extractPythonCodes(doc);
203+
204+
this.onDidChangeTextDocument({
205+
textDocument: doc,
206+
contentChanges: [{ text: pythonDoc }],
207+
});
222208
}
223209

224210
public async onHover(params: HoverParams, token: CancellationToken) {
@@ -244,7 +230,6 @@ export class PyrightLanguageProvider extends PyrightServer {
244230
}
245231

246232
public async onDidCloseTextDocument(params: DidCloseTextDocumentParams) {
247-
delete this.docChangeRecords[params.textDocument.uri];
248233
await super.onDidCloseTextDocument(params);
249234
}
250235

server/src/server.ts

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ import { CompletionProvider } from "./sas/CompletionProvider";
3737
import { LanguageServiceProvider, legend } from "./sas/LanguageServiceProvider";
3838
import type { LibCompleteItem } from "./sas/SyntaxDataProvider";
3939

40-
const servicePool: Record<string, LanguageServiceProvider> = {};
41-
const documentPool: Record<string, TextDocument> = {};
40+
interface DocumentInfo {
41+
document: TextDocument;
42+
changed: boolean;
43+
service?: LanguageServiceProvider;
44+
}
45+
46+
const documentPool: Record<string, DocumentInfo> = {};
4247

4348
let completionProvider: CompletionProvider;
4449
let connection: Connection;
@@ -116,6 +121,7 @@ export const init = (conn: Connection): void => {
116121
connection.onInitialized(() => _pyrightLanguageProvider.onInitialized());
117122

118123
connection.onRequest(SemanticTokensRequest.type, (params) => {
124+
syncIfDocChange(params.textDocument.uri);
119125
const languageService = getLanguageService(params.textDocument.uri);
120126

121127
return { data: languageService.getTokens() };
@@ -188,6 +194,7 @@ export const init = (conn: Connection): void => {
188194
});
189195

190196
connection.onDocumentSymbol(async (params, token) => {
197+
syncIfDocChange(params.textDocument.uri);
191198
const languageService = getLanguageService(params.textDocument.uri);
192199
const sasSymbols = languageService.getDocumentSymbols();
193200
const pythonSymbols =
@@ -209,6 +216,7 @@ export const init = (conn: Connection): void => {
209216

210217
// todo
211218
connection.onFoldingRanges((params) => {
219+
syncIfDocChange(params.textDocument.uri);
212220
const languageService = getLanguageService(params.textDocument.uri);
213221
return languageService.getFoldingRanges();
214222
});
@@ -267,6 +275,7 @@ export const init = (conn: Connection): void => {
267275
});
268276

269277
connection.onDocumentFormatting((params) => {
278+
syncIfDocChange(params.textDocument.uri);
270279
const languageService = getLanguageService(params.textDocument.uri);
271280
return languageService.formatter.format({
272281
tabWidth: params.options.tabSize,
@@ -281,27 +290,25 @@ export const init = (conn: Connection): void => {
281290
params.textDocument.version,
282291
params.textDocument.text,
283292
);
284-
documentPool[doc.uri] = doc;
293+
documentPool[doc.uri] = { document: doc, changed: false };
285294
await _pyrightLanguageProvider.onDidOpenTextDocument(params);
286295
});
287296

288297
connection.onDidCloseTextDocument(async (params) => {
289298
const uri = params.textDocument.uri;
290299
delete documentPool[uri];
291-
delete servicePool[uri];
292300
await _pyrightLanguageProvider.onDidCloseTextDocument(params);
293301
});
294302

295303
connection.onDidChangeTextDocument((params) => {
296304
const uri = params.textDocument.uri;
297-
const doc = documentPool[uri];
305+
const docInfo = documentPool[uri];
298306
TextDocument.update(
299-
doc,
307+
docInfo.document,
300308
params.contentChanges,
301309
params.textDocument.version,
302310
);
303-
delete servicePool[uri];
304-
_pyrightLanguageProvider.addContentChange(doc);
311+
docInfo.changed = true;
305312
});
306313

307314
connection.onDidChangeConfiguration(
@@ -378,6 +385,7 @@ export const init = (conn: Connection): void => {
378385
workDoneProgress,
379386
resultProgress,
380387
) => {
388+
syncAllChangedDoc();
381389
return await _pyrightLanguageProvider.onWorkspaceSymbol(
382390
params,
383391
token,
@@ -399,21 +407,6 @@ export const init = (conn: Connection): void => {
399407
},
400408
);
401409

402-
connection.onSignatureHelp(
403-
async (params: SignatureHelpParams, token: CancellationToken) => {
404-
return await dispatch(params, {
405-
async python(pyrightLanguageService) {
406-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
407-
return (await pyrightLanguageService.onSignatureHelp(
408-
params,
409-
token,
410-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
411-
)) as any;
412-
},
413-
});
414-
},
415-
);
416-
417410
connection.onPrepareRename(
418411
async (params: PrepareRenameParams, token: CancellationToken) => {
419412
return await dispatch(params, {
@@ -444,6 +437,9 @@ export const init = (conn: Connection): void => {
444437

445438
connection.onDidChangeWatchedFiles(
446439
async (params: DidChangeWatchedFilesParams) => {
440+
params.changes.forEach((item) => {
441+
syncIfDocChange(item.uri);
442+
});
447443
_pyrightLanguageProvider.onDidChangeWatchedFiles(params);
448444
},
449445
);
@@ -454,6 +450,7 @@ export const init = (conn: Connection): void => {
454450
token: CancellationToken,
455451
reporter: WorkDoneProgressReporter,
456452
) => {
453+
syncAllChangedDoc();
457454
return await _pyrightLanguageProvider.onExecuteCommand(
458455
params,
459456
token,
@@ -500,19 +497,20 @@ export const init = (conn: Connection): void => {
500497
};
501498

502499
function getLanguageService(uri: string) {
503-
if (!servicePool[uri]) {
504-
// re-create LanguageServer if document changed
505-
servicePool[uri] = new LanguageServiceProvider(documentPool[uri]);
500+
const docInfo = documentPool[uri];
501+
// re-create LanguageServer if document changed
502+
if (!docInfo.service || docInfo.changed) {
503+
docInfo.service = new LanguageServiceProvider(docInfo.document);
506504

507505
if (supportSASGetLibList) {
508-
servicePool[uri].setLibService((libId, resolve) =>
506+
docInfo.service.setLibService((libId, resolve) =>
509507
connection
510508
.sendRequest<LibCompleteItem[]>("sas/getLibList", { libId: libId })
511509
.then(resolve),
512510
);
513511
}
514512
}
515-
return servicePool[uri];
513+
return docInfo.service!;
516514
}
517515

518516
const dispatch = async <Ret>(
@@ -522,6 +520,7 @@ const dispatch = async <Ret>(
522520
python?: (pyrightLanguageService: PyrightLanguageProvider) => Promise<Ret>;
523521
},
524522
) => {
523+
syncIfDocChange(params.textDocument.uri);
525524
const languageService = getLanguageService(params.textDocument.uri);
526525
const codeZoneManager = languageService.getCodeZoneManager();
527526
const pos = params.position;
@@ -567,3 +566,18 @@ const isRangeIncluded = (a: Range, b: Range) => {
567566
}
568567
return false;
569568
};
569+
570+
const syncIfDocChange = (uri: string) => {
571+
const docInfo = documentPool[uri];
572+
if (!docInfo.changed) {
573+
return;
574+
}
575+
docInfo.changed = false;
576+
_pyrightLanguageProvider.addContentChange(docInfo.document);
577+
};
578+
579+
const syncAllChangedDoc = () => {
580+
for (const uri in documentPool) {
581+
syncIfDocChange(uri);
582+
}
583+
};

0 commit comments

Comments
 (0)