Skip to content

Commit

Permalink
Folding Support
Browse files Browse the repository at this point in the history
  • Loading branch information
EmranMR committed Jul 18, 2023
1 parent 93648b9 commit a518d6d
Show file tree
Hide file tree
Showing 11 changed files with 3,628 additions and 3,611 deletions.
57 changes: 27 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ keep maintaining and improving the grammar to include the entire
**Inertia** and so forth. Furthermore keeping the project up to date
with **future releases of Laravel**.

## Nova Users

All you need to do, to get started is to simply install the
[Laravel Suit Extension](https://extensions.panic.com/extensions/emran-mr/emran-mr.laravel/)
from the Extension Library.

## NeoVim Users

If you are NeoVim user and would like to give this parser a shot, I
Expand Down Expand Up @@ -82,15 +88,15 @@ of use I have narrowed everything down to the following rules/queries:
- optional: It will add a nice syntax highlighting for your
parameters
- This is also a php injection point. I initially had this aliased
as `(php-only)` however decided to keep it separate.
as `(php_only)`, however I decided to keep it separate.
- This is for all the inline directives such as `@extends(x)`,
`@yield(x)` and so forth
- you inject `php_only` in `(parameter)` to get a nice syntax
highlighting
- Do **_NOT_** add `(#set! injection.combined)`
- Because they are _parameter_.
- This is just for syntax highlighting and getting nice `php`
IDE autocompletion if needed.
- This is just for syntax highlighting and getting a nice `php`
IDE autocompletion if supported by your editor.

#### 4. (javascript)

Expand Down Expand Up @@ -134,13 +140,24 @@ tree-sitter queries:

## Folding

The grammar is written in a way so that you can easily add folding
functionality. All you have to do is to mark the regions between
`(directive_start)` and `(directive_end)`
The grammar is written in a way to support code folding. You write
them in `queries/folds.scm`.

As long as your editor supports `folds.scm` , you can easily write
predicates to implement foldings. This will allow you to clear up
clutter and focus on the feature you are working in hand.

You can have a look into `queries/folds.scm`, which has the excerpts
used to implement this feature in Nova.

The following is a list of `nodes` that you need to capture to
implement the folding

1. `(directive_start)` and `(directive_end)`
2. `(bracket_start)` and `(bracket_end)`

You will need to first find out what capture groups your editor uses.
I will add an example in `folds.scm` for your information once I write
one for Nova.
> Please note each **editor** uses it's own sets of
> predicates/variables so you need to look into their documentation.
## Quick Note about `queries/` folder

Expand Down Expand Up @@ -175,26 +192,6 @@ as well as in depth info about the `grammar` itself.

- [x] Write the grammar
- [x] Write the tests
- [x] Support folding
- [ ] Support Livewire 🪼
- [ ] support AlpineJS
- [ ] support for Javascript injection points (examples needed from
the users please raise an
[issue](https://github.com/EmranMR/tree-sitter-blade/issues))
- [ ] Write the queries (examples needed example needed from the
users, please raise an
[issue](https://github.com/EmranMR/tree-sitter-blade/issues))

<!-- ## For Nova Contributors & Issues
Head over to this repo:
- [blade extension for Nova](https://github.com/EmranMR/nova-blade)
You need to compile the parser to `.dylib` and then copy it in the
`Syntax/` folder of the extension.
You can download the build script here:
[**Parser Build Script**](https://docs.nova.app/syntax-reference/build_script.zip)
Please refer to the README file in that repo for more information
about contributing and using the build script. -->
4 changes: 2 additions & 2 deletions corpus/comment.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

(blade
(comment
(bracket)
(bracket_start)
(text)
(bracket)))
(bracket_end)))
12 changes: 6 additions & 6 deletions corpus/loops.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
(parameter)
(php)
(php_statement
(bracket)
(bracket_start)
(php_only)
(bracket))
(bracket_end))
(php)
(directive_end)))

Expand Down Expand Up @@ -86,9 +86,9 @@
(parameter)
(php)
(php_statement
(bracket)
(bracket_start)
(php_only)
(bracket))
(bracket_end))
(php)
(loop_operator
(directive))
Expand Down Expand Up @@ -117,9 +117,9 @@
(parameter))
(php)
(php_statement
(bracket)
(bracket_start)
(php_only)
(bracket))
(bracket_end))
(php)
(loop_operator
(directive)
Expand Down
8 changes: 4 additions & 4 deletions corpus/php-statements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

(blade
(php_statement
(bracket)
(bracket_start)
(php_only)
(bracket)))
(bracket_end)))

