Skip to content

Commit 926eb81

Browse files
kyliauKeen Yee Liau
authored andcommitted
refactor: remove Session.getDefaultLanguageService()
The method above could be merged with `Session.getLSAndScriptInfo()`, so we don't need to have so many layers of indirection.
1 parent 85b1533 commit 926eb81

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

server/src/session.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,16 @@ export class Session {
166166
*/
167167
private sendPendingDiagnostics(openFiles: string[]) {
168168
for (const fileName of openFiles) {
169-
const scriptInfo = this.projectService.getScriptInfo(fileName);
170-
if (!scriptInfo) {
169+
const result = this.getLSAndScriptInfo(fileName);
170+
if (!result) {
171171
continue;
172172
}
173-
174-
const ngLS = this.getDefaultLanguageService(scriptInfo);
175-
if (!ngLS) {
176-
continue;
177-
}
178-
179-
const diagnostics = ngLS.getSemanticDiagnostics(fileName);
173+
const diagnostics = result.languageService.getSemanticDiagnostics(fileName);
180174
// Need to send diagnostics even if it's empty otherwise editor state will
181175
// not be updated.
182176
this.connection.sendDiagnostics({
183177
uri: filePathToUri(fileName),
184-
diagnostics: diagnostics.map(d => tsDiagnosticToLspDiagnostic(d, scriptInfo)),
178+
diagnostics: diagnostics.map(d => tsDiagnosticToLspDiagnostic(d, result.scriptInfo)),
185179
});
186180
}
187181
}
@@ -224,16 +218,6 @@ export class Session {
224218
return project;
225219
}
226220

227-
/**
228-
* Returns a language service for a default project created for the specified `scriptInfo`. If the
229-
* project does not support a language service, nothing is returned.
230-
*/
231-
getDefaultLanguageService(scriptInfo: ts.server.ScriptInfo): ts.LanguageService|undefined {
232-
const project = this.getDefaultProjectForScriptInfo(scriptInfo);
233-
if (!project?.languageServiceEnabled) return;
234-
return project.getLanguageService();
235-
}
236-
237221
private onInitialize(params: lsp.InitializeParams): lsp.InitializeResult {
238222
return {
239223
capabilities: {
@@ -402,21 +386,26 @@ export class Session {
402386
return results;
403387
}
404388

405-
private getLSAndScriptInfo(textDocument: lsp.TextDocumentIdentifier):
389+
private getLSAndScriptInfo(textDocumentOrFileName: lsp.TextDocumentIdentifier|string):
406390
{languageService: ts.LanguageService, scriptInfo: ts.server.ScriptInfo}|undefined {
407-
const filePath = uriToFilePath(textDocument.uri);
391+
const filePath = lsp.TextDocumentIdentifier.is(textDocumentOrFileName) ?
392+
uriToFilePath(textDocumentOrFileName.uri) :
393+
textDocumentOrFileName;
408394
const scriptInfo = this.projectService.getScriptInfo(filePath);
409395
if (!scriptInfo) {
410396
this.error(`Script info not found for ${filePath}`);
411397
return;
412398
}
413399

414-
const languageService = this.getDefaultLanguageService(scriptInfo);
415-
if (!languageService) {
400+
const project = this.getDefaultProjectForScriptInfo(scriptInfo);
401+
if (!project?.languageServiceEnabled) {
416402
return;
417403
}
418404

419-
return {languageService, scriptInfo};
405+
return {
406+
languageService: project.getLanguageService(),
407+
scriptInfo,
408+
};
420409
}
421410

422411
private onHover(params: lsp.TextDocumentPositionParams) {

0 commit comments

Comments
 (0)