refactor(language-service): Return ts.CompletionInfo for getCompletionsAt() (#32116)
Part 3/3 of language-service refactoring: Change all language service APIs to return TS value since Angular LS will be a proper tsserver plugin. This reduces the need to transform results among Angular <--> TS <--> LSP. PR Close #32116
This commit is contained in:

committed by
Andrew Kushnir

parent
d6bbc4d76d
commit
5a562d8a0a
@ -6,17 +6,14 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {CompilePipeSummary} from '@angular/compiler';
|
||||
import * as tss from 'typescript/lib/tsserverlibrary';
|
||||
|
||||
import {getTemplateCompletions} from './completions';
|
||||
import {getTemplateCompletions, ngCompletionToTsCompletionEntry} from './completions';
|
||||
import {getDefinitionAndBoundSpan} from './definitions';
|
||||
import {getDeclarationDiagnostics, getTemplateDiagnostics, ngDiagnosticToTsDiagnostic} from './diagnostics';
|
||||
import {getDeclarationDiagnostics, getTemplateDiagnostics, ngDiagnosticToTsDiagnostic, uniqueBySpan} from './diagnostics';
|
||||
import {getHover} from './hover';
|
||||
import {Completion, Diagnostic, LanguageService, Span} from './types';
|
||||
import {Diagnostic, LanguageService} from './types';
|
||||
import {TypeScriptServiceHost} from './typescript_host';
|
||||
|
||||
|
||||
/**
|
||||
* Create an instance of an Angular `LanguageService`.
|
||||
*
|
||||
@ -53,21 +50,22 @@ class LanguageServiceImpl implements LanguageService {
|
||||
return uniqueBySpan(results).map(d => ngDiagnosticToTsDiagnostic(d, sourceFile));
|
||||
}
|
||||
|
||||
getPipesAt(fileName: string, position: number): CompilePipeSummary[] {
|
||||
getCompletionsAt(fileName: string, position: number): tss.CompletionInfo|undefined {
|
||||
this.host.getAnalyzedModules(); // same role as 'synchronizeHostData'
|
||||
const templateInfo = this.host.getTemplateAstAtPosition(fileName, position);
|
||||
if (templateInfo) {
|
||||
return templateInfo.pipes;
|
||||
if (!templateInfo) {
|
||||
return;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
getCompletionsAt(fileName: string, position: number): Completion[]|undefined {
|
||||
this.host.getAnalyzedModules(); // same role as 'synchronizeHostData'
|
||||
const templateInfo = this.host.getTemplateAstAtPosition(fileName, position);
|
||||
if (templateInfo) {
|
||||
return getTemplateCompletions(templateInfo);
|
||||
const results = getTemplateCompletions(templateInfo);
|
||||
if (!results || !results.length) {
|
||||
return;
|
||||
}
|
||||
return {
|
||||
isGlobalCompletion: false,
|
||||
isMemberCompletion: false,
|
||||
isNewIdentifierLocation: false,
|
||||
entries: results.map(ngCompletionToTsCompletionEntry),
|
||||
};
|
||||
}
|
||||
|
||||
getDefinitionAt(fileName: string, position: number): tss.DefinitionInfoAndBoundSpan|undefined {
|
||||
@ -86,21 +84,3 @@ class LanguageServiceImpl implements LanguageService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function uniqueBySpan<T extends{span: Span}>(elements: T[]): T[] {
|
||||
const result: T[] = [];
|
||||
const map = new Map<number, Set<number>>();
|
||||
for (const element of elements) {
|
||||
const {span} = element;
|
||||
let set = map.get(span.start);
|
||||
if (!set) {
|
||||
set = new Set();
|
||||
map.set(span.start, set);
|
||||
}
|
||||
if (!set.has(span.end)) {
|
||||
set.add(span.end);
|
||||
result.push(element);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
Reference in New Issue
Block a user