2016-11-22 12:10:23 -05: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-06-18 19:55:53 -04:00
|
|
|
import {CompileMetadataResolver, CompilePipeSummary} from '@angular/compiler';
|
|
|
|
import {DiagnosticTemplateInfo, getTemplateExpressionDiagnostics} from '@angular/compiler-cli/src/language_services';
|
2016-11-22 12:10:23 -05:00
|
|
|
|
|
|
|
import {getTemplateCompletions} from './completions';
|
|
|
|
import {getDefinition} from './definitions';
|
2019-06-18 19:55:53 -04:00
|
|
|
import {getDeclarationDiagnostics} from './diagnostics';
|
2016-11-22 12:10:23 -05:00
|
|
|
import {getHover} from './hover';
|
2019-07-31 13:55:45 -04:00
|
|
|
import {Completion, Diagnostic, DiagnosticKind, Diagnostics, Hover, LanguageService, LanguageServiceHost, Location, Span, TemplateSource} from './types';
|
2019-06-18 19:55:53 -04:00
|
|
|
import {offsetSpan, spanOf} from './utils';
|
|
|
|
|
2016-11-22 12:10:23 -05:00
|
|
|
|
2017-02-17 11:56:36 -05:00
|
|
|
|
2016-11-22 12:10:23 -05:00
|
|
|
/**
|
|
|
|
* Create an instance of an Angular `LanguageService`.
|
|
|
|
*
|
2018-10-19 07:12:20 -04:00
|
|
|
* @publicApi
|
2016-11-22 12:10:23 -05:00
|
|
|
*/
|
|
|
|
export function createLanguageService(host: LanguageServiceHost): LanguageService {
|
|
|
|
return new LanguageServiceImpl(host);
|
|
|
|
}
|
|
|
|
|
|
|
|
class LanguageServiceImpl implements LanguageService {
|
|
|
|
constructor(private host: LanguageServiceHost) {}
|
|
|
|
|
|
|
|
private get metadataResolver(): CompileMetadataResolver { return this.host.resolver; }
|
|
|
|
|
|
|
|
getTemplateReferences(): string[] { return this.host.getTemplateReferences(); }
|
|
|
|
|
2019-07-31 13:55:45 -04:00
|
|
|
getDiagnostics(fileName: string): Diagnostic[] {
|
|
|
|
const results: Diagnostic[] = [];
|
|
|
|
const templates = this.host.getTemplates(fileName);
|
2016-11-22 12:10:23 -05:00
|
|
|
if (templates && templates.length) {
|
2019-06-18 19:55:53 -04:00
|
|
|
results.push(...this.getTemplateDiagnostics(fileName, templates));
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:55:45 -04:00
|
|
|
const declarations = this.host.getDeclarations(fileName);
|
2016-11-22 12:10:23 -05:00
|
|
|
if (declarations && declarations.length) {
|
|
|
|
const summary = this.host.getAnalyzedModules();
|
|
|
|
results.push(...getDeclarationDiagnostics(declarations, summary));
|
|
|
|
}
|
|
|
|
|
|
|
|
return uniqueBySpan(results);
|
|
|
|
}
|
|
|
|
|
2017-05-09 19:16:50 -04:00
|
|
|
getPipesAt(fileName: string, position: number): CompilePipeSummary[] {
|
2019-06-18 19:55:53 -04:00
|
|
|
let templateInfo = this.host.getTemplateAstAtPosition(fileName, position);
|
2016-11-22 12:10:23 -05:00
|
|
|
if (templateInfo) {
|
2017-05-09 19:16:50 -04:00
|
|
|
return templateInfo.pipes;
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
2017-05-09 19:16:50 -04:00
|
|
|
return [];
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:55:45 -04:00
|
|
|
getCompletionsAt(fileName: string, position: number): Completion[]|undefined {
|
2019-06-18 19:55:53 -04:00
|
|
|
let templateInfo = this.host.getTemplateAstAtPosition(fileName, position);
|
2016-11-22 12:10:23 -05:00
|
|
|
if (templateInfo) {
|
|
|
|
return getTemplateCompletions(templateInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:55:45 -04:00
|
|
|
getDefinitionAt(fileName: string, position: number): Location[]|undefined {
|
2019-06-18 19:55:53 -04:00
|
|
|
let templateInfo = this.host.getTemplateAstAtPosition(fileName, position);
|
2016-11-22 12:10:23 -05:00
|
|
|
if (templateInfo) {
|
|
|
|
return getDefinition(templateInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 12:57:32 -04:00
|
|
|
getHoverAt(fileName: string, position: number): Hover|undefined {
|
2019-06-18 19:55:53 -04:00
|
|
|
let templateInfo = this.host.getTemplateAstAtPosition(fileName, position);
|
2016-11-22 12:10:23 -05:00
|
|
|
if (templateInfo) {
|
|
|
|
return getHover(templateInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 19:55:53 -04:00
|
|
|
private getTemplateDiagnostics(fileName: string, templates: TemplateSource[]): Diagnostics {
|
|
|
|
const results: Diagnostics = [];
|
|
|
|
for (const template of templates) {
|
|
|
|
const ast = this.host.getTemplateAst(template, fileName);
|
|
|
|
if (ast) {
|
|
|
|
if (ast.parseErrors && ast.parseErrors.length) {
|
|
|
|
results.push(...ast.parseErrors.map<Diagnostic>(
|
|
|
|
e => ({
|
|
|
|
kind: DiagnosticKind.Error,
|
|
|
|
span: offsetSpan(spanOf(e.span), template.span.start),
|
|
|
|
message: e.msg
|
|
|
|
})));
|
|
|
|
} else if (ast.templateAst && ast.htmlAst) {
|
|
|
|
const info: DiagnosticTemplateInfo = {
|
|
|
|
templateAst: ast.templateAst,
|
|
|
|
htmlAst: ast.htmlAst,
|
|
|
|
offset: template.span.start,
|
|
|
|
query: template.query,
|
|
|
|
members: template.members
|
2016-11-22 12:10:23 -05:00
|
|
|
};
|
2019-06-18 19:55:53 -04:00
|
|
|
const expressionDiagnostics = getTemplateExpressionDiagnostics(info);
|
|
|
|
results.push(...expressionDiagnostics);
|
|
|
|
}
|
|
|
|
if (ast.errors) {
|
|
|
|
results.push(...ast.errors.map<Diagnostic>(
|
|
|
|
e => ({kind: e.kind, span: e.span || template.span, message: e.message})));
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-18 19:55:53 -04:00
|
|
|
return results;
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:55:45 -04: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 12:10:23 -05:00
|
|
|
}
|
|
|
|
}
|
2019-07-31 13:55:45 -04:00
|
|
|
return result;
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|