Skip to content

Commit

Permalink
fixup! feat(language-service): add support for text replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
ayazhafiz committed Oct 11, 2019
1 parent 6cbffec commit 0523e56
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/language-service/src/completions.ts
Expand Up @@ -32,11 +32,14 @@ function getClosestTextStartOffset(templateInfo: AstResult, position: number): n
const {htmlAst, templateAst, template} = templateInfo;
const templatePosition = position - template.span.start;
const templatePath = findTemplateAstAt(templateInfo.templateAst, templatePosition);
// Closest HTML template AST node to the queried template position, like an element or attribute.
const templateAstTail = templatePath.tail;
if (!templateAstTail) {
return 0;
}

// If the closest HTML template node has a Angular template syntax AST inside it, extract it.
// Otherwise, get the text of the HTML template node, which is the closest starting text offset.
const ast: AST|string = templateAstTail.visit(
{
visitNgContent: () => '',
Expand All @@ -53,15 +56,17 @@ function getClosestTextStartOffset(templateInfo: AstResult, position: number): n
visitDirectiveProperty: ast => ast.value,
},
null);

if (!(ast instanceof AST)) {
return ast.length;
return ast.length; // offset of HTML template node text
}

// Find the Angular template syntax AST closest to queried template position.
const closestAst = findAstAt(ast, templatePosition - templateAstTail.sourceSpan.start.offset);
const closestTail = closestAst.tail;
if (!closestTail) return 0;

// Return the closest starting text offset in the template syntax AST, which is either the value
// of the AST or nothing at all.
return closestTail.visit({
visitBinary: ast => 0,
visitChain: ast => 0,
Expand Down Expand Up @@ -152,6 +157,8 @@ export function getTemplateCompletions(
null);
}

// Define the span of the partial word the completion query was called on, which will be replaced
// by a selected completion.
const offset = getClosestTextStartOffset(templateInfo, position);
return result.map(entry => {
return {
Expand Down

0 comments on commit 0523e56

Please sign in to comment.