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:
Simrat Grewal
2022-08-10 17:42:50 -07:00
parent ce21cf3f66
commit 094334c1a8
4 changed files with 27 additions and 39 deletions

View File

@@ -13,8 +13,8 @@ local function setup_global_autocmd()
vim.cmd "au CursorHold * :lua require('symbols-outline')._highlight_current_item()" vim.cmd "au CursorHold * :lua require('symbols-outline')._highlight_current_item()"
end end
vim.cmd "au InsertLeave,WinEnter,BufEnter,BufWinEnter,TabEnter,BufWritePost * :lua require('symbols-outline')._refresh()" vim.cmd "au InsertLeave,WinEnter,BufEnter,BufWinEnter,TabEnter,BufWritePost * :lua require('symbols-outline')._refresh()"
vim.cmd "au WinEnter * lua require'symbols-outline.preview'.close()" vim.cmd "au WinEnter * lua require'symbols-outline.preview'.close()"
end end
------------------------- -------------------------
@@ -177,7 +177,6 @@ local function handler(response)
M.state.flattened_outline_items = parser.flatten(items) M.state.flattened_outline_items = parser.flatten(items)
writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items) writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items)
ui.setup_highlights()
M._highlight_current_item(M.state.code_win) M._highlight_current_item(M.state.code_win)
end end
@@ -202,6 +201,7 @@ end
function M.setup(opts) function M.setup(opts)
config.setup(opts) config.setup(opts)
ui.setup_highlights()
M.view = View:new() M.view = View:new()
setup_global_autocmd() setup_global_autocmd()

View File

@@ -180,19 +180,22 @@ end
function M.get_lines(flattened_outline_items) function M.get_lines(flattened_outline_items)
local lines = {} local lines = {}
local hl_info = {} 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 if config.options.show_guides then
-- makes the guides -- makes the guides
for index, _ in ipairs(line) do for index, _ in ipairs(line) do
local guide_hl_type = 'SymbolsOutlineConnector'
-- all items start with a space (or two) -- all items start with a space (or two)
if index == 1 then if index == 1 then
line[index] = ' ' line[index] = ' '
guide_hl_type = 'Normal'
-- if index is last, add a bottom marker if current item is last, -- if index is last, add a bottom marker if current item is last,
-- else add a middle marker -- else add a middle marker
elseif index == #line then elseif index == #line then
if value.isLast then if node.isLast then
line[index] = ui.markers.bottom line[index] = ui.markers.bottom
else else
line[index] = ui.markers.middle 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 -- else if the parent was not the last in its group, add a
-- vertical marker because there are items under us and we need -- vertical marker because there are items under us and we need
-- to point to those -- to point to those
elseif not value.hierarchy[index] then elseif not node.hierarchy[index] then
line[index] = ui.markers.vertical line[index] = ui.markers.vertical
end 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
end end
local final_prefix = {} local final_prefix = line
-- Add 1 space between the guides
for _, v in ipairs(line) do
table.insert(final_prefix, v)
table.insert(final_prefix, ' ')
end
local string_prefix = table_to_str(final_prefix) local string_prefix = table_to_str(final_prefix)
local hl_start = #string_prefix local hl_start = #string_prefix
local hl_end = #string_prefix + #value.icon local hl_end = #string_prefix + #node.icon
table.insert(lines, string_prefix .. value.icon .. ' ' .. value.name) table.insert(lines, string_prefix .. node.icon .. ' ' .. node.name)
local hl_type = config.options.symbols[symbols.kinds[value.kind]].hl local hl_type = config.options.symbols[symbols.kinds[node.kind]].hl
table.insert(hl_info, { hl_start, hl_end, hl_type }) table.insert(hl_info, { node_line, hl_start, hl_end, hl_type })
end end
return lines, hl_info return lines, hl_info
end end

View File

@@ -1,4 +1,3 @@
local vim = vim
local M = {} local M = {}
M.markers = { M.markers = {
@@ -25,11 +24,6 @@ function M.add_hover_highlight(bufnr, line, col_start)
) )
end 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() function M.setup_highlights()
-- Setup the FocusedSymbol highlight group if it hasn't been done already by -- Setup the FocusedSymbol highlight group if it hasn't been done already by
-- a theme or manually set -- a theme or manually set
@@ -52,20 +46,6 @@ function M.setup_highlights()
string.format('hi SymbolsOutlineConnector guifg=%s', comment_fg_gui) string.format('hi SymbolsOutlineConnector guifg=%s', comment_fg_gui)
) )
end 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 end
return M return M

View File

@@ -24,8 +24,8 @@ function M.write_outline(bufnr, lines)
end end
function M.add_highlights(bufnr, hl_info) function M.add_highlights(bufnr, hl_info)
for line, line_hl in ipairs(hl_info) do for _, line_hl in ipairs(hl_info) do
local hl_start, hl_end, hl_type = unpack(line_hl) local line, hl_start, hl_end, hl_type = unpack(line_hl)
vim.api.nvim_buf_add_highlight( vim.api.nvim_buf_add_highlight(
bufnr, bufnr,
hlns, hlns,