Skip to content

Commit

Permalink
Add custom shortcut support
Browse files Browse the repository at this point in the history
  • Loading branch information
kolen committed Feb 16, 2024
1 parent 5262824 commit 6f41b7d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
8 changes: 6 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,14 @@ module.exports = grammar({
tag_name: $ => /(\w+|\w[\w:-]+\w)/,
_attr_shortcut: $ => choice(
$.shortcut_class,
$.shortcut_id
$.shortcut_id,
$.shortcut_custom
),
shortcut_class: $ => seq('.', $.css_identifier),
shortcut_id: $ => seq('#', $.css_identifier),
// In real slim, custom shortcuts only parsed if prefix is
// configured, otherwise parsed as inline text
shortcut_custom: $ => token(prec(-1, /[^ \t\na-zA-Z0-9_-]+[a-zA-Z0-9_-]*/)),

nested: $ => $._block,

Expand Down Expand Up @@ -169,7 +173,7 @@ module.exports = grammar({
optional($._text_nested)
),

_element_rest_text: $ => token(prec(-1, /[^ \t][^\n]*/)),
_element_rest_text: $ => token(prec(-2, /[^ \t][^\n]*/)),

// From css grammar https://github.com/tree-sitter/tree-sitter-css/blob/master/grammar.js
// Originally: /\A(#{keys}+)((?:\p{Word}|-|\/\d+|:(\w|-)+)*)/
Expand Down
17 changes: 16 additions & 1 deletion src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@
{
"type": "SYMBOL",
"name": "shortcut_id"
},
{
"type": "SYMBOL",
"name": "shortcut_custom"
}
]
},
Expand Down Expand Up @@ -349,6 +353,17 @@
}
]
},
"shortcut_custom": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": -1,
"content": {
"type": "PATTERN",
"value": "[^ \\t\\na-zA-Z0-9_-]+[a-zA-Z0-9_-]*"
}
}
},
"nested": {
"type": "SYMBOL",
"name": "_block"
Expand Down Expand Up @@ -959,7 +974,7 @@
"type": "TOKEN",
"content": {
"type": "PREC",
"value": -1,
"value": -2,
"content": {
"type": "PATTERN",
"value": "[^ \\t][^\\n]*"
Expand Down
8 changes: 8 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
"type": "shortcut_class",
"named": true
},
{
"type": "shortcut_custom",
"named": true
},
{
"type": "shortcut_id",
"named": true
Expand Down Expand Up @@ -656,6 +660,10 @@
"type": "scss",
"named": false
},
{
"type": "shortcut_custom",
"named": true
},
{
"type": "strict",
"named": false
Expand Down
15 changes: 15 additions & 0 deletions test/corpus/inline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ p = make_text(style: "standard")

---

(source_file
(element
(tag_name)
(nested_inline
(ruby_block_output
(ruby)))))

=========================
Inline output - immediate
=========================

p=make_text

---

(source_file
(element
(tag_name)
Expand Down
17 changes: 17 additions & 0 deletions test/corpus/tags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,20 @@ Tag with space before traling space modifier
(shortcut_class
(css_identifier)))
(element_text)))

=============================
Extension attribute shortcuts
=============================

span.example@@example*example

---

(source_file
(element
(tag_name)
(attr_shortcuts
(shortcut_class
(css_identifier))
(shortcut_custom)
(shortcut_custom))))

0 comments on commit 6f41b7d

Please sign in to comment.