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
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as ts from 'typescript/lib/tsserverlibrary';
|
2021-01-28 13:31:04 -05:00
|
|
|
|
|
|
|
import {GetComponentLocationsForTemplateResponse, GetTcbResponse, NgLanguageService} from '../api';
|
|
|
|
|
2021-01-28 15:43:37 -05:00
|
|
|
import {LanguageService} from './language_service';
|
2020-12-04 11:33:27 -05:00
|
|
|
|
|
|
|
export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
|
2020-04-29 18:52:17 -04:00
|
|
|
const {project, languageService: tsLS, config} = info;
|
2020-04-29 18:08:22 -04:00
|
|
|
const angularOnly = config?.angularOnly === true;
|
|
|
|
|
2020-04-29 18:52:17 -04:00
|
|
|
const ngLS = new LanguageService(project, tsLS);
|
2020-04-29 18:08:22 -04:00
|
|
|
|
|
|
|
function getSemanticDiagnostics(fileName: string): ts.Diagnostic[] {
|
|
|
|
const diagnostics: ts.Diagnostic[] = [];
|
|
|
|
if (!angularOnly) {
|
|
|
|
diagnostics.push(...tsLS.getSemanticDiagnostics(fileName));
|
|
|
|
}
|
|
|
|
diagnostics.push(...ngLS.getSemanticDiagnostics(fileName));
|
|
|
|
return diagnostics;
|
|
|
|
}
|
|
|
|
|
2020-09-28 14:26:07 -04:00
|
|
|
function getQuickInfoAtPosition(fileName: string, position: number): ts.QuickInfo|undefined {
|
|
|
|
if (angularOnly) {
|
|
|
|
return ngLS.getQuickInfoAtPosition(fileName, position);
|
|
|
|
} else {
|
|
|
|
// If TS could answer the query, then return that result. Otherwise, return from Angular LS.
|
|
|
|
return tsLS.getQuickInfoAtPosition(fileName, position) ??
|
|
|
|
ngLS.getQuickInfoAtPosition(fileName, position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 16:54:18 -04:00
|
|
|
function getTypeDefinitionAtPosition(
|
|
|
|
fileName: string, position: number): readonly ts.DefinitionInfo[]|undefined {
|
|
|
|
if (angularOnly) {
|
|
|
|
return ngLS.getTypeDefinitionAtPosition(fileName, position);
|
|
|
|
} else {
|
|
|
|
// If TS could answer the query, then return that result. Otherwise, return from Angular LS.
|
|
|
|
return tsLS.getTypeDefinitionAtPosition(fileName, position) ??
|
|
|
|
ngLS.getTypeDefinitionAtPosition(fileName, position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:47:36 -04:00
|
|
|
function getDefinitionAndBoundSpan(
|
|
|
|
fileName: string, position: number): ts.DefinitionInfoAndBoundSpan|undefined {
|
|
|
|
if (angularOnly) {
|
|
|
|
return ngLS.getDefinitionAndBoundSpan(fileName, position);
|
|
|
|
} else {
|
|
|
|
// If TS could answer the query, then return that result. Otherwise, return from Angular LS.
|
|
|
|
return tsLS.getDefinitionAndBoundSpan(fileName, position) ??
|
|
|
|
ngLS.getDefinitionAndBoundSpan(fileName, position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 16:31:34 -05:00
|
|
|
function getReferencesAtPosition(fileName: string, position: number): ts.ReferenceEntry[]|
|
|
|
|
undefined {
|
|
|
|
return ngLS.getReferencesAtPosition(fileName, position);
|
2020-11-24 14:57:34 -05:00
|
|
|
}
|
|
|
|
|
2020-12-01 15:45:02 -05:00
|
|
|
function findRenameLocations(
|
|
|
|
fileName: string, position: number, findInStrings: boolean, findInComments: boolean,
|
|
|
|
providePrefixAndSuffixTextForRename?: boolean): readonly ts.RenameLocation[]|undefined {
|
feat(language-service): initial implementation for `findRenameLocations` (#40140)
This commit lays the groundwork for potentially providing rename
locations from the Ivy native LS. The approach is very similar to what
was done with the feature to find references. One difference, however,
is that we did not require the references to be fully "correct". That
is, the exact text spans did not matter so much, as long as we provide a
location that logically includes the referenced item.
An example of a necessary difference between rename locations and references is
directives. The entire element in the template is a "reference" of the
directive's class. However, it's not a valid location to be renamed. The
same goes for aliased inputs/outputs. The locations in the template
directly map to the class property, which is correct for references, but
would not be correct for rename locations, which should instead map to
the string node fo the alias.
As an initial approach to address the aforementioned issues with rename
locations, we check that all the rename location nodes have the same text. If
_any_ node has text that differs from the request, we do not return any
rename locations. This works as a way to prevent renames that could
break the the program by missing some required nodes in the rename action, but
allowing other nodes to be renamed.
PR Close #40140
2020-11-30 14:16:48 -05:00
|
|
|
// Most operations combine results from all extensions. However, rename locations are exclusive
|
|
|
|
// (results from only one extension are used) so our rename locations are a superset of the TS
|
|
|
|
// rename locations. As a result, we do not check the `angularOnly` flag here because we always
|
|
|
|
// want to include results at TS locations as well as locations in templates.
|
|
|
|
return ngLS.findRenameLocations(fileName, position);
|
2020-12-01 15:45:02 -05:00
|
|
|
}
|
|
|
|
|
2020-12-01 17:55:57 -05:00
|
|
|
function getRenameInfo(fileName: string, position: number): ts.RenameInfo {
|
|
|
|
// See the comment in `findRenameLocations` explaining why we don't check the `angularOnly`
|
|
|
|
// flag.
|
|
|
|
return ngLS.getRenameInfo(fileName, position);
|
|
|
|
}
|
|
|
|
|
2020-10-13 14:14:13 -04:00
|
|
|
function getCompletionsAtPosition(
|
|
|
|
fileName: string, position: number,
|
|
|
|
options: ts.GetCompletionsAtPositionOptions): ts.WithMetadata<ts.CompletionInfo>|undefined {
|
|
|
|
if (angularOnly) {
|
|
|
|
return ngLS.getCompletionsAtPosition(fileName, position, options);
|
|
|
|
} else {
|
|
|
|
// If TS could answer the query, then return that result. Otherwise, return from Angular LS.
|
|
|
|
return tsLS.getCompletionsAtPosition(fileName, position, options) ??
|
|
|
|
ngLS.getCompletionsAtPosition(fileName, position, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCompletionEntryDetails(
|
|
|
|
fileName: string, position: number, entryName: string,
|
|
|
|
formatOptions: ts.FormatCodeOptions|ts.FormatCodeSettings|undefined, source: string|undefined,
|
|
|
|
preferences: ts.UserPreferences|undefined): ts.CompletionEntryDetails|undefined {
|
|
|
|
if (angularOnly) {
|
|
|
|
return ngLS.getCompletionEntryDetails(
|
|
|
|
fileName, position, entryName, formatOptions, preferences);
|
|
|
|
} else {
|
|
|
|
// If TS could answer the query, then return that result. Otherwise, return from Angular LS.
|
|
|
|
return tsLS.getCompletionEntryDetails(
|
|
|
|
fileName, position, entryName, formatOptions, source, preferences) ??
|
|
|
|
ngLS.getCompletionEntryDetails(fileName, position, entryName, formatOptions, preferences);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCompletionEntrySymbol(
|
|
|
|
fileName: string, position: number, name: string, source: string|undefined): ts.Symbol|
|
|
|
|
undefined {
|
|
|
|
if (angularOnly) {
|
|
|
|
return ngLS.getCompletionEntrySymbol(fileName, position, name);
|
|
|
|
} else {
|
|
|
|
// If TS could answer the query, then return that result. Otherwise, return from Angular LS.
|
|
|
|
return tsLS.getCompletionEntrySymbol(fileName, position, name, source) ??
|
|
|
|
ngLS.getCompletionEntrySymbol(fileName, position, name);
|
|
|
|
}
|
|
|
|
}
|
2021-01-13 17:52:29 -05:00
|
|
|
/**
|
|
|
|
* Gets global diagnostics related to the program configuration and compiler options.
|
|
|
|
*/
|
|
|
|
function getCompilerOptionsDiagnostics(): ts.Diagnostic[] {
|
|
|
|
const diagnostics: ts.Diagnostic[] = [];
|
|
|
|
if (!angularOnly) {
|
|
|
|
diagnostics.push(...tsLS.getCompilerOptionsDiagnostics());
|
|
|
|
}
|
|
|
|
diagnostics.push(...ngLS.getCompilerOptionsDiagnostics());
|
|
|
|
return diagnostics;
|
|
|
|
}
|
2020-10-13 14:14:13 -04:00
|
|
|
|
2021-02-22 17:14:31 -05:00
|
|
|
function getTcb(fileName: string, position: number): GetTcbResponse|undefined {
|
2020-12-04 11:33:27 -05:00
|
|
|
return ngLS.getTcb(fileName, position);
|
|
|
|
}
|
|
|
|
|
2021-01-28 13:31:04 -05:00
|
|
|
/**
|
|
|
|
* Given an external template, finds the associated components that use it as a `templateUrl`.
|
|
|
|
*/
|
|
|
|
function getComponentLocationsForTemplate(fileName: string):
|
|
|
|
GetComponentLocationsForTemplateResponse {
|
|
|
|
return ngLS.getComponentLocationsForTemplate(fileName);
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:08:22 -04:00
|
|
|
return {
|
|
|
|
...tsLS,
|
|
|
|
getSemanticDiagnostics,
|
2020-09-29 17:25:28 -04:00
|
|
|
getTypeDefinitionAtPosition,
|
2020-09-28 14:26:07 -04:00
|
|
|
getQuickInfoAtPosition,
|
2020-09-30 11:47:36 -04:00
|
|
|
getDefinitionAndBoundSpan,
|
2020-11-24 14:57:34 -05:00
|
|
|
getReferencesAtPosition,
|
2020-12-01 15:45:02 -05:00
|
|
|
findRenameLocations,
|
2020-12-01 17:55:57 -05:00
|
|
|
getRenameInfo,
|
2020-10-13 14:14:13 -04:00
|
|
|
getCompletionsAtPosition,
|
|
|
|
getCompletionEntryDetails,
|
|
|
|
getCompletionEntrySymbol,
|
2020-12-04 11:33:27 -05:00
|
|
|
getTcb,
|
2021-01-13 17:52:29 -05:00
|
|
|
getCompilerOptionsDiagnostics,
|
2021-01-28 13:31:04 -05:00
|
|
|
getComponentLocationsForTemplate,
|
2020-04-29 18:08:22 -04:00
|
|
|
};
|
|
|
|
}
|
2020-12-16 14:15:14 -05:00
|
|
|
|
|
|
|
export function getExternalFiles(project: ts.server.Project): string[] {
|
|
|
|
if (!project.hasRoots()) {
|
|
|
|
return []; // project has not been initialized
|
|
|
|
}
|
|
|
|
const typecheckFiles: string[] = [];
|
|
|
|
for (const scriptInfo of project.getScriptInfos()) {
|
|
|
|
if (scriptInfo.scriptKind === ts.ScriptKind.External) {
|
|
|
|
// script info for typecheck file is marked as external, see
|
|
|
|
// getOrCreateTypeCheckScriptInfo() in
|
|
|
|
// packages/language-service/ivy/language_service.ts
|
|
|
|
typecheckFiles.push(scriptInfo.fileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return typecheckFiles;
|
|
|
|
}
|