Skip to content

Commit

Permalink
fix: Override tree-sitter highlights for Djot
Browse files Browse the repository at this point in the history
  • Loading branch information
treeman committed Jan 29, 2025
1 parent 8433042 commit 1e17751
Show file tree
Hide file tree
Showing 3 changed files with 345 additions and 4 deletions.
2 changes: 1 addition & 1 deletion posts/2024-03-19-lets_create_a_tree-sitter_grammar.dj
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ A. Syntect gives better highlighting for some languages (like Rust and C).

A. The highlighter library feels a bit immature.

A newer library broke the highlight groups I got from some grammars and I don't see any support for how to add a language specific class to `span` for injected languages.
A newer version broke the highlight groups I got from some grammars and I don't see any support for how to add a language specific class to `span` for injected languages.

Because of these issues I'll evaluate what highlighter to use on a case-by-case basis, with Syntect as the default choice.

Expand Down
343 changes: 343 additions & 0 deletions queries/djot.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,343 @@
(heading) @markup.heading

((heading
(marker) @_heading.marker) @markup.heading.1
(#eq? @_heading.marker "# "))

((heading
(marker) @_heading.marker) @markup.heading.2
(#eq? @_heading.marker "## "))

((heading
(marker) @_heading.marker) @markup.heading.3
(#eq? @_heading.marker "### "))

((heading
(marker) @_heading.marker) @markup.heading.4
(#eq? @_heading.marker "##### "))

((heading
(marker) @_heading.marker) @markup.heading.5
(#eq? @_heading.marker "###### "))

((heading
(marker) @_heading.marker) @markup.heading.6
(#eq? @_heading.marker "####### "))

(thematic_break) @string.special

[
(div_marker_begin)
(div_marker_end)
] @punctuation.delimiter

([
(code_block)
(raw_block)
(frontmatter)
] @markup.raw.block
(#set! priority 90))

; Remove @markup.raw for code with a language spec
(code_block
.
(code_block_marker_begin)
(language)
(code) @none
(#set! priority 90))

[
(code_block_marker_begin)
(code_block_marker_end)
(raw_block_marker_begin)
(raw_block_marker_end)
] @punctuation.delimiter

(language) @attribute

(inline_attribute
_ @conceal
(#set! conceal ""))

((language_marker) @punctuation.delimiter
(#set! conceal ""))

(block_quote) @markup.quote

(block_quote_marker) @punctuation.special

(table_header) @markup.heading

(table_header
"|" @punctuation.special)

(table_row
"|" @punctuation.special)

(table_separator) @punctuation.special

(table_caption
(marker) @punctuation.special)

(table_caption) @markup.italic

[
(list_marker_dash)
(list_marker_plus)
(list_marker_star)
(list_marker_definition)
(list_marker_decimal_period)
(list_marker_decimal_paren)
(list_marker_decimal_parens)
(list_marker_lower_alpha_period)
(list_marker_lower_alpha_paren)
(list_marker_lower_alpha_parens)
(list_marker_upper_alpha_period)
(list_marker_upper_alpha_paren)
(list_marker_upper_alpha_parens)
(list_marker_lower_roman_period)
(list_marker_lower_roman_paren)
(list_marker_lower_roman_parens)
(list_marker_upper_roman_period)
(list_marker_upper_roman_paren)
(list_marker_upper_roman_parens)
] @markup.list

(list_marker_task
(unchecked)) @markup.list.unchecked

(list_marker_task
(checked)) @markup.list.checked

; Colorize `x` in `[x]`
((checked) @constant.builtin
(#offset! @constant.builtin 0 1 0 -1))

[
(ellipsis)
(en_dash)
(em_dash)
(quotation_marks)
] @string.special

(list_item
(term) @type.definition)

; Conceal { and } but leave " and '
((quotation_marks) @string.special
(#any-of? @string.special "\"}" "'}")
(#offset! @string.special 0 1 0 0)
(#set! conceal ""))

((quotation_marks) @string.special
(#any-of? @string.special "\\\"" "\\'" "{'" "{\"")
(#offset! @string.special 0 0 0 -1)
(#set! conceal ""))

[
(hard_line_break)
(backslash_escape)
] @string.escape

; Only conceal \ but leave escaped character.
((backslash_escape) @string.escape
(#offset! @string.escape 0 0 0 -1)
(#set! conceal ""))

(frontmatter_marker) @punctuation.delimiter

(emphasis) @markup.italic

(strong) @markup.strong

(symbol) @string.special.symbol

(insert) @markup.underline

(delete) @markup.strikethrough

[
(highlighted)
(superscript)
(subscript)
] @string.special

([
(emphasis_begin)
(emphasis_end)
(strong_begin)
(strong_end)
(superscript_begin)
(superscript_end)
(subscript_begin)
(subscript_end)
(highlighted_begin)
(highlighted_end)
(insert_begin)
(insert_end)
(delete_begin)
(delete_end)
(verbatim_marker_begin)
(verbatim_marker_end)
(math_marker)
(math_marker_begin)
(math_marker_end)
(raw_inline_attribute)
(raw_inline_marker_begin)
(raw_inline_marker_end)
] @punctuation.delimiter
(#set! conceal ""))

((math) @markup.math
(#set! priority 90))

(verbatim) @markup.raw

((raw_inline) @markup.raw
(#set! priority 90))

[
(comment)
(inline_comment)
] @comment

(span
[
"["
"]"
] @punctuation.bracket)

(inline_attribute
[
"{"
"}"
] @punctuation.bracket)

(block_attribute
[
"{"
"}"
] @punctuation.bracket)

[
(class)
(class_name)
] @type

(identifier) @tag

(key_value
"=" @operator)

(key_value
(key) @property)

(key_value
(value) @string)

(link_text
[
"["
"]"
] @punctuation.bracket
(#set! conceal ""))

(autolink
[
"<"
">"
] @punctuation.bracket
(#set! conceal ""))

(inline_link
(inline_link_destination) @markup.link.url
(#set! conceal ""))

(link_reference_definition
":" @punctuation.special)

(full_reference_link
(link_text) @markup.link)

(full_reference_link
(link_label) @markup.link.label
(#set! conceal ""))

(collapsed_reference_link
"[]" @punctuation.bracket
(#set! conceal ""))

(full_reference_link
[
"["
"]"
] @punctuation.bracket
(#set! conceal ""))

(collapsed_reference_link
(link_text) @markup.link)

(collapsed_reference_link
(link_text) @markup.link.label)

(inline_link
(link_text) @markup.link)

(full_reference_image
(link_label) @markup.link.label)

(full_reference_image
[
"["
"]"
] @punctuation.bracket)

(collapsed_reference_image
"[]" @punctuation.bracket)

(image_description
[
"!["
"]"
] @punctuation.bracket)

(image_description) @markup.italic

(link_reference_definition
[
"["
"]"
] @punctuation.bracket)

(link_reference_definition
(link_label) @markup.link.label)

(inline_link_destination
[
"("
")"
] @punctuation.bracket)

[
(autolink)
(inline_link_destination)
(link_destination)
(link_reference_definition)
] @markup.link.url

(footnote
(reference_label) @markup.link.label)

(footnote_reference
(reference_label) @markup.link.label)

[
(footnote_marker_begin)
(footnote_marker_end)
] @punctuation.bracket

(todo) @comment.todo

(note) @comment.note

(fixme) @comment.error
4 changes: 1 addition & 3 deletions src/markup/syntax_highlight/treesitter_highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ static HIGHLIGHT_NAMES: &[&str] = &[
"markup.symbol",
"markup.todo",
"module",
"nospell",
"number",
"number.float",
"operator",
Expand All @@ -152,7 +151,6 @@ static HIGHLIGHT_NAMES: &[&str] = &[
"punctuation.bracket",
"punctuation.delimiter",
"punctuation.special",
"spell",
"string",
"string.escape",
"string.special",
Expand Down Expand Up @@ -201,7 +199,7 @@ fn init_configurations() -> HashMap<String, HighlightConfiguration> {
HighlightConfiguration::new(
tree_sitter_djot::language(),
"djot",
tree_sitter_djot::HIGHLIGHTS_QUERY,
include_str!("../../../queries/djot.scm"),
tree_sitter_djot::INJECTIONS_QUERY,
"",
)
Expand Down

0 comments on commit 1e17751

Please sign in to comment.