2022-08-31 23:19:37 +00:00
|
|
|
local util = require('navigator.util')
|
|
|
|
local log = util.log
|
|
|
|
local trace = util.trace
|
2022-02-17 11:21:34 +00:00
|
|
|
local clone = require('guihua.util').clone
|
2022-08-31 23:19:37 +00:00
|
|
|
|
|
|
|
local M = {}
|
2021-05-21 11:39:46 +00:00
|
|
|
local function filename(url)
|
2021-09-19 02:42:34 +00:00
|
|
|
if url == nil then
|
|
|
|
return ''
|
|
|
|
end
|
2022-02-17 11:21:34 +00:00
|
|
|
return url:match('^.+/(.+)$') or url
|
2021-05-21 11:39:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function extension(url)
|
2022-02-17 11:21:34 +00:00
|
|
|
local ext = url:match('^.+(%..+)$') or 'txt'
|
2021-05-21 11:39:46 +00:00
|
|
|
return string.sub(ext, 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_pads(win_width, text, postfix)
|
2021-05-24 10:20:28 +00:00
|
|
|
local trim = false
|
2021-05-21 11:39:46 +00:00
|
|
|
local margin = win_width - #text - #postfix
|
|
|
|
if margin < 0 then
|
2021-06-11 11:33:22 +00:00
|
|
|
trace('line too long', win_width, #text, #postfix)
|
2021-05-21 11:39:46 +00:00
|
|
|
if #postfix > 1 then
|
2021-05-24 10:20:28 +00:00
|
|
|
trim = true
|
2021-05-21 11:39:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
local sz = #text
|
|
|
|
if sz < 30 then
|
|
|
|
sz = 30
|
|
|
|
end
|
2021-06-08 22:34:36 +00:00
|
|
|
local space
|
2021-05-21 11:39:46 +00:00
|
|
|
local i = math.floor((sz + 10) / 10)
|
|
|
|
i = i * 10 - #text
|
2021-06-11 11:33:22 +00:00
|
|
|
trace(i, #text, #postfix, postfix, text, win_width)
|
2021-06-09 20:39:37 +00:00
|
|
|
if i + #text + #postfix < win_width then
|
2021-06-08 06:41:34 +00:00
|
|
|
local rem = win_width - i - #text - #postfix
|
2021-06-09 20:39:37 +00:00
|
|
|
rem = math.floor(rem / 10)
|
2021-06-08 22:34:36 +00:00
|
|
|
if rem > 0 then
|
|
|
|
i = i + rem * 10
|
2021-06-11 11:33:22 +00:00
|
|
|
-- log(i)
|
2021-06-08 06:41:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-27 23:31:16 +00:00
|
|
|
if i > 3 then
|
|
|
|
i = i - 3
|
|
|
|
end
|
2021-05-21 11:39:46 +00:00
|
|
|
space = string.rep(' ', i)
|
|
|
|
trace(text, i, postfix, win_width)
|
2021-05-24 10:20:28 +00:00
|
|
|
return space, trim
|
2021-05-21 11:39:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.prepare_for_render(items, opts)
|
|
|
|
opts = opts or {}
|
|
|
|
if items == nil or #items < 1 then
|
2023-02-12 20:34:02 +00:00
|
|
|
vim.notify('no item found or empty fields', vim.log.levels.INFO)
|
2021-05-21 11:39:46 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local item = clone(items[1])
|
2022-02-17 11:21:34 +00:00
|
|
|
local display_items = { item }
|
2021-05-21 11:39:46 +00:00
|
|
|
local last_summary_idx = 1
|
|
|
|
local total_ref_in_file = 1
|
2021-09-13 13:18:54 +00:00
|
|
|
local total = opts.total
|
2022-02-17 11:21:34 +00:00
|
|
|
local icon = ' '
|
|
|
|
local lspapi = opts.api or '∑'
|
2021-05-21 11:39:46 +00:00
|
|
|
|
2022-02-17 11:21:34 +00:00
|
|
|
local ok, devicons = pcall(require, 'nvim-web-devicons')
|
2021-05-21 11:39:46 +00:00
|
|
|
if ok then
|
|
|
|
local fn = filename(items[1].filename)
|
|
|
|
local ext = extension(fn)
|
|
|
|
icon = devicons.get_icon(fn, ext) or icon
|
|
|
|
end
|
2022-06-01 15:41:26 +00:00
|
|
|
-- local call_by_presented = false
|
2022-08-31 18:32:14 +00:00
|
|
|
opts.width = opts.width or math.floor(vim.api.nvim_get_option('columns') * 0.8)
|
2021-06-10 12:12:25 +00:00
|
|
|
local win_width = opts.width -- buf
|
2021-05-21 11:39:46 +00:00
|
|
|
|
|
|
|
for i = 1, #items do
|
2021-06-09 20:39:37 +00:00
|
|
|
local space
|
|
|
|
local trim
|
2021-05-21 11:39:46 +00:00
|
|
|
local lspapi_display = lspapi
|
2022-02-17 11:21:34 +00:00
|
|
|
items[i].symbol_name = items[i].symbol_name or '' -- some LSP API does not have range for this
|
2021-06-09 20:39:37 +00:00
|
|
|
|
|
|
|
local fn = display_items[last_summary_idx].filename
|
|
|
|
local dfn = items[i].display_filename
|
2021-05-21 11:39:46 +00:00
|
|
|
if last_summary_idx == 1 then
|
|
|
|
lspapi_display = items[i].symbol_name .. ' ' .. lspapi_display
|
2021-09-13 13:18:54 +00:00
|
|
|
|
2021-06-09 20:39:37 +00:00
|
|
|
trace(items[1], lspapi_display, display_items[last_summary_idx])
|
2021-05-21 11:39:46 +00:00
|
|
|
end
|
|
|
|
|
2021-06-27 23:31:16 +00:00
|
|
|
display_items[last_summary_idx].filename_only = true
|
2021-05-21 11:39:46 +00:00
|
|
|
-- trace(items[i], items[i].filename, last_summary_idx, display_items[last_summary_idx].filename)
|
2021-05-26 12:15:32 +00:00
|
|
|
-- TODO refact display_filename generate part
|
2022-06-11 05:03:28 +00:00
|
|
|
if items[i].filename == fn or opts.hide_filename then
|
2021-09-20 15:44:55 +00:00
|
|
|
space, trim = get_pads(opts.width, icon .. ' ' .. dfn, lspapi_display .. ' 14 of 33 ')
|
2021-06-09 20:39:37 +00:00
|
|
|
if trim and opts.width > 50 and #dfn > opts.width - 20 then
|
|
|
|
local fn1 = string.sub(dfn, 1, opts.width - 50)
|
|
|
|
local fn2 = string.sub(dfn, #dfn - 10, #dfn)
|
2023-10-18 22:47:41 +00:00
|
|
|
display_items[last_summary_idx].display_filename = fn1 .. '' .. fn2
|
2021-05-26 12:15:32 +00:00
|
|
|
space = ' '
|
|
|
|
-- log("trim", fn1, fn2)
|
2021-05-24 10:20:28 +00:00
|
|
|
end
|
2022-02-17 11:21:34 +00:00
|
|
|
local api_disp = string.format(
|
|
|
|
'%s %s%s%s %i',
|
|
|
|
icon,
|
|
|
|
display_items[last_summary_idx].display_filename,
|
|
|
|
space,
|
|
|
|
lspapi_display,
|
|
|
|
total_ref_in_file
|
|
|
|
)
|
2021-09-13 13:18:54 +00:00
|
|
|
|
|
|
|
if total then
|
|
|
|
api_disp = api_disp .. ' of: ' .. tostring(total)
|
|
|
|
end
|
|
|
|
|
|
|
|
display_items[last_summary_idx].text = api_disp
|
2021-05-21 11:39:46 +00:00
|
|
|
total_ref_in_file = total_ref_in_file + 1
|
|
|
|
else
|
|
|
|
lspapi_display = lspapi
|
|
|
|
item = clone(items[i])
|
|
|
|
|
2023-10-18 22:47:41 +00:00
|
|
|
space, trim =
|
|
|
|
get_pads(opts.width, icon .. ' ' .. item.display_filename, lspapi_display .. ' 12 of 34')
|
2021-06-10 12:12:25 +00:00
|
|
|
if trim and opts.width > 52 and #item.display_filename > opts.width - 20 then
|
2022-02-17 11:21:34 +00:00
|
|
|
item.display_filename = string.sub(item.display_filename, 1, opts.width - 52)
|
2023-09-26 16:36:37 +00:00
|
|
|
.. ''
|
2022-02-17 11:21:34 +00:00
|
|
|
.. string.sub(item.display_filename, #item.display_filename - 10, #item.display_filename)
|
2021-05-26 12:15:32 +00:00
|
|
|
space = ' '
|
2021-05-24 10:20:28 +00:00
|
|
|
end
|
2022-02-17 11:21:34 +00:00
|
|
|
item.text = string.format('%s %s%s%s 1', icon, item.display_filename, space, lspapi_display)
|
2021-05-21 11:39:46 +00:00
|
|
|
|
|
|
|
trace(item.text)
|
|
|
|
table.insert(display_items, item)
|
|
|
|
total_ref_in_file = 1
|
|
|
|
last_summary_idx = #display_items
|
|
|
|
end
|
|
|
|
-- content of code lines
|
|
|
|
item = clone(items[i])
|
2022-10-19 09:02:20 +00:00
|
|
|
if opts.side_panel then
|
|
|
|
item.text = item.text:gsub('%s+', ' ')
|
|
|
|
else
|
|
|
|
item.text = require('navigator.util').trim_and_pad(item.text)
|
|
|
|
end
|
2022-02-17 11:21:34 +00:00
|
|
|
item.text = string.format('%4i: %s', item.lnum, item.text)
|
|
|
|
local ts_report = ''
|
2021-05-21 11:39:46 +00:00
|
|
|
if item.lhs then
|
2021-06-15 20:46:04 +00:00
|
|
|
ts_report = _NgConfigValues.icons.value_changed
|
2021-05-21 11:39:46 +00:00
|
|
|
end
|
|
|
|
|
2022-06-11 05:03:28 +00:00
|
|
|
-- log(item.text, item.symbol_name, item.uri)
|
|
|
|
-- log(item.text)
|
2021-05-22 05:34:46 +00:00
|
|
|
if item.definition then
|
2023-11-07 12:00:36 +00:00
|
|
|
log('definition', item.call_by, item.symbol_name, item.text)
|
2022-10-19 09:02:20 +00:00
|
|
|
if opts.side_panel then
|
|
|
|
ts_report = _NgConfigValues.icons.value_definition
|
|
|
|
else
|
|
|
|
ts_report = ts_report .. _NgConfigValues.icons.value_definition .. ' '
|
|
|
|
end
|
2021-05-22 05:34:46 +00:00
|
|
|
end
|
2021-05-29 11:08:30 +00:00
|
|
|
local header_len = #ts_report + 4 -- magic number 2
|
2021-06-11 11:33:22 +00:00
|
|
|
trace(ts_report, header_len)
|
2021-05-22 05:34:46 +00:00
|
|
|
|
2021-05-21 11:39:46 +00:00
|
|
|
item.text = item.text:gsub('%s*[%[%(%{]*%s*$', '')
|
2022-08-27 01:49:08 +00:00
|
|
|
if item.call_by ~= nil and item.call_by ~= '' then
|
2023-10-18 22:47:41 +00:00
|
|
|
ts_report = ts_report .. _NgConfigValues.icons.match_kinds['function'] .. item.call_by
|
2021-05-21 11:39:46 +00:00
|
|
|
end
|
2021-05-22 05:34:46 +00:00
|
|
|
if #ts_report > 1 then
|
2021-05-24 10:20:28 +00:00
|
|
|
space, trim = get_pads(win_width, item.text, ts_report)
|
2022-08-31 18:32:14 +00:00
|
|
|
|
|
|
|
local l = math.max(20, opts.width - math.min(20, #ts_report))
|
|
|
|
if trim and #item.text < l then
|
|
|
|
trim = false
|
|
|
|
end
|
2021-05-24 10:20:28 +00:00
|
|
|
if trim then
|
2022-08-31 18:32:14 +00:00
|
|
|
item.text = string.sub(item.text, 1, l)
|
2022-08-31 23:19:37 +00:00
|
|
|
item.text = util.sub_match(item.text)
|
2022-06-08 06:56:46 +00:00
|
|
|
-- let check if there are unmatched "/'
|
2021-05-24 10:20:28 +00:00
|
|
|
end
|
2021-05-22 05:34:46 +00:00
|
|
|
if #space + #item.text + #ts_report >= win_width then
|
2022-08-29 08:20:27 +00:00
|
|
|
if #item.text + #ts_report >= win_width then
|
2022-08-27 01:49:08 +00:00
|
|
|
space = ' '
|
|
|
|
local len = math.max(win_width - #item.text - 4, 16)
|
2022-08-29 08:20:27 +00:00
|
|
|
|
|
|
|
trace('exceeding', #item.text, #ts_report, win_width, len)
|
2022-08-27 01:49:08 +00:00
|
|
|
ts_report = ts_report:sub(1, len)
|
2021-05-21 11:39:46 +00:00
|
|
|
else
|
2021-05-22 05:34:46 +00:00
|
|
|
local remain = win_width - #item.text - #ts_report
|
2022-02-17 11:21:34 +00:00
|
|
|
trace('remain', remain)
|
2021-05-21 11:39:46 +00:00
|
|
|
space = string.rep(' ', remain)
|
|
|
|
end
|
|
|
|
end
|
2021-05-22 05:34:46 +00:00
|
|
|
item.text = item.text .. space .. ts_report
|
2021-05-21 11:39:46 +00:00
|
|
|
end
|
|
|
|
local tail = display_items[#display_items].text
|
|
|
|
if tail ~= item.text then -- deduplicate
|
|
|
|
trace(item.text)
|
|
|
|
trace(item.call_by)
|
|
|
|
table.insert(display_items, item)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-27 23:31:16 +00:00
|
|
|
display_items[last_summary_idx].filename_only = true
|
2021-05-21 11:39:46 +00:00
|
|
|
-- display_items[last_summary_idx].text=string.format("%s [%i]", display_items[last_summary_idx].filename,
|
|
|
|
-- total_ref_in_file)
|
|
|
|
return display_items
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|