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
|
|
|
|
*/
|
|
|
|
|
2017-01-27 17:39:48 -08:00
|
|
|
import {CompileMetadataResolver, CompileNgModuleMetadata, CompilerConfig, DomElementSchemaRegistry, HtmlParser, I18NHtmlParser, Lexer, NgAnalyzedModules, Parser, TemplateParser} from '@angular/compiler';
|
2016-11-22 09:10:23 -08:00
|
|
|
|
2017-04-13 11:56:00 -07:00
|
|
|
import {AstResult, TemplateInfo} from './common';
|
2016-11-22 09:10:23 -08:00
|
|
|
import {getTemplateCompletions} from './completions';
|
|
|
|
import {getDefinition} from './definitions';
|
|
|
|
import {getDeclarationDiagnostics, getTemplateDiagnostics} from './diagnostics';
|
|
|
|
import {getHover} from './hover';
|
2017-04-13 11:56:00 -07:00
|
|
|
import {Completions, Definition, Diagnostic, DiagnosticKind, Diagnostics, Hover, LanguageService, LanguageServiceHost, Pipes, Span, TemplateSource} from './types';
|
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`.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
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(); }
|
|
|
|
|
2017-03-24 09:57:32 -07:00
|
|
|
getDiagnostics(fileName: string): Diagnostics|undefined {
|
2016-11-22 09:10:23 -08:00
|
|
|
let results: Diagnostics = [];
|
|
|
|
let templates = this.host.getTemplates(fileName);
|
|
|
|
if (templates && templates.length) {
|
|
|
|
results.push(...getTemplateDiagnostics(fileName, this, templates));
|
|
|
|
}
|
|
|
|
|
|
|
|
let declarations = this.host.getDeclarations(fileName);
|
|
|
|
if (declarations && declarations.length) {
|
|
|
|
const summary = this.host.getAnalyzedModules();
|
|
|
|
results.push(...getDeclarationDiagnostics(declarations, summary));
|
|
|
|
}
|
|
|
|
|
|
|
|
return uniqueBySpan(results);
|
|
|
|
}
|
|
|
|
|
|
|
|
getPipesAt(fileName: string, position: number): Pipes {
|
|
|
|
let templateInfo = this.getTemplateAstAtPosition(fileName, position);
|
|
|
|
if (templateInfo) {
|
|
|
|
return templateInfo.pipes.map(
|
|
|
|
pipeInfo => ({name: pipeInfo.name, symbol: pipeInfo.type.reference}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getCompletionsAt(fileName: string, position: number): Completions {
|
|
|
|
let templateInfo = this.getTemplateAstAtPosition(fileName, position);
|
|
|
|
if (templateInfo) {
|
|
|
|
return getTemplateCompletions(templateInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefinitionAt(fileName: string, position: number): Definition {
|
|
|
|
let templateInfo = this.getTemplateAstAtPosition(fileName, position);
|
|
|
|
if (templateInfo) {
|
|
|
|
return getDefinition(templateInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 09:57:32 -07:00
|
|
|
getHoverAt(fileName: string, position: number): Hover|undefined {
|
2016-11-22 09:10:23 -08:00
|
|
|
let templateInfo = this.getTemplateAstAtPosition(fileName, position);
|
|
|
|
if (templateInfo) {
|
|
|
|
return getHover(templateInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 09:57:32 -07:00
|
|
|
private getTemplateAstAtPosition(fileName: string, position: number): TemplateInfo|undefined {
|
2016-11-22 09:10:23 -08:00
|
|
|
let template = this.host.getTemplateAt(fileName, position);
|
|
|
|
if (template) {
|
|
|
|
let astResult = this.getTemplateAst(template, fileName);
|
2017-04-28 15:10:30 -07:00
|
|
|
if (astResult && astResult.htmlAst && astResult.templateAst && astResult.directive &&
|
|
|
|
astResult.directives && astResult.pipes && astResult.expressionParser)
|
2016-11-22 09:10:23 -08:00
|
|
|
return {
|
|
|
|
position,
|
|
|
|
fileName,
|
|
|
|
template,
|
|
|
|
htmlAst: astResult.htmlAst,
|
2017-04-28 15:10:30 -07:00
|
|
|
directive: astResult.directive,
|
|
|
|
directives: astResult.directives,
|
|
|
|
pipes: astResult.pipes,
|
2016-11-22 09:10:23 -08:00
|
|
|
templateAst: astResult.templateAst,
|
2017-04-28 15:10:30 -07:00
|
|
|
expressionParser: astResult.expressionParser
|
2016-11-22 09:10:23 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTemplateAst(template: TemplateSource, contextFile: string): AstResult {
|
2017-04-28 15:10:30 -07:00
|
|
|
let result: AstResult|undefined = undefined;
|
2016-11-22 09:10:23 -08:00
|
|
|
try {
|
2017-01-06 20:43:17 -08:00
|
|
|
const resolvedMetadata =
|
2016-11-22 09:10:23 -08:00
|
|
|
this.metadataResolver.getNonNormalizedDirectiveMetadata(template.type as any);
|
2017-01-06 20:43:17 -08:00
|
|
|
const metadata = resolvedMetadata && resolvedMetadata.metadata;
|
2016-11-23 09:42:19 -08:00
|
|
|
if (metadata) {
|
2016-11-22 09:10:23 -08:00
|
|
|
const rawHtmlParser = new HtmlParser();
|
|
|
|
const htmlParser = new I18NHtmlParser(rawHtmlParser);
|
|
|
|
const expressionParser = new Parser(new Lexer());
|
2017-02-17 08:56:36 -08:00
|
|
|
const config = new CompilerConfig();
|
2016-11-22 09:10:23 -08:00
|
|
|
const parser = new TemplateParser(
|
2017-03-24 09:57:32 -07:00
|
|
|
config, expressionParser, new DomElementSchemaRegistry(), htmlParser, null !, []);
|
2017-04-04 10:31:01 -07:00
|
|
|
const htmlResult = htmlParser.parse(template.source, '', true);
|
2016-11-22 09:10:23 -08:00
|
|
|
const analyzedModules = this.host.getAnalyzedModules();
|
2017-04-28 15:10:30 -07:00
|
|
|
let errors: Diagnostic[]|undefined = undefined;
|
2016-11-22 09:10:23 -08:00
|
|
|
let ngModule = analyzedModules.ngModuleByPipeOrDirective.get(template.type);
|
|
|
|
if (!ngModule) {
|
|
|
|
// Reported by the the declaration diagnostics.
|
|
|
|
ngModule = findSuitableDefaultModule(analyzedModules);
|
|
|
|
}
|
|
|
|
if (ngModule) {
|
2017-01-06 20:43:17 -08:00
|
|
|
const resolvedDirectives = ngModule.transitiveModule.directives.map(
|
|
|
|
d => this.host.resolver.getNonNormalizedDirectiveMetadata(d.reference));
|
2017-04-28 15:10:30 -07:00
|
|
|
const directives = removeMissing(resolvedDirectives).map(d => d.metadata.toSummary());
|
2016-11-22 09:10:23 -08:00
|
|
|
const pipes = ngModule.transitiveModule.pipes.map(
|
|
|
|
p => this.host.resolver.getOrLoadPipeMetadata(p.reference).toSummary());
|
|
|
|
const schemas = ngModule.schemas;
|
2017-04-13 11:56:00 -07:00
|
|
|
const parseResult = parser.tryParseHtml(htmlResult, metadata, directives, pipes, schemas);
|
2016-11-22 09:10:23 -08:00
|
|
|
result = {
|
|
|
|
htmlAst: htmlResult.rootNodes,
|
2016-11-23 09:42:19 -08:00
|
|
|
templateAst: parseResult.templateAst,
|
|
|
|
directive: metadata, directives, pipes,
|
2016-11-22 09:10:23 -08:00
|
|
|
parseErrors: parseResult.errors, expressionParser, errors
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
let span = template.span;
|
|
|
|
if (e.fileName == contextFile) {
|
|
|
|
span = template.query.getSpanAt(e.line, e.column) || span;
|
|
|
|
}
|
|
|
|
result = {errors: [{kind: DiagnosticKind.Error, message: e.message, span}]};
|
|
|
|
}
|
2017-04-28 15:10:30 -07:00
|
|
|
return result || {};
|
2016-11-22 09:10:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 15:10:30 -07:00
|
|
|
function removeMissing<T>(values: (T | null | undefined)[]): T[] {
|
|
|
|
return values.filter(e => !!e) as T[];
|
|
|
|
}
|
|
|
|
|
2016-11-22 09:10:23 -08:00
|
|
|
function uniqueBySpan < T extends {
|
|
|
|
span: Span;
|
|
|
|
}
|
|
|
|
> (elements: T[] | undefined): T[]|undefined {
|
|
|
|
if (elements) {
|
|
|
|
const result: T[] = [];
|
|
|
|
const map = new Map<number, Set<number>>();
|
|
|
|
for (const element of elements) {
|
|
|
|
let span = element.span;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 15:10:30 -07:00
|
|
|
function findSuitableDefaultModule(modules: NgAnalyzedModules): CompileNgModuleMetadata|undefined {
|
|
|
|
let result: CompileNgModuleMetadata|undefined = undefined;
|
2016-11-22 09:10:23 -08:00
|
|
|
let resultSize = 0;
|
|
|
|
for (const module of modules.ngModules) {
|
|
|
|
const moduleSize = module.transitiveModule.directives.length;
|
|
|
|
if (moduleSize > resultSize) {
|
|
|
|
result = module;
|
|
|
|
resultSize = moduleSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2017-01-27 17:39:48 -08:00
|
|
|
}
|