nix-community / tree-sitter-nix

Nix grammar for tree-sitter [maintainer=@cstrahan]
MIT License
167 stars 28 forks source link

Nix injection incorrectly highlights elements as strings #49

Open mydumpfire opened 6 months ago

mydumpfire commented 6 months ago

This is how bash is normally highlighted (on helix, with the built-in tokyonight theme):

image

And this is how it looks like in an injected nix string block:

image

A lot of stuff that shouldn't be highlighted as a string, gets highlighted as one.

On the other hand, neovim doesn't seem to have this problem:

image

Anyone know what's causing this issue?

asymmetric commented 6 months ago

Are you sure you're using tree-sitter-nix in both editors? Generally, it would be good to post minimal configurations so that people can reproduce.

mydumpfire commented 6 months ago

Yes, they're both using treesitter as the syntax highlighter. I can use :Inspect and :InspectTree on the neovim buffer and view the structure:

image

image

To reproduce:

For helix, you don't need a config. You should be able to reproduce it with the defaults: hx -c ''.

For neovim, you just need to have nvim-treesitter installed with the nix parser. You can put this into your ~/.config/nvim/init.lua:

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system {
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable",
    lazypath,
  }
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup {
  "folke/tokyonight.nvim",
  {
    "nvim-treesitter/nvim-treesitter",
    build = ":TSUpdate",
    config = function()
      local configs = require("nvim-treesitter.configs")

      configs.setup {
        ensure_installed = { "nix" },
        sync_install = false,
        highlight = { enable = true },
        indent = { enable = true },
      }
    end,
  },
}

vim.cmd.colorscheme("tokyonight")

For other installation methods: https://github.com/nvim-treesitter/nvim-treesitter/wiki/Installation

the-mikedavis commented 6 months ago

This is because nvim-treesitter highlights (command argument: (word)): https://github.com/nvim-treesitter/nvim-treesitter/blob/8a9bef9327bc06c9c4158d4873d87bb1bf3ed112/queries/bash/highlights.scm#L172-L175

Helix doesn't highlight the (word) nodes so they keep the string highlight.