2020-04-29 18:08:22 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2020-04-29 18:08:22 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2020-12-03 21:08:00 -05:00
|
|
|
import {AST, TmplAstBoundEvent, TmplAstNode} from '@angular/compiler';
|
2020-10-30 18:16:39 -04:00
|
|
|
import {CompilerOptions, ConfigurationHost, readConfiguration} from '@angular/compiler-cli';
|
2020-09-30 13:59:38 -04:00
|
|
|
import {absoluteFromSourceFile, AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system';
|
2020-07-31 00:26:02 -04:00
|
|
|
import {TypeCheckShimGenerator} from '@angular/compiler-cli/src/ngtsc/typecheck';
|
|
|
|
import {OptimizeFor, TypeCheckingProgramStrategy} from '@angular/compiler-cli/src/ngtsc/typecheck/api';
|
2020-04-29 18:08:22 -04:00
|
|
|
import * as ts from 'typescript/lib/tsserverlibrary';
|
|
|
|
|
2020-10-30 18:16:39 -04:00
|
|
|
import {LanguageServiceAdapter, LSParseConfigHost} from './adapters';
|
2020-10-09 19:46:55 -04:00
|
|
|
import {CompilerFactory} from './compiler_factory';
|
2020-11-18 20:30:52 -05:00
|
|
|
import {CompletionBuilder, CompletionNodeContext} from './completions';
|
2020-09-30 11:47:36 -04:00
|
|
|
import {DefinitionBuilder} from './definitions';
|
2020-09-28 14:26:07 -04:00
|
|
|
import {QuickInfoBuilder} from './quick_info';
|
2020-10-13 14:14:13 -04:00
|
|
|
import {ReferenceBuilder} from './references';
|
2020-11-18 20:30:52 -05:00
|
|
|
import {getTargetAtPosition, TargetNode, TargetNodeKind} from './template_target';
|
2020-10-13 13:28:15 -04:00
|
|
|
import {getTemplateInfoAtPosition, isTypeScriptFile} from './utils';
|
2020-09-28 14:26:07 -04:00
|
|
|
|
2020-04-29 18:08:22 -04:00
|
|
|
export class LanguageService {
|
2020-04-29 18:52:17 -04:00
|
|
|
private options: CompilerOptions;
|
2020-11-04 15:28:59 -05:00
|
|
|
readonly compilerFactory: CompilerFactory;
|
2020-07-31 00:26:02 -04:00
|
|
|
private readonly strategy: TypeCheckingProgramStrategy;
|
2020-09-30 13:59:38 -04:00
|
|
|
private readonly adapter: LanguageServiceAdapter;
|
2020-11-09 19:20:02 -05:00
|
|
|
private readonly parseConfigHost: LSParseConfigHost;
|
2020-04-29 18:52:17 -04:00
|
|
|
|
|
|
|
constructor(project: ts.server.Project, private readonly tsLS: ts.LanguageService) {
|
2020-10-30 18:16:39 -04:00
|
|
|
this.parseConfigHost = new LSParseConfigHost(project.projectService.host);
|
2020-11-09 19:20:02 -05:00
|
|
|
this.options = parseNgCompilerOptions(project, this.parseConfigHost);
|
2020-12-11 14:01:51 -05:00
|
|
|
|
|
|
|
// Projects loaded into the Language Service often include test files which are not part of the
|
|
|
|
// app's main compilation unit, and these test files often include inline NgModules that declare
|
|
|
|
// components from the app. These declarations conflict with the main declarations of such
|
|
|
|
// components in the app's NgModules. This conflict is not normally present during regular
|
|
|
|
// compilation because the app and the tests are part of separate compilation units.
|
|
|
|
//
|
|
|
|
// As a temporary mitigation of this problem, we instruct the compiler to ignore classes which
|
|
|
|
// are not exported. In many cases, this ensures the test NgModules are ignored by the compiler
|
|
|
|
// and only the real component declaration is used.
|
|
|
|
this.options.compileNonExportedClasses = false;
|
|
|
|
|
2020-07-31 00:26:02 -04:00
|
|
|
this.strategy = createTypeCheckingProgramStrategy(project);
|
2020-09-30 13:59:38 -04:00
|
|
|
this.adapter = new LanguageServiceAdapter(project);
|
2020-11-04 15:28:59 -05:00
|
|
|
this.compilerFactory = new CompilerFactory(this.adapter, this.strategy, this.options);
|
2020-04-29 18:52:17 -04:00
|
|
|
this.watchConfigFile(project);
|
|
|
|
}
|
2020-04-29 18:08:22 -04:00
|
|
|
|
2020-11-09 19:20:02 -05:00
|
|
|
getCompilerOptions(): CompilerOptions {
|
|
|
|
return this.options;
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:08:22 -04:00
|
|
|
getSemanticDiagnostics(fileName: string): ts.Diagnostic[] {
|
2020-11-04 15:28:59 -05:00
|
|
|
const compiler = this.compilerFactory.getOrCreateWithChangedFile(fileName);
|
2020-09-30 13:59:38 -04:00
|
|
|
const ttc = compiler.getTemplateTypeChecker();
|
|
|
|
const diagnostics: ts.Diagnostic[] = [];
|
|
|
|
if (isTypeScriptFile(fileName)) {
|
2020-10-09 19:46:55 -04:00
|
|
|
const program = compiler.getNextProgram();
|
2020-07-31 00:26:02 -04:00
|
|
|
const sourceFile = program.getSourceFile(fileName);
|
2020-09-30 13:59:38 -04:00
|
|
|
if (sourceFile) {
|
|
|
|
diagnostics.push(...ttc.getDiagnosticsForFile(sourceFile, OptimizeFor.SingleFile));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const components = compiler.getComponentsWithTemplateFile(fileName);
|
|
|
|
for (const component of components) {
|
|
|
|
if (ts.isClassDeclaration(component)) {
|
|
|
|
diagnostics.push(...ttc.getDiagnosticsForComponent(component));
|
|
|
|
}
|
2020-07-31 00:26:02 -04:00
|
|
|
}
|
2020-04-30 18:48:20 -04:00
|
|
|
}
|
2020-10-09 19:46:55 -04:00
|
|
|
this.compilerFactory.registerLastKnownProgram();
|
2020-09-30 13:59:38 -04:00
|
|
|
return diagnostics;
|
2020-07-31 00:26:02 -04:00
|
|
|
}
|
|
|
|
|
2020-09-30 11:47:36 -04:00
|
|
|
getDefinitionAndBoundSpan(fileName: string, position: number): ts.DefinitionInfoAndBoundSpan
|
|
|
|
|undefined {
|
2020-11-04 15:28:59 -05:00
|
|
|
const compiler = this.compilerFactory.getOrCreateWithChangedFile(fileName);
|
2020-10-21 14:01:10 -04:00
|
|
|
const results =
|
|
|
|
new DefinitionBuilder(this.tsLS, compiler).getDefinitionAndBoundSpan(fileName, position);
|
2020-10-09 19:46:55 -04:00
|
|
|
this.compilerFactory.registerLastKnownProgram();
|
|
|
|
return results;
|
2020-09-30 11:47:36 -04:00
|
|
|
}
|
|
|
|
|
2020-10-02 16:54:18 -04:00
|
|
|
getTypeDefinitionAtPosition(fileName: string, position: number):
|
|
|
|
readonly ts.DefinitionInfo[]|undefined {
|
2020-11-04 15:28:59 -05:00
|
|
|
const compiler = this.compilerFactory.getOrCreateWithChangedFile(fileName);
|
2020-10-21 14:01:10 -04:00
|
|
|
const results =
|
|
|
|
new DefinitionBuilder(this.tsLS, compiler).getTypeDefinitionsAtPosition(fileName, position);
|
2020-10-09 19:46:55 -04:00
|
|
|
this.compilerFactory.registerLastKnownProgram();
|
|
|
|
return results;
|
2020-10-02 16:54:18 -04:00
|
|
|
}
|
|
|
|
|
2020-09-28 14:26:07 -04:00
|
|
|
getQuickInfoAtPosition(fileName: string, position: number): ts.QuickInfo|undefined {
|
2020-11-04 15:28:59 -05:00
|
|
|
const compiler = this.compilerFactory.getOrCreateWithChangedFile(fileName);
|
2020-10-13 13:28:15 -04:00
|
|
|
const templateInfo = getTemplateInfoAtPosition(fileName, position, compiler);
|
|
|
|
if (templateInfo === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const positionDetails = getTargetAtPosition(templateInfo.template, position);
|
|
|
|
if (positionDetails === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const results =
|
2020-11-18 17:07:30 -05:00
|
|
|
new QuickInfoBuilder(
|
|
|
|
this.tsLS, compiler, templateInfo.component, positionDetails.nodeInContext.node)
|
2020-10-13 13:28:15 -04:00
|
|
|
.get();
|
2020-10-09 19:46:55 -04:00
|
|
|
this.compilerFactory.registerLastKnownProgram();
|
|
|
|
return results;
|
2020-04-29 18:08:22 -04:00
|
|
|
}
|
2020-04-29 18:52:17 -04:00
|
|
|
|
2020-11-19 16:31:34 -05:00
|
|
|
getReferencesAtPosition(fileName: string, position: number): ts.ReferenceEntry[]|undefined {
|
|
|
|
const compiler = this.compilerFactory.getOrCreateWithChangedFile(fileName);
|
|
|
|
const results =
|
|
|
|
new ReferenceBuilder(this.strategy, this.tsLS, compiler).get(fileName, position);
|
|
|
|
this.compilerFactory.registerLastKnownProgram();
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2020-10-13 14:14:13 -04:00
|
|
|
private getCompletionBuilder(fileName: string, position: number):
|
|
|
|
CompletionBuilder<TmplAstNode|AST>|null {
|
|
|
|
const compiler = this.compilerFactory.getOrCreateWithChangedFile(fileName);
|
|
|
|
const templateInfo = getTemplateInfoAtPosition(fileName, position, compiler);
|
|
|
|
if (templateInfo === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const positionDetails = getTargetAtPosition(templateInfo.template, position);
|
|
|
|
if (positionDetails === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return new CompletionBuilder(
|
2020-11-18 17:07:30 -05:00
|
|
|
this.tsLS, compiler, templateInfo.component, positionDetails.nodeInContext.node,
|
2020-11-18 20:30:52 -05:00
|
|
|
nodeContextFromTarget(positionDetails.nodeInContext), positionDetails.parent,
|
|
|
|
positionDetails.template);
|
2020-10-13 14:14:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
getCompletionsAtPosition(
|
|
|
|
fileName: string, position: number, options: ts.GetCompletionsAtPositionOptions|undefined):
|
|
|
|
ts.WithMetadata<ts.CompletionInfo>|undefined {
|
|
|
|
const builder = this.getCompletionBuilder(fileName, position);
|
|
|
|
if (builder === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const result = builder.getCompletionsAtPosition(options);
|
|
|
|
this.compilerFactory.registerLastKnownProgram();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
getCompletionEntryDetails(
|
|
|
|
fileName: string, position: number, entryName: string,
|
|
|
|
formatOptions: ts.FormatCodeOptions|ts.FormatCodeSettings|undefined,
|
|
|
|
preferences: ts.UserPreferences|undefined): ts.CompletionEntryDetails|undefined {
|
|
|
|
const builder = this.getCompletionBuilder(fileName, position);
|
|
|
|
if (builder === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const result = builder.getCompletionEntryDetails(entryName, formatOptions, preferences);
|
|
|
|
this.compilerFactory.registerLastKnownProgram();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-11-18 20:30:52 -05:00
|
|
|
getCompletionEntrySymbol(fileName: string, position: number, entryName: string): ts.Symbol
|
|
|
|
|undefined {
|
2020-10-13 14:14:13 -04:00
|
|
|
const builder = this.getCompletionBuilder(fileName, position);
|
|
|
|
if (builder === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-11-18 20:30:52 -05:00
|
|
|
const result = builder.getCompletionEntrySymbol(entryName);
|
2020-10-13 14:14:13 -04:00
|
|
|
this.compilerFactory.registerLastKnownProgram();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:52:17 -04:00
|
|
|
private watchConfigFile(project: ts.server.Project) {
|
|
|
|
// TODO: Check the case when the project is disposed. An InferredProject
|
|
|
|
// could be disposed when a tsconfig.json is added to the workspace,
|
|
|
|
// in which case it becomes a ConfiguredProject (or vice-versa).
|
|
|
|
// We need to make sure that the FileWatcher is closed.
|
|
|
|
if (!(project instanceof ts.server.ConfiguredProject)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const {host} = project.projectService;
|
|
|
|
host.watchFile(
|
|
|
|
project.getConfigFilePath(), (fileName: string, eventKind: ts.FileWatcherEventKind) => {
|
|
|
|
project.log(`Config file changed: ${fileName}`);
|
|
|
|
if (eventKind === ts.FileWatcherEventKind.Changed) {
|
2020-11-09 19:20:02 -05:00
|
|
|
this.options = parseNgCompilerOptions(project, this.parseConfigHost);
|
2020-04-29 18:52:17 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 18:16:39 -04:00
|
|
|
function parseNgCompilerOptions(
|
|
|
|
project: ts.server.Project, host: ConfigurationHost): CompilerOptions {
|
2020-11-09 19:20:02 -05:00
|
|
|
if (!(project instanceof ts.server.ConfiguredProject)) {
|
|
|
|
return {};
|
2020-04-29 18:52:17 -04:00
|
|
|
}
|
2020-11-09 19:20:02 -05:00
|
|
|
const {options, errors} =
|
|
|
|
readConfiguration(project.getConfigFilePath(), /* existingOptions */ undefined, host);
|
|
|
|
if (errors.length > 0) {
|
2020-10-30 18:16:39 -04:00
|
|
|
project.setProjectErrors(errors);
|
2020-11-09 19:20:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
2020-04-29 18:08:22 -04:00
|
|
|
}
|
2020-07-31 00:26:02 -04:00
|
|
|
|
|
|
|
function createTypeCheckingProgramStrategy(project: ts.server.Project):
|
|
|
|
TypeCheckingProgramStrategy {
|
|
|
|
return {
|
|
|
|
supportsInlineOperations: false,
|
|
|
|
shimPathForComponent(component: ts.ClassDeclaration): AbsoluteFsPath {
|
|
|
|
return TypeCheckShimGenerator.shimFor(absoluteFromSourceFile(component.getSourceFile()));
|
|
|
|
},
|
|
|
|
getProgram(): ts.Program {
|
|
|
|
const program = project.getLanguageService().getProgram();
|
|
|
|
if (!program) {
|
|
|
|
throw new Error('Language service does not have a program!');
|
|
|
|
}
|
|
|
|
return program;
|
|
|
|
},
|
|
|
|
updateFiles(contents: Map<AbsoluteFsPath, string>) {
|
|
|
|
for (const [fileName, newText] of contents) {
|
|
|
|
const scriptInfo = getOrCreateTypeCheckScriptInfo(project, fileName);
|
|
|
|
const snapshot = scriptInfo.getSnapshot();
|
|
|
|
const length = snapshot.getLength();
|
|
|
|
scriptInfo.editContent(0, length, newText);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOrCreateTypeCheckScriptInfo(
|
|
|
|
project: ts.server.Project, tcf: string): ts.server.ScriptInfo {
|
|
|
|
// First check if there is already a ScriptInfo for the tcf
|
|
|
|
const {projectService} = project;
|
|
|
|
let scriptInfo = projectService.getScriptInfo(tcf);
|
|
|
|
if (!scriptInfo) {
|
|
|
|
// ScriptInfo needs to be opened by client to be able to set its user-defined
|
|
|
|
// content. We must also provide file content, otherwise the service will
|
|
|
|
// attempt to fetch the content from disk and fail.
|
|
|
|
scriptInfo = projectService.getOrCreateScriptInfoForNormalizedPath(
|
|
|
|
ts.server.toNormalizedPath(tcf),
|
|
|
|
true, // openedByClient
|
|
|
|
'', // fileContent
|
|
|
|
ts.ScriptKind.TS, // scriptKind
|
|
|
|
);
|
|
|
|
if (!scriptInfo) {
|
|
|
|
throw new Error(`Failed to create script info for ${tcf}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add ScriptInfo to project if it's missing. A ScriptInfo needs to be part of
|
|
|
|
// the project so that it becomes part of the program.
|
|
|
|
if (!project.containsScriptInfo(scriptInfo)) {
|
|
|
|
project.addRoot(scriptInfo);
|
|
|
|
}
|
|
|
|
return scriptInfo;
|
|
|
|
}
|
2020-11-18 20:30:52 -05:00
|
|
|
|
|
|
|
function nodeContextFromTarget(target: TargetNode): CompletionNodeContext {
|
|
|
|
switch (target.kind) {
|
|
|
|
case TargetNodeKind.ElementInTagContext:
|
|
|
|
return CompletionNodeContext.ElementTag;
|
|
|
|
case TargetNodeKind.ElementInBodyContext:
|
|
|
|
// Completions in element bodies are for new attributes.
|
|
|
|
return CompletionNodeContext.ElementAttributeKey;
|
2020-12-03 21:08:00 -05:00
|
|
|
case TargetNodeKind.AttributeInKeyContext:
|
|
|
|
return CompletionNodeContext.ElementAttributeKey;
|
|
|
|
case TargetNodeKind.AttributeInValueContext:
|
|
|
|
if (target.node instanceof TmplAstBoundEvent) {
|
|
|
|
return CompletionNodeContext.EventValue;
|
|
|
|
} else {
|
|
|
|
return CompletionNodeContext.None;
|
|
|
|
}
|
2020-11-18 20:30:52 -05:00
|
|
|
default:
|
|
|
|
// No special context is available.
|
|
|
|
return CompletionNodeContext.None;
|
|
|
|
}
|
|
|
|
}
|