Skip to content

Commit 6a8ef1a

Browse files
authored
fix: use moustache for svelte5 onhandler completion (#2883)
1 parent 2a31d04 commit 6a8ef1a

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

.changeset/fast-rivers-tan.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte-language-server': patch
3+
---
4+
5+
fix: use moustache for svelte5 onhandler completion

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,18 @@ export class HTMLPlugin
198198
return;
199199
}
200200

201-
if (item.label.startsWith('on:')) {
201+
if (item.label.startsWith('on')) {
202+
const isLegacyDirective = item.label.startsWith('on:');
203+
const modifierTabStop = isLegacyDirective ? '$2' : '';
202204
item.textEdit = {
203205
...item.textEdit,
204206
newText: item.textEdit.newText.replace(
205207
attributeValuePlaceHolder,
206-
`$2=${startQuote}$1${endQuote}`
208+
`${modifierTabStop}=${startQuote}$1${endQuote}`
207209
)
208210
};
209211
// In Svelte 5, people should use `onclick` instead of `on:click`
210-
if (document.isSvelte5) {
212+
if (isLegacyDirective && document.isSvelte5) {
211213
item.sortText = 'z' + (item.sortText ?? item.label);
212214
}
213215
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,4 +381,32 @@ describe('HTML Plugin', () => {
381381
}
382382
});
383383
});
384+
385+
if (!isSvelte5Plus) {
386+
return;
387+
}
388+
389+
it('provide event handler completions (svelte 5+)', async () => {
390+
const { plugin, document } = setup('<div on');
391+
392+
const completions = await plugin.getCompletions(document, Position.create(0, 7));
393+
const onClick = completions?.items.find((item) => item.label === 'onclick');
394+
395+
const expected: CompletionItem = {
396+
label: 'onclick',
397+
kind: CompletionItemKind.Value,
398+
documentation: {
399+
kind: 'markdown',
400+
value: 'A pointing device button has been pressed and released on an element.'
401+
},
402+
textEdit: TextEdit.replace(
403+
Range.create(Position.create(0, 5), Position.create(0, 7)),
404+
'onclick={$1}'
405+
),
406+
insertTextFormat: InsertTextFormat.Snippet,
407+
command: undefined
408+
};
409+
410+
assert.deepStrictEqual(onClick, expected);
411+
});
384412
});

0 commit comments

Comments
 (0)