From 094334c1a8b7e308f83aa8d526fdfd68a1b728f4 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Wed, 10 Aug 2022 17:42:50 -0700 Subject: [PATCH] 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 --- lua/symbols-outline.lua | 6 +++--- lua/symbols-outline/parser.lua | 36 +++++++++++++++++++++------------- lua/symbols-outline/ui.lua | 20 ------------------- lua/symbols-outline/writer.lua | 4 ++-- 4 files changed, 27 insertions(+), 39 deletions(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index 68805fc..3cea2c4 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -13,8 +13,8 @@ local function setup_global_autocmd() vim.cmd "au CursorHold * :lua require('symbols-outline')._highlight_current_item()" end -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 InsertLeave,WinEnter,BufEnter,BufWinEnter,TabEnter,BufWritePost * :lua require('symbols-outline')._refresh()" + vim.cmd "au WinEnter * lua require'symbols-outline.preview'.close()" end ------------------------- @@ -177,7 +177,6 @@ local function handler(response) M.state.flattened_outline_items = parser.flatten(items) writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items) - ui.setup_highlights() M._highlight_current_item(M.state.code_win) end @@ -202,6 +201,7 @@ end function M.setup(opts) config.setup(opts) + ui.setup_highlights() M.view = View:new() setup_global_autocmd() diff --git a/lua/symbols-outline/parser.lua b/lua/symbols-outline/parser.lua index 9067e32..c84674a 100644 --- a/lua/symbols-outline/parser.lua +++ b/lua/symbols-outline/parser.lua @@ -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 diff --git a/lua/symbols-outline/ui.lua b/lua/symbols-outline/ui.lua index eb54762..5d3936e 100644 --- a/lua/symbols-outline/ui.lua +++ b/lua/symbols-outline/ui.lua @@ -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 diff --git a/lua/symbols-outline/writer.lua b/lua/symbols-outline/writer.lua index cf3606b..69c6fd8 100644 --- a/lua/symbols-outline/writer.lua +++ b/lua/symbols-outline/writer.lua @@ -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,