From f9b0833707f00f46cc249e4419400d5486068d51 Mon Sep 17 00:00:00 2001 From: ray-x Date: Mon, 6 Nov 2023 17:19:26 +1100 Subject: [PATCH] logs cleanup --- lua/navigator/lspwrapper.lua | 18 +++++++++++++----- lua/navigator/reference.lua | 27 ++++++++++++++++++++++++++- lua/navigator/render.lua | 2 +- lua/navigator/treesitter.lua | 24 +++++++++++------------- lua/navigator/util.lua | 4 ++-- 5 files changed, 53 insertions(+), 22 deletions(-) diff --git a/lua/navigator/lspwrapper.lua b/lua/navigator/lspwrapper.lua index 622540d..f31ebab 100644 --- a/lua/navigator/lspwrapper.lua +++ b/lua/navigator/lspwrapper.lua @@ -310,7 +310,7 @@ end function M.locations_to_items(locations, ctx) ctx = ctx or {} local max_items = ctx.max_items or 1000 -- - log(ctx, max_items) + trace(ctx, max_items) local client_id = ctx.client_id or 1 local enc = util.encoding(client_id) if not locations or vim.tbl_isempty(locations) then @@ -327,22 +327,26 @@ function M.locations_to_items(locations, ctx) local second_part locations, second_part = slice_locations(locations, max_items) if second_part and #second_part > 0 then - log(#locations, locations[1], #second_part, second_part and second_part[1]) + log('second part', #locations, #second_part) + trace(#locations, locations[1], #second_part, second_part and second_part[1]) end trace(locations) vim.cmd([[set eventignore+=FileType]]) + local now = uv.now() local unload_bufnrs = {} local file_cnt = {} for i, loc in ipairs(locations) do + + local looptimer = uv.now() local item = lsp.util.locations_to_items({ loc }, enc)[1] item.range = locations[i].range or locations[i].targetRange item.uri = locations[i].uri or locations[i].targetUri item.definition = locations[i].definition if is_win then - log(item.uri, cwd) -- file:///C:/path/to/file + trace(item.uri, cwd) -- file:///C:/path/to/file end file_cnt[item.uri] = (file_cnt[item.uri] or 0) + 1 -- only load top 30 file.items @@ -350,7 +354,7 @@ function M.locations_to_items(locations, ctx) local unload, def local context = '' if not proj_file then - log('not proj file', i, item.uri) + trace('not proj file', i, item.uri) end if TS_analysis_enabled and not ctx.no_show and proj_file then local ts_context = nts.ref_context @@ -362,7 +366,7 @@ function M.locations_to_items(locations, ctx) unload = bufnr end context = ts_context({ bufnr = bufnr, pos = item.range }) or 'not found' - log(i, context) + trace('ts ctx', i, context, uv.now() - looptimer) -- TODO: unload buffers if unload then @@ -395,6 +399,7 @@ function M.locations_to_items(locations, ctx) table.insert(unload_bufnrs, unload) end end + trace('perf: ts ctx', i, uv.now() - looptimer, uv.now() - now) trace(uri_def[item.uri], item.range) -- set to log if need to get all in rnge local def1 = uri_def[item.uri] if def1 and def1.start and item.range then @@ -425,6 +430,9 @@ function M.locations_to_items(locations, ctx) item.lhs = check_lhs(item.text, item.symbol_name) table.insert(items, item) + + trace('perf: ts render', uv.now() - looptimer, uv.now() - now) + loop_timer = uv.now() end trace(uri_def) diff --git a/lua/navigator/reference.lua b/lua/navigator/reference.lua index 2083827..768cf3e 100644 --- a/lua/navigator/reference.lua +++ b/lua/navigator/reference.lua @@ -26,6 +26,18 @@ local function order_locations(locations) end) return locations end + +local function warmup_treesitter(options) + local api = vim.api + local parsers = require('nvim-treesitter.parsers') + local bufnr = options.bufnr or api.nvim_get_current_buf() + local parser = parsers.get_parser(bufnr) + if not parser then + log('err: ts not loaded ' .. vim.o.ft) + return + end +end + local M = {} local ref_view = function(err, locations, ctx, cfg) cfg = cfg or {} @@ -239,7 +251,7 @@ local function fetch_lsp_references(bufnr, params, callback) 'textDocument/references', params, function(err, result, ctx, cfg) - log(result) + trace(result) if err then log('[nav-rename] Error while finding references: ' .. err.message) return @@ -260,6 +272,19 @@ local ref_req = function() -- do not call it twice _NgConfigValues.closer() end + + local warmup_ts + if _NgConfigValues.treesitter_analysis then + warmup_ts = vim.loop.new_async(function() + warmup_treesitter(cfg) + if warmup_ts:is_active() then + warmup_ts:close() + end + end) + vim.defer_fn(function() + warmup_ts:send() + end, 5) + end -- lsp.call_async("textDocument/references", ref_params, ref_hdlr) -- return asyncresult, canceller local bufnr = vim.api.nvim_get_current_buf() local ref_params = vim.lsp.util.make_position_params() diff --git a/lua/navigator/render.lua b/lua/navigator/render.lua index f67d25a..2cc6bc0 100644 --- a/lua/navigator/render.lua +++ b/lua/navigator/render.lua @@ -150,7 +150,7 @@ function M.prepare_for_render(items, opts) -- log(item.text, item.symbol_name, item.uri) -- log(item.text) if item.definition then - log('definition', item) + log('definition', item.call_by, item.symbol_name, item.text) if opts.side_panel then ts_report = _NgConfigValues.icons.value_definition else diff --git a/lua/navigator/treesitter.lua b/lua/navigator/treesitter.lua index 4779b49..9ebabf7 100644 --- a/lua/navigator/treesitter.lua +++ b/lua/navigator/treesitter.lua @@ -22,12 +22,11 @@ end local api = vim.api local util = require('navigator.util') local M = {} - -local cwd = vim.loop.cwd() -local log = require('navigator.util').log -local lerr = require('navigator.util').error -local trace = function(...) end -trace = log +local uv = vim.uv or vim.loop +local cwd = uv.cwd() +local log = util.log +local lerr = util.error +local trace = util.trace local get_icon = function(kind) if kind == nil or _NgConfigValues.icons.match_kinds[kind] == nil then @@ -92,8 +91,7 @@ function M.find_definition(range, bufnr) local node_at_point = root:named_descendant_for_range(symbolpos[1], symbolpos[2], symbolpos[1], symbolpos[2]) if not node_at_point then - lerr('no node at cursor') - return + return log('Err: no node at cursor', range) end local definition = locals.find_definition(node_at_point, bufnr) @@ -123,9 +121,9 @@ function M.get_tsnode_at_pos(pos, bufnr, ignore_injected_langs) end local cursor_range = { pos.start.line, pos.start.character } - local buf = bufnr - local root_lang_tree = parsers.get_parser(buf) + local root_lang_tree = parsers.get_parser(bufnr) if not root_lang_tree then + log('Err: ts not loaded ' .. vim.o.ft, bufnr) return end @@ -197,7 +195,7 @@ function M.ref_context(opts) while expr do local line = ts_utils._get_line_for_node(expr, type_patterns, transform_fn, bufnr) - log('line', line) + trace('line', line) if line ~= '' and not vim.tbl_contains(lines, line) then table.insert(lines, 1, line) end @@ -312,7 +310,7 @@ local function get_definitions(bufnr) and node:parent():type() == 'qualified_type' and string.find(node:parent():parent():type(), 'interface') then - log('add node', node) + trace('add node', node) nodes_set[start] = { node = node, type = match or 'field' } end end @@ -456,7 +454,7 @@ function M.goto_adjacent_usage(bufnr, delta) local def_node, scope = ts_locals.find_definition(node_at_point, bufnr) local usages = ts_locals.find_usages(def_node, scope, bufnr) - log(usages) + trace(usages) local index = index_of(usages, node_at_point) if not index then diff --git a/lua/navigator/util.lua b/lua/navigator/util.lua index 6c09782..02bbb14 100644 --- a/lua/navigator/util.lua +++ b/lua/navigator/util.lua @@ -140,7 +140,7 @@ local function getDir(path) end function M.get_relative_path(base_path, my_path) - M.log('rel path', base_path, my_path) + M.trace('rel path', base_path, my_path) base_path = string.lower(base_path) my_path = string.lower(my_path) local base_data = getDir(base_path) @@ -489,7 +489,7 @@ function M.info(msg) end function M.dedup(locations) - local m = math.min(10, #locations) -- dedup first 10 elements + local m = math.min(10, #locations) -- dedup first 10 elements local dict = {} local del = {} for i = 1, m, 1 do