From cff926303d0c90bde9e2046c27419c8269476c7c Mon Sep 17 00:00:00 2001 From: Marco Kellershoff Date: Fri, 22 May 2026 14:41:17 +0200 Subject: [PATCH] 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. --- Makefile | 3 - README.md | 17 ++-- doc/advanced-setup.md | 155 ++++++++++++------------------- doc/go.txt | 1 - init.lua | 12 +-- lua/go.lua | 10 -- lua/go/asyncmake.lua | 10 +- lua/go/coverage.lua | 21 ++--- lua/go/fixplurals.lua | 6 +- lua/go/gopls_impl.lua | 6 +- lua/go/gotest.lua | 3 +- lua/go/health.lua | 24 +---- lua/go/mcp/context.lua | 12 +-- lua/go/snips.lua | 2 +- lua/go/treesitter.lua | 153 ++++++++++++++++++++++++++++++ lua/go/ts/go.lua | 24 ++--- lua/go/ts/nodes.lua | 2 - lua/go/ts/textobjects.lua | 51 ---------- lua/go/ts/utils.lua | 16 ++-- lua/tests/go_commands_spec.lua | 6 +- lua/tests/go_comment_spec.lua | 10 +- lua/tests/go_fixplurals_spec.lua | 3 +- lua/tests/go_impl_spec.lua | 7 +- lua/tests/go_mockgen_spec.lua | 5 +- lua/tests/go_runner_spec.lua | 5 +- lua/tests/go_tags_spec.lua | 4 +- lua/tests/go_test_spec.lua | 13 +-- lua/tests/go_ts_node_spec.lua | 3 - lua/tests/init.lua | 8 -- lua/tests/init.vim | 11 --- lua/tests/install-parsers.lua | 124 ++----------------------- lua/tests/minimal.lua | 7 -- lua/tests/minimal.vim | 2 - samplevimrc.vim | 3 - 34 files changed, 287 insertions(+), 452 deletions(-) create mode 100644 lua/go/treesitter.lua delete mode 100644 lua/go/ts/textobjects.lua diff --git a/Makefile b/Makefile index fe4647bec..71f535d12 100644 --- a/Makefile +++ b/Makefile @@ -39,9 +39,6 @@ localtestsetup: @test -d $(PACKER_DIR)/guihua.lua ||\ git clone --depth 1 https://github.com/ray-x/guihua.lua $(PACKER_DIR)/guihua.lua - @test -d $(PACKER_DIR)/nvim-treesitter ||\ - git clone --depth 1 -b main https://github.com/nvim-treesitter/nvim-treesitter $(PACKER_DIR)/nvim-treesitter - @test -d $(PACKER_DIR)/go.nvim || ln -s ${shell pwd} $(PACKER_DIR) nvim --headless -u lua/tests/minimal.vim -i NONE -c "TSUpdate go" -c "q" diff --git a/README.md b/README.md index b9484f142..4989e7af7 100644 --- a/README.md +++ b/README.md @@ -81,13 +81,19 @@ The plugin covers most features required for a gopher. ## Installation -Use your favorite package manager to install. The dependency `nvim-treesitter` **main** branch (and optionally, treesitter-objects) should be -installed the first time you use it. Also Run `TSInstall go` to install the go parser if not installed yet. `sed` is -recommended to run this plugin. +Use your favorite package manager to install. +Additionally, you need to have tree-sitter-go installed for +syntax highlighting and some code generation features. > [!NOTE] -> The 13K stars nvim-treesitter was archived when I was refactoring the code to its main branch. This plugin no longer -> requires nvim-treesitter but requires neovim 0.12 if you use master version. I do not guarantee the behavior of nvim +> The 13K stars nvim-treesitter was archived when I was refactoring +> the code to its main branch. +> +> This plugin no longer requires nvim-treesitter +> but requires neovim 0.12 and +> tree-sitter-go for syntax highlighting and some code generation features. +> +> I do not guarantee the behavior of nvim > 0.11 will still be correct. To use nvim 0.11 pls use go.nvim v0.11 release ### [lazy.nvim](https://github.com/folke/lazy.nvim) @@ -98,7 +104,6 @@ recommended to run this plugin. dependencies = { -- optional packages "ray-x/guihua.lua", "neovim/nvim-lspconfig", - -- { "nvim-treesitter/nvim-treesitter", branch = 'main' } -- optional for master version }, opts = function() diff --git a/doc/advanced-setup.md b/doc/advanced-setup.md index 8d11ab2b2..47f23cf73 100644 --- a/doc/advanced-setup.md +++ b/doc/advanced-setup.md @@ -27,7 +27,6 @@ Use your favorite package manager to install. The dependency treesitter main bra ### vim-plug ```viml -Plug 'nvim-treesitter/nvim-treesitter' Plug 'neovim/nvim-lspconfig' Plug 'ray-x/go.nvim' 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 use 'ray-x/go.nvim' use 'ray-x/guihua.lua' -- required if using treesitter main branch use 'neovim/nvim-lspconfig' -use 'nvim-treesitter/nvim-treesitter' ``` ## Default Configuration @@ -139,7 +137,6 @@ require('go').setup({ dap_retries = 20, -- see dap option max_retries dap_enrich_config = nil, -- see dap option enrich_config build_tags = "tag1,tag2", -- set default build tags - textobjects = true, -- enable default text objects through treesittter-text-objects test_runner = 'go', -- one of {`go`, `dlv`, `ginkgo`, `gotestsum`} verbose_tests = true, -- set to add verbose flag to tests deprecated, see '-v' option 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 ## Text Object -I did not provide textobject support in the plugin. Please use treesitter textobject plugin. My treesitter config: +I did not provide textobject support in the plugin. Please use treesitter textobject plugin. ```lua - require "nvim-treesitter.configs".setup { - incremental_selection = { - enable = enable, - keymaps = { - -- mappings for incremental selection (visual mappings) - init_selection = "gnn", -- maps in normal mode to init the node/scope selection - node_incremental = "grn", -- increment to the upper named parent - scope_incremental = "grc", -- increment to the upper scope (as defined in locals.scm) - node_decremental = "grm" -- decrement to the previous node - } - }, +--- https://github.com/nvim-treesitter/nvim-treesitter-textobjects +require("nvim-treesitter-textobjects").setup { + move = { + -- whether to set jumps in the jumplist + set_jumps = true, + }, +} - textobjects = { - -- syntax-aware textobjects - enable = enable, - lsp_interop = { - enable = enable, - peek_definition_code = { - ["DF"] = "@function.outer", - ["DF"] = "@class.outer" - } - }, - keymaps = { - ["iL"] = { - -- you can define your own textobjects directly here - go = "(function_definition) @function", - }, - -- or you use the queries from supported languages with textobjects.scm - ["af"] = "@function.outer", - ["if"] = "@function.inner", - ["aC"] = "@class.outer", - ["iC"] = "@class.inner", - ["ac"] = "@conditional.outer", - ["ic"] = "@conditional.inner", - ["ae"] = "@block.outer", - ["ie"] = "@block.inner", - ["al"] = "@loop.outer", - ["il"] = "@loop.inner", - ["is"] = "@statement.inner", - ["as"] = "@statement.outer", - ["ad"] = "@comment.outer", - ["am"] = "@call.outer", - ["im"] = "@call.inner" - }, - move = { - enable = enable, - set_jumps = true, -- whether to set jumps in the jumplist - goto_next_start = { - ["]m"] = "@function.outer", - ["]]"] = "@class.outer" - }, - goto_next_end = { - ["]M"] = "@function.outer", - ["]["] = "@class.outer" - }, - goto_previous_start = { - ["[m"] = "@function.outer", - ["[["] = "@class.outer" - }, - goto_previous_end = { - ["[M"] = "@function.outer", - ["[]"] = "@class.outer" - } - }, - select = { - enable = enable, - keymaps = { - -- You can use the capture groups defined in textobjects.scm - ["af"] = "@function.outer", - ["if"] = "@function.inner", - ["ac"] = "@class.outer", - ["ic"] = "@class.inner", - -- Or you can define your own textobjects like this - ["iF"] = { - python = "(function_definition) @function", - cpp = "(function_definition) @function", - c = "(function_definition) @function", - java = "(method_declaration) @function", - go = "(method_declaration) @function" - } - } - }, - swap = { - enable = enable, - swap_next = { - ["a"] = "@parameter.inner" - }, - swap_previous = { - ["A"] = "@parameter.inner" - } - } - } - } +-- keymaps +-- You can use the capture groups defined in `textobjects.scm` +vim.keymap.set({ "n", "x", "o" }, "]m", function() + require("nvim-treesitter-textobjects.move").goto_next_start("@function.outer", "textobjects") +end) +vim.keymap.set({ "n", "x", "o" }, "]]", function() + require("nvim-treesitter-textobjects.move").goto_next_start("@class.outer", "textobjects") +end) +-- You can also pass a list to group multiple queries. +vim.keymap.set({ "n", "x", "o" }, "]o", function() + require("nvim-treesitter-textobjects.move").goto_next_start({"@loop.inner", "@loop.outer"}, "textobjects") +end) +-- You can also use captures from other query groups like `locals.scm` or `folds.scm` +vim.keymap.set({ "n", "x", "o" }, "]s", function() + require("nvim-treesitter-textobjects.move").goto_next_start("@local.scope", "locals") +end) +vim.keymap.set({ "n", "x", "o" }, "]z", function() + require("nvim-treesitter-textobjects.move").goto_next_start("@fold", "folds") +end) + +vim.keymap.set({ "n", "x", "o" }, "]M", function() + require("nvim-treesitter-textobjects.move").goto_next_end("@function.outer", "textobjects") +end) +vim.keymap.set({ "n", "x", "o" }, "][", function() + require("nvim-treesitter-textobjects.move").goto_next_end("@class.outer", "textobjects") +end) + +vim.keymap.set({ "n", "x", "o" }, "[m", function() + require("nvim-treesitter-textobjects.move").goto_previous_start("@function.outer", "textobjects") +end) +vim.keymap.set({ "n", "x", "o" }, "[[", function() + require("nvim-treesitter-textobjects.move").goto_previous_start("@class.outer", "textobjects") +end) + +vim.keymap.set({ "n", "x", "o" }, "[M", function() + require("nvim-treesitter-textobjects.move").goto_previous_end("@function.outer", "textobjects") +end) +vim.keymap.set({ "n", "x", "o" }, "[]", function() + require("nvim-treesitter-textobjects.move").goto_previous_end("@class.outer", "textobjects") +end) + +-- Go to either the start or the end, whichever is closer. +-- Use if you want more granular movements +vim.keymap.set({ "n", "x", "o" }, "]d", function() + require("nvim-treesitter-textobjects.move").goto_next("@conditional.outer", "textobjects") +end) +vim.keymap.set({ "n", "x", "o" }, "[d", function() + require("nvim-treesitter-textobjects.move").goto_previous("@conditional.outer", "textobjects") +end) ``` @@ -460,7 +421,6 @@ enable the gopls. If you want to use your own gopls setup, you can set it to fal 'ray-x/go.nvim', dependencies = { 'ray-x/guihua.lua', -- optional - 'nvim-treesitter/nvim-treesitter', 'neovim/nvim-lspconfig', }, opts = {} -- by default lsp_cfg = false @@ -559,7 +519,6 @@ The following vimrc will enable all features provided by go.nvim set termguicolors call plug#begin('~/.vim/plugged') Plug 'neovim/nvim-lspconfig' -Plug 'nvim-treesitter/nvim-treesitter' Plug 'mfussenegger/nvim-dap' Plug 'rcarriga/nvim-dap-ui' diff --git a/doc/go.txt b/doc/go.txt index e3ff5eb4b..904a2e952 100644 --- a/doc/go.txt +++ b/doc/go.txt @@ -733,7 +733,6 @@ You can setup go.nvim with following options: dap_retries = 20, dap_enrich_config = nil, build_tags = "", -- extra build tags for tests or debugger - textobjects = true, -- treesitter text objects test_runner = "go", -- one of {"go", "richgo", "dlv", "ginkgo", "gotestsum"} run_in_floaterm = false, -- run commands in float window floaterm = { diff --git a/init.lua b/init.lua index c7032eeb6..97a772ae8 100644 --- a/init.lua +++ b/init.lua @@ -5,18 +5,14 @@ local plugin_dir = vim.fn.expand('~/.local/share/nvim/site/pack/vendor/start') vim.opt.rtp:append('.') -- execute 'set rtp^=' . s:plugin_dir . '/plenary.nvim' --- execute 'set rtp^=' . s:plugin_dir . '/nvim-treesitter' -- execute 'set rtp^=' . s:plugin_dir . '/nvim-lspconfig' vim.opt.rtp:prepend(plugin_dir .. '/plenary.nvim') -vim.opt.rtp:prepend(plugin_dir .. '/nvim-treesitter') vim.opt.rtp:prepend(plugin_dir .. '/nvim-lspconfig') -- runtime! plugin/plenary.vim --- runtime! plugin/nvim-treesitter.vim -- runtime! plugin/playground.vim -- runtime! plugin/nvim-lspconfig.vim vim.cmd('runtime! plugin/plenary.vim') -vim.cmd('runtime! plugin/nvim-treesitter.vim') vim.cmd('runtime! plugin/playground.vim') vim.cmd('runtime! plugin/nvim-lspconfig.vim') @@ -51,16 +47,10 @@ require('go').setup({ vim.lsp.enable('gopls') -require('nvim-treesitter').setup({ - -- Directory to install parsers and queries to - install_dir = vim.fn.stdpath('data') .. '/site', -}) - vim.api.nvim_create_autocmd('FileType', { pattern = { 'go' }, callback = function() - local queries = require('nvim-treesitter.config').get_installed('queries') - if not vim.tbl_contains(queries, 'go') then + if not require('go.treesitter').are_parsers_installed({ 'go' }) then error('No queries for go found') end pcall(vim.treesitter.start) diff --git a/lua/go.lua b/lua/go.lua index 00a9edfa8..901fd1654 100644 --- a/lua/go.lua +++ b/lua/go.lua @@ -5,7 +5,6 @@ local vfn = vim.fn -- Keep this in sync with README.md -- Keep this in sync with doc/go.txt _GO_NVIM_CFG = { - treesitter_main = false, disable_defaults = false, -- true|false when true disable all default settings, user need to set all settings remap_commands = {}, -- Vim commands to remap or disable, e.g. `{ GoFmt = "GoFormat", GoDoc = false }` go = 'go', -- set to go1.18beta1 if necessary @@ -134,7 +133,6 @@ _GO_NVIM_CFG = { dap_enrich_config = nil, -- see dap option enrich_config dap_retries = 20, -- see dap option max_retries build_tags = '', --- you can provide extra build tags for tests or debugger - textobjects = true, -- treesitter binding for text objects test_runner = 'go', -- one of {`go`, `richgo`, `dlv`, `ginkgo`, `gotestsum`} verbose_tests = false, -- set to add verbose flag to tests deprecated see '-v' run_in_floaterm = false, -- set to true to run in float window. @@ -229,10 +227,6 @@ function go.setup(cfg) } end - -- ts master branch use nvim-treesitter.configs - -- ts main branch use nvim-treesitter.config - local has_ts_main = pcall(require, 'nvim-treesitter.config') - _GO_NVIM_CFG.treesitter_main = has_ts_main -- legacy options if type(cfg.null_ls) == 'boolean' then 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) require('go.codelens').setup() end - if _GO_NVIM_CFG.textobjects then - require('go.ts.textobjects').setup() - end - require('go.env').setup() end, 1) diff --git a/lua/go/asyncmake.lua b/lua/go/asyncmake.lua index 8ce0f1d77..aa5842561 100644 --- a/lua/go/asyncmake.lua +++ b/lua/go/asyncmake.lua @@ -6,8 +6,7 @@ local trace = util.trace local getopt = require('go.alt_getopt') local is_windows = util.is_windows() -local is_git_shell = is_windows - and (vim.fn.exists('$SHELL') and vim.fn.expand('$SHELL'):find('bash.exe') ~= nil) +local is_git_shell = is_windows and (vim.fn.exists('$SHELL') and vim.fn.expand('$SHELL'):find('bash.exe') ~= nil) local function compile_efm() local efm = [[%-G#\ %.%#]] @@ -65,10 +64,7 @@ function M.make(...) end end if vim.fn.empty(makeprg) == 0 and args[1] == 'go' then - vim.notify( - 'makeprg is already set to ' .. makeprg .. ' args: ' .. vim.inspect(args), - vim.log.levels.WARN - ) + vim.notify('makeprg is already set to ' .. makeprg .. ' args: ' .. vim.inspect(args), vim.log.levels.WARN) end -- local indent = "%\\%( %\\)" if not makeprg then @@ -286,7 +282,7 @@ M.runjob = function(cmd, runner, args, efm) end if next(errorlines) ~= nil and runner == 'golangci-lint' then efm = - [[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]] + [[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]] end sprite.on_close() diff --git a/lua/go/coverage.lua b/lua/go/coverage.lua index df0b078fb..df44bbfe9 100644 --- a/lua/go/coverage.lua +++ b/lua/go/coverage.lua @@ -327,8 +327,7 @@ M.run = function(...) else table.remove(args, 1) local test_coverage = M.read_cov(covfn) - vim.notify(string.format('total coverage: %d%%', test_coverage.total_covered / test_coverage.total_lines * 100), - vim.log.levels.INFO) + vim.notify(string.format('total coverage: %d%%', test_coverage.total_covered / test_coverage.total_lines * 100), vim.log.levels.INFO) return test_coverage end arg = select(2, ...) @@ -431,15 +430,15 @@ M.run = function(...) vim.notify( 'go coverage finished with message: ' - .. vim.inspect(cmd) - .. 'error: ' - .. vim.inspect(data) - .. '\n' - .. 'job ' - .. tostring(job_id) - .. '\n' - .. 'ev ' - .. event, + .. vim.inspect(cmd) + .. 'error: ' + .. vim.inspect(data) + .. '\n' + .. 'job ' + .. tostring(job_id) + .. '\n' + .. 'ev ' + .. event, vim.log.levels.WARN ) end, diff --git a/lua/go/fixplurals.lua b/lua/go/fixplurals.lua index 27d2ae58b..7d506253a 100644 --- a/lua/go/fixplurals.lua +++ b/lua/go/fixplurals.lua @@ -1,9 +1,5 @@ -- lua implementation of the fixplurals -local ok, ts_utils = pcall(require, 'nvim-treesitter.ts_utils') -if not ok then - ts_utils = require('guihua.ts_obsolete.ts_utils') -end local info = require('go.utils').info local get_node_text = vim.treesitter.get_node_text local function fixplurals() @@ -27,7 +23,7 @@ local function fixplurals() local type_node2 = next_node:named_child(1) local type_next = get_node_text(type_node2, 0) if type == type_next then - local range1 = ts_utils.node_to_lsp_range(p:named_child(1)) + local range1 = vim.treesitter.get_node_range(p:named_child(1)) range1['start']['character'] = range1['start']['character'] - 1 local edit1 = { range = range1, newText = '' } table.insert(edits, 1, edit1) diff --git a/lua/go/gopls_impl.lua b/lua/go/gopls_impl.lua index d3e97f398..894e7e6a3 100644 --- a/lua/go/gopls_impl.lua +++ b/lua/go/gopls_impl.lua @@ -14,8 +14,8 @@ M.config = { prefix_highlight = 'Comment', separator = ', ', highlight = 'Constant', - loadfile = true, -- should we load the implementations file and get details - debounce = 1000, -- delay in ms + loadfile = true, -- should we load the implementations file and get details + debounce = 1000, -- delay in ms virt_text_pos = nil, -- default to eol autocmd = { 'BufEnter', 'TextChanged', 'CursorMoved', 'CursorHold' }, } @@ -72,7 +72,7 @@ local function show_virtual_text(bufnr, line, implementations) local virtual_text_opts = { virt_text = { { M.config.prefix, M.config.prefix_highlight }, - { text, M.config.highlight }, + { text, M.config.highlight }, }, } if M.config.virt_text_pos then diff --git a/lua/go/gotest.lua b/lua/go/gotest.lua index 979ada9c5..2d1a3cfad 100644 --- a/lua/go/gotest.lua +++ b/lua/go/gotest.lua @@ -674,10 +674,9 @@ M.run_file = function() return log('no ts parser found') end local tree = parser:parse()[1] - local query = parse('go', require('go.ts.textobjects').query_test_func) + local query = parse('go', vim.treesitter.query.get('go', 'test_func')) local test_names = {} - local get_node_text = vim.treesitter.get_node_text for id, node in query:iter_captures(tree:root(), bufnr, 0, -1) do local name = query.captures[id] -- name of the capture in the query if name == 'test_name' then diff --git a/lua/go/health.lua b/lua/go/health.lua index e22f1d7e5..15e7bff95 100644 --- a/lua/go/health.lua +++ b/lua/go/health.lua @@ -128,39 +128,25 @@ local function plugin_check() local plugins = { 'lspconfig', - -- 'nvim-treesitter', 'guihua', 'nvim-dap-virtual-text', 'telescope', } local any_warn = false - local ts_installed = false for _, plugin in ipairs(plugins) do local pi = util.load_plugin(plugin) if pi ~= nil then ok(string.format('%s: plugin is installed', plugin)) - if plugin == 'nvim-treesitter' then - ts_installed = true - end else any_warn = true warn(string.format('%s: not installed/loaded', plugin)) end end - if ts_installed then - local has_ts_main = pcall(require, 'nvim-treesitter.config') - if has_ts_main then - any_warn = false - warn('nvim-treesitter main module loaded, WIP') - else - local _info = require('nvim-treesitter.info').installed_parsers() - if vim.tbl_contains(_info, 'go') then - ok('nvim-treesitter-go is installed') - else - warn('nvim-treesitter-go is not installed, Please run TSInstall go to install') - any_warn = true - end - end + if require('go.treesitter').are_parsers_installed({ 'go' }) then + ok('nvim-treesitter-go is installed') + else + warn('nvim-treesitter-go is not installed, Please install go treesitter parser and queries.') + any_warn = true end plugins = { ['nvim-dap'] = 'dap', diff --git a/lua/go/mcp/context.lua b/lua/go/mcp/context.lua index 61ba34728..abc575d3f 100644 --- a/lua/go/mcp/context.lua +++ b/lua/go/mcp/context.lua @@ -259,9 +259,7 @@ function M.get_symbol_context_via_lsp(bufnr, line, col, callback) local max_callers = 15 local callers = {} for _, call in ipairs(non_test_calls) do - table.insert(callers, format_caller_location( - call.from.uri, call.from.range.start.line, call.from.name - )) + table.insert(callers, format_caller_location(call.from.uri, call.from.range.start.line, call.from.name)) if #callers >= max_callers then break end @@ -273,9 +271,7 @@ function M.get_symbol_context_via_lsp(bufnr, line, col, callback) table.insert(callers, string.format(' ... and %d more test callers', skipped)) break end - table.insert(callers, format_caller_location( - call.from.uri, call.from.range.start.line, call.from.name - )) + table.insert(callers, format_caller_location(call.from.uri, call.from.range.start.line, call.from.name)) end table.insert(results, '\n* Callers (' .. #calls .. '):\n' .. table.concat(callers, '\n')) end @@ -351,8 +347,7 @@ function M.gather_diff_context(diff_text, callback) local symbol_list = vim.tbl_values(symbols) if #symbol_list == 0 then - table.insert(all_context, - string.format('## File: %s\n(changed lines do not contain function/type declarations)', file)) + table.insert(all_context, string.format('## File: %s\n(changed lines do not contain function/type declarations)', file)) files_pending = files_pending - 1 if files_pending == 0 then callback(table.concat(all_context, '\n\n')) @@ -428,4 +423,3 @@ function M.gather_buffer_context(bufnr, callback) end return M - diff --git a/lua/go/snips.lua b/lua/go/snips.lua index 3ad77b284..3e6c0bc2f 100644 --- a/lua/go/snips.lua +++ b/lua/go/snips.lua @@ -142,7 +142,7 @@ end local function return_value_nodes(info) set_query() local cursor_node = vim.treesitter.get_node({ bufnr = 0 }) - local scope_tree = ts_locals.get_scope_tree(cursor_node, 0) + local scope_tree = vim.treesitter.get_scope_tree(cursor_node, 0) local function_node for _, scope in ipairs(scope_tree) do diff --git a/lua/go/treesitter.lua b/lua/go/treesitter.lua new file mode 100644 index 000000000..55594c25d --- /dev/null +++ b/lua/go/treesitter.lua @@ -0,0 +1,153 @@ +local M = {} +local treesitter_go_url = 'https://github.com/tree-sitter/tree-sitter-go' +local treesitter_queries_go_url = 'https://github.com/neovim-treesitter/nvim-treesitter-queries-go' +local install_dir = vim.fn.stdpath('data') .. '/site' + +---Check if the given parsers are installed +---@param parsers string[] List of parser names to check +---@return boolean result if all parsers are installed, false otherwise +---@return nil|string[] List of missing parsers if any +M.are_parsers_installed = function(parsers) + -- Read from site/parsers/*.{so,dylib,dll} to get the list of installed parsers + -- and remove the path and extension to get the parser names + local installed_parsers = vim.fn.globpath(vim.fn.stdpath('data') .. '/site/parser', '*.{so,dylib,dll}', true, true) + local missing_parsers = {} + for i, parser in ipairs(installed_parsers) do + installed_parsers[i] = vim.fn.fnamemodify(parser, ':t:r') + end + for _, parser in ipairs(parsers) do + if not vim.list_contains(installed_parsers, parser) then + table.insert(missing_parsers, parser) + end + end + if #missing_parsers > 0 then + return false, missing_parsers + end + return true, nil +end + +local download_and_install_grammar = function() + -- get tmp dir + local tmp_dir = vim.fn.tempname() + local dl_dir = vim.fs.joinpath(tmp_dir, 'tree-sitter-queries-go') + + if vim.fn.isdirectory(dl_dir) == 1 then + -- Clean up any existing build + if vim.fn.has('win32') == 1 then + vim.fn.system('rmdir /s /q ' .. dl_dir) + else + vim.fn.system('rm -rf ' .. dl_dir) + end + end + + -- Clone the tree-sitter-go repository + print('Cloning tree-sitter-queries-go...') + local clone_result = vim.fn.system(string.format('git clone --depth 1 ' .. treesitter_queries_go_url .. ' "%s" 2>&1', dl_dir)) + print(clone_result) + + if vim.fn.isdirectory(vim.fs.joinpath(dl_dir, 'queries')) ~= 1 then + print('ERROR: Failed to clone tree-sitter-queries-go') + return false + end + + local system_calls = {} + if vim.fn.has('win32') == 1 then + system_calls[#system_calls + 1] = { + description = 'Creating queries directory', + cmd = string.format('mkdir "%s\\queries\\go"', install_dir), + } + system_calls[#system_calls + 1] = { + description = 'Copying queries', + cmd = string.format('xcopy "%s\\queries" "%s\\queries\\go" /E /I /Y', dl_dir, install_dir), + } + else + system_calls[#system_calls + 1] = { + description = 'Creating queries directory', + cmd = string.format('mkdir -p "%s/queries/go"', install_dir), + } + system_calls[#system_calls + 1] = { + description = 'Copying queries', + cmd = string.format('cp -r "%s/queries" "%s/queries/go"', dl_dir, install_dir), + } + end + for _, call in ipairs(system_calls) do + print(call.description .. '...') + local result = vim.fn.system(call.cmd .. ' 2>&1') + print(result) + if vim.v.shell_error ~= 0 then + print('ERROR: ' .. call.description .. ' failed') + return false + end + end +end + +local download_compile_and_install_parser = function() + -- get tmp dir + local tmp_dir = vim.fn.tempname() + local dl_dir = vim.fs.joinpath(tmp_dir, 'tree-sitter-go') + + if vim.fn.isdirectory(dl_dir) == 1 then + -- Clean up any existing build + if vim.fn.has('win32') == 1 then + vim.fn.system('rmdir /s /q ' .. dl_dir) + else + vim.fn.system('rm -rf ' .. dl_dir) + end + end + + -- Clone the tree-sitter-go repository + print('Cloning tree-sitter-go...') + local clone_result = vim.fn.system(string.format('git clone --depth 1 ' .. treesitter_go_url .. ' "%s" 2>&1', dl_dir)) + print(clone_result) + + if vim.fn.isdirectory(dl_dir .. '/src') ~= 1 then + print('ERROR: Failed to clone tree-sitter-go') + return false + end + + -- Compile the parser + print('Compiling parser...') + local parser_c = dl_dir .. '/src/parser.c' + local scanner_c = dl_dir .. '/src/scanner.c' + local output_so = install_dir .. '/parser/go.so' + + local sources = parser_c + if vim.fn.filereadable(scanner_c) == 1 then + sources = sources .. ' ' .. scanner_c + end + + local compile_cmd = string.format('cc -o "%s" -I"%s/src" %s -shared -Os -fPIC 2>&1', output_so, dl_dir, sources) + + print('Compile command: ' .. compile_cmd) + local compile_result = vim.fn.system(compile_cmd) + print('Compile output: ' .. compile_result) + + -- Check if compilation succeeded + if vim.fn.filereadable(output_so) == 1 then + print('✓ Successfully compiled go.so') + return true + else + print('✗ Failed to compile go.so') + return false + end +end + +M.install_parsers_and_grammars = function() + local install_dir = vim.fn.stdpath('data') .. '/site' + vim.fn.mkdir(vim.fs.joinpath(install_dir, 'parser'), 'p') + vim.fn.mkdir(vim.fs.joinpath(install_dir, 'queries'), 'p') + + local success, _ = M.are_parsers_installed({ 'go' }) + if success then + return true + end + if not download_compile_and_install_parser() then + return false + end + if not download_and_install_grammar() then + return false + end + return true +end + +return M diff --git a/lua/go/ts/go.lua b/lua/go/ts/go.lua index c4bc9db4a..5f790462f 100644 --- a/lua/go/ts/go.lua +++ b/lua/go/ts/go.lua @@ -6,29 +6,24 @@ local info = require('go.utils').info local debug = require('go.utils').debug local trace = require('go.utils').trace - local M = { query_struct = '(type_spec name:(type_identifier) @definition.struct type: (struct_type))', query_package = '(package_clause (package_identifier)@package.name)@package.clause', query_struct_id = '(type_spec name:(type_identifier) @definition.struct (struct_type))', query_em_struct_id = '(field_declaration name:(field_identifier) @definition.struct (struct_type))', - query_struct_block = - [[((type_declaration (type_spec name:(type_identifier) @struct.name type: (struct_type)))@struct.declaration)]], + query_struct_block = [[((type_declaration (type_spec name:(type_identifier) @struct.name type: (struct_type)))@struct.declaration)]], query_struct_block_type = [[((type_spec name:(type_identifier) @struct.name type: (struct_type))@struct.declaration)]], -- type(struct1, struct2) -- query_type_declaration = [[((type_declaration (type_spec name:(type_identifier)@type_decl.name type:(type_identifier)@type_decl.type))@type_decl.declaration)]], -- rename to gotype so not confuse with type query_type_declaration = [[((type_declaration (type_spec name:(type_identifier)@type_decl.name)))]], - query_em_struct_block = - [[(field_declaration name:(field_identifier)@struct.name type: (struct_type)) @struct.declaration]], + query_em_struct_block = [[(field_declaration name:(field_identifier)@struct.name type: (struct_type)) @struct.declaration]], query_struct_block_from_id = [[(((type_spec name:(type_identifier) type: (struct_type)))@block.struct_from_id)]], -- query_em_struct = "(field_declaration name:(field_identifier) @definition.struct type: (struct_type))", - query_interface_id = - [[((type_declaration (type_spec name:(type_identifier) @interface.name type:(interface_type)))@interface.declaration)]], + query_interface_id = [[((type_declaration (type_spec name:(type_identifier) @interface.name type:(interface_type)))@interface.declaration)]], -- query_interface_method = [[((method_spec name: (field_identifier)@method.name)@interface.method.declaration)]], query_interface_method = [[((method_elem name: (field_identifier)@method.name)@interface.method.declaration)]], -- -- this is a breaking change require TS parser update query_func = '((function_declaration name: (identifier)@function.name) @function.declaration)', - query_method = - '(method_declaration receiver: (parameter_list (parameter_declaration name:(identifier)@method.receiver.name type:(type_identifier)@method.receiver.type)) name:(field_identifier)@method.name)@method.declaration', + query_method = '(method_declaration receiver: (parameter_list (parameter_declaration name:(identifier)@method.receiver.name type:(type_identifier)@method.receiver.type)) name:(field_identifier)@method.name)@method.declaration', query_method_name = [[((method_declaration receiver: (parameter_list)@method.receiver name: (field_identifier)@method.name @@ -259,9 +254,9 @@ M.get_tbl_testcase_node_name = function(bufnr) end for _, node in pairs(nodes) do local n = get_tc_block(node, function(start_row, end_row, curr_row) - if (start_row <= curr_row and curr_row <= end_row) then -- curr_row starts from 1 - trace('valid node:', node) -- the nvim manual is out of sync for release version - return true -- cursor is in the same line, this is a strong match + if start_row <= curr_row and curr_row <= end_row then -- curr_row starts from 1 + trace('valid node:', node) -- the nvim manual is out of sync for release version + return true -- cursor is in the same line, this is a strong match end end) if n then @@ -275,7 +270,7 @@ M.get_tbl_testcase_node_name = function(bufnr) local id for i2, nodes2 in pairs(match2) do local name2 = tbl_case_kv_query.captures[i2] -- or tbl_case_kv_query.captures[pattern2] - for i, n2 in pairs(nodes2) do -- the order is abit random + for i, n2 in pairs(nodes2) do -- the order is abit random -- if name2 == 'test.name' then local start_row2, _, end_row2, _ = n2:range() if name2 == 'test.nameid' then @@ -331,7 +326,7 @@ M.get_sub_testcase_name = function(bufnr) -- tc_run is the first capture of a match, so we can use it to check if we are inside a test if name == 'tc.run' then local start_row, _, end_row, _ = node:range() - if (start_row < curr_row and curr_row <= end_row + 1) then + if start_row < curr_row and curr_row <= end_row + 1 then is_inside_test = true else is_inside_test = false @@ -367,7 +362,6 @@ M.get_import_node_at_pos = function(bufnr) return end - local parent_is_import = function(node) local n = node while n do diff --git a/lua/go/ts/nodes.lua b/lua/go/ts/nodes.lua index 30265e1a6..5c546ba33 100644 --- a/lua/go/ts/nodes.lua +++ b/lua/go/ts/nodes.lua @@ -1,7 +1,5 @@ -- part of the code from polarmutex/contextprint.nvim -local has_ts_main = pcall(require, 'nvim-treesitter.config') -local parsers local utils = require('go.ts.utils') local goutil = require('go.utils') local ulog = goutil.log diff --git a/lua/go/ts/textobjects.lua b/lua/go/ts/textobjects.lua deleted file mode 100644 index c833914a2..000000000 --- a/lua/go/ts/textobjects.lua +++ /dev/null @@ -1,51 +0,0 @@ -local util = require('go.utils') -local plugins = util.load_plugin - -local M = {} - -function M.setup() - if not plugins('nvim-treesitter') then - util.log('treesitter not avalible') - return - end - - local ok, configs = pcall(require, 'nvim-treesitter.configs') - if not ok then - -- treesitter main no longer provides textobjects by default - return - end - - configs.setup({ - textobjects = { - select = { - enable = true, - lookahead = true, - keymaps = { - -- You can use the capture groups defined in textobjects.scm - ['af'] = '@function.outer', - ['if'] = '@function.inner', - ['ac'] = '@class.outer', - ['ic'] = '@class.inner', - }, - }, - move = { - enable = true, - set_jumps = true, -- whether to set jumps in the jumplist - goto_next_start = { - [']]'] = '@function.outer', - }, - goto_next_end = { - [']['] = '@function.outer', - }, - goto_previous_start = { - ['[['] = '@function.outer', - }, - goto_previous_end = { - ['[]'] = '@function.outer', - }, - }, - }, - }) -end - -return M diff --git a/lua/go/ts/utils.lua b/lua/go/ts/utils.lua index c74b34a27..27eed57a1 100644 --- a/lua/go/ts/utils.lua +++ b/lua/go/ts/utils.lua @@ -61,11 +61,11 @@ local function get_definitions(bufnr) elseif capture_name:find('local.reference', 1, true) then -- qualified_type : e.g. io.Reader inside interface if - node:parent() - and node:parent():parent() - and node:type() == 'type_identifier' - and node:parent():type() == 'qualified_type' - and string.find(node:parent():parent():type(), 'interface') + node:parent() + and node:parent():parent() + and node:type() == 'type_identifier' + and node:parent():type() == 'qualified_type' + and string.find(node:parent():parent():type(), 'interface') then node_type = 'interface' end @@ -123,9 +123,9 @@ function M.list_definitions_toc(bufnr) local index = n + 1 - i local parent_def = parents[index] if - -- ts_utils.is_parent(parent_def.node, def.node) - vim.treesitter.is_ancestor(parent_def.node, def.node) - or (containers[parent_def.type] and vim.treesitter.is_ancestor(parent_def.node:parent(), def.node)) + -- ts_utils.is_parent(parent_def.node, def.node) + vim.treesitter.is_ancestor(parent_def.node, def.node) + or (containers[parent_def.type] and vim.treesitter.is_ancestor(parent_def.node:parent(), def.node)) then break else diff --git a/lua/tests/go_commands_spec.lua b/lua/tests/go_commands_spec.lua index 6caeb2a41..f9ca054ea 100644 --- a/lua/tests/go_commands_spec.lua +++ b/lua/tests/go_commands_spec.lua @@ -1,4 +1,4 @@ -local _ = require('plenary/busted') +require('plenary/busted') local eq = assert.are.same local cur_dir = vim.fn.expand('%:p:h') @@ -14,9 +14,7 @@ describe('should run Go commands', function() end vim.cmd([[packadd go.nvim]]) - vim.cmd([[packadd nvim-treesitter]]) - local status = require('plenary.reload').reload_module('go.nvim') - status = require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') + require('plenary.reload').reload_module('go.nvim') require('go').setup({ verbose = true }) local path = cur_dir .. '/lua/tests/fixtures/fmt/goimports.go' -- %:p:h ? %:p diff --git a/lua/tests/go_comment_spec.lua b/lua/tests/go_comment_spec.lua index a763edf53..19959c56b 100644 --- a/lua/tests/go_comment_spec.lua +++ b/lua/tests/go_comment_spec.lua @@ -1,17 +1,17 @@ +require('plenary/busted') + local eq = assert.are.same -local busted = require('plenary/busted') local cur_dir = vim.fn.expand('%:p:h') describe('should get nodes ', function() - local queries = require('nvim-treesitter.config').get_installed('queries') - if not vim.tbl_contains(queries, 'go') then + local queries, _ = require('go.treesitter').are_parsers_installed({ 'go' }) + if not queries then error('No queries for go found') end _GO_NVIM_CFG.verbose = true _GO_NVIM_CFG.comment_placeholder = '  ' - local status = require('plenary.reload').reload_module('go.nvim') - status = require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') + require('plenary.reload').reload_module('go.nvim') local name = vim.fn.tempname() .. '.go' print('tmp:' .. name) diff --git a/lua/tests/go_fixplurals_spec.lua b/lua/tests/go_fixplurals_spec.lua index a8ca726c8..4ffe84704 100644 --- a/lua/tests/go_fixplurals_spec.lua +++ b/lua/tests/go_fixplurals_spec.lua @@ -1,9 +1,8 @@ -local _ = require('plenary/busted') +require('plenary/busted') print('aaa') local eq = assert.are.same local cur_dir = vim.fn.expand('%:p:h') -- local status = require("plenary.reload").reload_module("go.nvim") --- status = require("plenary.reload").reload_module("nvim-treesitter") -- local ulog = require('go.utils').log describe('should run fixplurals', function() diff --git a/lua/tests/go_impl_spec.lua b/lua/tests/go_impl_spec.lua index 6369d656e..c45367fea 100644 --- a/lua/tests/go_impl_spec.lua +++ b/lua/tests/go_impl_spec.lua @@ -1,9 +1,8 @@ -local _ = require('plenary/busted') +require('plenary/busted') local eq = assert.are.same local cur_dir = vim.fn.expand('%:p:h') -- local status = require("plenary.reload").reload_module("go.nvim") --- status = require("plenary.reload").reload_module("nvim-treesitter") -- local ulog = require('go.utils').log describe('should run impl', function() it('should run impl', function() @@ -14,9 +13,7 @@ describe('should run impl', function() end vim.cmd([[packadd go.nvim]]) - vim.cmd([[packadd nvim-treesitter]]) - local status = require('plenary.reload').reload_module('go.nvim') - status = require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') + require('plenary.reload').reload_module('go.nvim') local name = vim.fn.tempname() .. '.go' local path = cur_dir .. '/lua/tests/fixtures/impl/impl_input.go' -- %:p:h ? %:p diff --git a/lua/tests/go_mockgen_spec.lua b/lua/tests/go_mockgen_spec.lua index 05808afd4..a47cff2ec 100644 --- a/lua/tests/go_mockgen_spec.lua +++ b/lua/tests/go_mockgen_spec.lua @@ -1,16 +1,13 @@ -local _ = require('plenary/busted') +require('plenary/busted') local fn = vim.fn local eq = assert.are.same local cur_dir = vim.fn.expand('%:p:h') -- local status = require("plenary.reload").reload_module("go.nvim") --- status = require("plenary.reload").reload_module("nvim-treesitter") -- local ulog = require('go.utils').log describe('should run mockgen', function() vim.cmd([[packadd go.nvim]]) - vim.cmd([[packadd nvim-treesitter]]) require('plenary.reload').reload_module('go.nvim') - require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') require('go').setup({ verbose = true, lsp_cfg = false }) it('should run mockgen', function() diff --git a/lua/tests/go_runner_spec.lua b/lua/tests/go_runner_spec.lua index 6d029a493..d3084848c 100644 --- a/lua/tests/go_runner_spec.lua +++ b/lua/tests/go_runner_spec.lua @@ -1,16 +1,13 @@ -local helpers = {} -local busted = require('plenary/busted') +require('plenary/busted') local eq = assert.are.same -- local status = require("plenary.reload").reload_module("go.nvim") --- status = require("plenary.reload").reload_module("nvim-treesitter") describe('should run runner', function() vim.cmd([[packadd go.nvim]]) require('go').setup({ verbose = true }) it('should run runner', function() -- - local result = {} local opts = { update_buffer = true, on_exit = function(code, signal, output) diff --git a/lua/tests/go_tags_spec.lua b/lua/tests/go_tags_spec.lua index f6e9582b0..d1f3969d0 100644 --- a/lua/tests/go_tags_spec.lua +++ b/lua/tests/go_tags_spec.lua @@ -1,10 +1,8 @@ -local helpers = {} -local busted = require('plenary/busted') +require('plenary/busted') local eq = assert.are.same local cur_dir = vim.fn.expand('%:p:h') -- local status = require("plenary.reload").reload_module("go.nvim") --- status = require("plenary.reload").reload_module("nvim-treesitter") -- time to wait for action to take effect local wait_time = 500 diff --git a/lua/tests/go_test_spec.lua b/lua/tests/go_test_spec.lua index 6de0059b1..b1f1c661f 100644 --- a/lua/tests/go_test_spec.lua +++ b/lua/tests/go_test_spec.lua @@ -1,6 +1,7 @@ +require('plenary/busted') + local eq = assert.are.same local cur_dir = vim.fn.expand('%:p:h') -local busted = require('plenary/busted') local godir = cur_dir .. '/lua/tests/fixtures' -- hack latest nvim treestitter get_node_text bug @@ -12,7 +13,6 @@ describe('should run func test', function() -- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name) vim.cmd([[packadd go.nvim]]) require('plenary.reload').reload_module('go.nvim') - require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') vim.wait(400, function() end) it('should test function', function() @@ -157,7 +157,6 @@ describe('should run test file', function() -- vim.fn.readfile('minimal.vim') -- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name) require('plenary.reload').reload_module('go.nvim') - require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') it('should test function', function() -- -- go.nvim may not auto loaded @@ -191,13 +190,11 @@ describe('should run test file with flags', function() -- vim.fn.readfile('minimal.vim') -- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name) require('plenary.reload').reload_module('go.nvim') - require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') it('should test function', function() -- -- go.nvim may not auto loaded vim.cmd([[packadd go.nvim]]) require('plenary.reload').reload_module('go.nvim') - require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') local path = 'coverage/branch_test.go' -- %:p:h ? %:p require('go').setup({ trace = true, @@ -226,14 +223,12 @@ describe('should run test package: ', function() -- vim.fn.readfile('minimal.vim') -- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name) require('plenary.reload').reload_module('go.nvim') - require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') it('should test function', function() -- -- go.nvim may not auto loaded vim.cmd([[packadd go.nvim]]) require('plenary.reload').reload_module('go.nvim') - require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') local path = 'coverage/branch_test.go' -- %:p:h ? %:p require('go').setup({ trace = true, @@ -253,7 +248,6 @@ describe('should run test: ', function() -- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name) vim.cmd([[packadd go.nvim]]) require('plenary.reload').reload_module('go.nvim') - require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') it('should test function', function() -- local path = 'coverage/branch_test.go' -- %:p:h ? %:p @@ -275,7 +269,6 @@ describe('should allow select test func: ', function() -- vim.fn.readfile('minimal.vim') -- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name) require('plenary.reload').reload_module('go.nvim') - require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') it('should test function', function() -- local path = 'coverage/branch_test.go' -- %:p:h ? %:p @@ -296,7 +289,6 @@ describe('should run test file with flags inside file: ', function() -- vim.fn.readfile('minimal.vim') -- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name) require('plenary.reload').reload_module('go.nvim') - require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') it('should test function with tag', function() -- local path = 'coverage/tag_test.go' -- %:p:h ? %:p @@ -327,7 +319,6 @@ describe('should run subcase tests: ', function() vim.cmd([[packadd go.nvim]]) require('plenary.reload').reload_module('go.nvim') - require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') if not nvim11 then eq(1, 1) diff --git a/lua/tests/go_ts_node_spec.lua b/lua/tests/go_ts_node_spec.lua index 950dd5caf..2c96175c8 100644 --- a/lua/tests/go_ts_node_spec.lua +++ b/lua/tests/go_ts_node_spec.lua @@ -41,7 +41,6 @@ describe('should get nodes ', function() vim.cmd([[w]]) local bufn = vim.fn.bufnr('') require('plenary.reload').reload_module('go.nvim') - require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') -- _GO_NVIM_CFG.verbose = true local nodes = require('go.ts.nodes') @@ -84,7 +83,6 @@ describe('should get nodes for play list ', function() local fix_path = cur_dir .. '/lua/tests/fixtures/ts/playlist.go' -- %:p:h ? %:p local lines = vim.fn.readfile(fix_path) require('plenary.reload').reload_module('go.nvim') - require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') -- local name = vim.fn.tempname() .. '.go' -- print('play list tmp:' .. name) @@ -168,7 +166,6 @@ describe('should get nodes for import golden ', function() vim.cmd(cmd) require('plenary.reload').reload_module('go.nvim') - require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter') local bufn = vim.fn.bufnr('') vim.fn.setpos('.', { bufn, 4, 4, 0 }) diff --git a/lua/tests/init.lua b/lua/tests/init.lua index 4bb98c4b0..124845cab 100644 --- a/lua/tests/init.lua +++ b/lua/tests/init.lua @@ -5,11 +5,9 @@ local plugin_dir = vim.fn.expand('~/.local/share/nvim/site/pack/vendor/start') vim.opt.rtp:append('.') vim.opt.rtp:prepend(plugin_dir .. '/plenary.nvim') -vim.opt.rtp:prepend(plugin_dir .. '/nvim-treesitter') vim.opt.rtp:prepend(plugin_dir .. '/nvim-lspconfig') vim.cmd('runtime! plugin/plenary.vim') -vim.cmd('runtime! plugin/nvim-treesitter.vim') vim.cmd('runtime! plugin/playground.vim') vim.cmd('runtime! plugin/nvim-lspconfig.vim') @@ -44,12 +42,6 @@ require('go').setup({ vim.lsp.enable('gopls') -require('nvim-treesitter').setup({ - -- Directory to install parsers and queries to - install_dir = vim.fn.stdpath('data') .. '/site', -}) -vim.opt.rtp:append(vim.fn.stdpath('data') .. '/site') - vim.api.nvim_create_autocmd('FileType', { pattern = { 'go' }, callback = function() diff --git a/lua/tests/init.vim b/lua/tests/init.vim index 175ceb50d..c06f86c01 100644 --- a/lua/tests/init.vim +++ b/lua/tests/init.vim @@ -1,11 +1,9 @@ let s:plugin_dir = expand('~/.local/share/nvim/site/pack/vendor/start') set rtp+=. execute 'set rtp^=' . s:plugin_dir . '/plenary.nvim' -execute 'set rtp^=' . s:plugin_dir . '/nvim-treesitter' execute 'set rtp^=' . s:plugin_dir . '/nvim-lspconfig' runtime! plugin/plenary.vim -runtime! plugin/nvim-treesitter.vim runtime! plugin/playground.vim runtime! plugin/nvim-lspconfig.vim @@ -31,13 +29,4 @@ require("go").setup({ log_path = vim.fn.expand("$HOME") .. "/tmp/gonvim.log", lsp_cfg = true, }) - -require'nvim-treesitter.configs'.setup { - ensure_installed = { "go" }, - sync_install = true, - auto_install = true, - highlight = { - enable = true, - } -} EOF diff --git a/lua/tests/install-parsers.lua b/lua/tests/install-parsers.lua index 990982475..d7c077c1d 100644 --- a/lua/tests/install-parsers.lua +++ b/lua/tests/install-parsers.lua @@ -1,5 +1,4 @@ #!/usr/bin/env -S nvim -l -vim.opt.runtimepath:append('../nvim-treesitter') vim.opt.runtimepath:append('.') local install_dir = vim.fn.stdpath('data') .. '/site' @@ -9,125 +8,14 @@ print('Installing to: ' .. install_dir) vim.fn.mkdir(install_dir .. '/parser', 'p') vim.fn.mkdir(install_dir .. '/queries', 'p') --- Manual compilation approach -local function compile_parser() - local build_dir = '/tmp/tree-sitter-go' - - -- Clean up any existing build - vim.fn.system('rm -rf ' .. build_dir) - - -- Clone the tree-sitter-go repository - print('Cloning tree-sitter-go...') - local clone_result = vim.fn.system(string.format('git clone --depth 1 https://github.com/tree-sitter/tree-sitter-go.git %s 2>&1', build_dir)) - print(clone_result) - - if vim.fn.isdirectory(build_dir .. '/src') ~= 1 then - print('ERROR: Failed to clone tree-sitter-go') - return false - end - - -- Compile the parser - print('Compiling parser...') - local parser_c = build_dir .. '/src/parser.c' - local scanner_c = build_dir .. '/src/scanner.c' - local output_so = install_dir .. '/parser/go.so' - - local sources = parser_c - if vim.fn.filereadable(scanner_c) == 1 then - sources = sources .. ' ' .. scanner_c - end - - local compile_cmd = string.format('cc -o "%s" -I"%s/src" %s -shared -Os -fPIC 2>&1', output_so, build_dir, sources) - - print('Compile command: ' .. compile_cmd) - local compile_result = vim.fn.system(compile_cmd) - print('Compile output: ' .. compile_result) - - -- Check if compilation succeeded - if vim.fn.filereadable(output_so) == 1 then - print('✓ Successfully compiled go.so') - return true - else - print('✗ Failed to compile go.so') - return false - end -end - --- Try nvim-treesitter first -print('Attempting nvim-treesitter installation...') -vim.opt.rtp:append(install_dir) - -local ok, _ = pcall(function() - require('nvim-treesitter').setup({ - install_dir = install_dir, - }) - - local install = require('nvim-treesitter.install') - install.update('go') - vim.wait(5000, function() - return false - end) -end) - --- Check if nvim-treesitter installation worked -local go_so_path = install_dir .. '/parser/go.so' -if vim.fn.filereadable(go_so_path) ~= 1 then - print('nvim-treesitter installation did not create go.so, trying manual compilation...') - if not compile_parser() then - print('ERROR: Manual compilation also failed') +vim.opt.rtp:append('.') +vim.cmd('runtime! plugin/go') +local ts = require('go.treesitter') +if not ts.are_parsers_installed({ 'go' }) then + print('go.so not found, attempting manual compilation...') + if not ts.install_parsers_and_grammars() then os.exit(1) end end --- Copy queries from nvim-treesitter source -print('\nCopying queries...') -local ts_queries_paths = { - vim.fn.expand('~/.local/share/nvim/site/pack/vendor/start/nvim-treesitter/queries/go'), - '../nvim-treesitter/queries/go', - '/tmp/tree-sitter-go/queries', -} - -local queries_copied = false -for _, ts_queries in ipairs(ts_queries_paths) do - if vim.fn.isdirectory(ts_queries) == 1 then - print('Copying queries from: ' .. ts_queries) - vim.fn.system(string.format('cp -r "%s" "%s/"', ts_queries, install_dir .. '/queries')) - queries_copied = true - break - end -end - -if not queries_copied then - print('WARNING: Could not find queries directory') -end - --- Verify installation -print('\nVerifying parser installation...') -local parser_dir = install_dir .. '/parser' -local queries_dir = install_dir .. '/queries/go' - -if vim.fn.isdirectory(parser_dir) == 1 then - local files = vim.fn.readdir(parser_dir) - print('Parser files: ' .. vim.inspect(files)) - - local go_so_exists = vim.fn.filereadable(parser_dir .. '/go.so') == 1 - print('go.so exists: ' .. tostring(go_so_exists)) - print('go.so size: ' .. vim.fn.getfsize(parser_dir .. '/go.so') .. ' bytes') - - if not go_so_exists then - print('ERROR: go.so not found!') - os.exit(1) - end -else - print('ERROR: Parser directory does not exist!') - os.exit(1) -end - -if vim.fn.isdirectory(queries_dir) == 1 then - local files = vim.fn.readdir(queries_dir) - print('Query files: ' .. vim.inspect(files)) -else - print('WARNING: Queries directory does not exist - tests may fail') -end - print('\n✓ Parser installation complete!') diff --git a/lua/tests/minimal.lua b/lua/tests/minimal.lua index c4e6092a7..ddadae960 100644 --- a/lua/tests/minimal.lua +++ b/lua/tests/minimal.lua @@ -1,6 +1,5 @@ vim.opt.rtp:append('.') vim.opt.rtp:append('../plenary.nvim/') -vim.opt.rtp:append('../nvim-treesitter') vim.opt.rtp:append('../nvim-lspconfig/') vim.opt.rtp:append('../guihua.lua/') @@ -10,7 +9,6 @@ vim.opt.rtp:prepend(parser_install_dir) vim.cmd([[ runtime! plugin/plenary.vim - runtime! plugin/nvim-treesitter.vim runtime! plugin/playground.vim runtime! plugin/nvim-lspconfig.vim runtime! plugin/guihua.lua @@ -37,11 +35,6 @@ require('go').setup({ lsp_cfg = true, }) -require('nvim-treesitter').setup({ - -- Directory to install parsers and queries to - install_dir = parser_install_dir, -}) - vim.o.swapfile = false vim.bo.swapfile = false diff --git a/lua/tests/minimal.vim b/lua/tests/minimal.vim index ea951181d..9729e66d3 100644 --- a/lua/tests/minimal.vim +++ b/lua/tests/minimal.vim @@ -1,10 +1,8 @@ set rtp +=. set rtp +=../plenary.nvim/ -set rtp +=../nvim-treesitter set rtp +=../nvim-lspconfig/ runtime! plugin/plenary.vim -runtime! plugin/nvim-treesitter.vim runtime! plugin/playground.vim runtime! plugin/nvim-lspconfig.vim runtime! plugin/guihua.lua diff --git a/samplevimrc.vim b/samplevimrc.vim index ac89f0d6e..fafe8f1d4 100644 --- a/samplevimrc.vim +++ b/samplevimrc.vim @@ -10,9 +10,6 @@ Plug 'theHamsta/nvim-dap-virtual-text' " Plug 'hrsh7th/nvim-compe' and other plugins you commenly use... -" optional, if you need treesitter symbol support -Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'} - call plug#end() " No need for rquire('lspconfig'), navigator will configure it for you