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 content 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 = {
-- Install official queries and filetype detection
-- alternatively, see section "Install official queries only"
{ "nushell/tree-sitter-nu" },
},
build = ":TSUpdate",
},
This repository is now tracked in nvim-treesitter. Therefore, manual installation is not recommended. However, you can install this repo as a neovim plugin to get the official queries.
In this section, you will find how to test your checkout or fork of this parser in neovim. The fact that this repo is tracked in nvim-treesitter makes it a bit more tricky:
Nvim-ts is not a general purpose installer; you can shoehorn additional parsers but it's not designed for replacing tracked parsers.
Therefore, this requires multiple steps: use an own version of nvim-treesitter, override install_info
and revision
, use your own nvim-treesitter, and finally install your desired version of nushell/tree-sitter-nu
.
Clone nvim-treesitter.
In there, you need to update lua/nvim-treesitter/parsers.lua
:
list.nu = {
install_info = {
url = "local or remote path to your version",
files = { "src/parser.c", "src/scanner.c" },
branch = "your branch, if not main",
},
maintainers = { "@abhisheksingh0x558" },
}
Update lockfile.json
:
"nu": {
"revision": "your commit hash (the full hash!)"
},
Install your local, modified nvim-treesitter. Where you did the installation from the very top of this page:
{
-- from
"nvim-treesitter/nvim-treesitter",
-- either
"url to your fork, if you want to pull from a repo",
-- or
dir = "path/to/your/checked-out/tree-sitter-nu",
config = function()
-- ...
end,
}
Run :TSInstall nu
in neovim to install your parser.
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 files = ["highlights.scm" "indents.scm" "injections.scm" "textobjects.scm"]
mkdir $local
$files | par-each {|file| 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.
Note To get an overview of how tree-sitter is parsing nushell code, I recommend poking around with nvim-treesitter/playground.