Skip to content

feat: support generics on snippets #2761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion packages/svelte-vscode/syntaxes/svelte.tmLanguage.src.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,38 @@ repository:
special-tags-modes:
patterns:
# Expressions or simple values.
- begin: (?<=(if|key|then|catch|snippet|html|render).*?)\G
- begin: (?<=(if|key|then|catch|html|render).*?)\G
end: (?=})
name: meta.embedded.expression.svelte source.ts
patterns: [ include: source.ts ]

# Snippet blocks - special because of how source.ts will parse generics: If it realizes "oh this is a function definition" it will
# go into arrow function parsing mode and not stop until it finds a `=>`, which means it will go past the `}` snippet open boundary
# and fuck up the rest of the file syntax highlighting
- begin: (?<=snippet.*?)\G
end: (?=})
name: meta.embedded.expression.svelte source.ts
patterns:
# Match an identifier, but only if it is before a generic
- match: \G\s*([_$[:alpha:]][_$[:alnum:]]*)\s*(?=<)
captures:
1: { name: entity.name.function.ts }
# Match optional `<` ... `>` with inner as source.ts
- begin: (?<=<)
end: (?=>)
contentName: meta.type.parameters.ts
patterns: [ include: source.ts ]
# Match the `(...)` but not starting at `(` because then TS would see it as an arrow function and parse past our snippet open boundary
- begin: (?<=>\s*\()
end: (?=})
name: meta.embedded.expression.svelte source.ts
patterns: [ include: source.ts ]
# If the above three don't match (because there's no generic) then this one kicks in
- begin: \G
end: (?=})
name: meta.embedded.expression.svelte source.ts
patterns: [ include: source.ts ]

# Const.
- begin: (?<=const.*?)\G
end: (?=})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function handleSnippet(
'const ',
[snippetBlock.expression.start, snippetBlock.expression.end],
IGNORE_POSITION_COMMENT,
' = ('
` = ${snippetBlock.typeParams ? `<${snippetBlock.typeParams}>` : ''}(`
];

if (parameters) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
///<reference types="svelte" />
;function $$render() {
const generic/*Ωignore_positionΩ*/ = <T extends string>(val: T)/*Ωignore_startΩ*/: ReturnType<import('svelte').Snippet>/*Ωignore_endΩ*/ => { async ()/*Ωignore_positionΩ*/ => {
val;
};return __sveltets_2_any(0)}; const complex_generic/*Ωignore_positionΩ*/ = <T extends { bracket: "<" } | "<" | Set<"<>">>(val: T)/*Ωignore_startΩ*/: ReturnType<import('svelte').Snippet>/*Ωignore_endΩ*/ => { async ()/*Ωignore_positionΩ*/ => {
val;
};return __sveltets_2_any(0)};
;
async () => {



};
return { props: /** @type {Record<string, never>} */ ({}), exports: {}, bindings: "", slots: {}, events: {} }}
const Input__SvelteComponent_ = __sveltets_2_isomorphic_component(__sveltets_2_partial(__sveltets_2_with_any_event($$render())));
/*Ωignore_startΩ*/type Input__SvelteComponent_ = InstanceType<typeof Input__SvelteComponent_>;
/*Ωignore_endΩ*/export default Input__SvelteComponent_;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script lang="ts">
</script>

{#snippet generic<T extends string>(val: T)}
{val}
{/snippet}

{#snippet complex_generic<T extends { bracket: "<" } | "<" | Set<"<>">>(val: T)}
{val}
{/snippet}