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-08-01 16:07:32 -04:00
|
|
|
import * as tss from 'typescript/lib/tsserverlibrary';
|
2019-08-21 17:36:00 -04:00
|
|
|
|
2019-08-28 13:18:18 -04:00
|
|
|
import {getTemplateCompletions} from './completions';
|
2019-08-20 14:13:46 -04:00
|
|
|
import {getDefinitionAndBoundSpan, getTsDefinitionAndBoundSpan} from './definitions';
|
2019-08-12 19:54:36 -04:00
|
|
|
import {getDeclarationDiagnostics, getTemplateDiagnostics, ngDiagnosticToTsDiagnostic, uniqueBySpan} from './diagnostics';
|
2019-09-16 22:07:43 -04:00
|
|
|
import {getHover, getTsHover} from './hover';
|
2019-08-12 19:54:36 -04:00
|
|
|
import {Diagnostic, LanguageService} from './types';
|
2019-08-09 18:52:49 -04:00
|
|
|
import {TypeScriptServiceHost} from './typescript_host';
|
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
|
|
|
*/
|
2019-08-09 18:52:49 -04:00
|
|
|
export function createLanguageService(host: TypeScriptServiceHost): LanguageService {
|
2016-11-22 12:10:23 -05:00
|
|
|
return new LanguageServiceImpl(host);
|
|
|
|
}
|
|
|
|
|
|
|
|
class LanguageServiceImpl implements LanguageService {
|
2019-08-09 18:52:49 -04:00
|
|
|
constructor(private readonly host: TypeScriptServiceHost) {}
|
2016-11-22 12:10:23 -05:00
|
|
|
|
2019-08-09 18:52:49 -04:00
|
|
|
getDiagnostics(fileName: string): tss.Diagnostic[] {
|
2019-08-05 22:37:30 -04:00
|
|
|
const analyzedModules = this.host.getAnalyzedModules(); // same role as 'synchronizeHostData'
|
2019-07-31 13:55:45 -04:00
|
|
|
const results: Diagnostic[] = [];
|
|
|
|
const templates = this.host.getTemplates(fileName);
|
2019-09-10 11:55:12 -04:00
|
|
|
|
2019-08-09 18:52:49 -04:00
|
|
|
for (const template of templates) {
|
2019-11-27 18:46:58 -05:00
|
|
|
const ast = this.host.getTemplateAst(template);
|
|
|
|
if (ast) {
|
|
|
|
results.push(...getTemplateDiagnostics(ast));
|
2019-08-21 17:36:00 -04:00
|
|
|
}
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
2019-09-10 11:55:12 -04: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) {
|
2019-09-10 11:55:12 -04:00
|
|
|
results.push(...getDeclarationDiagnostics(declarations, analyzedModules, this.host));
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
2019-09-10 11:55:12 -04:00
|
|
|
|
2019-08-09 18:52:49 -04:00
|
|
|
const sourceFile = fileName.endsWith('.ts') ? this.host.getSourceFile(fileName) : undefined;
|
|
|
|
return uniqueBySpan(results).map(d => ngDiagnosticToTsDiagnostic(d, sourceFile));
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
|
2019-08-12 19:54:36 -04:00
|
|
|
getCompletionsAt(fileName: string, position: number): tss.CompletionInfo|undefined {
|
2019-08-05 22:37:30 -04:00
|
|
|
this.host.getAnalyzedModules(); // same role as 'synchronizeHostData'
|
2019-08-21 17:36:00 -04:00
|
|
|
const ast = this.host.getTemplateAstAtPosition(fileName, position);
|
|
|
|
if (!ast) {
|
2019-08-12 19:54:36 -04:00
|
|
|
return;
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
2019-08-21 17:36:00 -04:00
|
|
|
const results = getTemplateCompletions(ast, position);
|
2019-08-12 19:54:36 -04:00
|
|
|
if (!results || !results.length) {
|
|
|
|
return;
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
2019-08-12 19:54:36 -04:00
|
|
|
return {
|
|
|
|
isGlobalCompletion: false,
|
|
|
|
isMemberCompletion: false,
|
|
|
|
isNewIdentifierLocation: false,
|
2019-10-24 14:35:03 -04:00
|
|
|
// Cast CompletionEntry.kind from ng.CompletionKind to ts.ScriptElementKind
|
|
|
|
entries: results as unknown as ts.CompletionEntry[],
|
2019-08-12 19:54:36 -04:00
|
|
|
};
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
|
2019-08-01 16:07:32 -04:00
|
|
|
getDefinitionAt(fileName: string, position: number): tss.DefinitionInfoAndBoundSpan|undefined {
|
2019-08-05 22:37:30 -04:00
|
|
|
this.host.getAnalyzedModules(); // same role as 'synchronizeHostData'
|
|
|
|
const templateInfo = this.host.getTemplateAstAtPosition(fileName, position);
|
2016-11-22 12:10:23 -05:00
|
|
|
if (templateInfo) {
|
2019-08-21 17:36:00 -04:00
|
|
|
return getDefinitionAndBoundSpan(templateInfo, position);
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
2019-08-20 14:13:46 -04:00
|
|
|
|
|
|
|
// Attempt to get Angular-specific definitions in a TypeScript file, like templates defined
|
|
|
|
// in a `templateUrl` property.
|
|
|
|
if (fileName.endsWith('.ts')) {
|
|
|
|
const sf = this.host.getSourceFile(fileName);
|
|
|
|
if (sf) {
|
2019-08-27 10:51:40 -04:00
|
|
|
return getTsDefinitionAndBoundSpan(sf, position, this.host.tsLsHost);
|
2019-08-20 14:13:46 -04:00
|
|
|
}
|
|
|
|
}
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
|
2019-08-01 16:07:32 -04:00
|
|
|
getHoverAt(fileName: string, position: number): tss.QuickInfo|undefined {
|
2019-08-05 22:37:30 -04:00
|
|
|
this.host.getAnalyzedModules(); // same role as 'synchronizeHostData'
|
|
|
|
const templateInfo = this.host.getTemplateAstAtPosition(fileName, position);
|
2016-11-22 12:10:23 -05:00
|
|
|
if (templateInfo) {
|
2019-10-11 20:15:07 -04:00
|
|
|
return getHover(templateInfo, position, this.host);
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
2019-09-16 22:07:43 -04:00
|
|
|
|
|
|
|
// Attempt to get Angular-specific hover information in a TypeScript file, the NgModule a
|
|
|
|
// directive belongs to.
|
|
|
|
if (fileName.endsWith('.ts')) {
|
|
|
|
const sf = this.host.getSourceFile(fileName);
|
|
|
|
if (sf) {
|
|
|
|
return getTsHover(sf, position, this.host);
|
|
|
|
}
|
|
|
|
}
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
}
|