Compare commits

...

1 Commits
master ... v0.1

@ -79,11 +79,42 @@ lua <<EOF
require'navigator'.setup()
EOF
```
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.
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
- lspconfig

@ -9,12 +9,15 @@ _NgConfigValues = {
-- -- 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
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
}
}
}
@ -23,27 +26,21 @@ vim.cmd("command! -nargs=0 LspRestart call v:lua.reload_lsp()")
local extend_config = function(opts)
opts = opts or {}
if next(opts) == nil then
return
end
if next(opts) == nil then return end
for key, value in pairs(opts) do
if _NgConfigValues[key] == nil then
error(string.format("[] Key %s not valid", key))
return
end
if type(M.config_values[key]) == "table" then
for k, v in pairs(value) do
_NgConfigValues[key][k] = v
end
if type(_NgConfigValues[key]) == "table" then
for k, v in pairs(value) do _NgConfigValues[key][k] = v end
else
_NgConfigValues[key] = value
end
end
end
M.config_values = function()
return _NgConfigValues
end
M.config_values = function() return _NgConfigValues end
M.setup = function(cfg)
extend_config(cfg)

@ -2,40 +2,36 @@ local M = {}
local ListView = require "guihua.listview"
local TextView = require "guihua.textview"
local util = require "navigator.util"
local log = require "navigator.util".log
local verbose = require "navigator.util".verbose
local log = require"navigator.util".log
local verbose = require"navigator.util".verbose
local api = vim.api
function M.new_preview(opts)
return TextView:new(
{
loc = "top_center",
rect = {
height = opts.preview_heigh or 12,
width = opts.width or 100,
pos_x = opts.pos_x or 0,
pos_y = opts.pos_y or 4
},
-- data = display_data,
relative = opts.relative,
data = opts.items,
syntax = opts.syntax,
enter = opts.enter or false,
hl_line = opts.hl_line
}
)
return TextView:new({
loc = "top_center",
rect = {
height = opts.preview_heigh or 12,
width = opts.width or 100,
pos_x = opts.pos_x or 0,
pos_y = opts.pos_y or 4
},
-- data = display_data,
relative = opts.relative,
data = opts.items,
syntax = opts.syntax,
enter = opts.enter or false,
hl_line = opts.hl_line
})
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
if opts.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
if not api.nvim_buf_is_loaded(bufnr) then vim.fn.bufload(bufnr) end
--
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
log(opts)
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")
if syntax == nil or #syntax < 1 then
syntax = api.nvim_buf_get_option(bufnr, "ft")
end
if syntax == nil or #syntax < 1 then syntax = api.nvim_buf_get_option(bufnr, "ft") end
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.hl_line = opts.lnum - range.start.line
if win_opts.hl_line < 0 then
win_opts.hl_line = 1
end
if win_opts.hl_line < 0 then win_opts.hl_line = 1 end
verbose(opts.lnum, range.start.line, win_opts.hl_line)
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
local line_beg = opts.lnum - 1
if line_beg >= 2 then
line_beg = line_beg - 2
end
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}
@ -95,7 +91,7 @@ function M.new_list_view(opts)
if opts.rawdata then
data = items
else
data = require "guihua.util".aggregate_filename(items, opts)
data = require"guihua.util".aggregate_filename(items, opts)
end
local wwidth = api.nvim_get_option("columns")
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
-- replace
-- TODO: 10 vimrc opt
if #data > 10 and opts.prompt == nil then
prompt = true
end
if #data > 10 and opts.prompt == nil then prompt = true end
local height = math.min(#data, math.floor(wheight / 2))
local offset_y = height
if prompt then
offset_y = offset_y + 1
end
return ListView:new(
{
loc = "top_center",
prompt = prompt,
relative = opts.relative,
style = opts.style,
api = opts.api,
rect = {
height = height,
local offset_y = height + 1
if prompt then offset_y = offset_y + 1 end
return ListView:new({
loc = "top_center",
prompt = prompt,
relative = opts.relative,
style = opts.style,
api = opts.api,
rect = {height = height, width = width, pos_x = 0, pos_y = 0},
-- data = display_data,
data = data,
on_confirm = opts.on_confirm or function(pos)
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,
pos_x = 0,
pos_y = 0
},
-- data = display_data,
data = data,
on_confirm = opts.on_confirm or function(pos)
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
}
)
lnum = l.lnum,
col = l.col,
offset_x = 0,
offset_y = offset_y
})
end
})
end
end

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

@ -3,7 +3,8 @@ local function set_keymap(...) vim.api.nvim_set_keymap(...) end
local event_hdlrs = {
{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()"}
}
@ -11,13 +12,15 @@ local key_maps = {
{key = "gr", func = "references()"}, {mode = "i", key = "<M-k>", func = "signature_help()"},
{key = "gs", func = "signature_help()"}, {key = "g0", func = "document_symbol()"},
{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').bufs_ts()"}, {key = "K", func = "hover()"},
{key = "ga", mode = "n", func = "code_action()"}, {key = "ga", mode = "v", func = "range_code_action()"},
{key = "<Leader>re", func = "rename()"}, {key = "<Leader>gi", func = "incoming_calls()"},
{key = "<Leader>go", func = "outgoing_calls()"}, {key = "gi", func = "implementation()"},
{key = "gt", func = "type_definition()"}, {key = "gL", func = "diagnostic.show_line_diagnostics()"},
{key = "ga", mode = "n", func = "code_action()"},
{key = "ga", mode = "v", func = "range_code_action()"}, {key = "<Leader>re", func = "rename()"},
{key = "<Leader>gi", func = "incoming_calls()"}, {key = "<Leader>go", func = "outgoing_calls()"},
{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 = "]d", func = "diagnostic.goto_next()"}, {key = "[d", func = "diagnostic.goto_prev()"},
{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 doc_fmt then
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
-- 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
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"
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,
vim.api.nvim_command [[augroup nvim_lsp_autos]]
vim.api.nvim_command [[autocmd!]]
@ -94,7 +102,8 @@ local function set_event_handler(user_opts)
else
f = "lua vim.lsp.buf." .. value.func
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)
end
vim.api.nvim_command([[augroup END]])
@ -103,15 +112,16 @@ end
local M = {}
function M.setup(user_opts)
user_opts = user_opts or {}
user_opts.cap = vim.lsp.protocol.make_client_capabilities()
set_mapping(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
vim.lsp.handlers["callHierarchy/incomingCalls"] = require"navigator.hierarchy".incoming_calls_handler
vim.lsp.handlers["callHierarchy/outgoingCalls"] = require"navigator.hierarchy".outgoing_calls_handler
vim.lsp.handlers["callHierarchy/incomingCalls"] =
require"navigator.hierarchy".incoming_calls_handler
vim.lsp.handlers["callHierarchy/outgoingCalls"] =
require"navigator.hierarchy".outgoing_calls_handler
end
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
end
vim.lsp.handlers["textDocument/typeDefinition"] = require"navigator.definition".typeDefinition_handler
vim.lsp.handlers["textDocument/implementation"] = require"navigator.implementation".implementation_handler
vim.lsp.handlers["textDocument/typeDefinition"] =
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["textDocument/publishDiagnostics"] = require"navigator.diagnostics".diagnostic_handler
vim.lsp.handlers["textDocument/publishDiagnostics"] =
require"navigator.diagnostics".diagnostic_handler
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
end

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

Loading…
Cancel
Save