EmranMR / tree-sitter-blade

tree-sitter grammar for Laravel blade files
MIT License
186 stars 8 forks source link

Neovim highlights and injections #7

Closed unansweredocd closed 1 year ago

unansweredocd commented 1 year ago

Hey,

Thanks for doing this once again. After messing around with this parser I finally got it to work a bit. For one, I struggled with the filetype at first, turns out I had to add it to neovim. After that I couldn't get highlighting to work, but I figured that out too.

Here's what I put in my init.lua, which is the starting point for most nvim configurations.


-- add filetype
vim.filetype.add({
  pattern = {
    ['.*blade.php'] = 'blade'
  }
})

-- tell neovim about blade parser.

local parser_config = require "nvim-treesitter.parsers".get_parser_configs()
parser_config.blade = {
  install_info = {
    url = "~/Code/tree-sitter-blade", -- local path or git repo
    files = {"src/parser.c"}, -- note that some parsers also require src/scanner.c or src/scanner.cc
    -- optional entries:
    branch = "main", -- default branch in case of git repo if different from master
    generate_requires_npm = false, -- if stand-alone parser without npm dependencies
    requires_generate_from_grammar = false, -- if folder contains pre-generated src/parser.c
  }
}

This resulted in having two things working, Neovim understanding that .blade.php files are of the blade filetype, and the :InspectTree command from seeing the things that are in the parser you wrote.

After that, highlighting. Couldn't get it to work at all, but when I copied the contents of highlights.scm and injections.scm to ~/.config/nvim/queries/blade/{highlights,injections.scm} I finally reached a point where things started working.

The contents of ~/.config/nvim/queries/blade/highlights.scm are

(directive) @function
(directive_start) @function
(directive_end) @function
(comment) @comment

And the contents of ~/.config/nvim/querie/blade/injections.scm are

((html) @injection.content
    (#set! injection.combined)
    (#set! injection.language html))

; ((php) @injection.content
    ; (#set! injection.combined)
    ; (#set! injection.language php))

; directive parameters
; ((parameter) @injection.content
    ; (#set! injection.language php))

Which results in the screenshot below. If I enable php, things break a bit, same goes for the directive parameters bit.

Screenshot 2023-07-02 at 14 15 20

Now, let it be clear. I don't know what I'm doing, so other Neovim users should definitely tell me what I'm doing wrong. For now, it kind of works, which beats all the blade experiences in Neovim before this one.

EmranMR commented 1 year ago

Hi @unansweredocd I am not a neovim user. However based on the fact that you face problems by enabling php injections, it might be very well due to #5.

Can you wrap your php injection points inside <?php x ?> like the following and feedback here?

If so, I will merge this to #5 . Unfortunately until the split parser is merged into the main branch of tree-sitter-php I would suggest withholding the php injection for time being.

Feel free to subscribe to both issues, so that you stay up to date with any new developments.

unansweredocd commented 1 year ago

Hey @EmranMR, that's alright. The beauty of treesitter is that everyone can benefit without paying for expensive options like phpstorm.

I've tried below, in the injections file, and that seems to fix stuff a bit. It doesn't fix the extends example, but that seems a weird thing to do anyway.

Screenshot 2023-07-02 at 20 11 06

Here's the injection file


((html) @injection.content
    (#set! injection.combined)
    (#set! injection.language html))

((php) @injection.content
    (#set! injection.language php))
EmranMR commented 1 year ago

Ah perfect! In the future for the parameters you need to add the following to the injections.scm as well. I realised in Nova it actually adds a nice syntax highlighting when you do that! Especially for loops and if_statements.

((parameter) @injection.content
    (#set! injection.language php))

The reason I kept this one separate is because in the php nodes, you could possibly do(#set! injection.combined), as technically all {{}} are in the same scope, unlike the parameters.

Although I am yet to test this, when the tree-sitter-php split parser is out, because in blade the semicolon is optional, so it might actually cause more syntax highlighting problems down the line when doing injection.combined

If that happens to be the case, I will just change all injection nodes to php for ease of use. 😜

I will close this for now so that we can follow the thread on #5