tree-sitter-grammars / tree-sitter-svelte

Svelte grammar for tree-sitter
MIT License
11 stars 7 forks source link

bug: {:else if ...} is not highlighted correctly #14

Open charbelnicolas opened 4 weeks ago

charbelnicolas commented 4 weeks ago

Did you check existing issues?

Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)

tree-sitter 0.24.3 (bdfe32402e85673bbc693216f0a6ef72c98bb665)

Describe the bug

When using {:else if} it won't highlight it in the proper color Image

Steps To Reproduce/Bad Parse Tree

Use an {#if ...} statement with an {:else if ...} inside as shown in the above screenschot...

Expected Behavior/Parse Tree

{:else if ...} should be highlighted in the same color as the {#if ...} statement

Repro

{#if true}
  hello
{:else if true}
  hello again
{/if}
este36 commented 2 weeks ago

Hello!

Just a little message to say that I worked on this issue and fixed it.

Hopefully, my pull request will be considered soon!

If not, here a solution to work with a local version of the parser.

if your editor is Neovim, you need to fallow these steps:

Clone the tree-sitter-svelte repo somewhere on your machine :

git clone https://github.com/tree-sitter-grammars/tree-sitter-svelte.git

Update grammar.js (in the root directory of the project) :

Link to my commit

Image

Build the parser :

npm install tree-sitter
npm install tree-sitter-html
tree-sitter generate

Update your Neovim config :

Place this code at the end of your init.lua config entry point. Be sure to replace the URL with the absolute path of your local version of the repo.


local parser_config = require "nvim-treesitter.parsers".get_parser_configs()
parser_config.svelte = {
    install_info = {
        url = "/root/path/of/localrepo/here",
        files = {"src/parser.c", "src/scanner.c"},
    },
    filetype = "svelte"
}

local project_path = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":p:h:h:h")
local query_files = vim.fn.globpath(project_path, "queries/svelte/**", nil, true)

for _, query_file in ipairs(query_files) do
    local query_name = vim.fn.fnamemodify(query_file, ":t:r")
    local query_content = table.concat(vim.fn.readfile(query_file), "\n")

    vim.treesitter.set_query("svelte", query_name, query_content)
end

Final step, reinstall the svelte parser :

:TSUninstall svelte
:TSInstall svelte
charbelnicolas commented 2 weeks ago

Wow, thank you so much! I Appreciate it! Hopefully your PR gets merged soon too so everyone can benefit from your fix :)