2016-11-22 09:10:23 -08:00
|
|
|
/**
|
|
|
|
|
* @license
|
2020-05-19 12:08:49 -07:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-11-22 09:10:23 -08: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-01-23 11:34:27 -08:00
|
|
|
import {NgAnalyzedModules} from '@angular/compiler';
|
2019-08-01 13:07:32 -07:00
|
|
|
import * as ts from 'typescript';
|
2020-10-01 13:30:03 -07:00
|
|
|
|
|
|
|
|
import {createQuickInfo} from '../common/quick_info';
|
|
|
|
|
|
2020-01-14 18:54:40 -08:00
|
|
|
import {locateSymbols} from './locate_symbol';
|
2019-12-19 18:33:26 -08:00
|
|
|
import * as ng from './types';
|
2020-01-23 11:34:27 -08:00
|
|
|
import {inSpan} from './utils';
|
2019-12-19 18:33:26 -08:00
|
|
|
|
2019-08-21 14:36:00 -07:00
|
|
|
/**
|
|
|
|
|
* Traverse the template AST and look for the symbol located at `position`, then
|
|
|
|
|
* return the corresponding quick info.
|
|
|
|
|
* @param info template AST
|
|
|
|
|
* @param position location of the symbol
|
2020-01-23 11:34:27 -08:00
|
|
|
* @param analyzedModules all NgModules in the program.
|
2019-08-21 14:36:00 -07:00
|
|
|
*/
|
2020-01-23 11:34:27 -08:00
|
|
|
export function getTemplateHover(
|
2020-04-08 20:05:32 -07:00
|
|
|
info: ng.AstResult, position: number, analyzedModules: NgAnalyzedModules): ts.QuickInfo|
|
|
|
|
|
undefined {
|
2020-01-14 18:54:40 -08:00
|
|
|
const symbolInfo = locateSymbols(info, position)[0];
|
2019-08-01 13:07:32 -07:00
|
|
|
if (!symbolInfo) {
|
|
|
|
|
return;
|
2016-11-22 09:10:23 -08:00
|
|
|
}
|
2020-01-23 11:34:27 -08:00
|
|
|
const {symbol, span, staticSymbol} = symbolInfo;
|
2020-01-14 18:54:40 -08:00
|
|
|
|
2020-01-23 11:34:27 -08:00
|
|
|
// The container is either the symbol's container (for example, 'AppComponent'
|
|
|
|
|
// is the container of the symbol 'title' in its template) or the NgModule
|
|
|
|
|
// that the directive belongs to (the container of AppComponent is AppModule).
|
2020-04-03 20:57:39 -07:00
|
|
|
let containerName: string|undefined = symbol.container?.name;
|
2020-01-23 11:34:27 -08:00
|
|
|
if (!containerName && staticSymbol) {
|
|
|
|
|
// If there is a static symbol then the target is a directive.
|
|
|
|
|
const ngModule = analyzedModules.ngModuleByPipeOrDirective.get(staticSymbol);
|
2020-04-03 20:57:39 -07:00
|
|
|
containerName = ngModule?.type.reference.name;
|
2019-10-11 19:15:07 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-03 20:57:39 -07:00
|
|
|
return createQuickInfo(
|
|
|
|
|
symbol.name, symbol.kind, span, containerName, symbol.type?.name, symbol.documentation);
|
2019-08-01 13:07:32 -07:00
|
|
|
}
|
2019-09-16 21:07:43 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get quick info for Angular semantic entities in TypeScript files, like Directives.
|
|
|
|
|
* @param position location of the symbol in the source file
|
2020-01-23 11:34:27 -08:00
|
|
|
* @param declarations All Directive-like declarations in the source file.
|
|
|
|
|
* @param analyzedModules all NgModules in the program.
|
2019-09-16 21:07:43 -05:00
|
|
|
*/
|
|
|
|
|
export function getTsHover(
|
2020-01-23 11:34:27 -08:00
|
|
|
position: number, declarations: ng.Declaration[],
|
|
|
|
|
analyzedModules: NgAnalyzedModules): ts.QuickInfo|undefined {
|
|
|
|
|
for (const {declarationSpan, metadata} of declarations) {
|
|
|
|
|
if (inSpan(position, declarationSpan)) {
|
|
|
|
|
const staticSymbol: ng.StaticSymbol = metadata.type.reference;
|
|
|
|
|
const directiveName = staticSymbol.name;
|
|
|
|
|
const kind = metadata.isComponent ? 'component' : 'directive';
|
|
|
|
|
const textSpan = ts.createTextSpanFromBounds(declarationSpan.start, declarationSpan.end);
|
|
|
|
|
const ngModule = analyzedModules.ngModuleByPipeOrDirective.get(staticSymbol);
|
2020-04-03 20:57:39 -07:00
|
|
|
const moduleName = ngModule?.type.reference.name;
|
2020-01-23 11:34:27 -08:00
|
|
|
return createQuickInfo(
|
|
|
|
|
directiveName, kind, textSpan, moduleName, ts.ScriptElementKind.classElement);
|
|
|
|
|
}
|
2019-09-16 21:07:43 -05:00
|
|
|
}
|
|
|
|
|
}
|