refactor(language-service): find expression ASTs using absolute spans (#33387)

Moves to using the absolute span of an expression AST (relative to an
entire template) rather than a relative span (relative to the start
of the expression) to find an expression AST given a position in a
template.

This is part of the changes needed to support text replacement in
templates (#33091).

PR Close #33387
This commit is contained in:
ayazhafiz 2019-10-24 19:07:04 -04:00 committed by Andrew Kushnir
parent 3f195fefa9
commit ee4fc12e42
3 changed files with 9 additions and 10 deletions

View File

@ -370,7 +370,7 @@ class ExpressionVisitor extends NullTemplateVisitor {
new PropertyRead( new PropertyRead(
span, span.toAbsolute(offset), span, span.toAbsolute(offset),
new ImplicitReceiver(span, span.toAbsolute(offset)), ''), new ImplicitReceiver(span, span.toAbsolute(offset)), ''),
valueRelativePosition); this.position);
} else { } else {
keyCompletions(); keyCompletions();
} }
@ -379,10 +379,9 @@ class ExpressionVisitor extends NullTemplateVisitor {
} }
visitBoundText(ast: BoundTextAst) { visitBoundText(ast: BoundTextAst) {
const expressionPosition = this.position - ast.sourceSpan.start.offset; if (inSpan(this.position, ast.value.sourceSpan)) {
if (inSpan(expressionPosition, ast.value.span)) {
const completions = getExpressionCompletions( const completions = getExpressionCompletions(
this.getExpressionScope(), ast.value, expressionPosition, this.info.template.query); this.getExpressionScope(), ast.value, this.position, this.info.template.query);
if (completions) { if (completions) {
this.result = this.symbolsToCompletions(completions); this.result = this.symbolsToCompletions(completions);
} }
@ -410,7 +409,7 @@ class ExpressionVisitor extends NullTemplateVisitor {
private get attributeValuePosition() { private get attributeValuePosition() {
if (this.attr && this.attr.valueSpan) { if (this.attr && this.attr.valueSpan) {
return this.position - this.attr.valueSpan.start.offset; return this.position;
} }
return 0; return 0;
} }

View File

@ -18,7 +18,8 @@ function findAstAt(ast: AST, position: number, excludeEmpty: boolean = false): A
const path: AST[] = []; const path: AST[] = [];
const visitor = new class extends NullAstVisitor { const visitor = new class extends NullAstVisitor {
visit(ast: AST) { visit(ast: AST) {
if ((!excludeEmpty || ast.span.start < ast.span.end) && inSpan(position, ast.span)) { if ((!excludeEmpty || ast.sourceSpan.start < ast.sourceSpan.end) &&
inSpan(position, ast.sourceSpan)) {
path.push(ast); path.push(ast);
visitAstChildren(ast, this); visitAstChildren(ast, this);
} }

View File

@ -39,11 +39,10 @@ export function locateSymbol(info: AstResult, position: number): SymbolInfo|unde
const dinfo = diagnosticInfoFromTemplateInfo(info); const dinfo = diagnosticInfoFromTemplateInfo(info);
const scope = getExpressionScope(dinfo, path, inEvent); const scope = getExpressionScope(dinfo, path, inEvent);
if (attribute.valueSpan) { if (attribute.valueSpan) {
const expressionOffset = attribute.valueSpan.start.offset; const result = getExpressionSymbol(scope, ast, templatePosition, info.template.query);
const result = getExpressionSymbol(
scope, ast, templatePosition - expressionOffset, info.template.query);
if (result) { if (result) {
symbol = result.symbol; symbol = result.symbol;
const expressionOffset = attribute.valueSpan.start.offset;
span = offsetSpan(result.span, expressionOffset); span = offsetSpan(result.span, expressionOffset);
} }
} }
@ -116,7 +115,7 @@ export function locateSymbol(info: AstResult, position: number): SymbolInfo|unde
const dinfo = diagnosticInfoFromTemplateInfo(info); const dinfo = diagnosticInfoFromTemplateInfo(info);
const scope = getExpressionScope(dinfo, path, /* includeEvent */ false); const scope = getExpressionScope(dinfo, path, /* includeEvent */ false);
const result = const result =
getExpressionSymbol(scope, ast.value, expressionPosition, info.template.query); getExpressionSymbol(scope, ast.value, templatePosition, info.template.query);
if (result) { if (result) {
symbol = result.symbol; symbol = result.symbol;
span = offsetSpan(result.span, ast.sourceSpan.start.offset); span = offsetSpan(result.span, ast.sourceSpan.start.offset);