feat(ui): Rework how highlights are applied to outlines
* Previously we used vim's pattern matching which is not a great way to do this in neovim, as tree-sitter doesn't support it. * Now we use the neovim apis which are range based
This commit is contained in:
@@ -180,19 +180,22 @@ end
|
||||
function M.get_lines(flattened_outline_items)
|
||||
local lines = {}
|
||||
local hl_info = {}
|
||||
for _, value in ipairs(flattened_outline_items) do
|
||||
local line = str_to_table(string.rep(' ', value.depth))
|
||||
|
||||
for node_line, node in ipairs(flattened_outline_items) do
|
||||
local line = str_to_table(string.rep(' ', node.depth))
|
||||
|
||||
if config.options.show_guides then
|
||||
-- makes the guides
|
||||
for index, _ in ipairs(line) do
|
||||
local guide_hl_type = 'SymbolsOutlineConnector'
|
||||
-- all items start with a space (or two)
|
||||
if index == 1 then
|
||||
line[index] = ' '
|
||||
guide_hl_type = 'Normal'
|
||||
-- if index is last, add a bottom marker if current item is last,
|
||||
-- else add a middle marker
|
||||
elseif index == #line then
|
||||
if value.isLast then
|
||||
if node.isLast then
|
||||
line[index] = ui.markers.bottom
|
||||
else
|
||||
line[index] = ui.markers.middle
|
||||
@@ -200,25 +203,30 @@ function M.get_lines(flattened_outline_items)
|
||||
-- else if the parent was not the last in its group, add a
|
||||
-- vertical marker because there are items under us and we need
|
||||
-- to point to those
|
||||
elseif not value.hierarchy[index] then
|
||||
elseif not node.hierarchy[index] then
|
||||
line[index] = ui.markers.vertical
|
||||
end
|
||||
|
||||
line[index] = line[index] .. ' '
|
||||
|
||||
local guide_hl_start = index * 2
|
||||
local guide_hl_end = index * 2 + #line[index]
|
||||
|
||||
table.insert(
|
||||
hl_info,
|
||||
{ node_line, guide_hl_start, guide_hl_end, guide_hl_type }
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
local final_prefix = {}
|
||||
-- Add 1 space between the guides
|
||||
for _, v in ipairs(line) do
|
||||
table.insert(final_prefix, v)
|
||||
table.insert(final_prefix, ' ')
|
||||
end
|
||||
local final_prefix = line
|
||||
|
||||
local string_prefix = table_to_str(final_prefix)
|
||||
local hl_start = #string_prefix
|
||||
local hl_end = #string_prefix + #value.icon
|
||||
table.insert(lines, string_prefix .. value.icon .. ' ' .. value.name)
|
||||
local hl_type = config.options.symbols[symbols.kinds[value.kind]].hl
|
||||
table.insert(hl_info, { hl_start, hl_end, hl_type })
|
||||
local hl_end = #string_prefix + #node.icon
|
||||
table.insert(lines, string_prefix .. node.icon .. ' ' .. node.name)
|
||||
local hl_type = config.options.symbols[symbols.kinds[node.kind]].hl
|
||||
table.insert(hl_info, { node_line, hl_start, hl_end, hl_type })
|
||||
end
|
||||
return lines, hl_info
|
||||
end
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
local vim = vim
|
||||
local M = {}
|
||||
|
||||
M.markers = {
|
||||
@@ -25,11 +24,6 @@ function M.add_hover_highlight(bufnr, line, col_start)
|
||||
)
|
||||
end
|
||||
|
||||
local function highlight_text(name, text, hl_group)
|
||||
vim.cmd(string.format('syn match %s /%s/', name, text))
|
||||
vim.cmd(string.format('hi def link %s %s', name, hl_group))
|
||||
end
|
||||
|
||||
function M.setup_highlights()
|
||||
-- Setup the FocusedSymbol highlight group if it hasn't been done already by
|
||||
-- a theme or manually set
|
||||
@@ -52,20 +46,6 @@ function M.setup_highlights()
|
||||
string.format('hi SymbolsOutlineConnector guifg=%s', comment_fg_gui)
|
||||
)
|
||||
end
|
||||
|
||||
-- markers
|
||||
highlight_text('marker_middle', M.markers.middle, 'SymbolsOutlineConnector')
|
||||
highlight_text(
|
||||
'marker_vertical',
|
||||
M.markers.vertical,
|
||||
'SymbolsOutlineConnector'
|
||||
)
|
||||
highlight_text(
|
||||
'markers_horizontal',
|
||||
M.markers.horizontal,
|
||||
'SymbolsOutlineConnector'
|
||||
)
|
||||
highlight_text('markers_bottom', M.markers.bottom, 'SymbolsOutlineConnector')
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -24,8 +24,8 @@ function M.write_outline(bufnr, lines)
|
||||
end
|
||||
|
||||
function M.add_highlights(bufnr, hl_info)
|
||||
for line, line_hl in ipairs(hl_info) do
|
||||
local hl_start, hl_end, hl_type = unpack(line_hl)
|
||||
for _, line_hl in ipairs(hl_info) do
|
||||
local line, hl_start, hl_end, hl_type = unpack(line_hl)
|
||||
vim.api.nvim_buf_add_highlight(
|
||||
bufnr,
|
||||
hlns,
|
||||
|
||||
Reference in New Issue
Block a user