Skip to content
Open
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
5 changes: 3 additions & 2 deletions extension.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ name = "Svelte Language Server"
language = "Svelte"

[grammars.svelte]
repository = "https://github.com/tree-sitter-grammars/tree-sitter-svelte"
commit = "3f06f705410683adb17d146b5eca28c62fe81ba6"
repository = "https://github.com/themixednuts/tree-sitter-htmlx"
commit = "80c3e698ec7379772c7f2aecb9b4b4c4ac52ff0b"
path = "crates/tree-sitter-svelte"
6 changes: 2 additions & 4 deletions languages/svelte/brackets.scm
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
("<" @open ">" @close)
("{" @open "}" @close)
("'" @open "'" @close)
("\"" @open "\"" @close)
(("'" @open "'" @close) (#set! rainbow.exclude))
(("\"" @open "\"" @close) (#set! rainbow.exclude))
("(" @open ")" @close)
; ("[" @open "]" @close)
; ("`" @open "`" @close)

((element (start_tag) @open [(end_tag) (erroneous_end_tag)] @close) (#set! newline.only))
15 changes: 15 additions & 0 deletions languages/svelte/folds.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
(comment)
(element)
(if_block)
(else_if_clause)
(else_clause)
(each_block)
(await_block)
(await_pending)
(await_branch)
(await_branch_children)
(key_block)
(orphan_branch)
(snippet_block)
] @fold
201 changes: 114 additions & 87 deletions languages/svelte/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,108 +1,135 @@
; Tag names
(tag_name) @tag

; comments
; Erroneous/mismatched end tags
(erroneous_end_tag_name) @tag

; DOCTYPE declaration
(doctype) @constant

; Attribute names
(attribute_name) @attribute

; Attribute values
(attribute_value) @string
(quoted_attribute_value) @string

; Comments
(comment) @comment

; property attribute
(attribute_directive) @attribute.function
(attribute_identifier) @attribute
(attribute_modifier) @attribute.special

; Style component attributes as @property
(start_tag
(
(tag_name) @_tag_name
(#match? @_tag_name "^[A-Z]")
)
(attribute
(attribute_name
(attribute_identifier) @tag.property
)
)
)

; Style self-closing component attributes as @tag.property
(self_closing_tag
(
(tag_name) @_tag_name
(#match? @_tag_name "^[A-Z]")
)
(attribute
(attribute_name
(attribute_identifier) @tag.property
)
)
)


; style elements starting with lowercase letters as tags
(
(tag_name) @tag
(#match? @tag "^[a-z]")
)

; style elements starting with uppercase letters as components (types)
; Also valid might be to treat them as constructors
(
(tag_name) @tag @tag.component.type.constructor
(#match? @tag "^[A-Z]")
)
; Character entities
(entity) @string.special

; Text content
(text) @text

; Raw text in script/style
(raw_text) @text.literal

; HTML punctuation
[
"<"
">"
"</"
"/>"
] @tag.punctuation.bracket


[
"{"
"}"
] @punctuation.bracket

[
"|"
] @punctuation.delimiter


[
"@"
"#"
":"
"/"
] @tag.punctuation.special

"=" @operator
"=" @punctuation.delimiter

; Component tags (PascalCase)
((tag_name) @type (#match? @type "^[A-Z]"))

; Treating (if, each, ...) as a keyword inside of blocks
; like {#if ...} or {#each ...}
(block_start_tag
tag: _ @keyword
)
; Namespaced tags (svelte:component, svelte:self, etc.)
(tag_name
namespace: (tag_namespace) @keyword
":" @punctuation.delimiter
name: (tag_local_name) @tag)

(block_tag
tag: _ @keyword
)
; Tag member access (Foo.Bar)
(tag_name
object: (tag_member) @type
"." @punctuation.delimiter
property: (tag_member) @tag)

(block_end_tag
tag: _ @keyword
)
; Directives (on:click, bind:value, etc.)
(attribute_directive) @keyword
(attribute_name ":" @punctuation.delimiter)
(attribute_identifier) @property
(attribute_modifier) @attribute
(attribute_modifiers "|" @punctuation.delimiter)

(expression_tag
tag: _ @keyword
)
; Expressions
(expression) @embedded

; Style quoted string attribute values
(quoted_attribute_value) @string
; Shorthand/spread attributes — content only, braces fall through to "{" "}" rule
(shorthand_attribute content: (_) @variable)

; Curly braces (expression context)
[
"{"
"}"
] @punctuation.bracket

; Highlight the `as` keyword in each blocks
(each_start
("as") @tag.keyword
)
"|" @punctuation.delimiter

; Comments inside tag attribute lists
(tag_comment kind: (line_comment) @comment)
(tag_comment kind: (block_comment) @comment)

; Highlight the snippet name as a function
; (e.g. {#snippet foo(bar)}
(snippet_name) @function
; Block keywords
[
"if"
"each"
"await"
"key"
"snippet"
"else"
"html"
"debug"
"const"
"render"
"attach"
"as"
] @keyword.control

; Block end keywords ({/if}, {/each}, etc.)
(block_keyword) @keyword.control

; Block delimiters
(block_open) @punctuation.bracket
(block_close) @punctuation.bracket

(shorthand_kind) @keyword.control
(branch_kind) @keyword.control

(expression_value) @embedded

; If block
(if_block expression: (expression) @embedded)
(else_if_clause expression: (expression_value) @embedded)

; Each block
(each_block expression: (expression) @embedded)
(each_block binding: (pattern) @variable)
(each_block index: (pattern) @variable)
(each_block key: (expression) @embedded)

; Await block
(await_block expression: (expression) @embedded)
(await_branch (pattern) @variable)
(await_block (pattern) @variable)
(orphan_branch (pattern) @variable)

; Key block
(key_block expression: (expression) @embedded)

; Snippet block
(snippet_block name: (snippet_name) @function)
(snippet_parameters parameter: (pattern) @variable)
(snippet_type_parameters) @type

; Malformed blocks (e.g. { #if ...} with space before sigil)
(block_sigil) @keyword.control

; Snippet/render punctuation
["(" ")" ","] @punctuation.delimiter
17 changes: 5 additions & 12 deletions languages/svelte/indents.scm
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
[
(element)
(if_statement)
(each_statement)
(await_statement)
(snippet_statement)
(script_element)
(style_element)
(if_block)
(each_block)
(await_block)
(key_block)
(snippet_block)
(start_tag ">" @end)
(self_closing_tag "/>" @end)
(element
(start_tag) @start
[(end_tag) (erroneous_end_tag)]? @end)
(script_element
(start_tag) @start
[(end_tag) (erroneous_end_tag)]? @end)
(style_element
(start_tag) @start
[(end_tag) (erroneous_end_tag)]? @end)
] @indent
Loading