Skip to content

Commit ffa4e11

Browse files
ivanwonderkara
authored andcommitted
fix(language-service): use the HtmlAst to get the span of HTML tag (#36371)
The HTML tag may include `-` (e.g. `app-root`), so use the `HtmlAst` to get the span. PR Close #36371
1 parent 41a41e7 commit ffa4e11

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

packages/language-service/src/completions.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,22 @@ function isIdentifierPart(code: number) {
5555
* Gets the span of word in a template that surrounds `position`. If there is no word around
5656
* `position`, nothing is returned.
5757
*/
58-
function getBoundedWordSpan(templateInfo: AstResult, position: number): ts.TextSpan|undefined {
58+
function getBoundedWordSpan(
59+
templateInfo: AstResult, position: number, ast: HtmlAst|undefined): ts.TextSpan|undefined {
5960
const {template} = templateInfo;
6061
const templateSrc = template.source;
6162

6263
if (!templateSrc) return;
6364

65+
if (ast instanceof Element) {
66+
// The HTML tag may include `-` (e.g. `app-root`),
67+
// so use the HtmlAst to get the span before ayazhafiz refactor the code.
68+
return {
69+
start: templateInfo.template.span.start + ast.startSourceSpan!.start.offset + 1,
70+
length: ast.name.length
71+
};
72+
}
73+
6474
// TODO(ayazhafiz): A solution based on word expansion will always be expensive compared to one
6575
// based on ASTs. Whatever penalty we incur is probably manageable for small-length (i.e. the
6676
// majority of) identifiers, but the current solution involes a number of branchings and we can't
@@ -185,7 +195,7 @@ export function getTemplateCompletions(
185195
null);
186196
}
187197

188-
const replacementSpan = getBoundedWordSpan(templateInfo, position);
198+
const replacementSpan = getBoundedWordSpan(templateInfo, position, mostSpecific);
189199
return result.map(entry => {
190200
return {
191201
...entry,

packages/language-service/test/completions_spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -670,18 +670,18 @@ describe('completions', () => {
670670
@Component({
671671
selector: 'foo-component',
672672
template: \`
673-
<di~{div}></div>
673+
<test-comp~{test-comp}></test-comp>
674674
\`,
675675
})
676676
export class FooComponent {}
677677
`);
678-
const location = mockHost.getLocationMarkerFor(fileName, 'div');
678+
const location = mockHost.getLocationMarkerFor(fileName, 'test-comp');
679679
const completions = ngLS.getCompletionsAtPosition(fileName, location.start)!;
680680
expect(completions).toBeDefined();
681-
const completion = completions.entries.find(entry => entry.name === 'div')!;
681+
const completion = completions.entries.find(entry => entry.name === 'test-comp')!;
682682
expect(completion).toBeDefined();
683-
expect(completion.kind).toBe('html element');
684-
expect(completion.replacementSpan).toEqual({start: location.start - 2, length: 2});
683+
expect(completion.kind).toBe('component');
684+
expect(completion.replacementSpan).toEqual({start: location.start - 9, length: 9});
685685
});
686686

687687
it('should work for bindings', () => {

0 commit comments

Comments
 (0)