==================
{!! not escaped !!}
Expand All @@ -22,9 +22,9 @@

(blade
(php_statement
(bracket)
(bracket_start)
(php_only)
(bracket)))
(bracket_end)))
==================
inline raw
==================
Expand Down
18 changes: 9 additions & 9 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,32 @@ module.exports = grammar({

comment: ($) =>
seq(
alias('{{--', $.bracket),
alias('{{--', $.bracket_start),
optional(repeat($.text)),
alias('--}}', $.bracket)
alias('--}}', $.bracket_end)
),

// !keywords
keyword: ($) => alias(/@(csrf|viteReactRefresh)/, $.directive),
keyword: ($) =>
alias(/@(csrf|viteReactRefresh)/, $.directive),
// ! PHP Statements
php_statement: ($) =>
choice($._escaped, $._unescaped, $._raw),
_escaped: ($) =>
seq(
alias('{{', $.bracket),
alias('{{', $.bracket_start),
optional(repeat(alias($.text, $.php_only))),
alias('}}', $.bracket)
alias('}}', $.bracket_end)
),
_unescaped: ($) =>
seq(
alias('{!!', $.bracket),
alias('{!!', $.bracket_start),
optional(repeat(alias($.text, $.php_only))),
alias('!!}', $.bracket)
alias('!!}', $.bracket_end)
),

//! raw php
_raw: ($) =>
choice($._inline_raw, $._multi_line_raw),
_raw: ($) => choice($._inline_raw, $._multi_line_raw),

_inline_raw: ($) =>
seq(alias('@php', $.directive), $._directive_parameter),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tree-sitter-blade",
"version": "0.1.1",
"version": "0.2.0",
"description": "tree-sitter-blade for Laravel Blade files",
"main": "bindings/node",
"keywords": [
Expand Down
8 changes: 8 additions & 0 deletions queries/folds.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
( (directive_start) @start
(directive_end) @end.after
(#set! role block))


( (bracket_start) @start
(bracket_end) @end
(#set! role block))
12 changes: 6 additions & 6 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"value": "{{--"
},
"named": true,
"value": "bracket"
"value": "bracket_start"
},
{
"type": "CHOICE",
Expand All @@ -80,7 +80,7 @@
"value": "--}}"
},
"named": true,
"value": "bracket"
"value": "bracket_end"
}
]
},
Expand Down Expand Up @@ -120,7 +120,7 @@
"value": "{{"
},
"named": true,
"value": "bracket"
"value": "bracket_start"
},
{
"type": "CHOICE",
Expand Down Expand Up @@ -149,7 +149,7 @@
"value": "}}"
},
"named": true,
"value": "bracket"
"value": "bracket_end"
}
]
},
Expand All @@ -163,7 +163,7 @@
"value": "{!!"
},
"named": true,
"value": "bracket"
"value": "bracket_start"
},
{
"type": "CHOICE",
Expand Down Expand Up @@ -192,7 +192,7 @@
"value": "!!}"
},
"named": true,
"value": "bracket"
"value": "bracket_end"
}
]
},
Expand Down
18 changes: 15 additions & 3 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@
"required": true,
"types": [
{
"type": "bracket",
"type": "bracket_end",
"named": true
},
{
"type": "bracket_start",
"named": true
},
{
Expand Down Expand Up @@ -515,7 +519,11 @@
"required": false,
"types": [
{
"type": "bracket",
"type": "bracket_end",
"named": true
},
{
"type": "bracket_start",
"named": true
},
{
Expand Down Expand Up @@ -871,7 +879,11 @@
"named": false
},
{
"type": "bracket",
"type": "bracket_end",
"named": true
},
{
"type": "bracket_start",
"named": true
},
{
Expand Down
Loading

0 comments on commit a518d6d

Please sign in to comment.