2016-11-22 09:10:23 -08: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 13:07:32 -07:00
|
|
|
import * as ts from 'typescript';
|
2019-08-21 14:36:00 -07:00
|
|
|
import {AstResult} from './common';
|
2016-11-22 09:10:23 -08:00
|
|
|
import {locateSymbol} from './locate_symbol';
|
|
|
|
|
2019-08-01 13:07:32 -07:00
|
|
|
// Reverse mappings of enum would generate strings
|
|
|
|
const SYMBOL_SPACE = ts.SymbolDisplayPartKind[ts.SymbolDisplayPartKind.space];
|
|
|
|
const SYMBOL_PUNC = ts.SymbolDisplayPartKind[ts.SymbolDisplayPartKind.punctuation];
|
2016-11-22 09:10:23 -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
|
|
|
|
*/
|
|
|
|
export function getHover(info: AstResult, position: number): ts.QuickInfo|undefined {
|
|
|
|
const symbolInfo = locateSymbol(info, position);
|
2019-08-01 13:07:32 -07:00
|
|
|
if (!symbolInfo) {
|
|
|
|
return;
|
2016-11-22 09:10:23 -08:00
|
|
|
}
|
2019-08-01 13:07:32 -07:00
|
|
|
const {symbol, span} = symbolInfo;
|
|
|
|
const containerDisplayParts: ts.SymbolDisplayPart[] = symbol.container ?
|
|
|
|
[
|
|
|
|
{text: symbol.container.name, kind: symbol.container.kind},
|
|
|
|
{text: '.', kind: SYMBOL_PUNC},
|
|
|
|
] :
|
|
|
|
[];
|
|
|
|
return {
|
|
|
|
kind: symbol.kind as ts.ScriptElementKind,
|
|
|
|
kindModifiers: '', // kindModifier info not available on 'ng.Symbol'
|
|
|
|
textSpan: {
|
|
|
|
start: span.start,
|
|
|
|
length: span.end - span.start,
|
|
|
|
},
|
|
|
|
// this would generate a string like '(property) ClassX.propY'
|
|
|
|
// 'kind' in displayParts does not really matter because it's dropped when
|
|
|
|
// displayParts get converted to string.
|
|
|
|
displayParts: [
|
|
|
|
{text: '(', kind: SYMBOL_PUNC}, {text: symbol.kind, kind: symbol.kind},
|
|
|
|
{text: ')', kind: SYMBOL_PUNC}, {text: ' ', kind: SYMBOL_SPACE}, ...containerDisplayParts,
|
|
|
|
{text: symbol.name, kind: symbol.kind},
|
|
|
|
// TODO: Append type info as well, but Symbol doesn't expose that!
|
|
|
|
// Ideally hover text should be like '(property) ClassX.propY: string'
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|