#57 refactor keymapping on lsp attach

neovim_0.6
ray-x 3 years ago
parent d60b3c4024
commit 22028ff82e

@ -90,7 +90,7 @@ in the same line). Using treesitter for file preview highlighter etc
# Why a new plugin # 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: # Similar projects / special mentions:

@ -1,10 +1,11 @@
local M = {} local M = {}
_NgConfigValues = { _NgConfigValues = {
debug = false, -- log output not implemented debug = false, -- log output not implemented
width = 0.6, -- valeu of cols TODO allow float e.g. 0.6 width = 0.62, -- valeu of cols
preview_height = 0.35, height = 0.38, -- listview height
preview_lines = 40, preview_height = 0.38,
height = 0.35, preview_lines = 40, -- total lines in preview screen
preview_lines_before = 5, -- lines before the highlight line
default_mapping = true, default_mapping = true,
keymaps = {}, -- e.g keymaps={{key = "GR", func = "references()"}, } this replace gr default mapping keymaps = {}, -- e.g keymaps={{key = "GR", func = "references()"}, } this replace gr default mapping
@ -119,7 +120,7 @@ M.setup = function(cfg)
-- print("loading navigator") -- print("loading navigator")
require('navigator.lazyloader') require('navigator.lazyloader')
require('navigator.lspclient.clients').setup(_NgConfigValues) require('navigator.lspclient.clients').setup(_NgConfigValues)
require("navigator.lspclient.mapping").setup(_NgConfigValues) -- require("navigator.lspclient.mapping").setup(_NgConfigValues)
require("navigator.reference") require("navigator.reference")
require("navigator.definition") require("navigator.definition")
require("navigator.hierarchy") require("navigator.hierarchy")

@ -21,7 +21,7 @@ local function error_marker(result, client_id)
local ft = vim.fn.expand('%:h:t') -- get the current file extension local ft = vim.fn.expand('%:h:t') -- get the current file extension
local bufnr = vim.uri_to_bufnr(result.uri) 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()) -- log("not same buf", client_id, result.uri, bufnr, vim.fn.bufnr())
return return
end end
@ -38,11 +38,20 @@ local function error_marker(result, client_id)
end end
-- total line num of current buffer -- 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 -- 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 if total_num <= wheight then
return return
end end

@ -91,7 +91,9 @@ end
function M.preview_uri(opts) -- uri, width, line, col, offset_x, offset_y function M.preview_uri(opts) -- uri, width, line, col, offset_x, offset_y
local line_beg = opts.lnum - 1 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 line_beg = line_beg - 2
end end
local loc = {uri = opts.uri, range = {start = {line = line_beg}}} local loc = {uri = opts.uri, range = {start = {line = line_beg}}}

@ -1,6 +1,32 @@
-- lua-lru, LRU cache in Lua -- lua-lru, LRU cache in Lua
-- Copyright (c) 2015 Boris Nagaev -- Copyright (c) 2015 Boris Nagaev
-- See the LICENSE file for terms of use. -- 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 = {} local lru = {}
function lru.new(max_size, max_bytes) function lru.new(max_size, max_bytes)

@ -50,8 +50,6 @@ M.on_attach = function(client, bufnr)
config.lsp[client.name].on_attach(client, bufnr) config.lsp[client.name].on_attach(client, bufnr)
end end
require("navigator.lspclient.mapping").setup(_NgConfigValues)
end end
-- M.setup = function(cfg) -- M.setup = function(cfg)

@ -313,6 +313,7 @@ end
local function wait_lsp_startup(ft, retry, user_lsp_opts) local function wait_lsp_startup(ft, retry, user_lsp_opts)
retry = retry or false retry = retry or false
local clients = vim.lsp.get_active_clients() or {} local clients = vim.lsp.get_active_clients() or {}
local loaded = {} local loaded = {}
local capabilities = vim.lsp.protocol.make_client_capabilities() local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.completion.completionItem.snippetSupport = true capabilities.textDocument.completion.completionItem.snippetSupport = true

@ -1,7 +1,5 @@
local log = require"navigator.util".log local log = require"navigator.util".log
local function set_keymap(...) local trace = require"navigator.util".trace
vim.api.nvim_set_keymap(...)
end
local event_hdlrs = { local event_hdlrs = {
{ev = "BufWritePre", func = [[require "navigator.diagnostics".set_diag_loclist()]]}, {ev = "BufWritePre", func = [[require "navigator.diagnostics".set_diag_loclist()]]},
@ -42,11 +40,13 @@ local key_maps = {
{key = "<C-LeftMouse>", func = "definition()"}, {key = "<C-LeftMouse>", func = "definition()"},
{key = "g<LeftMouse>", func = "implementation()"}, {key = "g<LeftMouse>", func = "implementation()"},
{key = "<Leader>k", func = "require('navigator.dochighlight').hi_symbol()"}, {key = "<Leader>k", func = "require('navigator.dochighlight').hi_symbol()"},
{key = '<Space>wa', func = 'vim.lsp.buf.add_workspace_folder()'}, {key = '<Space>wa', func = 'add_workspace_folder()'},
{key = '<Space>wr', func = 'vim.lsp.buf.remove_workspace_folder()'}, {key = '<Space>wr', func = 'remove_workspace_folder()'},
{key = 'ff', func = 'formatting()'},
{key = 'ff', func = 'range_formatting()', mode='v'},
{key = '<Space>wl', func = 'print(vim.inspect(vim.lsp.buf.list_workspace_folders()))'}, {key = '<Space>wl', func = 'print(vim.inspect(vim.lsp.buf.list_workspace_folders()))'},
{key = "<Space>la", mode = "n", func = "require('navigator.codelens').run_action()"}, {key = "<Space>la", mode = "n", func = "require('navigator.codelens').run_action()"},
} }
-- LuaFormatter on -- LuaFormatter on
local M = {} local M = {}
@ -56,20 +56,51 @@ local ccls_mappings = {
{key = "<Leader>go", func = "require('navigator.cclshierarchy').outgoing_calls()"} {key = "<Leader>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 function set_mapping(user_opts)
local opts = {noremap = true, silent = true} local opts = {noremap = true, silent = true}
user_opts = user_opts or {} 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 bufnr = user_opts.bufnr or 0
local function buf_set_keymap(...) local function del_keymap(...)
vim.api.nvim_buf_set_keymap(bufnr, ...) vim.api.nvim_buf_del_keymap(bufnr, ...)
end end
local function set_keymap(...)
vim.api.nvim_buf_set_keymap(bufnr, ...)
end
-- local function buf_set_option(...) -- local function buf_set_option(...)
-- vim.api.nvim_buf_set_option(bufnr, ...) -- vim.api.nvim_buf_set_option(bufnr, ...)
-- end -- 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 for _, v in pairs(user_key) do
local exists = false local exists = false
for _, default in pairs(key_maps) do for _, default in pairs(key_maps) do
@ -82,9 +113,7 @@ local function set_mapping(user_opts)
table.insert(key_maps, v) table.insert(key_maps, v)
end end
end end
-- log(key_maps) local fmtkey, rfmtkey
-- local key_opts = {vim.tbl_deep_extend("force", key_maps, unpack(result))}
for _, value in pairs(key_maps) do for _, value in pairs(key_maps) do
local f = "<Cmd>lua vim.lsp.buf." .. value.func .. "<CR>" local f = "<Cmd>lua vim.lsp.buf." .. value.func .. "<CR>"
if string.find(value.func, "require") then if string.find(value.func, "require") then
@ -96,57 +125,31 @@ local function set_mapping(user_opts)
end end
local k = value.key local k = value.key
local m = value.mode or "n" local m = value.mode or "n"
-- log("binding", k, f) if string.find(value.func, "range_formatting") then
set_keymap(m, k, f, opts) rfmtkey = value.key
end elseif string.find(value.func, "formatting") then
fmtkey = value.key
-- 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
end end
trace("binding", k, f)
set_keymap(m, k, f, opts)
end end
if ccls then
-- log("override ccls", ccls_mappings)
for _, value in pairs(ccls_mappings) do
local f = "<Cmd>lua " .. value.func .. "<CR>"
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 user_opts.cap.document_formatting then
if doc_fmt then if doc_fmt then
buf_set_keymap("n", "<space>ff", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
vim.cmd([[ vim.cmd([[
aug NavigatorAuFormat aug NavigatorAuFormat
au! au!
autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting() autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting()
aug END aug END
]]) ]])
else
del_keymap('n', fmtkey)
end end
-- if user_opts.cap.document_range_formatting then -- if user_opts.cap.document_range_formatting then
if range_fmt then if not range_fmt then
buf_set_keymap("v", "<space>ff", "<cmd>lua vim.lsp.buf.range_formatting()<CR>", opts) del_keymap("v", rfmtkey)
end end
log("enable format ", doc_fmt, range_fmt) log("enable format ", doc_fmt, range_fmt)
end end
@ -162,7 +165,7 @@ end
local function set_event_handler(user_opts) local function set_event_handler(user_opts)
user_opts = user_opts or {} user_opts = user_opts or {}
local file_types = 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, -- 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 [[augroup nvim_lsp_autos]]
vim.api.nvim_command [[autocmd!]] vim.api.nvim_command [[autocmd!]]

Loading…
Cancel
Save