2016-11-22 12:10:23 -05: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
|
|
|
|
*/
|
|
|
|
|
2017-05-09 19:16:50 -04:00
|
|
|
import {AST, ASTWithSource, AstPath as AstPathBase, NullAstVisitor, visitAstChildren} from '@angular/compiler';
|
2017-08-15 20:04:45 -04:00
|
|
|
import {AstType} from '@angular/compiler-cli/src/language_services';
|
2016-11-22 12:10:23 -05:00
|
|
|
|
2017-05-09 19:16:50 -04:00
|
|
|
import {BuiltinType, Span, Symbol, SymbolQuery, SymbolTable} from './types';
|
|
|
|
import {inSpan} from './utils';
|
2016-11-22 12:10:23 -05:00
|
|
|
|
2017-05-09 19:16:50 -04:00
|
|
|
type AstPath = AstPathBase<AST>;
|
2016-12-13 14:20:45 -05:00
|
|
|
|
2017-05-09 19:16:50 -04:00
|
|
|
function findAstAt(ast: AST, position: number, excludeEmpty: boolean = false): AstPath {
|
|
|
|
const path: AST[] = [];
|
|
|
|
const visitor = new class extends NullAstVisitor {
|
|
|
|
visit(ast: AST) {
|
|
|
|
if ((!excludeEmpty || ast.span.start < ast.span.end) && inSpan(position, ast.span)) {
|
|
|
|
path.push(ast);
|
|
|
|
visitAstChildren(ast, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// We never care about the ASTWithSource node and its visit() method calls its ast's visit so
|
|
|
|
// the visit() method above would never see it.
|
|
|
|
if (ast instanceof ASTWithSource) {
|
|
|
|
ast = ast.ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
visitor.visit(ast);
|
|
|
|
|
|
|
|
return new AstPathBase<AST>(path, position);
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getExpressionCompletions(
|
2017-03-24 12:57:32 -04:00
|
|
|
scope: SymbolTable, ast: AST, position: number, query: SymbolQuery): Symbol[]|undefined {
|
2017-05-09 19:16:50 -04:00
|
|
|
const path = findAstAt(ast, position);
|
2016-11-22 12:10:23 -05:00
|
|
|
if (path.empty) return undefined;
|
2017-03-24 12:57:32 -04:00
|
|
|
const tail = path.tail !;
|
2016-11-22 12:10:23 -05:00
|
|
|
let result: SymbolTable|undefined = scope;
|
|
|
|
|
2016-12-13 14:20:45 -05:00
|
|
|
function getType(ast: AST): Symbol { return new AstType(scope, query, {}).getType(ast); }
|
2016-11-22 12:10:23 -05:00
|
|
|
|
|
|
|
// If the completion request is in a not in a pipe or property access then the global scope
|
|
|
|
// (that is the scope of the implicit receiver) is the right scope as the user is typing the
|
|
|
|
// beginning of an expression.
|
|
|
|
tail.visit({
|
|
|
|
visitBinary(ast) {},
|
|
|
|
visitChain(ast) {},
|
|
|
|
visitConditional(ast) {},
|
|
|
|
visitFunctionCall(ast) {},
|
|
|
|
visitImplicitReceiver(ast) {},
|
|
|
|
visitInterpolation(ast) { result = undefined; },
|
|
|
|
visitKeyedRead(ast) {},
|
|
|
|
visitKeyedWrite(ast) {},
|
|
|
|
visitLiteralArray(ast) {},
|
|
|
|
visitLiteralMap(ast) {},
|
|
|
|
visitLiteralPrimitive(ast) {},
|
|
|
|
visitMethodCall(ast) {},
|
|
|
|
visitPipe(ast) {
|
|
|
|
if (position >= ast.exp.span.end &&
|
|
|
|
(!ast.args || !ast.args.length || position < (<AST>ast.args[0]).span.start)) {
|
|
|
|
// We are in a position a pipe name is expected.
|
|
|
|
result = query.getPipes();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
visitPrefixNot(ast) {},
|
2017-05-11 13:15:54 -04:00
|
|
|
visitNonNullAssert(ast) {},
|
2016-11-22 12:10:23 -05:00
|
|
|
visitPropertyRead(ast) {
|
|
|
|
const receiverType = getType(ast.receiver);
|
|
|
|
result = receiverType ? receiverType.members() : scope;
|
|
|
|
},
|
|
|
|
visitPropertyWrite(ast) {
|
|
|
|
const receiverType = getType(ast.receiver);
|
|
|
|
result = receiverType ? receiverType.members() : scope;
|
|
|
|
},
|
|
|
|
visitQuote(ast) {
|
|
|
|
// For a quote, return the members of any (if there are any).
|
|
|
|
result = query.getBuiltinType(BuiltinType.Any).members();
|
|
|
|
},
|
|
|
|
visitSafeMethodCall(ast) {
|
|
|
|
const receiverType = getType(ast.receiver);
|
|
|
|
result = receiverType ? receiverType.members() : scope;
|
|
|
|
},
|
|
|
|
visitSafePropertyRead(ast) {
|
|
|
|
const receiverType = getType(ast.receiver);
|
|
|
|
result = receiverType ? receiverType.members() : scope;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return result && result.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getExpressionSymbol(
|
|
|
|
scope: SymbolTable, ast: AST, position: number,
|
2017-03-24 12:57:32 -04:00
|
|
|
query: SymbolQuery): {symbol: Symbol, span: Span}|undefined {
|
2017-05-09 19:16:50 -04:00
|
|
|
const path = findAstAt(ast, position, /* excludeEmpty */ true);
|
2016-11-22 12:10:23 -05:00
|
|
|
if (path.empty) return undefined;
|
2017-03-24 12:57:32 -04:00
|
|
|
const tail = path.tail !;
|
2016-11-22 12:10:23 -05:00
|
|
|
|
2016-12-13 14:20:45 -05:00
|
|
|
function getType(ast: AST): Symbol { return new AstType(scope, query, {}).getType(ast); }
|
2016-11-22 12:10:23 -05:00
|
|
|
|
2017-03-24 12:57:32 -04:00
|
|
|
let symbol: Symbol|undefined = undefined;
|
|
|
|
let span: Span|undefined = undefined;
|
2016-11-22 12:10:23 -05:00
|
|
|
|
|
|
|
// If the completion request is in a not in a pipe or property access then the global scope
|
|
|
|
// (that is the scope of the implicit receiver) is the right scope as the user is typing the
|
|
|
|
// beginning of an expression.
|
|
|
|
tail.visit({
|
|
|
|
visitBinary(ast) {},
|
|
|
|
visitChain(ast) {},
|
|
|
|
visitConditional(ast) {},
|
|
|
|
visitFunctionCall(ast) {},
|
|
|
|
visitImplicitReceiver(ast) {},
|
|
|
|
visitInterpolation(ast) {},
|
|
|
|
visitKeyedRead(ast) {},
|
|
|
|
visitKeyedWrite(ast) {},
|
|
|
|
visitLiteralArray(ast) {},
|
|
|
|
visitLiteralMap(ast) {},
|
|
|
|
visitLiteralPrimitive(ast) {},
|
|
|
|
visitMethodCall(ast) {
|
|
|
|
const receiverType = getType(ast.receiver);
|
|
|
|
symbol = receiverType && receiverType.members().get(ast.name);
|
|
|
|
span = ast.span;
|
|
|
|
},
|
|
|
|
visitPipe(ast) {
|
|
|
|
if (position >= ast.exp.span.end &&
|
|
|
|
(!ast.args || !ast.args.length || position < (<AST>ast.args[0]).span.start)) {
|
|
|
|
// We are in a position a pipe name is expected.
|
|
|
|
const pipes = query.getPipes();
|
|
|
|
if (pipes) {
|
|
|
|
symbol = pipes.get(ast.name);
|
|
|
|
span = ast.span;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
visitPrefixNot(ast) {},
|
2017-05-11 13:15:54 -04:00
|
|
|
visitNonNullAssert(ast) {},
|
2016-11-22 12:10:23 -05:00
|
|
|
visitPropertyRead(ast) {
|
|
|
|
const receiverType = getType(ast.receiver);
|
|
|
|
symbol = receiverType && receiverType.members().get(ast.name);
|
|
|
|
span = ast.span;
|
|
|
|
},
|
|
|
|
visitPropertyWrite(ast) {
|
|
|
|
const receiverType = getType(ast.receiver);
|
|
|
|
symbol = receiverType && receiverType.members().get(ast.name);
|
|
|
|
span = ast.span;
|
|
|
|
},
|
|
|
|
visitQuote(ast) {},
|
|
|
|
visitSafeMethodCall(ast) {
|
|
|
|
const receiverType = getType(ast.receiver);
|
|
|
|
symbol = receiverType && receiverType.members().get(ast.name);
|
|
|
|
span = ast.span;
|
|
|
|
},
|
|
|
|
visitSafePropertyRead(ast) {
|
|
|
|
const receiverType = getType(ast.receiver);
|
|
|
|
symbol = receiverType && receiverType.members().get(ast.name);
|
|
|
|
span = ast.span;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (symbol && span) {
|
|
|
|
return {symbol, span};
|
|
|
|
}
|
|
|
|
}
|