Compare commits

...

1 Commits
master ... v0.1

@ -79,11 +79,42 @@ lua <<EOF
require'navigator'.setup() require'navigator'.setup()
EOF EOF
``` ```
Generally speaking, you could remove most part of your lspconfig.lua and use the hooks in navigator.lua. As the Generally speaking, you could remove most part of your lspconfig.lua and use the hooks in navigator.lua. As the
navigator will bind keys and handler for you. The lsp will be loaded lazily based on filetype. navigator will bind keys and handler for you. The lsp will be loaded lazily based on filetype.
Additional configration:
```lua
require.'navigator'.setup({
debug = false, -- log output not implemented
code_action_icon = " ",
width = nil, -- valeu of cols TODO allow float e.g. 0.6
height = nil,
on_attach = nil,
-- function(client, bufnr)
-- -- your on_attach will be called at end of navigator on_attach
-- end,
sumneko_root_path = vim.fn.expand("$HOME") .. "/github/sumneko/lua-language-server",
sumneko_binary = vim.fn.expand("$HOME") ..
"/github/sumneko/lua-language-server/bin/macOS/lua-language-server",
code_action_prompt = {enable = true, sign = true, sign_priority = 40, virtual_text = true},
lsp = {
format_on_save = true, -- set to false to disasble lsp code format on save (if you are using prettier/efm/formater etc)
tsserver = {
filetypes = {'typescript'} -- disable javascript etc,
-- set to {} to disable the lspclient for all filetype
}
}
})
```
## Dependency ## Dependency
- lspconfig - lspconfig

@ -9,12 +9,15 @@ _NgConfigValues = {
-- -- your on_attach will be called at end of navigator on_attach -- -- your on_attach will be called at end of navigator on_attach
-- end, -- end,
sumneko_root_path = vim.fn.expand("$HOME") .. "/github/sumneko/lua-language-server", sumneko_root_path = vim.fn.expand("$HOME") .. "/github/sumneko/lua-language-server",
sumneko_binary = vim.fn.expand("$HOME") .. "/github/sumneko/lua-language-server/bin/macOS/lua-language-server", sumneko_binary = vim.fn.expand("$HOME") ..
code_action_prompt = { "/github/sumneko/lua-language-server/bin/macOS/lua-language-server",
enable = true, code_action_prompt = {enable = true, sign = true, sign_priority = 40, virtual_text = true},
sign = true, lsp = {
sign_priority = 40, format_on_save = true, -- set to false to disasble lsp code format on save (if you are using prettier/efm/formater etc)
virtual_text = true tsserver = {
filetypes = {'typescript'} -- disable javascript etc,
-- set to {} to disable the lspclient for all filetype
}
} }
} }
@ -23,27 +26,21 @@ vim.cmd("command! -nargs=0 LspRestart call v:lua.reload_lsp()")
local extend_config = function(opts) local extend_config = function(opts)
opts = opts or {} opts = opts or {}
if next(opts) == nil then if next(opts) == nil then return end
return
end
for key, value in pairs(opts) do for key, value in pairs(opts) do
if _NgConfigValues[key] == nil then if _NgConfigValues[key] == nil then
error(string.format("[] Key %s not valid", key)) error(string.format("[] Key %s not valid", key))
return return
end end
if type(M.config_values[key]) == "table" then if type(_NgConfigValues[key]) == "table" then
for k, v in pairs(value) do for k, v in pairs(value) do _NgConfigValues[key][k] = v end
_NgConfigValues[key][k] = v
end
else else
_NgConfigValues[key] = value _NgConfigValues[key] = value
end end
end end
end end
M.config_values = function() M.config_values = function() return _NgConfigValues end
return _NgConfigValues
end
M.setup = function(cfg) M.setup = function(cfg)
extend_config(cfg) extend_config(cfg)

