feat(language-service): add command for getting components for a template file (#40655)
This commit adds a feature to the Angular Language Service that enables getting the locations for components that use a template file. Part of https://github.com/angular/vscode-ng-language-service/issues/1081 PR Close #40655
This commit is contained in:
parent
00070376ed
commit
5cde4ad591
|
@ -33,10 +33,13 @@ export type GetTcbResponse = {
|
||||||
selections: ts.TextSpan[],
|
selections: ts.TextSpan[],
|
||||||
}|undefined;
|
}|undefined;
|
||||||
|
|
||||||
|
export type GetComponentLocationsForTemplateResponse = ts.DocumentSpan[];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* `NgLanguageService` describes an instance of an Angular language service,
|
* `NgLanguageService` describes an instance of an Angular language service,
|
||||||
* whose API surface is a strict superset of TypeScript's language service.
|
* whose API surface is a strict superset of TypeScript's language service.
|
||||||
*/
|
*/
|
||||||
export interface NgLanguageService extends ts.LanguageService {
|
export interface NgLanguageService extends ts.LanguageService {
|
||||||
getTcb(fileName: string, position: number): GetTcbResponse;
|
getTcb(fileName: string, position: number): GetTcbResponse;
|
||||||
|
getComponentLocationsForTemplate(fileName: string): GetComponentLocationsForTemplateResponse;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,11 +11,13 @@ import {CompilerOptions, ConfigurationHost, readConfiguration} from '@angular/co
|
||||||
import {NgCompiler} from '@angular/compiler-cli/src/ngtsc/core';
|
import {NgCompiler} from '@angular/compiler-cli/src/ngtsc/core';
|
||||||
import {ErrorCode, ngErrorCode} from '@angular/compiler-cli/src/ngtsc/diagnostics';
|
import {ErrorCode, ngErrorCode} from '@angular/compiler-cli/src/ngtsc/diagnostics';
|
||||||
import {absoluteFrom, absoluteFromSourceFile, AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system';
|
import {absoluteFrom, absoluteFromSourceFile, AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system';
|
||||||
|
import {isNamedClassDeclaration} from '@angular/compiler-cli/src/ngtsc/reflection';
|
||||||
import {TypeCheckShimGenerator} from '@angular/compiler-cli/src/ngtsc/typecheck';
|
import {TypeCheckShimGenerator} from '@angular/compiler-cli/src/ngtsc/typecheck';
|
||||||
import {OptimizeFor, TypeCheckingProgramStrategy} from '@angular/compiler-cli/src/ngtsc/typecheck/api';
|
import {OptimizeFor, TypeCheckingProgramStrategy} from '@angular/compiler-cli/src/ngtsc/typecheck/api';
|
||||||
import {findFirstMatchingNode} from '@angular/compiler-cli/src/ngtsc/typecheck/src/comments';
|
import {findFirstMatchingNode} from '@angular/compiler-cli/src/ngtsc/typecheck/src/comments';
|
||||||
import * as ts from 'typescript/lib/tsserverlibrary';
|
import * as ts from 'typescript/lib/tsserverlibrary';
|
||||||
import {GetTcbResponse} from '../api';
|
|
||||||
|
import {GetComponentLocationsForTemplateResponse, GetTcbResponse} from '../api';
|
||||||
|
|
||||||
import {LanguageServiceAdapter, LSParseConfigHost} from './adapters';
|
import {LanguageServiceAdapter, LSParseConfigHost} from './adapters';
|
||||||
import {CompilerFactory} from './compiler_factory';
|
import {CompilerFactory} from './compiler_factory';
|
||||||
|
@ -200,6 +202,29 @@ export class LanguageService {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getComponentLocationsForTemplate(fileName: string): GetComponentLocationsForTemplateResponse {
|
||||||
|
return this.withCompiler<GetComponentLocationsForTemplateResponse>((compiler) => {
|
||||||
|
const components = compiler.getComponentsWithTemplateFile(fileName);
|
||||||
|
const componentDeclarationLocations: ts.DocumentSpan[] =
|
||||||
|
Array.from(components.values()).map(c => {
|
||||||
|
let contextSpan: ts.TextSpan|undefined = undefined;
|
||||||
|
let textSpan: ts.TextSpan;
|
||||||
|
if (isNamedClassDeclaration(c)) {
|
||||||
|
textSpan = ts.createTextSpanFromBounds(c.name.getStart(), c.name.getEnd());
|
||||||
|
contextSpan = ts.createTextSpanFromBounds(c.getStart(), c.getEnd());
|
||||||
|
} else {
|
||||||
|
textSpan = ts.createTextSpanFromBounds(c.getStart(), c.getEnd());
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
fileName: c.getSourceFile().fileName,
|
||||||
|
textSpan,
|
||||||
|
contextSpan,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
return componentDeclarationLocations;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
getTcb(fileName: string, position: number): GetTcbResponse {
|
getTcb(fileName: string, position: number): GetTcbResponse {
|
||||||
return this.withCompiler<GetTcbResponse>(compiler => {
|
return this.withCompiler<GetTcbResponse>(compiler => {
|
||||||
const templateInfo = getTemplateInfoAtPosition(fileName, position, compiler);
|
const templateInfo = getTemplateInfoAtPosition(fileName, position, compiler);
|
||||||
|
|
|
@ -7,7 +7,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as ts from 'typescript/lib/tsserverlibrary';
|
import * as ts from 'typescript/lib/tsserverlibrary';
|
||||||
import {GetTcbResponse, NgLanguageService} from '../api';
|
|
||||||
|
import {GetComponentLocationsForTemplateResponse, GetTcbResponse, NgLanguageService} from '../api';
|
||||||
|
|
||||||
import {LanguageService} from './language_service';
|
import {LanguageService} from './language_service';
|
||||||
|
|
||||||
export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
|
export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
|
||||||
|
@ -132,6 +134,14 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
|
||||||
return ngLS.getTcb(fileName, position);
|
return ngLS.getTcb(fileName, position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given an external template, finds the associated components that use it as a `templateUrl`.
|
||||||
|
*/
|
||||||
|
function getComponentLocationsForTemplate(fileName: string):
|
||||||
|
GetComponentLocationsForTemplateResponse {
|
||||||
|
return ngLS.getComponentLocationsForTemplate(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...tsLS,
|
...tsLS,
|
||||||
getSemanticDiagnostics,
|
getSemanticDiagnostics,
|
||||||
|
@ -146,6 +156,7 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
|
||||||
getCompletionEntrySymbol,
|
getCompletionEntrySymbol,
|
||||||
getTcb,
|
getTcb,
|
||||||
getCompilerOptionsDiagnostics,
|
getCompilerOptionsDiagnostics,
|
||||||
|
getComponentLocationsForTemplate,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -141,6 +141,11 @@ export function create(info: tss.server.PluginCreateInfo): NgLanguageService {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getComponentLocationsForTemplate(fileName: string) {
|
||||||
|
// Not implemented in VE Language Service
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
// First clone the original TS language service
|
// First clone the original TS language service
|
||||||
...tsLS,
|
...tsLS,
|
||||||
|
@ -154,5 +159,6 @@ export function create(info: tss.server.PluginCreateInfo): NgLanguageService {
|
||||||
getReferencesAtPosition,
|
getReferencesAtPosition,
|
||||||
findRenameLocations,
|
findRenameLocations,
|
||||||
getTcb,
|
getTcb,
|
||||||
|
getComponentLocationsForTemplate,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue