feat: Line numbers next to symbols for quick jumping

See reasoning in:
simrat39/symbols-outline.nvim#212

This feature is turned off by default to avoid clutter, and many users
might not want this feature on by default.
This commit is contained in:
hedy
2023-11-08 08:02:42 +08:00
parent 03859e9ab5
commit 0e785762f7
4 changed files with 138 additions and 8 deletions

View File

@@ -25,6 +25,7 @@ M.defaults = {
show_relative_numbers = false,
show_cursorline = true,
show_symbol_details = true,
show_symbol_lineno = false,
preview_bg_highlight = 'Pmenu',
winblend = 0,
autofold_depth = nil,

View File

@@ -94,6 +94,8 @@ end
function M.get_lines(flattened_outline_items)
local lines = {}
local hl_info = {}
local guide_hl_info = {}
local lineno_max = 0
for node_line, node in ipairs(flattened_outline_items) do
local depth = node.depth
@@ -102,8 +104,12 @@ function M.get_lines(flattened_outline_items)
local line = t_utils.str_to_table(string.rep(' ', depth + marker_space))
local running_length = 1
if node.range_start+1 > lineno_max then
lineno_max = node.range_start+1
end
local function add_guide_hl(from, to)
table.insert(hl_info, {
table.insert(guide_hl_info, {
node_line,
from,
to,
@@ -184,7 +190,46 @@ function M.get_lines(flattened_outline_items)
node.prefix_length = #string_prefix + #node.icon + 1
end
return lines, hl_info
local final_hl = {}
if config.options.show_symbol_lineno then
-- Width of the highest lineno value
local max_width = #tostring(lineno_max)
-- Padded prefix to the right of lineno for better readability if linenos
-- get more than 2 digits.
local prefix = string.rep(' ', math.max(2, max_width) - 1)
-- Offset to hl_info due to adding lineno on the left of each symbol line
local total_offset = #prefix
for i, node in ipairs(flattened_outline_items) do
lines[i] = prefix .. lines[i]
table.insert(final_hl, {
hl_info[i][1], -- node_line
hl_info[i][2] + total_offset, -- start
hl_info[i][3] + total_offset, -- end
hl_info[i][4] -- type
})
node.prefix_length = node.prefix_length + total_offset
end
if config.options.guides.enabled then
for _, hl in ipairs(guide_hl_info) do
table.insert(final_hl, {
hl[1],
hl[2] + total_offset,
hl[3] + total_offset,
hl[4]
})
end
end
else
-- Merge lists hl_info and guide_hl_info
final_hl = hl_info
if config.options.guides.enabled then
for _, hl in ipairs(guide_hl_info) do
table.insert(final_hl, hl)
end
end
end
return lines, final_hl
end
function M.get_details(flattened_outline_items)
@@ -195,4 +240,18 @@ function M.get_details(flattened_outline_items)
return lines
end
function M.get_lineno(flattened_outline_items)
local lines = {}
local max = 0
for _, value in ipairs(flattened_outline_items) do
local line = value.range_start+1
if line > max then
max = line
end
-- Not padded here
table.insert(lines, tostring(line))
end
return lines, max
end
return M

View File

@@ -65,6 +65,26 @@ function M.write_details(bufnr, lines)
end
end
function M.write_lineno(bufnr, lines, max)
if not is_buffer_outline(bufnr) then
return
end
if not config.options.show_symbol_lineno then
return
end
local maxwidth = #tostring(max)
for index, value in ipairs(lines) do
local leftpad = string.rep(' ', maxwidth-#value)
vim.api.nvim_buf_set_extmark(bufnr, ns, index - 1, -1, {
virt_text = { {leftpad..value, 'LineNr' } },
virt_text_pos = 'overlay',
virt_text_win_col = 0,
hl_mode = 'combine',
})
end
end
local function clear_virt_text(bufnr)
vim.api.nvim_buf_clear_namespace(bufnr, -1, 0, -1)
end
@@ -101,8 +121,10 @@ function M.parse_and_write(bufnr, flattened_outline_items)
clear_virt_text(bufnr)
local details = parser.get_details(flattened_outline_items)
local lineno, lineno_max = parser.get_lineno(flattened_outline_items)
M.add_highlights(bufnr, hl_info, flattened_outline_items)
M.write_details(bufnr, details)
M.write_lineno(bufnr, lineno, lineno_max)
end
return M