Skip to content

Commit 623d3b3

Browse files
atscottKeen Yee Liau
authored andcommitted
refactor: add helper functions to session.ts
In preparation for adding `typeDefinitionProvider` to the capabilities, refactor some common code in the `session.ts` file into helper functions.
1 parent b048f5f commit 623d3b3

File tree

1 file changed

+38
-41
lines changed

1 file changed

+38
-41
lines changed

server/src/session.ts

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -347,29 +347,26 @@ export class Session {
347347
}
348348
}
349349

350-
private onDefinition(params: lsp.TextDocumentPositionParams) {
351-
const {position, textDocument} = params;
352-
const filePath = uriToFilePath(textDocument.uri);
353-
const scriptInfo = this.projectService.getScriptInfo(filePath);
354-
if (!scriptInfo) {
355-
this.connection.console.log(`Script info not found for ${filePath}`);
350+
private onDefinition(params: lsp.TextDocumentPositionParams): lsp.LocationLink[]|undefined {
351+
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
352+
if (lsInfo === undefined) {
356353
return;
357354
}
358-
359-
const {fileName} = scriptInfo;
360-
const langSvc = this.getDefaultLanguageService(scriptInfo);
361-
if (!langSvc) {
362-
return;
363-
}
364-
365-
const offset = lspPositionToTsPosition(scriptInfo, position);
366-
const definition = langSvc.getDefinitionAndBoundSpan(fileName, offset);
355+
const {languageService, scriptInfo} = lsInfo;
356+
const offset = lspPositionToTsPosition(scriptInfo, params.position);
357+
const definition = languageService.getDefinitionAndBoundSpan(scriptInfo.fileName, offset);
367358
if (!definition || !definition.definitions) {
368359
return;
369360
}
370361
const originSelectionRange = tsTextSpanToLspRange(scriptInfo, definition.textSpan);
362+
return this.tsDefinitionsToLspLocationLinks(definition.definitions, originSelectionRange);
363+
}
364+
365+
private tsDefinitionsToLspLocationLinks(
366+
definitions: readonly ts.DefinitionInfo[],
367+
originSelectionRange?: lsp.Range): lsp.LocationLink[] {
371368
const results: lsp.LocationLink[] = [];
372-
for (const d of definition.definitions) {
369+
for (const d of definitions) {
373370
const scriptInfo = this.projectService.getScriptInfo(d.fileName);
374371

375372
// Some definitions, like definitions of CSS files, may not be recorded files with a
@@ -393,22 +390,31 @@ export class Session {
393390
return results;
394391
}
395392

396-
private onHover(params: lsp.TextDocumentPositionParams) {
397-
const {position, textDocument} = params;
393+
private getLSAndScriptInfo(textDocument: lsp.TextDocumentIdentifier):
394+
{languageService: ts.LanguageService, scriptInfo: ts.server.ScriptInfo}|undefined {
398395
const filePath = uriToFilePath(textDocument.uri);
399-
if (!filePath) {
400-
return;
401-
}
402396
const scriptInfo = this.projectService.getScriptInfo(filePath);
403397
if (!scriptInfo) {
398+
this.connection.console.log(`Script info not found for ${filePath}`);
404399
return;
405400
}
406-
const langSvc = this.getDefaultLanguageService(scriptInfo);
407-
if (!langSvc) {
401+
402+
const languageService = this.getDefaultLanguageService(scriptInfo);
403+
if (!languageService) {
404+
return;
405+
}
406+
407+
return {languageService, scriptInfo};
408+
}
409+
410+
private onHover(params: lsp.TextDocumentPositionParams) {
411+
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
412+
if (lsInfo === undefined) {
408413
return;
409414
}
410-
const offset = lspPositionToTsPosition(scriptInfo, position);
411-
const info = langSvc.getQuickInfoAtPosition(scriptInfo.fileName, offset);
415+
const {languageService, scriptInfo} = lsInfo;
416+
const offset = lspPositionToTsPosition(scriptInfo, params.position);
417+
const info = languageService.getQuickInfoAtPosition(scriptInfo.fileName, offset);
412418
if (!info) {
413419
return;
414420
}
@@ -437,31 +443,22 @@ export class Session {
437443
}
438444

439445
private onCompletion(params: lsp.CompletionParams) {
440-
const {position, textDocument} = params;
441-
const filePath = uriToFilePath(textDocument.uri);
442-
if (!filePath) {
443-
return;
444-
}
445-
const scriptInfo = this.projectService.getScriptInfo(filePath);
446-
if (!scriptInfo) {
447-
return;
448-
}
449-
const {fileName} = scriptInfo;
450-
const langSvc = this.getDefaultLanguageService(scriptInfo);
451-
if (!langSvc) {
446+
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
447+
if (lsInfo === undefined) {
452448
return;
453449
}
454-
const offset = lspPositionToTsPosition(scriptInfo, position);
455-
const completions = langSvc.getCompletionsAtPosition(
456-
fileName, offset,
450+
const {languageService, scriptInfo} = lsInfo;
451+
const offset = lspPositionToTsPosition(scriptInfo, params.position);
452+
const completions = languageService.getCompletionsAtPosition(
453+
scriptInfo.fileName, offset,
457454
{
458455
// options
459456
});
460457
if (!completions) {
461458
return;
462459
}
463460
return completions.entries.map(
464-
(e) => tsCompletionEntryToLspCompletionItem(e, position, scriptInfo));
461+
(e) => tsCompletionEntryToLspCompletionItem(e, params.position, scriptInfo));
465462
}
466463

467464
/**

0 commit comments

Comments
 (0)