injection.scm
or highlights.scm
v0.9.0
Release Note for the Upgrade Guide,
if you have any questions, feel free to start a discussion or
raise an issueinjections.scm
you're good to go ✅
to enjoy all the new features ✨This project aims to write the tree-sitter grammar for Laravel Blade.
The grammar could potentially be used in any editors or services that are based on Tree-Sitter. Such as, but not limited to:
If you found this project helpful, I would really appreciate if you can sponsor me so that I could keep maintaining and improving the grammar to include the entire Laravel Ecosystem inlcluding Livewire, AlpineJS, Inertia and so forth. Furthermore keeping the project up to date with future releases of Laravel.
Simply install the Laravel Suite Extension from the Extension Library. That includes:
php injection
out of the box!If you are a NeoVim user and would like to give this parser a shot, I would highly recommend checking out the NeoVim Discussion
Especially if you encounter any issues!
You could have a look at the queries/injection.scm
and simply copy
and paste. But here are some detailed information:
(php)
html/php
into your document((text) @injection.content
(#not-has-ancestor? @injection.content "envoy")
(#set! injection.combined)
(#set! injection.language php))
(php_only)
The php_only
injections are now finally possible thanks to the
contribution by @CalebDW on the
tree-sitter-php#192
repo! 🚀
You inject php_only
into (php_only)
nodes. This is for all the
php directives including {{ x }}
, {!! x !!}}
and so forth
((php_only) @injection.content
(#set! injection.language php_only))
(parameter)
(php_only)
, however I decided to keep it separate.@extends(x)
,
@yield(x)
and so forthphp_only
in (parameter)
to get a nice syntax
highlighting((parameter) @injection.content
(#set! injection.language php_only))
@task
shell
syntax highlighting and possibly
completion when writing your envoys@before, @after ...
you will correctly get a
php_only
injections provided by the code above, automatically.
You can use whatever you want, zsh etc Here is an an example with
bash
((text) @injection.content
(#has-ancestor? @injection.content "envoy")
(#set! injection.combined)
(#set! injection.language bash))
::attributes
you could extend you
tree-sitter-html
queries at
nvim/after/queries/html/injections.scm
using the following
snippet provided by @calebDW. see
issue#33
if interested.;; extends
; AlpineJS attributes
(attribute
(attribute_name) @_attr
(#lua-match? @_attr "^x%-%l")
(quoted_attribute_value
(attribute_value) @injection.content)
(#set! injection.language "javascript"))
; Blade escaped JS attributes
; <x-foo ::bar="baz" />
(element
(_
(tag_name) @_tag
(#lua-match? @_tag "^x%-%l")
(attribute
(attribute_name) @_attr
(#lua-match? @_attr "^::%l")
(quoted_attribute_value
(attribute_value) @injection.content)
(#set! injection.language "javascript"))))
; Blade PHP attributes
; <x-foo :bar="$baz" />
(element
(_
(tag_name) @_tag
(#lua-match? @_tag "^x%-%l")
(attribute
(attribute_name) @_attr
(#lua-match? @_attr "^:%l")
(quoted_attribute_value
(attribute_value) @injection.content)
(#set! injection.language "php_only"))))
❗ Please note each editor uses it's own sets of predicates/variables so you need to look into their documentation.
This is what you need to use inside your highlights.scm
. For ease
of use, I managed to boil everything down to the following
tree-sitter queries:
@theme_selector
@csrf
or inline directives such as
@extends()
(directive)
you are painting @extends
in
@extends(x)
and the keywords@theme_selector
@php x @endphp
@php
@theme_selector
@php x @endphp
@endphp
@theme_selector
)attribute
directives to get highlighted as an attribute
@theme_selector
{{-- x --}}
(comment)
@theme_selector
{{}}
use this to
capture@theme_selector
{{}}
use this to
capture❗ Please note each editor uses it's own sets of predicates/variables so you need to look into their documentation.
The grammar is written in a way to support code folding. You write
them in the 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 at 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
(directive_start)
and (directive_end)
(bracket_start)
and (bracket_end)
; Example Query used in Nova to implement folding
( (directive_start) @start
(directive_end) @end.after
(#set! role block))
( (bracket_start) @start
(bracket_end) @end
(#set! role block))
queries/
folderThis is for tree-sitter cli
. Your editor or service might use a
different capture group name or predicates.
Consequently, you will need to find out how to approach queries or
where they are stored and used in your editor's documentation. For
example Nova does not use anything in this folder and uses its
own Queries
folder instead.
If something does not look right please raise an issue with a detailed examples of what you are trying to achieve. Add any of the following if necessary:
If you need help with anything else or would like to share tips and tricks for your fellow devs, feel free to use the discussion tab and create a discussion!
See the contribution guidelines for more details,
as well as in depth info about the grammar
itself.