Skip to content

Commit c2bb07a

Browse files
authored
(fix) don't trigger html completions inside moustache tags (#90)
Fixes #89
1 parent 89ebffc commit c2bb07a

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

packages/language-server/src/plugins/html/HTMLPlugin.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class HTMLPlugin implements OnRegister, HoverProvider, CompletionsProvide
1414

1515
onRegister(docManager: DocumentManager, configManager: LSConfigManager) {
1616
this.configManager = configManager;
17-
docManager.on('documentChange', document => {
17+
docManager.on('documentChange', (document) => {
1818
const html = this.lang.parseHTMLDocument(document);
1919
this.documents.set(document, html);
2020
});
@@ -43,6 +43,10 @@ export class HTMLPlugin implements OnRegister, HoverProvider, CompletionsProvide
4343
return null;
4444
}
4545

46+
if (this.isInsideMoustacheTag(html, document, position)) {
47+
return null;
48+
}
49+
4650
const emmetResults: CompletionList = {
4751
isIncomplete: true,
4852
items: [],
@@ -64,9 +68,20 @@ export class HTMLPlugin implements OnRegister, HoverProvider, CompletionsProvide
6468
return null;
6569
}
6670

71+
if (this.isInsideMoustacheTag(html, document, position)) {
72+
return null;
73+
}
74+
6775
return this.lang.doTagComplete(document, position, html);
6876
}
6977

78+
private isInsideMoustacheTag(html: HTMLDocument, document: Document, position: Position) {
79+
const offset = document.offsetAt(position);
80+
const node = html.findNodeAt(offset);
81+
const charactersInNode = document.getText().substring(node.start, offset);
82+
return charactersInNode.lastIndexOf('{') > charactersInNode.lastIndexOf('}');
83+
}
84+
7085
getDocumentSymbols(document: Document): SymbolInformation[] {
7186
if (!this.featureEnabled('documentSymbols')) {
7287
return [];

packages/language-server/test/plugins/html/HTMLPlugin.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,42 @@ describe('HTML Plugin', () => {
5454
insertTextFormat: InsertTextFormat.PlainText,
5555
});
5656
});
57+
58+
it('does not provide completions inside of moustache tag', async () => {
59+
const { plugin, document } = setup('<div on:click={() =>');
60+
61+
const completions = plugin.getCompletions(document, Position.create(0, 20));
62+
assert.strictEqual(completions, null);
63+
64+
const tagCompletion = plugin.doTagComplete(document, Position.create(0, 20));
65+
assert.strictEqual(tagCompletion, null);
66+
});
67+
68+
it('does provide completions outside of moustache tag', async () => {
69+
const { plugin, document } = setup('<div on:click={bla} >');
70+
71+
const completions = plugin.getCompletions(document, Position.create(0, 21));
72+
assert.deepEqual(completions?.items[0], <CompletionItem>{
73+
filterText: '</div>',
74+
insertTextFormat: 2,
75+
kind: 10,
76+
label: '</div>',
77+
textEdit: {
78+
newText: '$0</div>',
79+
range: {
80+
end: {
81+
character: 21,
82+
line: 0,
83+
},
84+
start: {
85+
character: 21,
86+
line: 0,
87+
},
88+
},
89+
},
90+
});
91+
92+
const tagCompletion = plugin.doTagComplete(document, Position.create(0, 21));
93+
assert.strictEqual(tagCompletion, '$0</div>');
94+
});
5795
});

0 commit comments

Comments
 (0)