From 8d77c3ab1e994a8afc6a42698b73a4222b820357 Mon Sep 17 00:00:00 2001 From: ray-x Date: Wed, 29 Jun 2022 14:51:28 +1000 Subject: [PATCH] bug fix for #166 not all items shown in listview. Also add flag allow control when the ts info will be added update readme --- README.md | 2 ++ lua/navigator.lua | 1 + lua/navigator/lspwrapper.lua | 23 ++++++++++++++++++----- lua/navigator/reference.lua | 8 +++++--- lua/navigator/render.lua | 7 ++++--- lua/navigator/treesitter.lua | 2 +- 6 files changed, 31 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 173096c..1eb1317 100644 --- a/README.md +++ b/README.md @@ -262,6 +262,8 @@ require'navigator'.setup({ -- this kepmap gK will override "gD" mapping function declaration() in default kepmap -- please check mapping.lua for all keymaps treesitter_analysis = true, -- treesitter variable context + treesitter_analysis_max_num = 100, -- how many items to run treesitter analysis + -- this value prevent slow in large projects, e.g. found 100000 reference in a project transparency = 50, -- 0 ~ 100 blur the main window, 100: fully transparent, 0: opaque, set to nil or 100 to disable it lsp_signature_help = true, -- if you would like to hook ray-x/lsp_signature plugin in navigator diff --git a/lua/navigator.lua b/lua/navigator.lua index 4983ce0..403b3c7 100755 --- a/lua/navigator.lua +++ b/lua/navigator.lua @@ -28,6 +28,7 @@ _NgConfigValues = { end, ts_fold = false, treesitter_analysis = true, -- treesitter variable context + treesitter_analysis_max_num = 100, -- how many items to run treesitter analysis transparency = 50, -- 0 ~ 100 blur the main window, 100: fully transparent, 0: opaque, set to nil to disable it lsp_signature_help = true, -- if you would like to hook ray-x/lsp_signature plugin in navigator -- setup here. if it is nil, navigator will not init signature help diff --git a/lua/navigator/lspwrapper.lua b/lua/navigator/lspwrapper.lua index 7825b1d..0c27e34 100644 --- a/lua/navigator/lspwrapper.lua +++ b/lua/navigator/lspwrapper.lua @@ -162,7 +162,7 @@ function M.call_async(method, params, handler, bufnr) -- results_lsp, canceller end -local function ts_functions(uri) +local function ts_functions(uri, optional) local unload_bufnr local ts_enabled, _ = pcall(require, 'nvim-treesitter.locals') if not ts_enabled or not TS_analysis_enabled then @@ -187,6 +187,9 @@ local function ts_functions(uri) ts_nodes_time:delete(uri) end end + if optional then + return + end local unload = false if not api.nvim_buf_is_loaded(bufnr) then trace('! load buf !', uri, bufnr) @@ -206,7 +209,7 @@ local function ts_functions(uri) return funcs, unload_bufnr end -local function ts_definition(uri, range) +local function ts_definition(uri, range, optional) local unload_bufnr local ts_enabled, _ = pcall(require, 'nvim-treesitter.locals') if not ts_enabled or not TS_analysis_enabled then @@ -224,6 +227,9 @@ local function ts_definition(uri, range) log('ts def from cache') return tsnodes end + if optional then + return + end local ts_def = require('navigator.treesitter').find_definition local bufnr = vim.uri_to_bufnr(uri) local x = os.clock() @@ -314,6 +320,13 @@ end -- log(locations, second_part) -- end +local function ts_optional(i, unload_buf_size) + if unload_buf_size then + return unload_buf_size > _NgConfigValues.treesitter_analysis_max_num + end + return i > _NgConfigValues.treesitter_analysis_max_num +end + function M.locations_to_items(locations, ctx) ctx = ctx or {} local max_items = ctx.max_items or 100000 -- @@ -353,14 +366,14 @@ function M.locations_to_items(locations, ctx) local proj_file = item.uri:find(cwd) or is_win or i < 30 local unload, def if TS_analysis_enabled and proj_file then - funcs, unload = ts_functions(item.uri) + funcs, unload = ts_functions(item.uri, ts_optional(i, #unload_bufnrs)) if unload then table.insert(unload_bufnrs, unload) end if not uri_def[item.uri] then -- find def in file - def, unload = ts_definition(item.uri, item.range) + def, unload = ts_definition(item.uri, item.range, ts_optional(i, #unload_bufnrs)) if def and def.start then uri_def[item.uri] = def if def.start then -- find for the 1st time @@ -421,7 +434,7 @@ function M.locations_to_items(locations, ctx) vim.cmd([[set eventignore-=FileType]]) trace(items) - return items, width + 24, second_part -- TODO handle long line? + return items, width + 30, second_part -- TODO handle long line? end function M.symbol_to_items(locations) diff --git a/lua/navigator/reference.lua b/lua/navigator/reference.lua index 6b4a8e1..5571bd3 100644 --- a/lua/navigator/reference.lua +++ b/lua/navigator/reference.lua @@ -14,9 +14,9 @@ local ref_view = function(err, locations, ctx, cfg) local truncate = cfg and cfg.truncate or 20 local opts = {} trace('arg1', err, ctx, locations) - trace(locations) + log(#locations, locations[1]) if ctx.combine then - -- wait for both request + -- wait for both reference and definition LSP request if ctx.results == nil then return end @@ -29,7 +29,7 @@ local ref_view = function(err, locations, ctx, cfg) if _NgConfigValues.debug then local logctx = { results = {} } logctx = vim.tbl_extend('keep', logctx, ctx) - log(logctx, 'result size', #ctx.results, 'item', ctx.results[1]) + log(logctx, 'result size', 'def', #ctx.results.definitions, 'ref', #ctx.results.references) end if definitions.error and references.error then vim.notify('lsp ref callback error' .. vim.inspect(ctx.result), vim.lsp.log_levels.WARN) @@ -115,11 +115,13 @@ local ref_view = function(err, locations, ctx, cfg) if vim.tbl_isempty(second_part) then return end + ctx.max_items = #second_part local items2 = locations_to_items(second_part, ctx) vim.list_extend(thread_items, items2) local data = require('navigator.render').prepare_for_render(thread_items, opts) + log('thread data size', #data) listview.ctrl:on_data_update(data) if nv_ref_async then vim.loop.close(nv_ref_async) diff --git a/lua/navigator/render.lua b/lua/navigator/render.lua index fb8153f..8259eed 100644 --- a/lua/navigator/render.lua +++ b/lua/navigator/render.lua @@ -179,12 +179,13 @@ function M.prepare_for_render(items, opts) if #ts_report > 1 then space, trim = get_pads(win_width, item.text, ts_report) if trim then - item.text = string.sub(item.text, 1, opts.width - 20) .. '' - local _, j = string.gsub(item.text, [["]], "") + local ts_r = ts_report or '' + item.text = string.sub(item.text, 1, math.max(1, opts.width - math.max(20, #ts_r))) + local _, j = string.gsub(item.text, [["]], '') if j % 2 == 1 then item.text = item.text .. '"' end - _, j = string.gsub(item.text, [[']], "") + _, j = string.gsub(item.text, [[']], '') if j % 2 == 1 then item.text = item.text .. [[']] end diff --git a/lua/navigator/treesitter.lua b/lua/navigator/treesitter.lua index f6bb069..86f38e6 100644 --- a/lua/navigator/treesitter.lua +++ b/lua/navigator/treesitter.lua @@ -448,7 +448,7 @@ local function get_all_nodes(bufnr, filter, summary) -- hack for lua and maybe other language aswell local parent = tsdata:parent() if parent ~= nil then - log(parent:type(), vim.treesitter.get_node_text(parent, bufnr), item.node_text, item.type) + log(parent:type(), vim.treesitter.get_node_text(parent, bufnr):sub(1, 30), item.node_text, item.type) end if parent ~= nil