@ -2,40 +2,36 @@ local M = {}
local ListView = require "guihua.listview" local ListView = require "guihua.listview"
local TextView = require "guihua.textview" local TextView = require "guihua.textview"
local util = require "navigator.util" local util = require "navigator.util"
local log = require "navigator.util".log local log = require"navigator.util".log
local verbose = require "navigator.util".verbose local verbose = require"navigator.util".verbose
local api = vim.api local api = vim.api
function M.new_preview(opts) function M.new_preview(opts)
return TextView:new( return TextView:new({
{ loc = "top_center",
loc = "top_center", rect = {
rect = { height = opts.preview_heigh or 12,
height = opts.preview_heigh or 12, width = opts.width or 100,
width = opts.width or 100, pos_x = opts.pos_x or 0,
pos_x = opts.pos_x or 0, pos_y = opts.pos_y or 4
pos_y = opts.pos_y or 4 },
}, -- data = display_data,
-- data = display_data, relative = opts.relative,
relative = opts.relative, data = opts.items,
data = opts.items, syntax = opts.syntax,
syntax = opts.syntax, enter = opts.enter or false,
enter = opts.enter or false, hl_line = opts.hl_line
hl_line = opts.hl_line })
}
)
end end
function M._preview_location(opts) --location, width, pos_x, pos_y function M._preview_location(opts) -- location, width, pos_x, pos_y
local uri = opts.location.targetUri or opts.location.uri local uri = opts.location.targetUri or opts.location.uri
if opts.uri == nil then if opts.uri == nil then
log("invalid/nil uri ") log("invalid/nil uri ")
return return
end end
local bufnr = vim.uri_to_bufnr(uri) local bufnr = vim.uri_to_bufnr(uri)
if not api.nvim_buf_is_loaded(bufnr) then if not api.nvim_buf_is_loaded(bufnr) then vim.fn.bufload(bufnr) end
vim.fn.bufload(bufnr)
end
-- --
local range = opts.location.targetRange or opts.location.range local range = opts.location.targetRange or opts.location.range
@ -53,20 +49,22 @@ function M._preview_location(opts) --location, width, pos_x, pos_y
opts.lnum = range.start.line + 1 opts.lnum = range.start.line + 1
log(opts) log(opts)
end end
local contents = api.nvim_buf_get_lines(bufnr, range.start.line, (range["end"].line or 1) + 10, false) 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") local syntax = api.nvim_buf_get_option(bufnr, "syntax")
if syntax == nil or #syntax < 1 then if syntax == nil or #syntax < 1 then syntax = api.nvim_buf_get_option(bufnr, "ft") end
syntax = api.nvim_buf_get_option(bufnr, "ft")
end
verbose(syntax, contents) verbose(syntax, contents)
local win_opts = {syntax = syntax, width = opts.width, pos_x = opts.offset_x or 0, pos_y = opts.offset_y or 10} 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.items = contents
win_opts.hl_line = opts.lnum - range.start.line win_opts.hl_line = opts.lnum - range.start.line
if win_opts.hl_line < 0 then if win_opts.hl_line < 0 then win_opts.hl_line = 1 end
win_opts.hl_line = 1
end
verbose(opts.lnum, range.start.line, win_opts.hl_line) verbose(opts.lnum, range.start.line, win_opts.hl_line)
local w = M.new_preview(win_opts) local w = M.new_preview(win_opts)
@ -75,9 +73,7 @@ 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 >= 2 then line_beg = line_beg - 2 end
line_beg = line_beg - 2
end
local loc = {uri = opts.uri, targetRange = {start = {line = line_beg}}} local loc = {uri = opts.uri, targetRange = {start = {line = line_beg}}}
-- TODO: options for 8 -- TODO: options for 8
loc.targetRange["end"] = {line = opts.lnum + 8} loc.targetRange["end"] = {line = opts.lnum + 8}
@ -95,7 +91,7 @@ function M.new_list_view(opts)
if opts.rawdata then if opts.rawdata then
data = items data = items
else else
data = require "guihua.util".aggregate_filename(items, opts) data = require"guihua.util".aggregate_filename(items, opts)
end end
local wwidth = api.nvim_get_option("columns") local wwidth = api.nvim_get_option("columns")
local width = math.min(opts.width or config.width or 120, math.floor(wwidth * 0.8)) local width = math.min(opts.width or config.width or 120, math.floor(wwidth * 0.8))
@ -104,56 +100,44 @@ function M.new_list_view(opts)
if data and not vim.tbl_isempty(data) then if data and not vim.tbl_isempty(data) then
-- replace -- replace
-- TODO: 10 vimrc opt -- TODO: 10 vimrc opt
if #data > 10 and opts.prompt == nil then if #data > 10 and opts.prompt == nil then prompt = true end
prompt = true
end
local height = math.min(#data, math.floor(wheight / 2)) local height = math.min(#data, math.floor(wheight / 2))
local offset_y = height local offset_y = height + 1
if prompt then if prompt then offset_y = offset_y + 1 end
offset_y = offset_y + 1 return ListView:new({
end loc = "top_center",
return ListView:new( prompt = prompt,
{ relative = opts.relative,
loc = "top_center", style = opts.style,
prompt = prompt, api = opts.api,
relative = opts.relative, rect = {height = height, width = width, pos_x = 0, pos_y = 0},
style = opts.style, -- data = display_data,
api = opts.api, data = data,
rect = { on_confirm = opts.on_confirm or function(pos)
height = height, if pos == 0 then pos = 1 end
local l = data[pos]
if l.filename ~= nil then
verbose("openfile ", l.filename, l.lnum, l.col)
util.open_file_at(l.filename, l.lnum, l.col)
end
end,
on_move = opts.on_move or function(pos)
if pos == 0 then pos = 1 end
local l = data[pos]
verbose("on move", pos, l.text or l, l.uri, l.filename)
-- todo fix
if l.uri == nil then l.uri = "file:///" .. l.filename end
return M.preview_uri({
uri = l.uri,
width = width, width = width,
pos_x = 0, lnum = l.lnum,
pos_y = 0 col = l.col,
}, offset_x = 0,
-- data = display_data, offset_y = offset_y
data = data, })
on_confirm = opts.on_confirm or function(pos) end
if pos == 0 then })
pos = 1
end
local l = data[pos]
if l.filename ~= nil then
verbose("openfile ", l.filename, l.lnum, l.col)
util.open_file_at(l.filename, l.lnum, l.col)
end
end,
on_move = opts.on_move or function(pos)
if pos == 0 then
pos = 1
end
local l = data[pos]
verbose("on move", pos, l.text or l, l.uri, l.filename)
-- todo fix
if l.uri == nil then
l.uri = "file:///" .. l.filename
end
return M.preview_uri(
{uri = l.uri, width = width, lnum = l.lnum, col = l.col, offset_x = 0, offset_y = offset_y}
)
end
}
)
end end
end end

@ -1,29 +1,26 @@
-- todo allow config passed in -- todo allow config passed in
local log = require "navigator.util".log local log = require"navigator.util".log
local verbose = require "navigator.util".verbose local verbose = require"navigator.util".verbose
_Loading = false _Loading = false
if packer_plugins ~= nil then if packer_plugins ~= nil then
-- packer installed -- packer installed
local loader = require "packer".loader local loader = require"packer".loader
if not packer_plugins["neovim/nvim-lspconfig"] or not packer_plugins["neovim/nvim-lspconfig"].loaded then if not packer_plugins["neovim/nvim-lspconfig"] or
loader("nvim-lspconfig") not packer_plugins["neovim/nvim-lspconfig"].loaded then loader("nvim-lspconfig") end
end
if not packer_plugins["ray-x/guihua.lua"] or not packer_plugins["guihua.lua"].loaded then if not packer_plugins["ray-x/guihua.lua"] or not packer_plugins["guihua.lua"].loaded then
loader("guihua.lua") loader("guihua.lua")
-- if lazyloading -- if lazyloading
end end
end end
local has_lsp, lspconfig = pcall(require, "lspconfig") local has_lsp, lspconfig = pcall(require, "lspconfig")
if not has_lsp then if not has_lsp then error("loading lsp config") end
error("loading lsp config")
end
local highlight = require "navigator.lspclient.highlight" local highlight = require "navigator.lspclient.highlight"
local util = lspconfig.util local util = lspconfig.util
local config = require "navigator".config_values() local config = require"navigator".config_values()
local cap = vim.lsp.protocol.make_client_capabilities() local cap = vim.lsp.protocol.make_client_capabilities()
local on_attach = require("navigator.lspclient.attach").on_attach local on_attach = require("navigator.lspclient.attach").on_attach
@ -40,11 +37,8 @@ local setups = {
filetypes = {"go", "gomod"}, filetypes = {"go", "gomod"},
message_level = vim.lsp.protocol.MessageType.Error, message_level = vim.lsp.protocol.MessageType.Error,
cmd = { cmd = {
"gopls", "gopls", -- share the gopls instance if there is one already
-- share the gopls instance if there is one already "-remote=auto", --[[ debug options ]] --
"-remote=auto",
--[[ debug options ]]
--
-- "-logfile=auto", -- "-logfile=auto",
-- "-debug=:0", -- "-debug=:0",
"-remote.debug=:0" "-remote.debug=:0"
@ -73,10 +67,7 @@ local setups = {
}, },
clangd = { clangd = {
cmd = { cmd = {
"clangd", "clangd", "--background-index", "--suggest-missing-includes", "--clang-tidy",
"--background-index",
"--suggest-missing-includes",
"--clang-tidy",
"--header-insertion=iwyu" "--header-insertion=iwyu"
}, },
filetypes = {"c", "cpp", "objc", "objcpp"}, filetypes = {"c", "cpp", "objc", "objcpp"},
@ -87,7 +78,8 @@ local setups = {
}, },
rust_analyzer = { rust_analyzer = {
root_dir = function(fname) root_dir = function(fname)
return util.root_pattern("Cargo.toml", "rust-project.json", ".git")(fname) or util.path.dirname(fname) return util.root_pattern("Cargo.toml", "rust-project.json", ".git")(fname) or
util.path.dirname(fname)
end, end,
filetypes = {"rust"}, filetypes = {"rust"},
message_level = vim.lsp.protocol.MessageType.error, message_level = vim.lsp.protocol.MessageType.error,
@ -105,7 +97,7 @@ local setups = {
on_attach = function(client, bufnr) on_attach = function(client, bufnr)
client.resolved_capabilities.execute_command = true client.resolved_capabilities.execute_command = true
highlight.diagnositc_config_sign() highlight.diagnositc_config_sign()
require "sqls".setup {picker = "telescope"} -- or default require"sqls".setup {picker = "telescope"} -- or default
end, end,
settings = { settings = {
cmd = {"sqls", "-config", "$HOME/.config/sqls/config.yml"} cmd = {"sqls", "-config", "$HOME/.config/sqls/config.yml"}
@ -133,15 +125,7 @@ local setups = {
diagnostics = { diagnostics = {
enable = true, enable = true,
-- Get the language server to recognize the `vim` global -- Get the language server to recognize the `vim` global
globals = { globals = {"vim", "describe", "it", "before_each", "after_each", "teardown", "pending"}
"vim",
"describe",
"it",
"before_each",
"after_each",
"teardown",
"pending"
}
}, },
workspace = { workspace = {
-- Make the server aware of Neovim runtime files -- Make the server aware of Neovim runtime files
@ -172,49 +156,17 @@ local setups = {
init_options = { init_options = {
compilationDatabaseDirectory = "build", compilationDatabaseDirectory = "build",
root_dir = [[ util.root_pattern("compile_commands.json", "compile_flags.txt", "CMakeLists.txt", "Makefile", ".git") or util.path.dirname ]], root_dir = [[ util.root_pattern("compile_commands.json", "compile_flags.txt", "CMakeLists.txt", "Makefile", ".git") or util.path.dirname ]],
index = { index = {threads = 2},
threads = 2 clang = {excludeArgs = {"-frounding-math"}}
},
clang = {
excludeArgs = {"-frounding-math"}
}
} }
} }
} }
local servers = { local servers = {
"angularls", "angularls", "gopls", "tsserver", "flow", "bashls", "dockerls", "julials", "pyls", "pyright",
"gopls", "jedi_language_server", "jdtls", "sumneko_lua", "vimls", "html", "jsonls", "solargraph", "cssls",
"tsserver", "yamlls", "clangd", "ccls", "sqls", "denols", "dartls", "dotls", "kotlin_language_server",
"flow", "nimls", "intelephense", "vuels", "phpactor", "omnisharp", "r_language_server", "rust_analyzer",
"bashls",
"dockerls",
"julials",
"pyls",
"pyright",
"jedi_language_server",
"jdtls",
"sumneko_lua",
"vimls",
"html",
"jsonls",
"solargraph",
"cssls",
"yamlls",
"clangd",
"ccls",
"sqls",
"denols",
"dartls",
"dotls",
"kotlin_language_server",
"nimls",
"intelephense",
"vuels",
"phpactor",
"omnisharp",
"r_language_server",
"rust_analyzer",
"terraformls" "terraformls"
} }
@ -232,11 +184,7 @@ local function load_cfg(ft, client, cfg, loaded)
local should_load = false local should_load = false
if lspft ~= nil and #lspft > 0 then if lspft ~= nil and #lspft > 0 then
for _, value in ipairs(lspft) do for _, value in ipairs(lspft) do if ft == value then should_load = true end end
if ft == value then
should_load = true
end
end
if should_load then if should_load then
for _, c in pairs(loaded) do for _, c in pairs(loaded) do
if client == c then if client == c then
@ -252,41 +200,40 @@ local function load_cfg(ft, client, cfg, loaded)
-- need to verify the lsp server is up -- need to verify the lsp server is up
end end
local function wait_lsp_startup(ft, retry) local function wait_lsp_startup(ft, retry, 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 = {}
for i = 1, 2 do for _ = 1, 2 do
for _, client in ipairs(clients) do for _, client in ipairs(clients) do
if client ~= nil then if client ~= nil then table.insert(loaded, client.name) end
table.insert(loaded, client.name)
end
end end
for _, lspclient in ipairs(servers) do for _, lspclient in ipairs(servers) do
if lsp_opts[lspclient] ~= nil and lsp_opts[lspclient].filetypes ~= nil then
if not vim.tbl_contains(lsp_opts[lspclient].filetypes, ft) then
log("ft", ft, "disabled for", lspclient)
goto continue
end
end
local cfg = setups[lspclient] or default_cfg local cfg = setups[lspclient] or default_cfg
load_cfg(ft, lspclient, cfg, loaded) load_cfg(ft, lspclient, cfg, loaded)
::continue::
end end
if not retry or ft == nil then if not retry or ft == nil then return end
return
end
-- --
local timer = vim.loop.new_timer() local timer = vim.loop.new_timer()
local i = 0 local i = 0
vim.wait( vim.wait(1000, function()
1000, clients = vim.lsp.get_active_clients() or {}
function() i = i + 1
clients = vim.lsp.get_active_clients() or {} if i > 5 or #clients > 0 then
i = i + 1 timer:close() -- Always close handles to avoid leaks.
if i > 5 or #clients > 0 then log("active", #clients, i)
timer:close() -- Always close handles to avoid leaks.
log("active", #clients, i)
_Loading = false
return true
end
_Loading = false _Loading = false
end, return true
200 end
) _Loading = false
end, 200)
end end
end end
@ -294,13 +241,11 @@ vim.cmd([[autocmd FileType * lua require'navigator.lspclient.clients'.setup()]])
local function setup(user_opts) local function setup(user_opts)
verbose(debug.traceback()) verbose(debug.traceback())
if _Loading == true then log(user_opts)
return user_opts = user_opts or _NgConfigValues -- incase setup was triggered from autocmd
end if _Loading == true then return end
local ft = vim.bo.filetype local ft = vim.bo.filetype
if ft == nil then if ft == nil then ft = vim.api.nvim_buf_get_option(0, "filetype") end
ft = vim.api.nvim_buf_get_option(0, "filetype")
end
if ft == nil or ft == "" then if ft == nil or ft == "" then
log("nil filetype") log("nil filetype")
@ -309,22 +254,10 @@ local function setup(user_opts)
log("loading for ft ", ft) log("loading for ft ", ft)
local retry = true local retry = true
local disable_ft = { local disable_ft = {
"NvimTree", "NvimTree", "guihua", "clap_input", "clap_spinner", "vista", "TelescopePrompt", "csv", "txt",
"guihua", "markdown", "defx"
"clap_input",
"clap_spinner",
"vista",
"TelescopePrompt",
"csv",
"txt",
"markdown",
"defx"
} }
for i = 1, #disable_ft do for i = 1, #disable_ft do if ft == disable_ft[i] then return end end
if ft == disable_ft[i] then
return
end
end
if lspconfig == nil then if lspconfig == nil then
error("lsp-config need installed and enabled") error("lsp-config need installed and enabled")
return return
@ -332,9 +265,9 @@ local function setup(user_opts)
highlight.diagnositc_config_sign() highlight.diagnositc_config_sign()
highlight.add_highlight() highlight.add_highlight()
local lsp_opts = user_opts.lsp
_Loading = true _Loading = true
wait_lsp_startup(ft, retry) wait_lsp_startup(ft, retry, lsp_opts)
_Loading = false _Loading = false
end end
return {setup = setup, cap = cap} return {setup = setup, cap = cap}

@ -3,7 +3,8 @@ local function set_keymap(...) vim.api.nvim_set_keymap(...) end
local event_hdlrs = { local event_hdlrs = {
{ev = "BufWritePre", func = "diagnostic.set_loclist({open_loclist = false})"}, {ev = "BufWritePre", func = "diagnostic.set_loclist({open_loclist = false})"},
{ev = "CursorHold", func = "document_highlight()"}, {ev = "CursorHoldI", func = "document_highlight()"}, {ev = "CursorHold", func = "document_highlight()"},
{ev = "CursorHoldI", func = "document_highlight()"},
{ev = "CursorMoved", func = "clear_references()"} {ev = "CursorMoved", func = "clear_references()"}
} }
@ -11,13 +12,15 @@ local key_maps = {
{key = "gr", func = "references()"}, {mode = "i", key = "<M-k>", func = "signature_help()"}, {key = "gr", func = "references()"}, {mode = "i", key = "<M-k>", func = "signature_help()"},
{key = "gs", func = "signature_help()"}, {key = "g0", func = "document_symbol()"}, {key = "gs", func = "signature_help()"}, {key = "g0", func = "document_symbol()"},
{key = "gW", func = "workspace_symbol()"}, {key = "<c-]>", func = "definition()"}, {key = "gW", func = "workspace_symbol()"}, {key = "<c-]>", func = "definition()"},
{key = "gD", func = "declaration()"}, {key = "gp", func = "require('navigator.definition').definition_preview()"}, {key = "gD", func = "declaration()"},
{key = "gp", func = "require('navigator.definition').definition_preview()"},
{key = "gT", func = "require('navigator.treesitter').buf_ts()"}, {key = "gT", func = "require('navigator.treesitter').buf_ts()"},
{key = "GT", func = "require('navigator.treesitter').bufs_ts()"}, {key = "K", func = "hover()"}, {key = "GT", func = "require('navigator.treesitter').bufs_ts()"}, {key = "K", func = "hover()"},
{key = "ga", mode = "n", func = "code_action()"}, {key = "ga", mode = "v", func = "range_code_action()"}, {key = "ga", mode = "n", func = "code_action()"},
{key = "<Leader>re", func = "rename()"}, {key = "<Leader>gi", func = "incoming_calls()"}, {key = "ga", mode = "v", func = "range_code_action()"}, {key = "<Leader>re", func = "rename()"},
{key = "<Leader>go", func = "outgoing_calls()"}, {key = "gi", func = "implementation()"}, {key = "<Leader>gi", func = "incoming_calls()"}, {key = "<Leader>go", func = "outgoing_calls()"},
{key = "gt", func = "type_definition()"}, {key = "gL", func = "diagnostic.show_line_diagnostics()"}, {key = "gi", func = "implementation()"}, {key = "gt", func = "type_definition()"},
{key = "gL", func = "diagnostic.show_line_diagnostics()"},
{key = "gG", func = "require('navigator.diagnostics').show_diagnostic()"}, {key = "gG", func = "require('navigator.diagnostics').show_diagnostic()"},
{key = "]d", func = "diagnostic.goto_next()"}, {key = "[d", func = "diagnostic.goto_prev()"}, {key = "]d", func = "diagnostic.goto_next()"}, {key = "[d", func = "diagnostic.goto_prev()"},
{key = "]r", func = "require('navigator.treesitter').goto_next_usage()"}, {key = "]r", func = "require('navigator.treesitter').goto_next_usage()"},
@ -74,15 +77,20 @@ local function set_mapping(user_opts)
-- 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) buf_set_keymap("n", "<space>ff", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
vim.cmd([[autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting()]]) if _NgConfigValues.lsp.format_on_save then
vim.cmd([[autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting()]])
end
end end
-- if user_opts.cap.document_range_formatting then -- if user_opts.cap.document_range_formatting then
if range_fmt then buf_set_keymap("v", "<space>ff", "<cmd>lua vim.lsp.buf.range_formatting()<CR>", opts) end if range_fmt then
buf_set_keymap("v", "<space>ff", "<cmd>lua vim.lsp.buf.range_formatting()<CR>", opts)
end
end 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 = "c,cpp,h,go,python,vim,sh,javascript,html,css,lua,typescript,rust" 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"
-- 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!]]
@ -94,7 +102,8 @@ local function set_event_handler(user_opts)
else else
f = "lua vim.lsp.buf." .. value.func f = "lua vim.lsp.buf." .. value.func
end end
local cmd = "autocmd FileType " .. file_types .. " autocmd nvim_lsp_autos " .. value.ev .. " <buffer> silent! " .. f local cmd = "autocmd FileType " .. file_types .. " autocmd nvim_lsp_autos " .. value.ev ..
" <buffer> silent! " .. f
vim.api.nvim_command(cmd) vim.api.nvim_command(cmd)
end end
vim.api.nvim_command([[augroup END]]) vim.api.nvim_command([[augroup END]])
@ -103,15 +112,16 @@ end
local M = {} local M = {}
function M.setup(user_opts) function M.setup(user_opts)
user_opts = user_opts or {}
user_opts.cap = vim.lsp.protocol.make_client_capabilities()
set_mapping(user_opts) set_mapping(user_opts)
set_event_handler(user_opts) set_event_handler(user_opts)
local cap = user_opts.cap or {}
local cap = user_opts.cap or vim.lsp.protocol.make_client_capabilities()
if cap.call_hierarchy or cap.callHierarchy then if cap.call_hierarchy or cap.callHierarchy then
vim.lsp.handlers["callHierarchy/incomingCalls"] = require"navigator.hierarchy".incoming_calls_handler vim.lsp.handlers["callHierarchy/incomingCalls"] =
vim.lsp.handlers["callHierarchy/outgoingCalls"] = require"navigator.hierarchy".outgoing_calls_handler require"navigator.hierarchy".incoming_calls_handler
vim.lsp.handlers["callHierarchy/outgoingCalls"] =
require"navigator.hierarchy".outgoing_calls_handler
end end
vim.lsp.handlers["textDocument/references"] = require"navigator.reference".reference_handler vim.lsp.handlers["textDocument/references"] = require"navigator.reference".reference_handler
@ -122,15 +132,21 @@ function M.setup(user_opts)
vim.lsp.handlers["textDocument/declaration"] = require"navigator.definition".declaration_handler vim.lsp.handlers["textDocument/declaration"] = require"navigator.definition".declaration_handler
end end
vim.lsp.handlers["textDocument/typeDefinition"] = require"navigator.definition".typeDefinition_handler vim.lsp.handlers["textDocument/typeDefinition"] =
vim.lsp.handlers["textDocument/implementation"] = require"navigator.implementation".implementation_handler require"navigator.definition".typeDefinition_handler
vim.lsp.handlers["textDocument/implementation"] =
require"navigator.implementation".implementation_handler
vim.lsp.handlers["textDocument/documentSymbol"] = require"navigator.symbols".document_symbol_handler vim.lsp.handlers["textDocument/documentSymbol"] =
require"navigator.symbols".document_symbol_handler
vim.lsp.handlers["workspace/symbol"] = require"navigator.symbols".workspace_symbol_handler vim.lsp.handlers["workspace/symbol"] = require"navigator.symbols".workspace_symbol_handler
vim.lsp.handlers["textDocument/publishDiagnostics"] = require"navigator.diagnostics".diagnostic_handler vim.lsp.handlers["textDocument/publishDiagnostics"] =
require"navigator.diagnostics".diagnostic_handler
local hassig, sig = pcall(require, "lsp_signature") local hassig, sig = pcall(require, "lsp_signature")
if not hassig then vim.lsp.handlers["textDocument/signatureHelp"] = require"navigator.signature".signature_handler end if not hassig then
vim.lsp.handlers["textDocument/signatureHelp"] = require"navigator.signature".signature_handler
end
-- vim.lsp.handlers["textDocument/hover"] = require 'navigator.hover'.hover_handler -- vim.lsp.handlers["textDocument/hover"] = require 'navigator.hover'.hover_handler
end end

@ -1,10 +1,7 @@
-- retreives data form file -- retreives data form file
-- and line to highlight -- and line to highlight
-- Some of function copied from https://github.com/RishabhRD/nvim-lsputils -- Some of function copied from https://github.com/RishabhRD/nvim-lsputils
local M = {log_path = vim.lsp.get_log_path()}
local M = {
log_path = vim.lsp.get_log_path()
}
function M.get_data_from_file(filename, startLine) function M.get_data_from_file(filename, startLine)
local displayLine local displayLine
if startLine < 3 then if startLine < 3 then
@ -28,10 +25,7 @@ function M.get_data_from_file(filename, startLine)
startLine = startLine + 1 startLine = startLine + 1
end end
end end
return { return {data = data, line = displayLine}
data = data,
line = displayLine
}
end end
function M.get_base(path) function M.get_base(path)
@ -47,9 +41,7 @@ end
local function getDir(path) local function getDir(path)
local data = {} local data = {}
local len = #path local len = #path
if len <= 1 then if len <= 1 then return nil end
return nil
end
local last_index = 1 local last_index = 1
for i = 2, len do for i = 2, len do
local cur_char = path:sub(i, i) local cur_char = path:sub(i, i)
@ -68,35 +60,22 @@ function M.get_relative_path(base_path, my_path)
local base_len = #base_data local base_len = #base_data
local my_len = #my_data local my_len = #my_data
if base_len > my_len then if base_len > my_len then return my_path end
return my_path
end
if base_data[1] ~= my_data[1] then if base_data[1] ~= my_data[1] then return my_path end
return my_path
end
local cur = 0 local cur = 0
for i = 1, base_len do for i = 1, base_len do
if base_data[i] ~= my_data[i] then if base_data[i] ~= my_data[i] then break end
break
end
cur = i cur = i
end end
local data = "" local data = ""
for i = cur + 1, my_len do for i = cur + 1, my_len do data = data .. my_data[i] .. "/" end
data = data .. my_data[i] .. "/"
end
data = data .. M.get_base(my_path) data = data .. M.get_base(my_path)
return data return data
end end
local default_config = { local default_config = {plugin = "navigator", use_console = false, use_file = true, level = "error"}
plugin = "navigator",
use_console = false,
use_file = true,
level = "debug"
}
M._log = require("guihua.log").new({level = default_config.level}, true) M._log = require("guihua.log").new({level = default_config.level}, true)
@ -104,24 +83,16 @@ M._log = require("guihua.log").new({level = default_config.level}, true)
M.log = M._log.info M.log = M._log.info
M.verbose = M._log.debug M.verbose = M._log.debug
function M.fmt(...) function M.fmt(...) M._log.fmt_info(...) end
M._log.fmt_info(...)
end
function M.split(inputstr, sep) function M.split(inputstr, sep)
if sep == nil then if sep == nil then sep = "%s" end
sep = "%s"
end
local t = {} local t = {}
for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do table.insert(t, str) end
table.insert(t, str)
end
return t return t
end end
function M.trim_space(s) function M.trim_space(s) return s:match("^%s*(.-)%s*$") end
return s:match("^%s*(.-)%s*$")
end
function M.quickfix_extract(line) function M.quickfix_extract(line)
-- check if it is a line of file pos been selected -- check if it is a line of file pos been selected
@ -137,7 +108,10 @@ function M.quickfix_extract(line)
M.log(line) M.log(line)
return nil return nil
end end
local location = {uri = "file:///" .. sep[1], range = {start = {line = sep[2] - 3 > 0 and sep[2] - 3 or 1}}} local location = {
uri = "file:///" .. sep[1],
range = {start = {line = sep[2] - 3 > 0 and sep[2] - 3 or 1}}
}
location.range["end"] = {line = sep[2] + 15} location.range["end"] = {line = sep[2] + 15}
return location return location
end end
@ -156,9 +130,7 @@ function M.getArgs(inputstr)
return cmd, t return cmd, t
end end
function M.p(t) function M.p(t) print(vim.inspect(t)) end
print(vim.inspect(t))
end
function M.printError(msg) function M.printError(msg)
vim.cmd("echohl ErrorMsg") vim.cmd("echohl ErrorMsg")
@ -176,18 +148,14 @@ function M.open_log()
vim.cmd("edit " .. path) vim.cmd("edit " .. path)
end end
function table.pack(...) function table.pack(...) return {n = select("#", ...), ...} end
return {n = select("#", ...), ...}
end
function M.show(...) function M.show(...)
local string = "" local string = ""
local args = table.pack(...) local args = table.pack(...)
for i = 1, args.n do for i = 1, args.n do string = string .. tostring(args[i]) .. "\t" end
string = string .. tostring(args[i]) .. "\t"
end
return string .. "\n" return string .. "\n"
end end
@ -197,48 +165,26 @@ function M.split2(s, sep)
sep = sep or " " sep = sep or " "
local pattern = string.format("([^%s]+)", sep) local pattern = string.format("([^%s]+)", sep)
string.gsub( string.gsub(s, pattern, function(c) fields[#fields + 1] = c end)
s,
pattern,
function(c)
fields[#fields + 1] = c
end
)
return fields return fields
end end
M.open_file = function(filename) M.open_file = function(filename) vim.api.nvim_command(string.format("e! %s", filename)) end
vim.api.nvim_command(string.format("e! %s", filename))
end
M.open_file_at = function(filename, line, col) M.open_file_at = function(filename, line, col)
vim.api.nvim_command(string.format("e! +%s %s", line, filename)) vim.api.nvim_command(string.format("e! +%s %s", line, filename))
col = col or 1 col = col or 1
vim.api.nvim_command(string.format("normal! %dl", col-1)) vim.api.nvim_command(string.format("normal! %dl", col - 1))
end end
function M.exists(var) function M.exists(var) for k, _ in pairs(_G) do if k == var then return true end end end
for k, _ in pairs(_G) do
if k == var then
return true
end
end
end
function M.partial(func, arg) function M.partial(func, arg) return (function(...) return func(arg, ...) end) end
return (function(...)
return func(arg, ...)
end)
end
local exclude_ft = {"scrollbar", "help", "NvimTree"} local exclude_ft = {"scrollbar", "help", "NvimTree"}
function M.exclude(fname) function M.exclude(fname)
for i = 1, #exclude_ft do for i = 1, #exclude_ft do if string.find(fname, exclude_ft[i]) then return true end end
if string.find(fname, exclude_ft[i]) then
return true
end
end
return false return false
end end
@ -250,33 +196,28 @@ local api = vim.api
local bufs local bufs
function M.set_virt_eol(bufnr, lnum, chunks, priority, id) function M.set_virt_eol(bufnr, lnum, chunks, priority, id)
if nss == nil then if nss == nil then nss = api.nvim_create_namespace("navigator_search") end
nss = api.nvim_create_namespace("navigator_search")
end
bufnr = bufnr == 0 and api.nvim_get_current_buf() or bufnr bufnr = bufnr == 0 and api.nvim_get_current_buf() or bufnr
bufs[bufnr] = true bufs[bufnr] = true
-- id may be nil -- id may be nil
return api.nvim_buf_set_extmark(bufnr, nss, lnum, -1, {id = id, virt_text = chunks, priority = priority}) return api.nvim_buf_set_extmark(bufnr, nss, lnum, -1,
{id = id, virt_text = chunks, priority = priority})
end end
function M.clear_buf(bufnr) function M.clear_buf(bufnr)
if not bufnr then if not bufnr then return end
return
end
bufnr = bufnr == 0 and api.nvim_get_current_buf() or bufnr bufnr = bufnr == 0 and api.nvim_get_current_buf() or bufnr
if bufs[bufnr] then if bufs[bufnr] then
if api.nvim_buf_is_valid(bufnr) then if api.nvim_buf_is_valid(bufnr) then
api.nvim_buf_clear_namespace(bufnr, nss, 0, -1) api.nvim_buf_clear_namespace(bufnr, nss, 0, -1)
-- nvim_buf_del_extmark -- nvim_buf_del_extmark
end end
bufs[bufnr] = nil bufs[bufnr] = nil
end end
end end
function M.clear_all_buf() function M.clear_all_buf()
for bufnr in pairs(bufs) do for bufnr in pairs(bufs) do M.clear_buf(bufnr) end
M.clear_buf(bufnr)
end
bufs = {} bufs = {}
end end

Loading…
Cancel
Save