Extend your lazy config with treesitter and the nu parser. The parser doesn't have to be listed under dependencies.
{
"nvim-treesitter/nvim-treesitter",
config = function()
require("nvim-treesitter.configs").setup {
ensure_installed = { "nu" }, -- Ensure the "nu" parser is installed
highlight = {
enable = true, -- Enable syntax highlighting
},
-- OPTIONAL!! These enable ts-specific textobjects.
-- So you can hit `yaf` to copy the closest function,
-- `dif` to clear the contet of the closest function,
-- or whatever keys you map to what query.
textobjects = {
select = {
enable = true,
keymaps = {
-- You can use the capture groups defined in textobjects.scm
-- For example:
-- Nushell only
["aP"] = "@pipeline.outer",
["iP"] = "@pipeline.inner",
-- supported in other languages as well
["af"] = "@function.outer",
["if"] = "@function.inner",
["al"] = "@loop.outer",
["il"] = "@loop.inner",
["aC"] = "@conditional.outer",
["iC"] = "@conditional.inner",
["iS"] = "@statement.inner",
["aS"] = "@statement.outer",
}, -- keymaps
}, -- select
}, -- textobjects
}
end,
dependencies = {
-- Additional Nushell parser
{ "nushell/tree-sitter-nu", build = ":TSUpdate nu" },
},
build = ":TSUpdate",
},
The ability to add syntax highlighting can be provided by tree-sitter using nvim-treesitter (please refer to its own installation instructions).
local parser_config = require("nvim-treesitter.parsers").get_parser_configs()
parser_config.nu = {
install_info = {
url = "https://github.com/nushell/tree-sitter-nu",
files = { "src/parser.c", "src/scanner.c" },
branch = "main",
},
filetype = "nu",
}
Run :TSInstall nu
in neovim to install the above parser.
Note To get an overview of how tree-sitter is parsing nushell code, I recommend poking around with nvim-treesitter/playground.
With tree-sitter available, you can now add highlights queries to associate
highlight groups with tree-sitter nodes. Run :highlight
in neovim for a list
of highlight groups.
If you are using the lazy
package manager for neovim, you can run the
following snippet to install the highlights file and enable the highlighting:
let remote = "https://raw.githubusercontent.com/nushell/tree-sitter-nu/main/queries/nu/"
let local = (
$env.XDG_DATA_HOME?
| default ($env.HOME | path join ".local" "share")
| path join "nvim" "lazy" "nvim-treesitter" "queries" "nu"
)
let file = "highlights.scm"
mkdir $local
http get ([$remote $file] | str join "/") | save --force ($local | path join $file)
You need to run this snippet whenever the highlights change and :TSUpdate nu
whenever there is a new version of the parser.