From 5cf30dc2edb64c8e07fb74a628b02bd27c85905a Mon Sep 17 00:00:00 2001 From: mrdgo Date: Thu, 21 Nov 2024 16:22:53 +0100 Subject: [PATCH 1/4] chore: migrate to new tree-sitter.json --- package.json | 12 +----------- tree-sitter.json | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 tree-sitter.json diff --git a/package.json b/package.json index 5c6f536..d6a2654 100644 --- a/package.json +++ b/package.json @@ -29,16 +29,6 @@ "tree-sitter-cli": "^0.22.5", "prebuildify": "^6.0.0" }, - "tree-sitter": [ - { - "scope": "source.nu", - "file-types": [ - "nu" - ], - "highlights": "queries/nu/highlights.scm", - "injections": "queries/nu/injections.scm" - } - ], "files": [ "grammar.js", "binding.gyp", @@ -47,4 +37,4 @@ "queries/*", "src/**" ] -} +} \ No newline at end of file diff --git a/tree-sitter.json b/tree-sitter.json new file mode 100644 index 0000000..9df0dd6 --- /dev/null +++ b/tree-sitter.json @@ -0,0 +1,36 @@ +{ + "grammars": [ + { + "name": "nu", + "camelcase": "Nu", + "scope": "source.nu", + "path": ".", + "file-types": [ + "nu" + ], + "highlights": "queries/nu/highlights.scm", + "injections": "queries/nu/injections.scm" + } + ], + "metadata": { + "version": "0.0.1", + "license": "MIT", + "description": "nu-lang", + "authors": [ + { + "name": "The Nushell Contributors" + } + ], + "links": { + "repository": "https://github.com/tree-sitter/tree-sitter-nu" + } + }, + "bindings": { + "c": true, + "go": true, + "node": true, + "python": true, + "rust": true, + "swift": true + } +} \ No newline at end of file From 7e58b4cdb7063b729d81e3313c438049f82acd4a Mon Sep 17 00:00:00 2001 From: mrdgo Date: Tue, 10 Dec 2024 14:55:59 +0100 Subject: [PATCH 2/4] fix: neovim installation Since someone added `tree-sitter-nu` to `nvim-treesitter`, this code is no longer needed. --- plugin/init.lua | 9 --------- 1 file changed, 9 deletions(-) diff --git a/plugin/init.lua b/plugin/init.lua index 5adbf9f..57a8e3a 100644 --- a/plugin/init.lua +++ b/plugin/init.lua @@ -6,12 +6,3 @@ vim.api.nvim_create_autocmd("FileType", { vim.bo[event.buf].commentstring = "# %s" end, }) - -require("nvim-treesitter.parsers").get_parser_configs().nu = { - install_info = { - url = "https://github.com/nushell/tree-sitter-nu", - files = { "src/parser.c", "src/scanner.c" }, - branch = "main", - }, - filetype = "nu", -} From a6c8b7e3a57814077803d698426d3dc87ce1ac73 Mon Sep 17 00:00:00 2001 From: mrdgo Date: Tue, 10 Dec 2024 14:56:31 +0100 Subject: [PATCH 3/4] fix: typo --- installation/neovim.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/installation/neovim.md b/installation/neovim.md index dd11526..b57f7b1 100644 --- a/installation/neovim.md +++ b/installation/neovim.md @@ -15,7 +15,7 @@ Extend your lazy config with treesitter and the nu parser. The parser doesn't ha }, -- 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, + -- `dif` to clear the content of the closest function, -- or whatever keys you map to what query. textobjects = { select = { From a642630337b2706d5ffa2bad742b3300ff018c50 Mon Sep 17 00:00:00 2001 From: mrdgo Date: Tue, 10 Dec 2024 15:43:53 +0100 Subject: [PATCH 4/4] docs: overhaul neovim installation process --- installation/neovim.md | 84 +++++++++++++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 17 deletions(-) diff --git a/installation/neovim.md b/installation/neovim.md index b57f7b1..2879092 100644 --- a/installation/neovim.md +++ b/installation/neovim.md @@ -42,8 +42,9 @@ Extend your lazy config with treesitter and the nu parser. The parser doesn't ha } end, dependencies = { - -- Additional Nushell parser - { "nushell/tree-sitter-nu", build = ":TSUpdate nu" }, + -- Install official queries and filetype detection + -- alternatively, see section "Install official queries only" + { "nushell/tree-sitter-nu" }, }, build = ":TSUpdate", }, @@ -51,27 +52,71 @@ Extend your lazy config with treesitter and the nu parser. The parser doesn't ha ## Manual installation -The ability to add syntax highlighting can be provided by [tree-sitter] using -[nvim-treesitter] (please refer to its own installation instructions). +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. -```lua -local parser_config = require("nvim-treesitter.parsers").get_parser_configs() +## Use your own version of ts-nu + +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`. + +### Step 1 -parser_config.nu = { +Clone [nvim-treesitter]. + +### Step 2 + +In there, you need to update `lua/nvim-treesitter/parsers.lua`: + +```lua +list.nu = { install_info = { - url = "https://github.com/nushell/tree-sitter-nu", + url = "local or remote path to your version", files = { "src/parser.c", "src/scanner.c" }, - branch = "main", + branch = "your branch, if not main", }, - filetype = "nu", + maintainers = { "@abhisheksingh0x558" }, } ``` -Run `:TSInstall nu` in neovim to install the above parser. +### Step 3 -> **Note** -> To get an overview of how [tree-sitter] is parsing [nushell] code, I recommend -> poking around with [nvim-treesitter/playground]. +Update `lockfile.json`: + +```json + "nu": { + "revision": "your commit hash (the full hash!)" + }, +``` + +### Step 4 + +Install your local, modified [nvim-treesitter]. Where you did the installation from the very top of this page: + +```lua +{ + -- 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, +} +``` + +### Step 5 + +Run `:TSInstall nu` in neovim to install your parser. + +## Install official queries only 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 @@ -80,7 +125,7 @@ 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: -```nushell +```nu let remote = "https://raw.githubusercontent.com/nushell/tree-sitter-nu/main/queries/nu/" let local = ( $env.XDG_DATA_HOME? @@ -88,14 +133,19 @@ let local = ( | path join "nvim" "lazy" "nvim-treesitter" "queries" "nu" ) -let file = "highlights.scm" +let files = ["highlights.scm" "indents.scm" "injections.scm" "textobjects.scm"] mkdir $local -http get ([$remote $file] | str join "/") | save --force ($local | path join $file) +$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]. + + [tree-sitter]: https://tree-sitter.github.io/tree-sitter/ [nvim-treesitter]: https://github.com/nvim-treesitter/nvim-treesitter [nvim-treesitter/playground]: https://github.com/nvim-treesitter/playground