From 70356e73f318bb8a384b737c179a1da6621b1f43 Mon Sep 17 00:00:00 2001 From: ray-x Date: Fri, 23 Apr 2021 14:21:22 +1000 Subject: [PATCH] fix bugs for worspace symbols --- lua/navigator/codeAction.lua | 14 ++++++++------ lua/navigator/gui.lua | 24 ++++++++++++++++++------ lua/navigator/lspclient/mapping.lua | 2 +- lua/navigator/lspwrapper.lua | 10 +++++++--- lua/navigator/symbols.lua | 13 ++++++------- 5 files changed, 40 insertions(+), 23 deletions(-) diff --git a/lua/navigator/codeAction.lua b/lua/navigator/codeAction.lua index 40977f5..1a938a0 100644 --- a/lua/navigator/codeAction.lua +++ b/lua/navigator/codeAction.lua @@ -10,7 +10,7 @@ function code_action.code_action_handler(err, _, actions, num, _, _, customSelec print("No code actions available") return end - local data = {" Auto Fix Apply Exit"} + local data = {"[] Auto Fix Apply Exit"} for i, action in ipairs(actions) do local title = action.title:gsub("\r\n", "\\r\\n") title = title:gsub("\n", "\\n") @@ -82,12 +82,13 @@ end local function _update_virtual_text(line) local namespace = get_namespace() - api.nvim_buf_clear_namespace(0, namespace, 0, -1) + pcall(api.nvim_buf_clear_namespace, 0, namespace, 0, -1) if line then local icon_with_indent = " " .. config.code_action_icon - api.nvim_buf_set_extmark( + pcall( + api.nvim_buf_set_extmark, 0, namespace, line, @@ -166,7 +167,8 @@ local special_buffers = { ["LspSagaCodecode_action"] = true, ["lspsagafinder"] = true, ["NvimTree"] = true, - ["vist"] = true, + ["vista"] = true, + ["guihua"] = true, ["lspinfo"] = true, ["markdown"] = true, ["text"] = true @@ -175,8 +177,8 @@ local special_buffers = { -- return Action:action_callback() -- end -local action_vritual_call_back = function (line,diagnostics) - return code_action:render_action_virtual_text(line,diagnostics) +local action_vritual_call_back = function(line, diagnostics) + return code_action:render_action_virtual_text(line, diagnostics) end local code_action_req = function(_call_back_fn, diagnostics) diff --git a/lua/navigator/gui.lua b/lua/navigator/gui.lua index b6f6250..44cbfb9 100644 --- a/lua/navigator/gui.lua +++ b/lua/navigator/gui.lua @@ -43,8 +43,17 @@ function M._preview_location(opts) --location, width, pos_x, pos_y print("error invalid range") return end + if range.start.line == nil then + range.start.line = range["end"].line - 1 + opts.lnum = range["end"].line + 1 + log(opts) + end + if range["end"].line == nil then + range["end"].line = range.start.line + 1 + opts.lnum = range.start.line + 1 + log(opts) + end local contents = api.nvim_buf_get_lines(bufnr, range.start.line, (range["end"].line or 1) + 10, false) - -- local syntax = api.nvim_buf_get_option(bufnr, "syntax") if syntax == nil or #syntax < 1 then @@ -55,6 +64,7 @@ function M._preview_location(opts) --location, width, pos_x, pos_y local win_opts = {syntax = syntax, width = opts.width, pos_x = opts.offset_x or 0, pos_y = opts.offset_y or 10} win_opts.items = contents win_opts.hl_line = opts.lnum - range.start.line + if win_opts.hl_line < 0 then win_opts.hl_line = 1 end log(opts.lnum, range.start.line, win_opts.hl_line) local w = M.new_preview(win_opts) @@ -62,15 +72,16 @@ function M._preview_location(opts) --location, width, pos_x, pos_y end function M.preview_uri(opts) -- uri, width, line, col, offset_x, offset_y - verbose("uri", opts.uri, opts.lnum, opts.offset_x, opts.offset_y) - local line_beg = opts.line - if opts.lnum >= 2 then - line_beg = opts.lnum - 2 + local line_beg = opts.lnum - 1 + if line_beg >= 2 then + line_beg = line_beg - 2 end local loc = {uri = opts.uri, targetRange = {start = {line = line_beg}}} -- TODO: options for 8 loc.targetRange["end"] = {line = opts.lnum + 8} opts.location = loc + + -- log("uri", opts.uri, opts.lnum, opts.location) return M._preview_location(opts) end @@ -88,6 +99,7 @@ function M.new_list_view(opts) local prompt = opts.prompt or false if data and not vim.tbl_isempty(data) then -- replace + -- TODO: 10 vimrc opt if #data > 10 and opts.prompt == nil then prompt = true end @@ -132,7 +144,7 @@ function M.new_list_view(opts) l.uri = "file:///" .. l.filename end return M.preview_uri( - {uri = l.uri, width = width, lnum = l.lnum, col = l.col, offsetx = 0, offset_y = offset_y} + {uri = l.uri, width = width, lnum = l.lnum, col = l.col, offset_x = 0, offset_y = offset_y} ) end } diff --git a/lua/navigator/lspclient/mapping.lua b/lua/navigator/lspclient/mapping.lua index 9f62e9c..642c6f5 100644 --- a/lua/navigator/lspclient/mapping.lua +++ b/lua/navigator/lspclient/mapping.lua @@ -22,7 +22,7 @@ local key_maps = { {key = "K", func = "hover()"}, {key = "ga", mode = 'n', func = "code_action()"}, - {key = "ca", mode = 'v', func = "range_code_action()"}, + {key = "ga", mode = 'v', func = "range_code_action()"}, {key = "re", func = "rename()"}, {key = "gi", func = "incoming_calls()"}, diff --git a/lua/navigator/lspwrapper.lua b/lua/navigator/lspwrapper.lua index a867dde..325cd3f 100644 --- a/lua/navigator/lspwrapper.lua +++ b/lua/navigator/lspwrapper.lua @@ -3,6 +3,7 @@ local util = require "navigator.util" local lsp = require "vim.lsp" local log = require "navigator.util".log local symbol_kind = require "navigator.lspclient.lspkind".symbol_kind +local cwd = vim.fn.getcwd(0) function M.lines_from_locations(locations, include_filename) local fnamemodify = (function(filename) if include_filename then @@ -38,10 +39,13 @@ function M.symbols_to_items(result) item.text = kind .. ": " .. item.text end item.filename = vim.uri_to_fname(item.uri) + + item.display_filename = item.filename:gsub(cwd .. "/", "./", 1) + if item.range == nil or item.range.start == nil then - log(result[i], item) + log("range not set", result[i], item) end - item.lnum = item.range.start.line + item.lnum = item.range.start.line + 1 if item.containerName ~= nil then item.text = " " .. item.containerName .. item.text @@ -50,7 +54,7 @@ function M.symbols_to_items(result) end end -- local items = locations_to_items(locations) - log(locations[1]) + -- log(locations[1]) return locations end diff --git a/lua/navigator/symbols.lua b/lua/navigator/symbols.lua index d9525c5..b592b0e 100644 --- a/lua/navigator/symbols.lua +++ b/lua/navigator/symbols.lua @@ -26,7 +26,6 @@ function M.document_symbols(opts) for _, loc in ipairs(locations) do table.insert(lines, string.format("%s:%s:%s", loc.filename, loc.lnum, loc.text)) end - local cmd = table.concat(lines, "\n") if #lines > 0 then gui.new_list_view({data = lines}) else @@ -40,8 +39,8 @@ function M.workspace_symbols(opts) local lspopts = { loc = "top_center", prompt = true, - rawdata = true, - api = "華 " + -- rawdata = true, + api = " " } vim.list_extend(lspopts, opts) local params = vim.lsp.util.make_position_params() @@ -61,10 +60,10 @@ function M.workspace_symbols(opts) end local items = symbols_to_items(result) - -- verbose(results_lsp) + -- log(#items, items[1]) if #items > 0 then - lspopts.data = items + lspopts.items = items gui.new_list_view(lspopts) else print("symbols not found") @@ -124,7 +123,7 @@ function M.document_symbol_handler(err, _, result, _, bufnr) end -- verbose(locations) -- local items = locations_to_items(locations) - gui.new_list_view({items = locations, prompt = true, rawdata = true, api = "華 "}) + gui.new_list_view({items = locations, prompt = true, rawdata = true, api = " "}) -- if locations == nil or vim.tbl_isempty(locations) then -- print "References not found" @@ -171,7 +170,7 @@ function M.workspace_symbol_handler(err, _, result, _, bufnr) -- table.insert(locations, item) -- end -- local items = locations_to_items(locations) - gui.new_list_view({items = items, prompt = true, rowdata = true, api = "華 "}) + gui.new_list_view({items = items, prompt = true, rowdata = true, api = " "}) -- if locations == nil or vim.tbl_isempty(locations) then -- print "References not found"