Skip to content

Commit

Permalink
refactor(language-service): find expression ASTs using absolute spans
Browse files Browse the repository at this point in the history
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 (angular#33091).
  • Loading branch information
ayazhafiz committed Oct 24, 2019
1 parent 4aa51b7 commit 51ca7ef
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
9 changes: 4 additions & 5 deletions packages/language-service/src/completions.ts
Expand Up @@ -370,7 +370,7 @@ class ExpressionVisitor extends NullTemplateVisitor {
new PropertyRead(
span, span.toAbsolute(offset),
new ImplicitReceiver(span, span.toAbsolute(offset)), ''),
valueRelativePosition);
this.position);
} else {
keyCompletions();
}
Expand All @@ -379,10 +379,9 @@ class ExpressionVisitor extends NullTemplateVisitor {
}

visitBoundText(ast: BoundTextAst) {
const expressionPosition = this.position - ast.sourceSpan.start.offset;
if (inSpan(expressionPosition, ast.value.span)) {
if (inSpan(this.position, ast.value.sourceSpan)) {
const completions = getExpressionCompletions(
this.getExpressionScope(), ast.value, expressionPosition, this.info.template.query);
this.getExpressionScope(), ast.value, this.position, this.info.template.query);
if (completions) {
this.result = this.symbolsToCompletions(completions);
}
Expand Down Expand Up @@ -410,7 +409,7 @@ class ExpressionVisitor extends NullTemplateVisitor {

private get attributeValuePosition() {
if (this.attr && this.attr.valueSpan) {
return this.position - this.attr.valueSpan.start.offset;
return this.position;
}
return 0;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/language-service/src/expressions.ts
Expand Up @@ -18,7 +18,8 @@ function findAstAt(ast: AST, position: number, excludeEmpty: boolean = false): A
const path: AST[] = [];
const visitor = new class extends NullAstVisitor {
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);
visitAstChildren(ast, this);
}
Expand Down
7 changes: 3 additions & 4 deletions packages/language-service/src/locate_symbol.ts
Expand Up @@ -39,11 +39,10 @@ export function locateSymbol(info: AstResult, position: number): SymbolInfo|unde
const dinfo = diagnosticInfoFromTemplateInfo(info);
const scope = getExpressionScope(dinfo, path, inEvent);
if (attribute.valueSpan) {
const expressionOffset = attribute.valueSpan.start.offset;
const result = getExpressionSymbol(
scope, ast, templatePosition - expressionOffset, info.template.query);
const result = getExpressionSymbol(scope, ast, templatePosition, info.template.query);
if (result) {
symbol = result.symbol;
const expressionOffset = attribute.valueSpan.start.offset;
span = offsetSpan(result.span, expressionOffset);
}
}
Expand Down Expand Up @@ -116,7 +115,7 @@ export function locateSymbol(info: AstResult, position: number): SymbolInfo|unde
const dinfo = diagnosticInfoFromTemplateInfo(info);
const scope = getExpressionScope(dinfo, path, /* includeEvent */ false);
const result =
getExpressionSymbol(scope, ast.value, expressionPosition, info.template.query);
getExpressionSymbol(scope, ast.value, templatePosition, info.template.query);
if (result) {
symbol = result.symbol;
span = offsetSpan(result.span, ast.sourceSpan.start.offset);
Expand Down

0 comments on commit 51ca7ef

Please sign in to comment.