navigator.lua/lua/navigator/util.lua

414 lines
8.3 KiB
Lua
Raw Normal View History

2021-04-19 02:56:32 +00:00
-- retreives data form file
-- and line to highlight
-- Some of function copied from https://github.com/RishabhRD/nvim-lsputils
2021-12-29 04:28:34 +00:00
local M = { log_path = vim.lsp.get_log_path() }
2021-06-25 00:33:04 +00:00
-- local is_windows = uv.os_uname().version:match("Windows")
2022-02-17 11:21:34 +00:00
local guihua = require('guihua.util')
local nvim_0_6_1
2021-06-25 00:33:04 +00:00
M.path_sep = function()
2021-12-29 04:28:34 +00:00
local is_win = vim.loop.os_uname().sysname:find('Windows')
2021-06-25 10:28:26 +00:00
if is_win then
2021-12-29 04:28:34 +00:00
return '\\'
2021-06-25 10:28:26 +00:00
else
2021-12-29 04:28:34 +00:00
return '/'
2021-06-25 10:28:26 +00:00
end
2021-06-25 00:33:04 +00:00
end
2021-06-25 12:17:28 +00:00
local path_sep = M.path_sep()
2021-06-25 00:33:04 +00:00
M.path_cur = function()
2021-12-29 04:28:34 +00:00
local is_win = vim.loop.os_uname().sysname:find('Windows')
2021-06-25 10:28:26 +00:00
if is_win then
2021-12-29 04:28:34 +00:00
return '.\\'
2021-06-25 10:28:26 +00:00
else
2021-12-29 04:28:34 +00:00
return './'
2021-06-25 10:28:26 +00:00
end
2021-06-25 00:33:04 +00:00
end
M.round = function(x)
2021-09-22 07:28:15 +00:00
return math.max(0, math.floor(x - 0.5))
end
2021-04-19 02:56:32 +00:00
function M.get_data_from_file(filename, startLine)
local displayLine
if startLine < 3 then
displayLine = startLine
startLine = 0
else
startLine = startLine - 2
displayLine = 2
end
2021-12-29 04:28:34 +00:00
local uri = 'file:///' .. filename
2021-04-19 02:56:32 +00:00
local bufnr = vim.uri_to_bufnr(uri)
2021-06-23 14:09:33 +00:00
if not vim.api.nvim_buf_is_loaded(bufnr) then
vim.fn.bufload(bufnr)
end
2021-04-19 02:56:32 +00:00
local data = vim.api.nvim_buf_get_lines(bufnr, startLine, startLine + 8, false)
if data == nil or vim.tbl_isempty(data) then
startLine = nil
else
local len = #data
startLine = startLine + 1
for i = 1, len, 1 do
2021-12-29 04:28:34 +00:00
data[i] = startLine .. ' ' .. data[i]
2021-04-19 02:56:32 +00:00
startLine = startLine + 1
end
end
2021-12-29 04:28:34 +00:00
return { data = data, line = displayLine }
2021-04-19 02:56:32 +00:00
end
2021-06-17 15:39:41 +00:00
M.merge = function(t1, t2)
for k, v in pairs(t2) do
t1[k] = v
end
return t1
end
M.map = function(modes, key, result, options)
2021-12-29 04:28:34 +00:00
options = M.merge({ noremap = true, silent = false, expr = false, nowait = false }, options or {})
2021-06-17 15:39:41 +00:00
local buffer = options.buffer
options.buffer = nil
2021-12-29 04:28:34 +00:00
if type(modes) ~= 'table' then
modes = { modes }
2021-06-17 15:39:41 +00:00
end
for i = 1, #modes do
if buffer then
vim.api.nvim_buf_set_keymap(0, modes[i], key, result, options)
else
vim.api.nvim_set_keymap(modes[i], key, result, options)
end
end
end
2021-04-19 02:56:32 +00:00
function M.get_base(path)
local len = #path
for i = len, 1, -1 do
2021-06-25 00:33:04 +00:00
if path:sub(i, i) == path_sep then
2021-04-19 02:56:32 +00:00
local ret = path:sub(i + 1, len)
return ret
end
end
end
local function getDir(path)
local data = {}
local len = #path
if len <= 1 then
return nil
end
local last_index = 1
for i = 2, len do
local cur_char = path:sub(i, i)
2021-06-25 00:33:04 +00:00
if cur_char == path_sep then
local my_data = path:sub(last_index + 1, i - 1)
table.insert(data, my_data)
last_index = i
2021-04-19 02:56:32 +00:00
end
end
return data
2021-04-19 02:56:32 +00:00
end
function M.get_relative_path(base_path, my_path)
local base_data = getDir(base_path)
local my_data = getDir(my_path)
local base_len = #base_data
local my_len = #my_data
if base_len > my_len then
return my_path
end
2021-04-19 02:56:32 +00:00
if base_data[1] ~= my_data[1] then
return my_path
end
2021-04-19 02:56:32 +00:00
local cur = 0
for i = 1, base_len do
if base_data[i] ~= my_data[i] then
break
end
2021-04-19 02:56:32 +00:00
cur = i
end
2021-12-29 04:28:34 +00:00
local data = ''
for i = cur + 1, my_len do
2021-06-25 00:33:04 +00:00
data = data .. my_data[i] .. path_sep
end
2021-04-19 02:56:32 +00:00
data = data .. M.get_base(my_path)
return data
end
2021-12-29 04:28:34 +00:00
local level = 'error'
if _NgConfigValues.debug == true then
2021-12-29 04:28:34 +00:00
level = 'info'
elseif _NgConfigValues.debug == 'trace' then
level = 'trace'
end
2021-12-29 04:28:34 +00:00
local default_config = { use_console = false, use_file = true, level = level }
if _NgConfigValues.debug_console_output then
default_config.use_console = true
default_config.use_file = false
end
2021-12-29 04:28:34 +00:00
M._log = require('guihua.log').new(default_config, true)
2021-04-19 02:56:32 +00:00
-- add log to you lsp.log
M.log = M._log.info
2021-05-22 00:54:10 +00:00
M.info = M._log.info
M.trace = M._log.trace
M.error = M._log.error
2021-04-19 02:56:32 +00:00
function M.fmt(...)
M._log.fmt_info(...)
end
2021-04-19 02:56:32 +00:00
function M.split(inputstr, sep)
if sep == nil then
2021-12-29 04:28:34 +00:00
sep = '%s'
end
2021-04-19 02:56:32 +00:00
local t = {}
2021-12-29 04:28:34 +00:00
for str in string.gmatch(inputstr, '([^' .. sep .. ']+)') do
table.insert(t, str)
end
2021-04-19 02:56:32 +00:00
return t
end
function M.quickfix_extract(line)
-- check if it is a line of file pos been selected
local split = M.split
line = vim.trim(line)
2021-12-29 04:28:34 +00:00
local sep = split(line, ' ')
2021-04-19 02:56:32 +00:00
if #sep < 2 then
M.log(line)
return nil
end
2021-12-29 04:28:34 +00:00
sep = split(sep[1], ':')
2021-04-19 02:56:32 +00:00
if #sep < 3 then
M.log(line)
return nil
end
local location = {
2021-12-29 04:28:34 +00:00
uri = 'file:///' .. sep[1],
range = { start = { line = sep[2] - 3 > 0 and sep[2] - 3 or 1 } },
}
2021-12-29 04:28:34 +00:00
location.range['end'] = { line = sep[2] + 15 }
2021-04-19 02:56:32 +00:00
return location
end
function M.getArgs(inputstr)
2021-12-29 04:28:34 +00:00
local sep = '%s'
2021-04-19 02:56:32 +00:00
local t = {}
local cmd
2021-12-29 04:28:34 +00:00
for str in string.gmatch(inputstr, '([^' .. sep .. ']+)') do
2021-04-19 02:56:32 +00:00
if not cmd then
cmd = str
else
table.insert(t, str)
end
end
return cmd, t
end
function M.p(t)
vim.notify(vim.inspect(t), vim.log.levels.INFO)
end
2021-04-19 02:56:32 +00:00
function M.printError(msg)
2021-12-29 04:28:34 +00:00
vim.cmd('echohl ErrorMsg')
2021-04-19 02:56:32 +00:00
vim.cmd(string.format([[echomsg '%s']], msg))
2021-12-29 04:28:34 +00:00
vim.cmd('echohl None')
2021-04-19 02:56:32 +00:00
end
function M.reload()
vim.lsp.stop_client(vim.lsp.get_active_clients())
2021-12-29 04:28:34 +00:00
vim.cmd([[edit]])
2021-04-19 02:56:32 +00:00
end
function M.open_log()
local path = vim.lsp.get_log_path()
2021-12-29 04:28:34 +00:00
vim.cmd('edit ' .. path)
2021-04-19 02:56:32 +00:00
end
function table.pack(...)
2021-12-29 04:28:34 +00:00
return { n = select('#', ...), ... }
end
2021-04-19 02:56:32 +00:00
function M.show(...)
2021-12-29 04:28:34 +00:00
local string = ''
2021-04-19 02:56:32 +00:00
local args = table.pack(...)
for i = 1, args.n do
2021-12-29 04:28:34 +00:00
string = string .. tostring(args[i]) .. '\t'
end
2021-04-19 02:56:32 +00:00
2021-12-29 04:28:34 +00:00
return string .. '\n'
2021-04-19 02:56:32 +00:00
end
2021-05-02 13:59:49 +00:00
function M.split2(s, sep)
2021-04-19 02:56:32 +00:00
local fields = {}
2021-12-29 04:28:34 +00:00
sep = sep or ' '
local pattern = string.format('([^%s]+)', sep)
string.gsub(s, pattern, function(c)
fields[#fields + 1] = c
end)
2021-04-19 02:56:32 +00:00
return fields
end
function M.trim_and_pad(txt)
local len = #txt
if len <= 1 then
return
end
local tab_en = txt[1] == '\t' or false
txt = vim.trim(txt)
if tab_en then
if len - txt > 2 then
return ' ' .. txt
end
if len - txt > 0 then
return ' ' .. txt
end
end
local rep = math.min(12, len - #txt)
return string.rep(' ', rep / 4) .. txt
end
M.open_file = function(filename)
2021-12-29 04:28:34 +00:00
vim.api.nvim_command(string.format('e! %s', filename))
end
2021-04-19 02:56:32 +00:00
2022-02-17 11:21:34 +00:00
M.open_file_at = guihua.open_file_at
2021-04-19 02:56:32 +00:00
function M.exists(var)
for k, _ in pairs(_G) do
if k == var then
return true
end
end
end
2021-04-19 02:56:32 +00:00
2021-12-29 04:28:34 +00:00
local exclude_ft = { 'scrollbar', 'help', 'NvimTree' }
2021-04-24 09:55:48 +00:00
function M.exclude(fname)
for i = 1, #exclude_ft do
if string.find(fname, exclude_ft[i]) then
return true
end
end
2021-04-24 09:55:48 +00:00
return false
end
2021-05-08 04:54:37 +00:00
--- virtual text
-- name space search
local nss
local api = vim.api
local bufs
function M.set_virt_eol(bufnr, lnum, chunks, priority, id)
if nss == nil then
2021-12-29 04:28:34 +00:00
nss = api.nvim_create_namespace('navigator_search')
end
2021-05-08 04:54:37 +00:00
bufnr = bufnr == 0 and api.nvim_get_current_buf() or bufnr
bufs[bufnr] = true
-- id may be nil
2021-12-29 04:28:34 +00:00
return api.nvim_buf_set_extmark(bufnr, nss, lnum, -1, { id = id, virt_text = chunks, priority = priority })
2021-05-08 04:54:37 +00:00
end
function M.clear_buf(bufnr)
if not bufnr then
return
end
2021-05-08 04:54:37 +00:00
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
2021-05-08 04:54:37 +00:00
end
bufs[bufnr] = nil
end
end
function M.clear_all_buf()
for bufnr in pairs(bufs) do
M.clear_buf(bufnr)
end
2021-05-08 04:54:37 +00:00
bufs = {}
end
2021-08-21 23:19:11 +00:00
function M.get_current_winid()
return api.nvim_get_current_win()
end
2022-02-17 11:21:34 +00:00
function M.nvim_0_6_1()
if nvim_0_6_1 ~= nil then
return nvim_0_6_1
end
2022-02-17 11:21:34 +00:00
nvim_0_6_1 = vim.fn.has('nvim-0.6.1') == 1
if nvim_0_6_1 == false then
M.warn('Please use navigator 0.3 version for neovim version < 0.6.1')
end
2022-02-17 11:21:34 +00:00
return nvim_0_6_1
end
function M.mk_handler(fn)
return function(...)
2022-02-17 11:21:34 +00:00
if M.nvim_0_6_1() then
return fn(...)
end
end
end
function M.partial(func, arg)
return (M.mk_handler(function(...)
return func(arg, ...)
end))
end
2021-12-29 04:28:34 +00:00
function M.empty(t)
if t == nil then
return true
end
2022-01-13 23:36:54 +00:00
if type(t) ~= 'table' then
return false
end
2021-12-29 04:28:34 +00:00
return next(t) == nil
end
function M.encoding(client)
2022-01-17 05:16:31 +00:00
if type(client) ~= 'table' then
if client == nil then
client = 1
end
client = vim.lsp.get_client_by_id(client)
end
local oe = client.offset_encoding
if oe == nil then
return 'utf-8'
end
if type(oe) == 'table' then
oe = oe[1] or 'utf-8'
end
return oe
end
-- alternatively: use vim.notify("namespace does not exist or is anonymous", vim.log.levels.ERROR)
function M.warn(msg)
2022-02-17 11:21:34 +00:00
vim.notify('WRN: ' .. msg, vim.lsp.log_levels.WARN)
end
function M.error(msg)
2022-02-17 11:21:34 +00:00
vim.notify('ERR: ' .. msg, vim.lsp.log_levels.EROR)
end
function M.info(msg)
2022-02-17 11:21:34 +00:00
vim.notify('INF: ' .. msg, vim.lsp.log_levels.INFO)
end
2021-04-19 02:56:32 +00:00
return M