diff --git a/lua/navigator/dochighlight.lua b/lua/navigator/dochighlight.lua index bbd938d..0fbf71c 100644 --- a/lua/navigator/dochighlight.lua +++ b/lua/navigator/dochighlight.lua @@ -240,13 +240,13 @@ local function documentHighlight() return end trace('dochl', result) - vim.lsp.util.buf_clear_references(bufnr) - vim.lsp.util.buf_highlight_references(bufnr, result) bufnr = bufnr or 0 if type(result) ~= 'table' then vim.lsp.util.buf_clear_references(bufnr) return end + vim.lsp.util.buf_clear_references(bufnr) + vim.lsp.util.buf_highlight_references(bufnr, result) table.sort(result, function(a, b) return before(a.range, b.range) end) diff --git a/lua/navigator/gui.lua b/lua/navigator/gui.lua index 98a8325..999fd14 100644 --- a/lua/navigator/gui.lua +++ b/lua/navigator/gui.lua @@ -1,234 +1,51 @@ local M = {} -local ListView = require "guihua.listview" -local TextView = require "guihua.textview" -local util = require "navigator.util" -local log = require"navigator.util".log -local trace = require"navigator.util".trace +local ListView = require('guihua.listview') +local TextView = require('guihua.textview') +local util = require('navigator.util') +local log = require('navigator.util').log +local trace = require('navigator.util').trace local api = vim.api -local top_center = require"guihua.location".top_center - -local path_sep = require"navigator.util".path_sep() -local path_cur = require"navigator.util".path_cur() -function M._preview_location(opts) -- location, width, pos_x, pos_y - local uri = opts.uri - if uri == nil then - log("invalid/nil uri ") - return - end - local bufnr = vim.uri_to_bufnr(uri) - if not api.nvim_buf_is_loaded(bufnr) then - vim.fn.bufload(bufnr) - end - -- - - local display_range = opts.location.range - -- if range.start == nil then - -- 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 - -- TODO: preview height - -- local contents = api.nvim_buf_get_lines(bufnr, range.start.line, range["end"].line, false) - -- - local syntax = api.nvim_buf_get_option(bufnr, "ft") - if syntax == nil or #syntax < 1 then - syntax = "c" - end - - -- trace(syntax, contents) - local win_opts = { - syntax = syntax, - width = opts.width, - height = display_range['end'].line - display_range.start.line + 1, - preview_height = opts.height or opts.preview_height, - pos_x = opts.offset_x, - pos_y = opts.offset_y, - range = opts.range, - display_range = display_range, - uri = uri, - allow_edit = opts.enable_edit - } - - if _NgConfigValues.external then - win_opts.external = true - win_opts.relative = nil - end - -- win_opts.items = contents - win_opts.hl_line = opts.lnum - display_range.start.line - if win_opts.hl_line < 0 then - win_opts.hl_line = 1 - end - trace(opts.lnum, opts.range.start.line, win_opts.hl_line) - log(win_opts) - local w = TextView:new({ - loc = "offset_center", - rect = { - height = win_opts.height, -- opts.preview_heigh or 12, -- TODO 12 - width = win_opts.width, - pos_x = win_opts.pos_x, - pos_y = win_opts.pos_y - }, - list_view_height = win_opts.height, - -- data = display_data, - relative = win_opts.relative, - -- data = opts.items, -- either items or uri - uri = win_opts.uri, - syntax = win_opts.syntax, - enter = win_opts.enter or false, - range = win_opts.range, - border = opts.border, - display_range = win_opts.display_range, - hl_line = win_opts.hl_line, - allow_edit = win_opts.allow_edit, - external = win_opts.external - }) - return w -end - -function M.preview_uri(opts) -- uri, width, line, col, offset_x, offset_y - -- local handle = vim.loop.new_async(vim.schedule_wrap(function() - local line_beg = opts.lnum - 1 - if line_beg >= _NgConfigValues.preview_lines_before then - line_beg = line_beg - _NgConfigValues.preview_lines_before - elseif line_beg >= 2 then - line_beg = line_beg - 2 - end - local loc = {uri = opts.uri, range = {start = {line = line_beg}}} - - -- TODO: preview height - loc.range["end"] = {line = opts.lnum + opts.preview_height} - opts.location = loc - - trace("uri", opts.uri, opts.lnum, opts.location.range.start.line, opts.location.range['end'].line) - return M._preview_location(opts) - -- handle:close() - -- end)) - -- handle:send() -end +local top_center = require('guihua.location').top_center +local path_sep = require('navigator.util').path_sep() +local path_cur = require('navigator.util').path_cur() function M.new_list_view(opts) - local config = require("navigator").config_values() + log(opts) + + local config = require('navigator').config_values() local items = opts.items local data = {} - local wwidth = api.nvim_get_option("columns") + local wwidth = api.nvim_get_option('columns') - local loc = "top_center" + local loc = 'top_center' - local mwidth = _NgConfigValues.width - local width = math.floor(wwidth * mwidth) - if config.width ~= nil and config.width > 0.3 and config.width < 0.99 then - width = math.floor(wwidth * config.width) - end - width = math.min(120, width, opts.width or 120) - local wheight = math.floor( - 1 + api.nvim_get_option("lines") * (_NgConfigValues.height + _NgConfigValues.preview_height)) - local pheight = math.max(_NgConfigValues.preview_lines, - math.floor(api.nvim_get_option("lines") * _NgConfigValues.preview_height)) - local prompt = opts.prompt or false + opts.min_width = opts.min_width or 0.3 + opts.min_height = opts.min_height or 0.3 + + opts.height_ratio = config.height + opts.width_ratio = config.width + opts.preview_height_ratio = _NgConfigValues.preview_height or 0.3 + opts.preview_lines = _NgConfigValues.preview_lines if opts.rawdata then - data = items + opts.data = items else - trace(items) - data = require"navigator.render".prepare_for_render(items, opts) + opts.data = require('navigator.render').prepare_for_render(items, opts) end - - local border = _NgConfigValues.border or 'shadow' - - if not data or vim.tbl_isempty(data) then + opts.border = _NgConfigValues.border or 'shadow' + opts.rect = { height = lheight, width = width, pos_x = 0, pos_y = 0 } + if not items or vim.tbl_isempty(items) then + log('empty data return') return end - -- replace - -- TODO: 10 vimrc opt - if #data > 10 and opts.prompt == nil then - loc = "top_center" - prompt = true - end - - local lheight = math.min(#data, math.floor(wheight * _NgConfigValues.height)) - - local r, _ = top_center(lheight, width) - - local offset_y = r + lheight - -- style shadow took 1 lines - if border ~= 'none' then - if border == 'shadow' then - offset_y = offset_y + 1 - else - offset_y = offset_y + 1 -- single? - end - end - -- if border is not set, this should be r+lheigh - if prompt then - offset_y = offset_y + 1 -- need to check this out - end - local idx = require"guihua.util".fzy_idx - local transparency = _NgConfigValues.transparency - if transparency == 100 then - transparency = nil - end - local ext = _NgConfigValues.external - if ext then - opts.relative = nil - end - return ListView:new({ - loc = loc, - prompt = prompt, - relative = opts.relative, - style = opts.style, - api = opts.api, - total = opts.total, - rect = {height = lheight, width = width, pos_x = 0, pos_y = 0}, - -- preview_height = pheight, - ft = opts.ft or 'guihua', - -- data = display_data, - data = data, - border = border, - external = ext, - on_confirm = opts.on_confirm or function(item, split_opts) - log(split_opts) - split_opts = split_opts or {} - if item.filename ~= nil then - log("openfile ", item.filename, item.lnum, item.col) - util.open_file_at(item.filename, item.lnum, item.col, split_opts.split) - end - end, - transparency = transparency, - on_move = opts.on_move or function(item) - trace("on move", item) - trace("on move", item.text or item, item.uri, item.filename) - -- todo fix - if item.uri == nil then - item.uri = "file:///" .. item.filename - end - return M.preview_uri({ - uri = item.uri, - width = width, - height = lheight, -- this is to cal offset - preview_height = pheight, - lnum = item.lnum, - col = item.col, - range = item.range, - offset_x = 0, - offset_y = offset_y, - border = border, - external = ext, - enable_edit = opts.enable_preview_edit or false - }) - end - }) + opts.transparency = _NgConfigValues.transparency + opts.external = _NgConfigValues.external + opts.preview_lines_before = 3 + log(opts) + return require('guihua.gui').new_list_view(opts) end return M @@ -297,3 +114,106 @@ return M uri = "file:///Users/username/lsp_test/js/curry.js" -- ]] + +-- function M._preview_location(opts) -- location, width, pos_x, pos_y +-- local uri = opts.uri +-- if uri == nil then +-- log('invalid/nil uri ') +-- return +-- end +-- local bufnr = vim.uri_to_bufnr(uri) +-- if not api.nvim_buf_is_loaded(bufnr) then +-- vim.fn.bufload(bufnr) +-- end +-- -- +-- +-- local display_range = opts.location.range +-- -- if range.start == nil then +-- -- 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 +-- -- TODO: preview height +-- -- local contents = api.nvim_buf_get_lines(bufnr, range.start.line, range["end"].line, false) +-- -- +-- local syntax = api.nvim_buf_get_option(bufnr, 'ft') +-- if syntax == nil or #syntax < 1 then +-- syntax = 'c' +-- end +-- +-- -- trace(syntax, contents) +-- local win_opts = { +-- syntax = syntax, +-- width = opts.width, +-- height = display_range['end'].line - display_range.start.line + 1, +-- preview_height = opts.height or opts.preview_height, +-- pos_x = opts.offset_x, +-- pos_y = opts.offset_y, +-- range = opts.range, +-- display_range = display_range, +-- uri = uri, +-- allow_edit = opts.enable_edit, +-- } +-- +-- if _NgConfigValues.external then +-- win_opts.external = true +-- win_opts.relative = nil +-- end +-- -- win_opts.items = contents +-- win_opts.hl_line = opts.lnum - display_range.start.line +-- if win_opts.hl_line < 0 then +-- win_opts.hl_line = 1 +-- end +-- trace(opts.lnum, opts.range.start.line, win_opts.hl_line) +-- log(win_opts) +-- local w = TextView:new({ +-- loc = 'offset_center', +-- rect = { +-- height = win_opts.height, -- opts.preview_heigh or 12, -- TODO 12 +-- width = win_opts.width, +-- pos_x = win_opts.pos_x, +-- pos_y = win_opts.pos_y, +-- }, +-- list_view_height = win_opts.height, +-- -- data = display_data, +-- relative = win_opts.relative, +-- -- data = opts.items, -- either items or uri +-- uri = win_opts.uri, +-- syntax = win_opts.syntax, +-- enter = win_opts.enter or false, +-- range = win_opts.range, +-- border = opts.border, +-- display_range = win_opts.display_range, +-- hl_line = win_opts.hl_line, +-- allow_edit = win_opts.allow_edit, +-- external = win_opts.external, +-- }) +-- return w +-- end + +-- function M.preview_uri(opts) -- uri, width, line, col, offset_x, offset_y +-- -- local handle = vim.loop.new_async(vim.schedule_wrap(function() +-- local line_beg = opts.lnum or 2 - 1 +-- if line_beg >= opts.preview_lines_before or 1 then +-- line_beg = line_beg - opts.preview_lines_before or 1 +-- elseif line_beg >= 2 then +-- line_beg = line_beg - 2 +-- end +-- local loc = { uri = opts.uri, range = { start = { line = line_beg } } } +-- +-- -- TODO: preview height +-- loc.range['end'] = { line = opts.lnum + opts.preview_height } +-- opts.location = loc +-- +-- trace('uri', opts.uri, opts.lnum, opts.location.range.start.line, opts.location.range['end'].line) +-- return M._preview_location(opts) +-- end diff --git a/lua/navigator/reference.lua b/lua/navigator/reference.lua index 11ef6f8..9f17d08 100644 --- a/lua/navigator/reference.lua +++ b/lua/navigator/reference.lua @@ -1,10 +1,10 @@ -local util = require "navigator.util" +local util = require('navigator.util') local mk_handler = util.mk_handler local log = util.log -local lsphelper = require "navigator.lspwrapper" -local gui = require "navigator.gui" -local lsp = require "navigator.lspwrapper" -local trace = require"navigator.util".trace +local lsphelper = require('navigator.lspwrapper') +local gui = require('navigator.gui') +local lsp = require('navigator.lspwrapper') +local trace = require('navigator.util').trace ListViewCtrl = ListViewCtrl or require('guihua.listviewctrl').ListViewCtrl -- local partial = util.partial -- local cwd = vim.loop.cwd() @@ -15,7 +15,7 @@ local M = {} local ref_view = function(err, locations, ctx, cfg) local truncate = cfg and cfg.truncate or 20 local opts = {} - trace("arg1", err, ctx, locations) + trace('arg1', err, ctx, locations) trace(locations) -- log("num", num) -- log("bfnr", bufnr) @@ -26,22 +26,22 @@ local ref_view = function(err, locations, ctx, cfg) end if type(locations) ~= 'table' then log(locations) - log("ctx", ctx) - print("incorrect setup", locations) + log('ctx', ctx) + print('incorrect setup', locations) return end if locations == nil or vim.tbl_isempty(locations) then - print "References not found" + print('References not found') return end local items, width, second_part = locations_to_items(locations, truncate) local thread_items = vim.deepcopy(items) - log("splits: ", #items, #second_part) + log('splits: ', #items, #second_part) - local ft = vim.api.nvim_buf_get_option(ctx.bufnr, "ft") + local ft = vim.api.nvim_buf_get_option(ctx.bufnr, 'ft') - local wwidth = vim.api.nvim_get_option("columns") + local wwidth = vim.api.nvim_get_option('columns') local mwidth = _NgConfigValues.width width = math.min(width + 30, 120, math.floor(wwidth * mwidth)) -- log(items) @@ -49,14 +49,19 @@ local ref_view = function(err, locations, ctx, cfg) opts = { total = #locations, items = items, + rawdata = false, ft = ft, width = width, - api = "Reference", - enable_preview_edit = true + api = 'Reference', + enable_preview_edit = true, } local listview = gui.new_list_view(opts) - trace("update items", listview.ctrl.class) + if listview == nil then + print('failed to create preview windows') + return + end + -- trace("update items", listview.ctrl.class) local nv_ref_async nv_ref_async = vim.loop.new_async(vim.schedule_wrap(function() log('$$$$$$$$ seperate thread... $$$$$$$$') @@ -67,12 +72,12 @@ local ref_view = function(err, locations, ctx, cfg) vim.list_extend(thread_items, items2) - local data = require"navigator.render".prepare_for_render(thread_items, opts) + local data = require('navigator.render').prepare_for_render(thread_items, opts) listview.ctrl:on_data_update(data) if nv_ref_async then vim.loop.close(nv_ref_async) else - log("invalid asy", nv_ref_async) + log('invalid asy', nv_ref_async) end end)) @@ -80,7 +85,6 @@ local ref_view = function(err, locations, ctx, cfg) vim.loop.new_thread(function(asy) asy:send() end, nv_ref_async) - end, 100) return listview, items, width @@ -109,11 +113,11 @@ local ref_req = function() _NgConfigValues.closer() end local ref_params = vim.lsp.util.make_position_params() - ref_params.context = {includeDeclaration = true} + ref_params.context = { includeDeclaration = true } -- lsp.call_async("textDocument/references", ref_params, ref_hdlr) -- return asyncresult, canceller local bufnr = vim.api.nvim_get_current_buf() - log(bufnr) - local ids, closer = vim.lsp.buf_request(bufnr, "textDocument/references", ref_params, ref_hdlr) + log('bufnr', bufnr) + local ids, closer = vim.lsp.buf_request(bufnr, 'textDocument/references', ref_params, ref_hdlr) log(ids) _NgConfigValues.closer = closer @@ -126,10 +130,9 @@ local ref = function() local ref_params = vim.lsp.util.make_position_params() vim.lsp.for_each_buffer_client(bufnr, function(client, client_id, bufnr) if client.resolved_capabilities.find_references then - client.request("textDocument/references", ref_params, ref_hdlr, bufnr) + client.request('textDocument/references', ref_params, ref_hdlr, bufnr) end end) - end -return {reference_handler = ref_hdlr, reference = ref_req, ref_view = ref_view} +return { reference_handler = ref_hdlr, reference = ref_req, ref_view = ref_view } diff --git a/lua/navigator/symbols.lua b/lua/navigator/symbols.lua index aaed5ac..0cb6b90 100644 --- a/lua/navigator/symbols.lua +++ b/lua/navigator/symbols.lua @@ -1,11 +1,11 @@ -local gui = require "navigator.gui" +local gui = require('navigator.gui') local M = {} -local log = require"navigator.util".log -local mk_handler = require"navigator.util".mk_handler -local lsphelper = require "navigator.lspwrapper" +local log = require('navigator.util').log +local mk_handler = require('navigator.util').mk_handler +local lsphelper = require('navigator.lspwrapper') local locations_to_items = lsphelper.locations_to_items -local clone = require"guihua.util".clone -local symbol_kind = require"navigator.lspclient.lspkind".symbol_kind +local clone = require('guihua.util').clone +local symbol_kind = require('navigator.lspclient.lspkind').symbol_kind local symbols_to_items = lsphelper.symbols_to_items -- function M.document_symbols(opts) @@ -37,19 +37,19 @@ local symbols_to_items = lsphelper.symbols_to_items function M.workspace_symbols(query) opts = opts or {} local lspopts = { - loc = "top_center", + loc = 'top_center', prompt = true, -- rawdata = true, - api = " " + api = ' ', } - query = query or pcall(vim.fn.input, "Query: ") + query = query or pcall(vim.fn.input, 'Query: ') local bufnr = vim.api.nvim_get_current_buf() vim.list_extend(lspopts, opts) - local params = {query = query} + local params = { query = query } vim.lsp.for_each_buffer_client(bufnr, function(client, client_id, _bufnr) if client.resolved_capabilities.workspace_symbol then - client.request("workspace/symbol", params, M.workspace_symbol_handler, _bufnr) + client.request('workspace/symbol', params, M.workspace_symbol_handler, _bufnr) end end) end @@ -57,36 +57,37 @@ end function M.document_symbols(opts) opts = opts or {} local lspopts = { - loc = "top_center", + loc = 'top_center', prompt = true, -- rawdata = true, - api = " " + api = ' ', } local bufnr = vim.api.nvim_get_current_buf() vim.list_extend(lspopts, opts) local params = vim.lsp.util.make_position_params() - params.context = {includeDeclaration = true} - params.query = opts.prompt or "" + params.context = { includeDeclaration = true } + params.query = opts.prompt or '' vim.lsp.for_each_buffer_client(bufnr, function(client, client_id, _bufnr) if client.resolved_capabilities.document_symbol then - client.request("textDocument/documentSymbol", params, M.document_symbol_handler, _bufnr) + client.request('textDocument/documentSymbol', params, M.document_symbol_handler, _bufnr) end end) end M.document_symbol_handler = mk_handler(function(err, result, ctx) if err then - print("failed to get document symbol", ctx) + print('failed to get document symbol', ctx) end + local bufnr = ctx.bufnr or 0 if not result or vim.tbl_isempty(result) then - print("symbol not found for buf", ctx) + print('symbol not found for buf', ctx) return end -- log(result) local locations = {} - local fname = vim.fn.expand("%:p:f") + local fname = vim.fn.expand('%:p:f') local uri = vim.uri_from_fname(fname) -- vim.list_extend(locations, vim.lsp.util.symbols_to_items(result) or {}) log(result[1]) @@ -98,13 +99,13 @@ M.document_symbol_handler = mk_handler(function(err, result, ctx) item.range = result[i].range or result[i].location.range item.uri = uri item.selectionRange = result[i].selectionRange - item.detail = result[i].detail or "" - if item.detail == "()" then - item.detail = "func" + item.detail = result[i].detail or '' + if item.detail == '()' then + item.detail = 'func' end item.lnum = result[i].range.start.line + 1 - item.text = "[" .. kind .. "]" .. item.name .. " " .. item.detail + item.text = '[' .. kind .. ']' .. item.name .. ' ' .. item.detail item.filename = fname @@ -120,45 +121,23 @@ M.document_symbol_handler = mk_handler(function(err, result, ctx) child.filename = fname child.uri = uri child.lnum = c.range.start.line + 1 - child.detail = c.detail or "" - child.text = "  [" .. ckind .. "] " .. child.name .. " " .. child.detail + child.detail = c.detail or '' + child.text = '  [' .. ckind .. '] ' .. child.name .. ' ' .. child.detail table.insert(locations, child) end end end - local ft = vim.api.nvim_buf_get_option(bufnr, "ft") - -- trace(locations) - -- local items = locations_to_items(locations) - gui.new_list_view({items = locations, prompt = true, rawdata = true, ft = ft, api = " "}) - - -- if locations == nil or vim.tbl_isempty(locations) then - -- print "References not found" - -- return - -- end - -- local items = locations_to_items(locations) - -- gui.new_list_view({items = items}) - -- local filename = vim.api.nvim_buf_get_name(bufnr) - -- local items = vim.lsp.util.symbols_to_items(result, bufnr) - -- local data = {} - -- for i, item in pairs(action.items) do - -- data[i] = item.text - -- if filename ~= item.filename then - -- local cwd = vim.loop.cwd() .. "/" - -- local add = util.get_relative_path(cwd, item.filename) - -- data[i] = data[i] .. " - " .. add - -- end - -- item.text = nil - -- end - -- opts.data = data + local ft = vim.api.nvim_buf_get_option(bufnr, 'ft') + gui.new_list_view({ items = locations, prompt = true, rawdata = true, ft = ft, api = ' ' }) end) M.workspace_symbol_handler = mk_handler(function(err, result, ctx, cfg) if err then - print("failed to get workspace symbol", ctx) + print('failed to get workspace symbol', ctx) end if not result or vim.tbl_isempty(result) then - print("symbol not found for buf", ctx) + print('symbol not found for buf', ctx) return end log(result[1]) @@ -178,8 +157,8 @@ M.workspace_symbol_handler = mk_handler(function(err, result, ctx, cfg) -- end -- local items = locations_to_items(locations) - local ft = vim.api.nvim_buf_get_option(ctx.bufnr, "ft") - gui.new_list_view({items = items, prompt = true, ft = ft, rowdata = true, api = " "}) + local ft = vim.api.nvim_buf_get_option(ctx.bufnr, 'ft') + -- gui.new_list_view({items = items, prompt = true, ft = ft, rowdata = true, api = " "}) -- if locations == nil or vim.tbl_isempty(locations) then -- print "References not found" diff --git a/vim.toml b/vim.toml index f63f3f6..09031f6 100644 --- a/vim.toml +++ b/vim.toml @@ -8,6 +8,9 @@ any = true [_G] property = true writable = "new-fields" +[_NgConfigValues] +property = true +writable = "new-fields" [debug] property = true