Skip to content

Commit 9bdc08d

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

File tree

2 files changed

+48
-50
lines changed

2 files changed

+48
-50
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 & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
RenameParams,
1818
ResultProgressReporter,
1919
SemanticTokensRequest,
20-
SignatureHelpParams,
2120
TextDocumentPositionParams,
2221
TextDocumentSyncKind,
2322
WorkDoneProgressReporter,
@@ -37,8 +36,13 @@ import { CompletionProvider } from "./sas/CompletionProvider";
3736
import { LanguageServiceProvider, legend } from "./sas/LanguageServiceProvider";
3837
import type { LibCompleteItem } from "./sas/SyntaxDataProvider";
3938

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

4347
let completionProvider: CompletionProvider;
4448
let connection: Connection;
@@ -116,6 +120,7 @@ export const init = (conn: Connection): void => {
116120
connection.onInitialized(() => _pyrightLanguageProvider.onInitialized());
117121

118122
connection.onRequest(SemanticTokensRequest.type, (params) => {
123+
syncIfDocChange(params.textDocument.uri);
119124
const languageService = getLanguageService(params.textDocument.uri);
120125

121126
return { data: languageService.getTokens() };
@@ -188,6 +193,7 @@ export const init = (conn: Connection): void => {
188193
});
189194

190195
connection.onDocumentSymbol(async (params, token) => {
196+
syncIfDocChange(params.textDocument.uri);
191197
const languageService = getLanguageService(params.textDocument.uri);
192198
const sasSymbols = languageService.getDocumentSymbols();
193199
const pythonSymbols =
@@ -209,6 +215,7 @@ export const init = (conn: Connection): void => {
209215

210216
// todo
211217
connection.onFoldingRanges((params) => {
218+
syncIfDocChange(params.textDocument.uri);
212219
const languageService = getLanguageService(params.textDocument.uri);
213220
return languageService.getFoldingRanges();
214221
});
@@ -267,6 +274,7 @@ export const init = (conn: Connection): void => {
267274
});
268275

269276
connection.onDocumentFormatting((params) => {
277+
syncIfDocChange(params.textDocument.uri);
270278
const languageService = getLanguageService(params.textDocument.uri);
271279
return languageService.formatter.format({
272280
tabWidth: params.options.tabSize,
@@ -281,27 +289,25 @@ export const init = (conn: Connection): void => {
281289
params.textDocument.version,
282290
params.textDocument.text,
283291
);
284-
documentPool[doc.uri] = doc;
292+
documentPool[doc.uri] = { document: doc, changed: false };
285293
await _pyrightLanguageProvider.onDidOpenTextDocument(params);
286294
});
287295

288296
connection.onDidCloseTextDocument(async (params) => {
289297
const uri = params.textDocument.uri;
290298
delete documentPool[uri];
291-
delete servicePool[uri];
292299
await _pyrightLanguageProvider.onDidCloseTextDocument(params);
293300
});
294301

295302
connection.onDidChangeTextDocument((params) => {
296303
const uri = params.textDocument.uri;
297-
const doc = documentPool[uri];
304+
const docInfo = documentPool[uri];
298305
TextDocument.update(
299-
doc,
306+
docInfo.document,
300307
params.contentChanges,
301308
params.textDocument.version,
302309
);
303-
delete servicePool[uri];
304-
_pyrightLanguageProvider.addContentChange(doc);
310+
docInfo.changed = true;
305311
});
306312

307313
connection.onDidChangeConfiguration(
@@ -378,6 +384,7 @@ export const init = (conn: Connection): void => {
378384
workDoneProgress,
379385
resultProgress,
380386
) => {
387+
syncAllChangedDoc();
381388
return await _pyrightLanguageProvider.onWorkspaceSymbol(
382389
params,
383390
token,
@@ -399,21 +406,6 @@ export const init = (conn: Connection): void => {
399406
},
400407
);
401408

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-
417409
connection.onPrepareRename(
418410
async (params: PrepareRenameParams, token: CancellationToken) => {
419411
return await dispatch(params, {
@@ -444,6 +436,9 @@ export const init = (conn: Connection): void => {
444436

445437
connection.onDidChangeWatchedFiles(
446438
async (params: DidChangeWatchedFilesParams) => {
439+
params.changes.forEach((item) => {
440+
syncIfDocChange(item.uri);
441+
});
447442
_pyrightLanguageProvider.onDidChangeWatchedFiles(params);
448443
},
449444
);
@@ -454,6 +449,7 @@ export const init = (conn: Connection): void => {
454449
token: CancellationToken,
455450
reporter: WorkDoneProgressReporter,
456451
) => {
452+
syncAllChangedDoc();
457453
return await _pyrightLanguageProvider.onExecuteCommand(
458454
params,
459455
token,
@@ -500,19 +496,20 @@ export const init = (conn: Connection): void => {
500496
};
501497

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

507504
if (supportSASGetLibList) {
508-
servicePool[uri].setLibService((libId, resolve) =>
505+
docInfo.service.setLibService((libId, resolve) =>
509506
connection
510507
.sendRequest<LibCompleteItem[]>("sas/getLibList", { libId: libId })
511508
.then(resolve),
512509
);
513510
}
514511
}
515-
return servicePool[uri];
512+
return docInfo.service!;
516513
}
517514

518515
const dispatch = async <Ret>(
@@ -522,6 +519,7 @@ const dispatch = async <Ret>(
522519
python?: (pyrightLanguageService: PyrightLanguageProvider) => Promise<Ret>;
523520
},
524521
) => {
522+
syncIfDocChange(params.textDocument.uri);
525523
const languageService = getLanguageService(params.textDocument.uri);
526524
const codeZoneManager = languageService.getCodeZoneManager();
527525
const pos = params.position;
@@ -567,3 +565,18 @@ const isRangeIncluded = (a: Range, b: Range) => {
567565
}
568566
return false;
569567
};
568+
569+
const syncIfDocChange = (uri: string) => {
570+
const docInfo = documentPool[uri];
571+
if (!docInfo.changed) {
572+
return;
573+
}
574+
docInfo.changed = false;
575+
_pyrightLanguageProvider.addContentChange(docInfo.document);
576+
};
577+
578+
const syncAllChangedDoc = () => {
579+
for (const uri in documentPool) {
580+
syncIfDocChange(uri);
581+
}
582+
};

0 commit comments

Comments
 (0)