diff --git a/README.md b/README.md index dd3eec6..8ac84d0 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ in the same line). Using treesitter for file preview highlighter etc # Why a new plugin -I'd like to go beyond what the system is providing. +I'd like to go beyond what the system is offering. # Similar projects / special mentions: diff --git a/lua/navigator.lua b/lua/navigator.lua index 7ec9494..1bef8c3 100644 --- a/lua/navigator.lua +++ b/lua/navigator.lua @@ -1,10 +1,11 @@ local M = {} _NgConfigValues = { debug = false, -- log output not implemented - width = 0.6, -- valeu of cols TODO allow float e.g. 0.6 - preview_height = 0.35, - preview_lines = 40, - height = 0.35, + width = 0.62, -- valeu of cols + height = 0.38, -- listview height + preview_height = 0.38, + preview_lines = 40, -- total lines in preview screen + preview_lines_before = 5, -- lines before the highlight line default_mapping = true, keymaps = {}, -- e.g keymaps={{key = "GR", func = "references()"}, } this replace gr default mapping @@ -119,7 +120,7 @@ M.setup = function(cfg) -- print("loading navigator") require('navigator.lazyloader') require('navigator.lspclient.clients').setup(_NgConfigValues) - require("navigator.lspclient.mapping").setup(_NgConfigValues) + -- require("navigator.lspclient.mapping").setup(_NgConfigValues) require("navigator.reference") require("navigator.definition") require("navigator.hierarchy") diff --git a/lua/navigator/diagnostics.lua b/lua/navigator/diagnostics.lua index b2edefc..74465d6 100644 --- a/lua/navigator/diagnostics.lua +++ b/lua/navigator/diagnostics.lua @@ -21,7 +21,7 @@ local function error_marker(result, client_id) local ft = vim.fn.expand('%:h:t') -- get the current file extension local bufnr = vim.uri_to_bufnr(result.uri) - if bufnr ~= vim.fn.bufnr() then + if bufnr ~= vim.api.nvim_get_current_buf() then -- log("not same buf", client_id, result.uri, bufnr, vim.fn.bufnr()) return end @@ -38,11 +38,20 @@ local function error_marker(result, client_id) end -- total line num of current buffer - local winid = vim.fn.win_getid(vim.fn.winnr()) - local total_num = vim.fn.getbufinfo(vim.fn.winbufnr(winid))[1].linecount + + -- local winid = vim.fn.win_getid(vim.fn.winnr()) + -- local winid = vim.api.nvim_get_current_win() + bufnr = vim.api.nvim_get_current_buf() + local total_num = vim.fn.getbufinfo(bufnr)[1].linecount + -- local total_num = vim.fn.getbufinfo(vim.fn.winbufnr(winid))[1].linecount -- window size of current buffer - local wwidth = vim.fn.winwidth(winid) - local wheight = vim.fn.winheight(winid) + + local stats = vim.api.nvim_list_uis()[1] + local wwidth = stats.width; + local wheight = stats.height; + + -- local wwidth = vim.fn.winwidth(winid) + -- local wheight = vim.fn.winheight(winid) if total_num <= wheight then return end diff --git a/lua/navigator/gui.lua b/lua/navigator/gui.lua index 17453b6..13e6769 100644 --- a/lua/navigator/gui.lua +++ b/lua/navigator/gui.lua @@ -91,7 +91,9 @@ end function M.preview_uri(opts) -- uri, width, line, col, offset_x, offset_y local line_beg = opts.lnum - 1 - if line_beg >= 2 then + 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}}} diff --git a/lua/navigator/lru.lua b/lua/navigator/lru.lua index 2e79a51..c335312 100644 --- a/lua/navigator/lru.lua +++ b/lua/navigator/lru.lua @@ -1,6 +1,32 @@ -- lua-lru, LRU cache in Lua -- Copyright (c) 2015 Boris Nagaev -- See the LICENSE file for terms of use. +--[[ + +The MIT License (MIT) + +Copyright (c) 2015 Boris Nagaev + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +]]-- + local lru = {} function lru.new(max_size, max_bytes) diff --git a/lua/navigator/lspclient/attach.lua b/lua/navigator/lspclient/attach.lua index b055e29..39bd38a 100644 --- a/lua/navigator/lspclient/attach.lua +++ b/lua/navigator/lspclient/attach.lua @@ -50,8 +50,6 @@ M.on_attach = function(client, bufnr) config.lsp[client.name].on_attach(client, bufnr) end - require("navigator.lspclient.mapping").setup(_NgConfigValues) - end -- M.setup = function(cfg) diff --git a/lua/navigator/lspclient/clients.lua b/lua/navigator/lspclient/clients.lua index d8b5217..3c7c074 100644 --- a/lua/navigator/lspclient/clients.lua +++ b/lua/navigator/lspclient/clients.lua @@ -313,6 +313,7 @@ end local function wait_lsp_startup(ft, retry, user_lsp_opts) retry = retry or false local clients = vim.lsp.get_active_clients() or {} + local loaded = {} local capabilities = vim.lsp.protocol.make_client_capabilities() capabilities.textDocument.completion.completionItem.snippetSupport = true diff --git a/lua/navigator/lspclient/mapping.lua b/lua/navigator/lspclient/mapping.lua index 086ab4d..d8a4367 100644 --- a/lua/navigator/lspclient/mapping.lua +++ b/lua/navigator/lspclient/mapping.lua @@ -1,7 +1,5 @@ local log = require"navigator.util".log -local function set_keymap(...) - vim.api.nvim_set_keymap(...) -end +local trace = require"navigator.util".trace local event_hdlrs = { {ev = "BufWritePre", func = [[require "navigator.diagnostics".set_diag_loclist()]]}, @@ -42,11 +40,13 @@ local key_maps = { {key = "", func = "definition()"}, {key = "g", func = "implementation()"}, {key = "k", func = "require('navigator.dochighlight').hi_symbol()"}, - {key = 'wa', func = 'vim.lsp.buf.add_workspace_folder()'}, - {key = 'wr', func = 'vim.lsp.buf.remove_workspace_folder()'}, + {key = 'wa', func = 'add_workspace_folder()'}, + {key = 'wr', func = 'remove_workspace_folder()'}, + {key = 'ff', func = 'formatting()'}, + {key = 'ff', func = 'range_formatting()', mode='v'}, {key = 'wl', func = 'print(vim.inspect(vim.lsp.buf.list_workspace_folders()))'}, - {key = "la", mode = "n", func = "require('navigator.codelens').run_action()"}, + } -- LuaFormatter on local M = {} @@ -56,20 +56,51 @@ local ccls_mappings = { {key = "go", func = "require('navigator.cclshierarchy').outgoing_calls()"} } +local check_cap = function() + -- log(vim.lsp.buf_get_clients(0)) + local rsv, fmt, rfmt, ccls + for _, value in pairs(vim.lsp.buf_get_clients(0)) do + if value == nil or value.resolved_capabilities == nil then + return + end + if value.resolved_capabilities.document_formatting then + fmt = true + end + if value.resolved_capabilities.document_range_formatting then + rfmt = true + end + + -- log("override ccls", value.config) + if value.config.name == "ccls" then + ccls = true + end + end + return rsv, fmt, rfmt, ccls +end + local function set_mapping(user_opts) local opts = {noremap = true, silent = true} user_opts = user_opts or {} - local user_key = user_opts.keymaps or {} + local user_key = _NgConfigValues.keymaps or {} local bufnr = user_opts.bufnr or 0 - local function buf_set_keymap(...) - vim.api.nvim_buf_set_keymap(bufnr, ...) + local function del_keymap(...) + vim.api.nvim_buf_del_keymap(bufnr, ...) end + local function set_keymap(...) + vim.api.nvim_buf_set_keymap(bufnr, ...) + end -- local function buf_set_option(...) -- vim.api.nvim_buf_set_option(bufnr, ...) -- end + local rsv, range_fmt, doc_fmt, ccls = check_cap() + + if ccls then + vim.list_entend(key_maps, ccls_mappings) + end + for _, v in pairs(user_key) do local exists = false for _, default in pairs(key_maps) do @@ -82,9 +113,7 @@ local function set_mapping(user_opts) table.insert(key_maps, v) end end - -- log(key_maps) - - -- local key_opts = {vim.tbl_deep_extend("force", key_maps, unpack(result))} + local fmtkey, rfmtkey for _, value in pairs(key_maps) do local f = "lua vim.lsp.buf." .. value.func .. "" if string.find(value.func, "require") then @@ -96,57 +125,31 @@ local function set_mapping(user_opts) end local k = value.key local m = value.mode or "n" - -- log("binding", k, f) - set_keymap(m, k, f, opts) - end - - -- format setup - - local range_fmt = false - local doc_fmt = false - local ccls = false - -- log(vim.lsp.buf_get_clients(0)) - for _, value in pairs(vim.lsp.buf_get_clients(0)) do - if value == nil or value.resolved_capabilities == nil then - return - end - if value.resolved_capabilities.document_formatting then - doc_fmt = true - end - if value.resolved_capabilities.document_range_formatting then - range_fmt = true - end - - -- log("override ccls", value.config) - if value.config.name == "ccls" then - - ccls = true + if string.find(value.func, "range_formatting") then + rfmtkey = value.key + elseif string.find(value.func, "formatting") then + fmtkey = value.key end + trace("binding", k, f) + set_keymap(m, k, f, opts) end - if ccls then - -- log("override ccls", ccls_mappings) - for _, value in pairs(ccls_mappings) do - local f = "lua " .. value.func .. "" - local k = value.key - local m = value.mode or "n" - set_keymap(m, k, f, opts) - end - end -- if user_opts.cap.document_formatting then if doc_fmt then - buf_set_keymap("n", "ff", "lua vim.lsp.buf.formatting()", opts) vim.cmd([[ aug NavigatorAuFormat au! autocmd BufWritePre lua vim.lsp.buf.formatting() aug END ]]) + else + del_keymap('n', fmtkey) end -- if user_opts.cap.document_range_formatting then - if range_fmt then - buf_set_keymap("v", "ff", "lua vim.lsp.buf.range_formatting()", opts) + if not range_fmt then + del_keymap("v", rfmtkey) end + log("enable format ", doc_fmt, range_fmt) end @@ -162,7 +165,7 @@ end local function set_event_handler(user_opts) user_opts = user_opts or {} local file_types = - "c,cpp,h,go,python,vim,sh,javascript,html,css,lua,typescript,rust,javascriptreact,typescriptreact,json,yaml,kotlin,php,dart,nim,terraform" + "c,cpp,h,go,python,vim,sh,javascript,html,css,lua,typescript,rust,javascriptreact,typescriptreact,json,yaml,kotlin,php,dart,nim,terraform,java" -- local format_files = "c,cpp,h,go,python,vim,javascript,typescript" --html,css, vim.api.nvim_command [[augroup nvim_lsp_autos]] vim.api.nvim_command [[autocmd!]]