Skip to content

Commit 8289f1f

Browse files
committed
feat(tree-sitter): remove external tree-sitter deps
Remove all external tree-sitter dependencies and instead use the built-in tree-sitter support in Neovim. This simplifies the setup process and reduces maintenance overhead. - Removed the `nvim-treesitter` dependency and related configuration. - Updated documentation to reflect the changes in tree-sitter usage. - Refactored code to utilize Neovim's built-in tree-sitter API. - Added a new `lua/treesitter.lua` module to handle tree-sitter related functionality. - Updated tests to work with the new tree-sitter implementation. - All tests pass locally (but had to fix local test setup; not included in this PR). - Many things are broken like (`:Gomvp`), but this is also broken in master - Also removed nvim-treesitter-textobjects dep and updated docs - `luacheck` reports a lot of warnings/errors, but they are also present in master, so I didn't fix them in this PR.
1 parent 5c741a2 commit 8289f1f

34 files changed

Lines changed: 287 additions & 452 deletions

Makefile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ localtestsetup:
3939
@test -d $(PACKER_DIR)/guihua.lua ||\
4040
git clone --depth 1 https://github.com/ray-x/guihua.lua $(PACKER_DIR)/guihua.lua
4141

42-
@test -d $(PACKER_DIR)/nvim-treesitter ||\
43-
git clone --depth 1 -b main https://github.com/nvim-treesitter/nvim-treesitter $(PACKER_DIR)/nvim-treesitter
44-
4542
@test -d $(PACKER_DIR)/go.nvim || ln -s ${shell pwd} $(PACKER_DIR)
4643

4744
nvim --headless -u lua/tests/minimal.vim -i NONE -c "TSUpdate go" -c "q"

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,19 @@ The plugin covers most features required for a gopher.
8181

8282
## Installation
8383

84-
Use your favorite package manager to install. The dependency `nvim-treesitter` **main** branch (and optionally, treesitter-objects) should be
85-
installed the first time you use it. Also Run `TSInstall go` to install the go parser if not installed yet. `sed` is
86-
recommended to run this plugin.
84+
Use your favorite package manager to install.
85+
Additionally, you need to have tree-sitter-go installed for
86+
syntax highlighting and some code generation features.
8787

8888
> [!NOTE]
89-
> The 13K stars nvim-treesitter was archived when I was refactoring the code to its main branch. This plugin no longer
90-
> requires nvim-treesitter but requires neovim 0.12 if you use master version. I do not guarantee the behavior of nvim
89+
> The 13K stars nvim-treesitter was archived when I was refactoring
90+
> the code to its main branch.
91+
>
92+
> This plugin no longer requires nvim-treesitter
93+
> but requires neovim 0.12 and
94+
> tree-sitter-go for syntax highlighting and some code generation features.
95+
>
96+
> I do not guarantee the behavior of nvim
9197
> 0.11 will still be correct. To use nvim 0.11 pls use go.nvim v0.11 release
9298
9399
### [lazy.nvim](https://github.com/folke/lazy.nvim)
@@ -98,7 +104,6 @@ recommended to run this plugin.
98104
dependencies = { -- optional packages
99105
"ray-x/guihua.lua",
100106
"neovim/nvim-lspconfig",
101-
-- { "nvim-treesitter/nvim-treesitter", branch = 'main' } -- optional for master version
102107
},
103108
opts = function()
104109

doc/advanced-setup.md

Lines changed: 57 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ Use your favorite package manager to install. The dependency treesitter main bra
2727

2828
### vim-plug
2929
```viml
30-
Plug 'nvim-treesitter/nvim-treesitter'
3130
Plug 'neovim/nvim-lspconfig'
3231
Plug 'ray-x/go.nvim'
3332
Plug 'ray-x/guihua.lua' ; required if you using treesitter main branch
@@ -38,7 +37,6 @@ Plug 'ray-x/guihua.lua' ; required if you using treesitter main branch
3837
use 'ray-x/go.nvim'
3938
use 'ray-x/guihua.lua' -- required if using treesitter main branch
4039
use 'neovim/nvim-lspconfig'
41-
use 'nvim-treesitter/nvim-treesitter'
4240
```
4341
## Default Configuration
4442

