2016-11-22 09:10:23 -08:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2019-08-09 15:52:49 -07:00
|
|
|
import {CompilePipeSummary} from '@angular/compiler';
|
2019-08-01 13:07:32 -07:00
|
|
|
import * as tss from 'typescript/lib/tsserverlibrary';
|
2019-08-09 15:52:49 -07:00
|
|
|
|
2016-11-22 09:10:23 -08:00
|
|
|
import {getTemplateCompletions} from './completions';
|
2019-08-01 13:07:32 -07:00
|
|
|
import {getDefinitionAndBoundSpan} from './definitions';
|
2019-08-09 15:52:49 -07:00
|
|
|
import {getDeclarationDiagnostics, getTemplateDiagnostics, ngDiagnosticToTsDiagnostic} from './diagnostics';
|
2016-11-22 09:10:23 -08:00
|
|
|
import {getHover} from './hover';
|
2019-08-09 15:52:49 -07:00
|
|
|
import {Completion, Diagnostic, LanguageService, Span} from './types';
|
|
|
|
import {TypeScriptServiceHost} from './typescript_host';
|
2016-11-22 09:10:23 -08:00
|
|
|
|
2017-02-17 08:56:36 -08:00
|
|
|
|
2016-11-22 09:10:23 -08:00
|
|
|
/**
|
|
|
|
* Create an instance of an Angular `LanguageService`.
|
|
|
|
*
|
2018-10-19 12:12:20 +01:00
|
|
|
* @publicApi
|
2016-11-22 09:10:23 -08:00
|
|
|
*/
|
2019-08-09 15:52:49 -07:00
|
|
|
export function createLanguageService(host: TypeScriptServiceHost): LanguageService {
|
2016-11-22 09:10:23 -08:00
|
|
|
return new LanguageServiceImpl(host);
|
|
|
|
}
|
|
|
|
|
|
|
|
class LanguageServiceImpl implements LanguageService {
|
2019-08-09 15:52:49 -07:00
|
|
|
constructor(private readonly host: TypeScriptServiceHost) {}
|
2016-11-22 09:10:23 -08:00
|
|
|
|
2019-08-05 19:37:30 -07:00
|
|
|
getTemplateReferences(): string[] {
|
|
|
|
this.host.getAnalyzedModules(); // same role as 'synchronizeHostData'
|
|
|
|
return this.host.getTemplateReferences();
|
|
|
|
}
|
2016-11-22 09:10:23 -08:00
|
|
|
|
2019-08-09 15:52:49 -07:00
|
|
|
getDiagnostics(fileName: string): tss.Diagnostic[] {
|
2019-08-05 19:37:30 -07:00
|
|
|
const analyzedModules = this.host.getAnalyzedModules(); // same role as 'synchronizeHostData'
|
2019-07-31 10:55:45 -07:00
|
|
|
const results: Diagnostic[] = [];
|
|
|
|
const templates = this.host.getTemplates(fileName);
|
2019-08-09 15:52:49 -07:00
|
|
|
for (const template of templates) {
|
|
|
|
const ast = this.host.getTemplateAst(template, fileName);
|
|
|
|
results.push(...getTemplateDiagnostics(template, ast));
|
2016-11-22 09:10:23 -08:00
|
|
|
}
|
2019-07-31 10:55:45 -07:00
|
|
|
const declarations = this.host.getDeclarations(fileName);
|
2016-11-22 09:10:23 -08:00
|
|
|
if (declarations && declarations.length) {
|
2019-08-05 19:37:30 -07:00
|
|
|
results.push(...getDeclarationDiagnostics(declarations, analyzedModules));
|
2016-11-22 09:10:23 -08:00
|
|
|
}
|
2019-08-09 15:52:49 -07:00
|
|
|
if (!results.length) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const sourceFile = fileName.endsWith('.ts') ? this.host.getSourceFile(fileName) : undefined;
|
|
|
|
return uniqueBySpan(results).map(d => ngDiagnosticToTsDiagnostic(d, sourceFile));
|
2016-11-22 09:10:23 -08:00
|
|
|
}
|
|
|
|
|
2017-05-09 16:16:50 -07:00
|
|
|
getPipesAt(fileName: string, position: number): CompilePipeSummary[] {
|
2019-08-05 19:37:30 -07:00
|
|
|
this.host.getAnalyzedModules(); // same role as 'synchronizeHostData'
|
|
|
|
const templateInfo = this.host.getTemplateAstAtPosition(fileName, position);
|
2016-11-22 09:10:23 -08:00
|
|
|
if (templateInfo) {
|
2017-05-09 16:16:50 -07:00
|
|
|
return templateInfo.pipes;
|
2016-11-22 09:10:23 -08:00
|
|
|
}
|
2017-05-09 16:16:50 -07:00
|
|
|
return [];
|
2016-11-22 09:10:23 -08:00
|
|
|
}
|
|
|
|
|
2019-07-31 10:55:45 -07:00
|
|
|
getCompletionsAt(fileName: string, position: number): Completion[]|undefined {
|
2019-08-05 19:37:30 -07:00
|
|
|
this.host.getAnalyzedModules(); // same role as 'synchronizeHostData'
|
|
|
|
const templateInfo = this.host.getTemplateAstAtPosition(fileName, position);
|
2016-11-22 09:10:23 -08:00
|
|
|
if (templateInfo) {
|
|
|
|
return getTemplateCompletions(templateInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 13:07:32 -07:00
|
|
|
getDefinitionAt(fileName: string, position: number): tss.DefinitionInfoAndBoundSpan|undefined {
|
2019-08-05 19:37:30 -07:00
|
|
|
this.host.getAnalyzedModules(); // same role as 'synchronizeHostData'
|
|
|
|
const templateInfo = this.host.getTemplateAstAtPosition(fileName, position);
|
2016-11-22 09:10:23 -08:00
|
|
|
if (templateInfo) {
|
2019-08-01 13:07:32 -07:00
|
|
|
return getDefinitionAndBoundSpan(templateInfo);
|
2016-11-22 09:10:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 13:07:32 -07:00
|
|
|
getHoverAt(fileName: string, position: number): tss.QuickInfo|undefined {
|
2019-08-05 19:37:30 -07:00
|
|
|
this.host.getAnalyzedModules(); // same role as 'synchronizeHostData'
|
|
|
|
const templateInfo = this.host.getTemplateAstAtPosition(fileName, position);
|
2016-11-22 09:10:23 -08:00
|
|
|
if (templateInfo) {
|
|
|
|
return getHover(templateInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 10:55:45 -07:00
|
|
|
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);
|
2016-11-22 09:10:23 -08:00
|
|
|
}
|
|
|
|
}
|
2019-07-31 10:55:45 -07:00
|
|
|
return result;
|
2016-11-22 09:10:23 -08:00
|
|
|
}
|