Skip to content

Commit

Permalink
Merge pull request #3 from mclaughlinconnor/highlight
Browse files Browse the repository at this point in the history
Add syntax highlighting
  • Loading branch information
mclaughlinconnor authored Dec 22, 2022
2 parents b504d31 + b8c5b52 commit 3b3c6f3
Show file tree
Hide file tree
Showing 14 changed files with 501 additions and 314 deletions.
2 changes: 1 addition & 1 deletion bindings/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json");

// Uncomment these to include any queries that this grammar contains

// pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm");
pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm");
// pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm");
// pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm");
// pub const TAGS_QUERY: &'static str = include_str!("../../queries/tags.scm");
Expand Down
43 changes: 23 additions & 20 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ module.exports = grammar({

include: ($) =>
seq(
'include',
alias('include', $.keyword),
optional($.filter),
alias(/[^\n]+/, $.filename),
),

while: ($) =>
seq(
'while',
alias('while', $.keyword),
$.iteration_iterator,
$._newline,
$.children,
Expand All @@ -69,17 +69,17 @@ module.exports = grammar({

_each_else: ($) =>
seq(
'else',
alias('else', $.keyword),
$._newline,
$.children,
),

each: ($) =>
prec.right(
seq(
choice('each', 'for'),
alias(choice('each', 'for'), $.keyword),
$.iteration_variable,
'in',
alias('in', $.keyword),
$.iteration_iterator,
$._newline,
$.children,
Expand Down Expand Up @@ -116,7 +116,7 @@ module.exports = grammar({
),
mixin_definition: ($) =>
seq(
'mixin',
alias('mixin', $.keyword),
alias($.tag_name, $.mixin_name),
optional($.mixin_attributes),
$._newline,
Expand Down Expand Up @@ -153,24 +153,24 @@ module.exports = grammar({
),
block_definition: ($) =>
seq(
'block',
alias('block', $.keyword),
$._block_content,
),
block_append: ($) =>
seq(
optional('block'),
'append',
alias(optional('block'), $.keyword),
alias('append', $.keyword),
$._block_content,
),
block_prepend: ($) =>
seq(
optional('block'),
'prepend',
alias(optional('block'), $.keyword),
alias('prepend', $.keyword),
$._block_content,
),
extends: ($) =>
seq(
'extends',
alias('extends', $.keyword),
alias(/[^\n]+/, $.filename),
),

Expand Down Expand Up @@ -210,22 +210,25 @@ module.exports = grammar({
seq(
choice(
seq(
choice(
'unless',
'if',
'else if',
alias(
choice(
'unless',
'if',
'else if',
),
$.keyword,
),
alias($._un_delimited_javascript, $.javascript),
),
'else',
alias('else', $.keyword),
),
$._newline,
$.children,
),
case: ($) =>
prec.right(
seq(
'case',
alias('case', $.keyword),
alias($._un_delimited_javascript_line, $.javascript),
$._newline,
$._indent,
Expand Down Expand Up @@ -253,11 +256,11 @@ module.exports = grammar({
_when_keyword: ($) =>
choice(
seq(
'when',
alias('when', $.keyword),
// `when`s don't work with properly with objects, so removing : from regex is fine.
alias(/[^:\n]+?/, $.javascript),
),
'default',
alias('default', $.keyword),
),
when: ($) =>
prec.left(
Expand Down
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
},
"devDependencies": {
"prettier": "^2.3.2",
"tree-sitter-cli": "^0.20.0"
}
"tree-sitter-cli": "^0.20.7"
},
"tree-sitter": [
{
"scope": "source.pug",
"file-types": [
"pug"
]
}
]
}
50 changes: 50 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
(comment) @comment

(tag_name) @constant
(
(tag_name) @constant.builtin
; https://www.script-example.com/html-tag-liste
(#any-of? @constant.builtin
"head" "title" "base" "link" "meta" "style"
"body" "article" "section" "nav" "aside" "h1" "h2" "h3" "h4" "h5" "h6" "hgroup" "header" "footer" "address"
"p" "hr" "pre" "blockquote" "ol" "ul" "menu" "li" "dl" "dt" "dd" "figure" "figcaption" "main" "div"
"a" "em" "strong" "small" "s" "cite" "q" "dfn" "abbr" "ruby" "rt" "rp" "data" "time" "code" "var" "samp" "kbd" "sub" "sup" "i" "b" "u" "mark" "bdi" "bdo" "span" "br" "wbr"
"ins" "del"
"picture" "source" "img" "iframe" "embed" "object" "param" "video" "audio" "track" "map" "area"
"table" "caption" "colgroup" "col" "tbody" "thead" "tfoot" "tr" "td" "th"
"form" "label" "input" "button" "select" "datalist" "optgroup" "option" "textarea" "output" "progress" "meter" "fieldset" "legend"
"details" "summary" "dialog"
"script" "noscript" "template" "slot" "canvas")
)

(content) @none

(id) @attribute
(class) @attribute

(quoted_attribute_value) @string
(attribute_name) @symbol
(
(attribute_name) @keyword
(#match? @keyword "^(\\(.*\\)|\\[.*\\]|\\*.*)$")
) @keyword

[
":"
"{{"
"}}"
"+"
"|"
] @punctuation.delimiter

(keyword) @keyword
((keyword) @include (#eq? @include "include"))
((keyword) @repeat (#any-of? @repeat "for" "each" "of" "in" "while"))
((keyword) @conditional (#any-of? @conditional "if" "else" "else if" "unless"))
((keyword) @keyword.function (#any-of? @keyword.function "block" "mixin"))

(filter_name) @method.call

(mixin_use (mixin_name) @method.call)
(mixin_definition (mixin_name) @function)

Loading

0 comments on commit 3b3c6f3

Please sign in to comment.