@@ -139,7 +137,6 @@ require('go').setup({
139137
dap_retries = 20, -- see dap option max_retries
140138
dap_enrich_config = nil, -- see dap option enrich_config
141139
build_tags = "tag1,tag2", -- set default build tags
142-
textobjects = true, -- enable default text objects through treesittter-text-objects
143140
test_runner = 'go', -- one of {`go`, `dlv`, `ginkgo`, `gotestsum`}
144141
verbose_tests = true, -- set to add verbose flag to tests deprecated, see '-v' option
145142
run_in_floaterm = false, -- set to true to run in a float window. :GoTermClose closes the floatterm
@@ -202,102 +199,66 @@ This will override your global `go.nvim` setup
202199

203200
## Text Object
204201

205-
I did not provide textobject support in the plugin. Please use treesitter textobject plugin. My treesitter config:
202+
I did not provide textobject support in the plugin. Please use treesitter textobject plugin.
206203

207204
```lua
208-
require "nvim-treesitter.configs".setup {
209-
incremental_selection = {
210-
enable = enable,
211-
keymaps = {
212-
-- mappings for incremental selection (visual mappings)
213-
init_selection = "gnn", -- maps in normal mode to init the node/scope selection
214-
node_incremental = "grn", -- increment to the upper named parent
215-
scope_incremental = "grc", -- increment to the upper scope (as defined in locals.scm)
216-
node_decremental = "grm" -- decrement to the previous node
217-
}
218-
},
205+
--- https://github.com/nvim-treesitter/nvim-treesitter-textobjects
206+
require("nvim-treesitter-textobjects").setup {
207+
move = {
208+
-- whether to set jumps in the jumplist
209+
set_jumps = true,
210+
},
211+
}
219212

220-
textobjects = {
221-
-- syntax-aware textobjects
222-
enable = enable,
223-
lsp_interop = {
224-
enable = enable,
225-
peek_definition_code = {
226-
["DF"] = "@function.outer",
227-
["DF"] = "@class.outer"
228-
}
229-
},
230-
keymaps = {
231-
["iL"] = {
232-
-- you can define your own textobjects directly here
233-
go = "(function_definition) @function",
234-
},
235-
-- or you use the queries from supported languages with textobjects.scm
236-
["af"] = "@function.outer",
237-
["if"] = "@function.inner",
238-
["aC"] = "@class.outer",
239-
["iC"] = "@class.inner",
240-
["ac"] = "@conditional.outer",
241-
["ic"] = "@conditional.inner",
242-
["ae"] = "@block.outer",
243-
["ie"] = "@block.inner",
244-
["al"] = "@loop.outer",
245-
["il"] = "@loop.inner",
246-
["is"] = "@statement.inner",
247-
["as"] = "@statement.outer",
248-
["ad"] = "@comment.outer",
249-
["am"] = "@call.outer",
250-
["im"] = "@call.inner"
251-
},
252-
move = {
253-
enable = enable,
254-
set_jumps = true, -- whether to set jumps in the jumplist
255-
goto_next_start = {
256-
["]m"] = "@function.outer",
257-
["]]"] = "@class.outer"
258-
},
259-
goto_next_end = {
260-
["]M"] = "@function.outer",
261-
["]["] = "@class.outer"
262-
},
263-
goto_previous_start = {
264-
["[m"] = "@function.outer",
265-
["[["] = "@class.outer"
266-
},
267-
goto_previous_end = {
268-
["[M"] = "@function.outer",
269-
["[]"] = "@class.outer"
270-
}
271-
},
272-
select = {
273-
enable = enable,
274-
keymaps = {
275-
-- You can use the capture groups defined in textobjects.scm
276-
["af"] = "@function.outer",
277-
["if"] = "@function.inner",
278-
["ac"] = "@class.outer",
279-
["ic"] = "@class.inner",
280-
-- Or you can define your own textobjects like this
281-
["iF"] = {
282-
python = "(function_definition) @function",
283-
cpp = "(function_definition) @function",
284-
c = "(function_definition) @function",
285-
java = "(method_declaration) @function",
286-
go = "(method_declaration) @function"
287-
}
288-
}
289-
},
290-
swap = {
291-
enable = enable,
292-
swap_next = {
293-
["<leader>a"] = "@parameter.inner"
294-
},
295-
swap_previous = {
296-
["<leader>A"] = "@parameter.inner"
297-
}
298-
}
299-
}
300-
}
213+
-- keymaps
214+
-- You can use the capture groups defined in `textobjects.scm`
215+
vim.keymap.set({ "n", "x", "o" }, "]m", function()
216+
require("nvim-treesitter-textobjects.move").goto_next_start("@function.outer", "textobjects")
217+
end)
218+
vim.keymap.set({ "n", "x", "o" }, "]]", function()
219+
require("nvim-treesitter-textobjects.move").goto_next_start("@class.outer", "textobjects")
220+
end)
221+
-- You can also pass a list to group multiple queries.
222+
vim.keymap.set({ "n", "x", "o" }, "]o", function()
223+
require("nvim-treesitter-textobjects.move").goto_next_start({"@loop.inner", "@loop.outer"}, "textobjects")
224+
end)
225+
-- You can also use captures from other query groups like `locals.scm` or `folds.scm`
226+
vim.keymap.set({ "n", "x", "o" }, "]s", function()
227+
require("nvim-treesitter-textobjects.move").goto_next_start("@local.scope", "locals")
228+
end)
229+
vim.keymap.set({ "n", "x", "o" }, "]z", function()
230+
require("nvim-treesitter-textobjects.move").goto_next_start("@fold", "folds")
231+
end)
232+
233+
vim.keymap.set({ "n", "x", "o" }, "]M", function()
234+
require("nvim-treesitter-textobjects.move").goto_next_end("@function.outer", "textobjects")
235+
end)
236+
vim.keymap.set({ "n", "x", "o" }, "][", function()
237+
require("nvim-treesitter-textobjects.move").goto_next_end("@class.outer", "textobjects")
238+
end)
239+
240+
vim.keymap.set({ "n", "x", "o" }, "[m", function()
241+
require("nvim-treesitter-textobjects.move").goto_previous_start("@function.outer", "textobjects")
242+
end)
243+
vim.keymap.set({ "n", "x", "o" }, "[[", function()
244+
require("nvim-treesitter-textobjects.move").goto_previous_start("@class.outer", "textobjects")
245+
end)
246+
247+
vim.keymap.set({ "n", "x", "o" }, "[M", function()
248+
require("nvim-treesitter-textobjects.move").goto_previous_end("@function.outer", "textobjects")
249+
end)
250+
vim.keymap.set({ "n", "x", "o" }, "[]", function()
251+
require("nvim-treesitter-textobjects.move").goto_previous_end("@class.outer", "textobjects")
252+
end)
253+
254+
-- Go to either the start or the end, whichever is closer.
255+
-- Use if you want more granular movements
256+
vim.keymap.set({ "n", "x", "o" }, "]d", function()
257+
require("nvim-treesitter-textobjects.move").goto_next("@conditional.outer", "textobjects")
258+
end)
259+
vim.keymap.set({ "n", "x", "o" }, "[d", function()
260+
require("nvim-treesitter-textobjects.move").goto_previous("@conditional.outer", "textobjects")
261+
end)
301262
```
302263

303264
</details>
@@ -460,7 +421,6 @@ enable the gopls. If you want to use your own gopls setup, you can set it to fal
460421
'ray-x/go.nvim',
461422
dependencies = {
462423
'ray-x/guihua.lua', -- optional
463-
'nvim-treesitter/nvim-treesitter',
464424
'neovim/nvim-lspconfig',
465425
},
466426
opts = {} -- by default lsp_cfg = false
@@ -559,7 +519,6 @@ The following vimrc will enable all features provided by go.nvim
559519
set termguicolors
560520
call plug#begin('~/.vim/plugged')
561521
Plug 'neovim/nvim-lspconfig'
562-
Plug 'nvim-treesitter/nvim-treesitter'
563522
564523
Plug 'mfussenegger/nvim-dap'
565524
Plug 'rcarriga/nvim-dap-ui'

doc/go.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,6 @@ You can setup go.nvim with following options:
733733
dap_retries = 20,
734734
dap_enrich_config = nil,
735735
build_tags = "", -- extra build tags for tests or debugger
736-
textobjects = true, -- treesitter text objects
737736
test_runner = "go", -- one of {"go", "richgo", "dlv", "ginkgo", "gotestsum"}
738737
run_in_floaterm = false, -- run commands in float window
739738
floaterm = {

init.lua

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@ local plugin_dir = vim.fn.expand('~/.local/share/nvim/site/pack/vendor/start')
55
vim.opt.rtp:append('.')
66

77
-- execute 'set rtp^=' . s:plugin_dir . '/plenary.nvim'
8-
-- execute 'set rtp^=' . s:plugin_dir . '/nvim-treesitter'
98
-- execute 'set rtp^=' . s:plugin_dir . '/nvim-lspconfig'
109
vim.opt.rtp:prepend(plugin_dir .. '/plenary.nvim')
11-
vim.opt.rtp:prepend(plugin_dir .. '/nvim-treesitter')
1210
vim.opt.rtp:prepend(plugin_dir .. '/nvim-lspconfig')
1311

1412
-- runtime! plugin/plenary.vim
15-
-- runtime! plugin/nvim-treesitter.vim
1613
-- runtime! plugin/playground.vim
1714
-- runtime! plugin/nvim-lspconfig.vim
1815
vim.cmd('runtime! plugin/plenary.vim')
19-
vim.cmd('runtime! plugin/nvim-treesitter.vim')
2016
vim.cmd('runtime! plugin/playground.vim')
2117
vim.cmd('runtime! plugin/nvim-lspconfig.vim')
2218

@@ -51,16 +47,10 @@ require('go').setup({
5147

5248
vim.lsp.enable('gopls')
5349

54-
require('nvim-treesitter').setup({
55-
-- Directory to install parsers and queries to
56-
install_dir = vim.fn.stdpath('data') .. '/site',
57-
})
58-
5950
vim.api.nvim_create_autocmd('FileType', {
6051
pattern = { 'go' },
6152
callback = function()
62-
local queries = require('nvim-treesitter.config').get_installed('queries')
63-
if not vim.tbl_contains(queries, 'go') then
53+
if not require('go.treesitter').are_parsers_installed({ 'go' }) then
6454
error('No queries for go found')
6555
end
6656
pcall(vim.treesitter.start)

lua/go.lua

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ local vfn = vim.fn
55
-- Keep this in sync with README.md
66
-- Keep this in sync with doc/go.txt
77
_GO_NVIM_CFG = {
8-
treesitter_main = false,
98
disable_defaults = false, -- true|false when true disable all default settings, user need to set all settings
109
remap_commands = {}, -- Vim commands to remap or disable, e.g. `{ GoFmt = "GoFormat", GoDoc = false }`
1110
go = 'go', -- set to go1.18beta1 if necessary
@@ -134,7 +133,6 @@ _GO_NVIM_CFG = {
134133
dap_enrich_config = nil, -- see dap option enrich_config
135134
dap_retries = 20, -- see dap option max_retries
136135
build_tags = '', --- you can provide extra build tags for tests or debugger
137-
textobjects = true, -- treesitter binding for text objects
138136
test_runner = 'go', -- one of {`go`, `richgo`, `dlv`, `ginkgo`, `gotestsum`}
139137
verbose_tests = false, -- set to add verbose flag to tests deprecated see '-v'
140138
run_in_floaterm = false, -- set to true to run in float window.
@@ -229,10 +227,6 @@ function go.setup(cfg)
229227
}
230228
end
231229

232-
-- ts master branch use nvim-treesitter.configs
233-
-- ts main branch use nvim-treesitter.config
234-
local has_ts_main = pcall(require, 'nvim-treesitter.config')
235-
_GO_NVIM_CFG.treesitter_main = has_ts_main
236230
-- legacy options
237231
if type(cfg.null_ls) == 'boolean' then
238232
vim.notify('go.nvim config: null_ls=boolean deprecated, refer to README for more info', vim.log.levels.WARN)
@@ -305,10 +299,6 @@ function go.setup(cfg)
305299
require('go.codelens').setup()
306300
end
307301

308-
if _GO_NVIM_CFG.textobjects then
309-
require('go.ts.textobjects').setup()
310-
end
311-
312302
require('go.env').setup()
313303
end, 1)
314304

lua/go/asyncmake.lua

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ local trace = util.trace
66
local getopt = require('go.alt_getopt')
77

88
local is_windows = util.is_windows()
9-
local is_git_shell = is_windows
10-
and (vim.fn.exists('$SHELL') and vim.fn.expand('$SHELL'):find('bash.exe') ~= nil)
9+
local is_git_shell = is_windows and (vim.fn.exists('$SHELL') and vim.fn.expand('$SHELL'):find('bash.exe') ~= nil)
1110

1211
local function compile_efm()
1312
local efm = [[%-G#\ %.%#]]
@@ -65,10 +64,7 @@ function M.make(...)
6564
end
6665
end
6766
if vim.fn.empty(makeprg) == 0 and args[1] == 'go' then
68-
vim.notify(
69-
'makeprg is already set to ' .. makeprg .. ' args: ' .. vim.inspect(args),
70-
vim.log.levels.WARN
71-
)
67+
vim.notify('makeprg is already set to ' .. makeprg .. ' args: ' .. vim.inspect(args), vim.log.levels.WARN)
7268
end
7369
-- local indent = "%\\%( %\\)"
7470
if not makeprg then
@@ -286,7 +282,7 @@ M.runjob = function(cmd, runner, args, efm)
286282
end
287283
if next(errorlines) ~= nil and runner == 'golangci-lint' then
288284
efm =
289-
[[level=%tarning\ msg="%m:\ [%f:%l:%c:\ %.%#]",level=%tarning\ msg="%m",level=%trror\ msg="%m:\ [%f:%l:%c:\ %.%#]",level=%trror\ msg="%m",%f:%l:%c:\ %m,%f:%l:\ %m,%f:%l\ %m]]
285+
[[level=%tarning\ msg="%m:\ [%f:%l:%c:\ %.%#]",level=%tarning\ msg="%m",level=%trror\ msg="%m:\ [%f:%l:%c:\ %.%#]",level=%trror\ msg="%m",%f:%l:%c:\ %m,%f:%l:\ %m,%f:%l\ %m]]
290286
end
291287

292288
sprite.on_close()

lua/go/coverage.lua

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,7 @@ M.run = function(...)
327327
else
328328
table.remove(args, 1)
329329
local test_coverage = M.read_cov(covfn)
330-
vim.notify(string.format('total coverage: %d%%', test_coverage.total_covered / test_coverage.total_lines * 100),
331-
vim.log.levels.INFO)
330+
vim.notify(string.format('total coverage: %d%%', test_coverage.total_covered / test_coverage.total_lines * 100), vim.log.levels.INFO)
332331
return test_coverage
333332
end
334333
arg = select(2, ...)
@@ -431,15 +430,15 @@ M.run = function(...)
431430

432431
vim.notify(
433432
'go coverage finished with message: '
434-
.. vim.inspect(cmd)
435-
.. 'error: '
436-
.. vim.inspect(data)
437-
.. '\n'
438-
.. 'job '
439-
.. tostring(job_id)
440-
.. '\n'
441-
.. 'ev '
442-
.. event,
433+
.. vim.inspect(cmd)
434+
.. 'error: '
435+
.. vim.inspect(data)
436+
.. '\n'
437+
.. 'job '
438+
.. tostring(job_id)
439+
.. '\n'
440+
.. 'ev '
441+
.. event,
443442
vim.log.levels.WARN
444443
)
445444
end,

0 commit comments

Comments
 (0)