From 50dd2e9275280cedb95184b09c6818f2e8f9334e Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Wed, 10 Aug 2022 14:46:01 -0700 Subject: [PATCH 01/28] feat(view): Turn View into a class and refactor closing --- lua/symbols-outline.lua | 36 +++++++++++++-------------- lua/symbols-outline/view.lua | 48 ++++++++++++++++++++++-------------- 2 files changed, 47 insertions(+), 37 deletions(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index bf253d1..5cb6540 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -6,7 +6,7 @@ local ui = require 'symbols-outline.ui' local writer = require 'symbols-outline.writer' local config = require 'symbols-outline.config' local utils = require 'symbols-outline.utils.init' -local view = require 'symbols-outline.view' +local View = require 'symbols-outline.view' local M = {} @@ -40,7 +40,7 @@ local function wipe_state() end local function __refresh() - if M.state.outline_buf ~= nil then + if M.view.bufnr ~= nil then local function refresh_handler(response) if response == nil or type(response) ~= 'table' then return @@ -52,7 +52,7 @@ local function __refresh() M.state.outline_items = items M.state.flattened_outline_items = parser.flatten(items) - writer.parse_and_write(M.state.outline_buf, M.state.flattened_outline_items) + writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items) end providers.request_symbols(refresh_handler) @@ -62,7 +62,7 @@ end M._refresh = utils.debounce(__refresh, 100) function M._goto_location(change_focus) - local current_line = vim.api.nvim_win_get_cursor(M.state.outline_win)[1] + local current_line = vim.api.nvim_win_get_cursor(M.view.winnr)[1] local node = M.state.flattened_outline_items[current_line] vim.api.nvim_win_set_cursor(M.state.code_win, { node.line + 1, node.character }) if change_focus then @@ -76,9 +76,9 @@ end function M._highlight_current_item(winnr) local has_provider = providers.has_provider() - local is_current_buffer_the_outline = M.state.outline_buf == vim.api.nvim_get_current_buf() + local is_current_buffer_the_outline = M.view.bufnr == vim.api.nvim_get_current_buf() - local doesnt_have_outline_buf = not M.state.outline_buf + local doesnt_have_outline_buf = not M.view.bufnr local should_exit = not has_provider or doesnt_have_outline_buf or is_current_buffer_the_outline @@ -106,10 +106,10 @@ function M._highlight_current_item(winnr) end -- clear old highlight - ui.clear_hover_highlight(M.state.outline_buf) + ui.clear_hover_highlight(M.view.bufnr) for _, value in ipairs(nodes) do - ui.add_hover_highlight(M.state.outline_buf, value.line_in_outline - 1, value.depth * 2) - vim.api.nvim_win_set_cursor(M.state.outline_win, { value.line_in_outline, 1 }) + ui.add_hover_highlight(M.view.bufnr, value.line_in_outline - 1, value.depth * 2) + vim.api.nvim_win_set_cursor(M.view.winnr, { value.line_in_outline, 1 }) end end @@ -142,14 +142,14 @@ local function handler(response) M.state.code_win = vim.api.nvim_get_current_win() - M.state.outline_buf, M.state.outline_win = view.setup_view() + M.view:setup_view() -- clear state when buffer is closed - vim.api.nvim_buf_attach(M.state.outline_buf, false, { + vim.api.nvim_buf_attach(M.view.bufnr, false, { on_detach = function(_, _) wipe_state() end, }) - setup_keymaps(M.state.outline_buf) + setup_keymaps(M.view.bufnr) setup_buffer_autocmd() local items = parser.parse(response) @@ -157,14 +157,14 @@ local function handler(response) M.state.outline_items = items M.state.flattened_outline_items = parser.flatten(items) - writer.parse_and_write(M.state.outline_buf, 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) end function M.toggle_outline() - if M.state.outline_buf == nil then + if M.view.bufnr == nil then M.open_outline() else M.close_outline() @@ -172,19 +172,19 @@ function M.toggle_outline() end function M.open_outline() - if M.state.outline_buf == nil then + if not M.view:is_open() then providers.request_symbols(handler) end end function M.close_outline() - if M.state.outline_buf then - vim.api.nvim_win_close(M.state.outline_win, true) - end + M.view:close() end function M.setup(opts) config.setup(opts) + + M.view = View:new() setup_global_autocmd() end diff --git a/lua/symbols-outline/view.lua b/lua/symbols-outline/view.lua index 4d3f4a0..848ac6d 100644 --- a/lua/symbols-outline/view.lua +++ b/lua/symbols-outline/view.lua @@ -1,43 +1,53 @@ local config = require 'symbols-outline.config' -local M = {} +local View = {} + +function View:new() + return setmetatable({ bufnr = nil, winnr = nil }, { __index = View }) +end ---creates the outline window and sets it up ----@return string bufnr ----@return string bufnr -function M.setup_view() +function View:setup_view() -- create a scratch unlisted buffer - local bufnr = vim.api.nvim_create_buf(false, true) + self.bufnr = vim.api.nvim_create_buf(false, true) -- delete buffer when window is closed / buffer is hidden - vim.api.nvim_buf_set_option(bufnr, 'bufhidden', 'delete') + vim.api.nvim_buf_set_option(self.bufnr, 'bufhidden', 'delete') -- create a split vim.cmd(config.get_split_command()) -- resize to a % of the current window size vim.cmd('vertical resize ' .. config.get_window_width()) -- get current (outline) window and attach our buffer to it - local winnr = vim.api.nvim_get_current_win() - vim.api.nvim_win_set_buf(winnr, bufnr) + self.winnr = vim.api.nvim_get_current_win() + vim.api.nvim_win_set_buf(self.winnr, self.bufnr) -- window stuff - vim.api.nvim_win_set_option(winnr, 'number', false) - vim.api.nvim_win_set_option(winnr, 'relativenumber', false) - vim.api.nvim_win_set_option(winnr, 'winfixwidth', true) + vim.api.nvim_win_set_option(self.winnr, 'number', false) + vim.api.nvim_win_set_option(self.winnr, 'relativenumber', false) + vim.api.nvim_win_set_option(self.winnr, 'winfixwidth', true) -- buffer stuff - vim.api.nvim_buf_set_name(bufnr, 'OUTLINE') - vim.api.nvim_buf_set_option(bufnr, 'filetype', 'Outline') - vim.api.nvim_buf_set_option(bufnr, 'modifiable', false) + vim.api.nvim_buf_set_name(self.bufnr, 'OUTLINE') + vim.api.nvim_buf_set_option(self.bufnr, 'filetype', 'Outline') + vim.api.nvim_buf_set_option(self.bufnr, 'modifiable', false) if config.options.show_numbers or config.options.show_relative_numbers then - vim.api.nvim_win_set_option(winnr, 'nu', true) + vim.api.nvim_win_set_option(self.winnr, 'nu', true) end if config.options.show_relative_numbers then - vim.api.nvim_win_set_option(winnr, 'rnu', true) + vim.api.nvim_win_set_option(self.winnr, 'rnu', true) end - - return bufnr, winnr end -return M +function View:close() + vim.api.nvim_win_close(self.winnr, true) + self.winnr = nil + self.bufnr = nil +end + +function View:is_open() + return self.winnr and self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr) and vim.api.nvim_win_is_valid(self.winnr) +end + +return View From 62b59f4de5d644828252be244c2bbc6dce9ff334 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Wed, 10 Aug 2022 14:47:25 -0700 Subject: [PATCH 02/28] toggle: Use View:is_open --- lua/symbols-outline.lua | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index 5cb6540..7d50ea7 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -1,5 +1,3 @@ -local vim = vim - local parser = require 'symbols-outline.parser' local providers = require 'symbols-outline.providers.init' local ui = require 'symbols-outline.ui' @@ -164,10 +162,10 @@ local function handler(response) end function M.toggle_outline() - if M.view.bufnr == nil then - M.open_outline() - else + if M.view:is_open() then M.close_outline() + else + M.open_outline() end end From d12af709500f0d0fc4dd2e537b71df2b0a8a40e4 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Wed, 10 Aug 2022 14:48:55 -0700 Subject: [PATCH 03/28] state: Remove unused winnr/bufnr --- lua/symbols-outline.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index 7d50ea7..ea56295 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -28,8 +28,6 @@ end M.state = { outline_items = {}, flattened_outline_items = {}, - outline_win = nil, - outline_buf = nil, code_win = 0, } From 374b80010a50d237321a594c655566d414cd82ae Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Wed, 10 Aug 2022 15:08:46 -0700 Subject: [PATCH 04/28] feat: Use vim.keymap.set for setting keymaps --- lua/symbols-outline.lua | 22 +++++++++++++--------- lua/symbols-outline/utils/init.lua | 7 ++++--- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index ea56295..3200016 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -57,7 +57,7 @@ end M._refresh = utils.debounce(__refresh, 100) -function M._goto_location(change_focus) +local function goto_location(change_focus) local current_line = vim.api.nvim_win_get_cursor(M.view.winnr)[1] local node = M.state.flattened_outline_items[current_line] vim.api.nvim_win_set_cursor(M.state.code_win, { node.line + 1, node.character }) @@ -114,21 +114,25 @@ local function setup_keymaps(bufnr) utils.nmap(bufnr, ...) end -- goto_location of symbol and focus that window - map(config.options.keymaps.goto_location, ":lua require('symbols-outline')._goto_location(true)") + map(config.options.keymaps.goto_location, function() + goto_location(true) + end) -- goto_location of symbol but stay in outline - map(config.options.keymaps.focus_location, ":lua require('symbols-outline')._goto_location(false)") + map(config.options.keymaps.focus_location, function() + goto_location(false) + end) -- hover symbol - map(config.options.keymaps.hover_symbol, ":lua require('symbols-outline.hover').show_hover()") + map(config.options.keymaps.hover_symbol, require('symbols-outline.hover').show_hover) -- preview symbol - map(config.options.keymaps.toggle_preview, ":lua require('symbols-outline.preview').toggle()") + map(config.options.keymaps.toggle_preview, require('symbols-outline.preview').toggle) -- rename symbol - map(config.options.keymaps.rename_symbol, ":lua require('symbols-outline.rename').rename()") + map(config.options.keymaps.rename_symbol, require('symbols-outline.rename').rename) -- code actions - map(config.options.keymaps.code_actions, ":lua require('symbols-outline.code_action').show_code_actions()") + map(config.options.keymaps.code_actions, require('symbols-outline.code_action').show_code_actions) -- show help - map(config.options.keymaps.show_help, ":lua require('symbols-outline.config').show_help()") + map(config.options.keymaps.show_help, require('symbols-outline.config').show_help) -- close outline - map(config.options.keymaps.close, ':bw!') + map(config.options.keymaps.close, M.view.close) end local function handler(response) diff --git a/lua/symbols-outline/utils/init.lua b/lua/symbols-outline/utils/init.lua index 0033c8f..fe2ff2a 100644 --- a/lua/symbols-outline/utils/init.lua +++ b/lua/symbols-outline/utils/init.lua @@ -2,14 +2,15 @@ local M = {} ---maps the table|string of keys to the action ---@param keys table|string ----@param action string +---@param action function|string function M.nmap(bufnr, keys, action) if type(keys) == 'string' then keys = { keys } end - for _, value in ipairs(keys) do - vim.api.nvim_buf_set_keymap(bufnr, 'n', value, action, { silent = true, noremap = true }) + + for _, lhs in ipairs(keys) do + vim.keymap.set('n', lhs, action, { silent = true, noremap = true, buffer = bufnr }) end end From 336bc4b38c125ae2104a54759a6ffaa3edd85521 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Wed, 10 Aug 2022 15:13:33 -0700 Subject: [PATCH 05/28] hover: Use new view api to get window --- lua/symbols-outline/hover.lua | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lua/symbols-outline/hover.lua b/lua/symbols-outline/hover.lua index 52d3b34..1e64b92 100644 --- a/lua/symbols-outline/hover.lua +++ b/lua/symbols-outline/hover.lua @@ -1,6 +1,4 @@ -local vim = vim - -local main = require 'symbols-outline' +local so = require 'symbols-outline' local util = vim.lsp.util local buf_request = require('symbols-outline.utils.lsp_utils').request @@ -19,10 +17,10 @@ end -- handler yoinked from the default implementation function M.show_hover() - local current_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1] - local node = main.state.flattened_outline_items[current_line] + local current_line = vim.api.nvim_win_get_cursor(so.view.winnr)[1] + local node = so.state.flattened_outline_items[current_line] - local hover_params = get_hover_params(node, main.state.code_win) + local hover_params = get_hover_params(node, so.state.code_win) buf_request(hover_params.bufnr, 'textDocument/hover', hover_params, function(_, result, _, config) if not (result and result.contents) then From 702a7d831b90c73dc2befb9fc50157fd568cae8c Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Wed, 10 Aug 2022 15:19:15 -0700 Subject: [PATCH 06/28] feat(rename): Use new view api --- lua/symbols-outline/rename.lua | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lua/symbols-outline/rename.lua b/lua/symbols-outline/rename.lua index 8bc5044..6513934 100644 --- a/lua/symbols-outline/rename.lua +++ b/lua/symbols-outline/rename.lua @@ -1,6 +1,4 @@ -local vim = vim - -local main = require 'symbols-outline' +local so = require 'symbols-outline' local buf_request = require('symbols-outline.utils.lsp_utils').request local M = {} @@ -17,10 +15,10 @@ local function get_rename_params(node, winnr) end function M.rename() - local current_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1] - local node = main.state.flattened_outline_items[current_line] + local current_line = vim.api.nvim_win_get_cursor(so.view.winnr)[1] + local node = so.state.flattened_outline_items[current_line] - local params = get_rename_params(node, main.state.code_win) + local params = get_rename_params(node, so.state.code_win) local new_name = vim.fn.input('New Name: ', node.name) if not new_name or new_name == '' or new_name == node.name then From 9ff33755cd93c18b4c9023a08c36ffb803b1e967 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Wed, 10 Aug 2022 15:22:47 -0700 Subject: [PATCH 07/28] cleanup: Disable auto_preview for now Till we clean this stuff up --- lua/symbols-outline.lua | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index 3200016..366be5b 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -14,14 +14,6 @@ local function setup_global_autocmd() end end -local function setup_buffer_autocmd() - if config.options.auto_preview then - vim.cmd "au CursorHold lua require'symbols-outline.preview'.show()" - else - vim.cmd "au CursorMoved lua require'symbols-outline.preview'.close()" - end -end - ------------------------- -- STATE ------------------------- @@ -123,8 +115,6 @@ local function setup_keymaps(bufnr) end) -- hover symbol map(config.options.keymaps.hover_symbol, require('symbols-outline.hover').show_hover) - -- preview symbol - map(config.options.keymaps.toggle_preview, require('symbols-outline.preview').toggle) -- rename symbol map(config.options.keymaps.rename_symbol, require('symbols-outline.rename').rename) -- code actions @@ -150,7 +140,6 @@ local function handler(response) end, }) setup_keymaps(M.view.bufnr) - setup_buffer_autocmd() local items = parser.parse(response) From d1065bc49254a064d57cc3852ea08f117c17a970 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Wed, 10 Aug 2022 15:24:37 -0700 Subject: [PATCH 08/28] fix: Fix close mapping --- lua/symbols-outline.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index 366be5b..faae984 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -122,7 +122,10 @@ local function setup_keymaps(bufnr) -- show help map(config.options.keymaps.show_help, require('symbols-outline.config').show_help) -- close outline - map(config.options.keymaps.close, M.view.close) + map(config.options.keymaps.close, + function () + M.view:close() + end) end local function handler(response) From ed6c058eee5856a804ab578a87f986f522f5e2b4 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Wed, 10 Aug 2022 15:29:39 -0700 Subject: [PATCH 09/28] chore: stylua --- lua/symbols-outline.lua | 48 +++++++++--- lua/symbols-outline/code_action.lua | 7 +- lua/symbols-outline/hover.lua | 31 +++++--- lua/symbols-outline/parser.lua | 4 +- lua/symbols-outline/preview.lua | 23 ++++-- lua/symbols-outline/providers/coc.lua | 10 ++- lua/symbols-outline/providers/jsx.lua | 85 ++++++++++++++-------- lua/symbols-outline/providers/markdown.lua | 10 ++- lua/symbols-outline/providers/nvim-lsp.lua | 17 ++++- lua/symbols-outline/rename.lua | 15 ++-- lua/symbols-outline/ui.lua | 31 ++++++-- lua/symbols-outline/utils/init.lua | 8 +- lua/symbols-outline/utils/lsp_utils.lua | 7 +- lua/symbols-outline/view.lua | 5 +- lua/symbols-outline/writer.lua | 9 ++- stylua.toml | 2 +- 16 files changed, 231 insertions(+), 81 deletions(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index faae984..93af910 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -52,7 +52,10 @@ M._refresh = utils.debounce(__refresh, 100) local function goto_location(change_focus) local current_line = vim.api.nvim_win_get_cursor(M.view.winnr)[1] local node = M.state.flattened_outline_items[current_line] - vim.api.nvim_win_set_cursor(M.state.code_win, { node.line + 1, node.character }) + vim.api.nvim_win_set_cursor( + M.state.code_win, + { node.line + 1, node.character } + ) if change_focus then vim.fn.win_gotoid(M.state.code_win) end @@ -64,11 +67,14 @@ end function M._highlight_current_item(winnr) local has_provider = providers.has_provider() - local is_current_buffer_the_outline = M.view.bufnr == vim.api.nvim_get_current_buf() + local is_current_buffer_the_outline = M.view.bufnr + == vim.api.nvim_get_current_buf() local doesnt_have_outline_buf = not M.view.bufnr - local should_exit = not has_provider or doesnt_have_outline_buf or is_current_buffer_the_outline + local should_exit = not has_provider + or doesnt_have_outline_buf + or is_current_buffer_the_outline -- Make a special case if we have a window number -- Because we might use this to manually focus so we dont want to quit this @@ -87,7 +93,10 @@ function M._highlight_current_item(winnr) local nodes = {} for index, value in ipairs(M.state.flattened_outline_items) do - if value.line == hovered_line or (hovered_line > value.range_start and hovered_line < value.range_end) then + if + value.line == hovered_line + or (hovered_line > value.range_start and hovered_line < value.range_end) + then value.line_in_outline = index table.insert(nodes, value) end @@ -96,7 +105,11 @@ function M._highlight_current_item(winnr) -- clear old highlight ui.clear_hover_highlight(M.view.bufnr) for _, value in ipairs(nodes) do - ui.add_hover_highlight(M.view.bufnr, value.line_in_outline - 1, value.depth * 2) + ui.add_hover_highlight( + M.view.bufnr, + value.line_in_outline - 1, + value.depth * 2 + ) vim.api.nvim_win_set_cursor(M.view.winnr, { value.line_in_outline, 1 }) end end @@ -114,17 +127,28 @@ local function setup_keymaps(bufnr) goto_location(false) end) -- hover symbol - map(config.options.keymaps.hover_symbol, require('symbols-outline.hover').show_hover) + map( + config.options.keymaps.hover_symbol, + require('symbols-outline.hover').show_hover + ) -- rename symbol - map(config.options.keymaps.rename_symbol, require('symbols-outline.rename').rename) + map( + config.options.keymaps.rename_symbol, + require('symbols-outline.rename').rename + ) -- code actions - map(config.options.keymaps.code_actions, require('symbols-outline.code_action').show_code_actions) + map( + config.options.keymaps.code_actions, + require('symbols-outline.code_action').show_code_actions + ) -- show help - map(config.options.keymaps.show_help, require('symbols-outline.config').show_help) + map( + config.options.keymaps.show_help, + require('symbols-outline.config').show_help + ) -- close outline - map(config.options.keymaps.close, - function () - M.view:close() + map(config.options.keymaps.close, function() + M.view:close() end) end diff --git a/lua/symbols-outline/code_action.lua b/lua/symbols-outline/code_action.lua index 4c7f992..c1c030a 100644 --- a/lua/symbols-outline/code_action.lua +++ b/lua/symbols-outline/code_action.lua @@ -25,7 +25,12 @@ function M.show_code_actions() local params = get_action_params(node, main.state.code_win) - buf_request(params.bufnr, 'textDocument/codeAction', params, vim.lsp.handlers['textDocument/codeAction']) + buf_request( + params.bufnr, + 'textDocument/codeAction', + params, + vim.lsp.handlers['textDocument/codeAction'] + ) end return M diff --git a/lua/symbols-outline/hover.lua b/lua/symbols-outline/hover.lua index 1e64b92..92ca455 100644 --- a/lua/symbols-outline/hover.lua +++ b/lua/symbols-outline/hover.lua @@ -22,19 +22,26 @@ function M.show_hover() local hover_params = get_hover_params(node, so.state.code_win) - buf_request(hover_params.bufnr, 'textDocument/hover', hover_params, function(_, result, _, config) - if not (result and result.contents) then - -- return { 'No information available' } - return + buf_request( + hover_params.bufnr, + 'textDocument/hover', + hover_params, + function(_, result, _, config) + if not (result and result.contents) then + -- return { 'No information available' } + return + end + local markdown_lines = util.convert_input_to_markdown_lines( + result.contents + ) + markdown_lines = util.trim_empty_lines(markdown_lines) + if vim.tbl_isempty(markdown_lines) then + -- return { 'No information available' } + return + end + return util.open_floating_preview(markdown_lines, 'markdown', config) end - local markdown_lines = util.convert_input_to_markdown_lines(result.contents) - markdown_lines = util.trim_empty_lines(markdown_lines) - if vim.tbl_isempty(markdown_lines) then - -- return { 'No information available' } - return - end - return util.open_floating_preview(markdown_lines, 'markdown', config) - end) + ) end return M diff --git a/lua/symbols-outline/parser.lua b/lua/symbols-outline/parser.lua index 7b3e510..4b07b13 100644 --- a/lua/symbols-outline/parser.lua +++ b/lua/symbols-outline/parser.lua @@ -131,7 +131,9 @@ function M.parse(response) end local result = client_response['result'] - if result == nil or type(result) ~= 'table' then goto continue end + if result == nil or type(result) ~= 'table' then + goto continue + end for _, value in pairs(result) do table.insert(all_results, value) diff --git a/lua/symbols-outline/preview.lua b/lua/symbols-outline/preview.lua index 04302d7..42ee98a 100644 --- a/lua/symbols-outline/preview.lua +++ b/lua/symbols-outline/preview.lua @@ -66,7 +66,10 @@ local function update_preview(code_buf) if state.preview_buf ~= nil then vim.api.nvim_buf_set_lines(state.preview_buf, 0, -1, 0, lines) - vim.api.nvim_win_set_cursor(state.preview_win, { node.line + 1, node.character }) + vim.api.nvim_win_set_cursor( + state.preview_win, + { node.line + 1, node.character } + ) end end @@ -119,14 +122,20 @@ local function update_hover() end local markdown_lines = {} if result ~= nil then - markdown_lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents) + markdown_lines = vim.lsp.util.convert_input_to_markdown_lines( + result.contents + ) end markdown_lines = vim.lsp.util.trim_empty_lines(markdown_lines) if vim.tbl_isempty(markdown_lines) then markdown_lines = { '###No info available!' } end - markdown_lines = vim.lsp.util.stylize_markdown(state.hover_buf, markdown_lines, {}) + markdown_lines = vim.lsp.util.stylize_markdown( + state.hover_buf, + markdown_lines, + {} + ) if state.hover_buf ~= nil then vim.api.nvim_buf_set_lines(state.hover_buf, 0, -1, 0, markdown_lines) @@ -219,10 +228,14 @@ end function M.close() if has_code_win() then - if state.preview_win ~= nil and vim.api.nvim_win_is_valid(state.preview_win) then + if + state.preview_win ~= nil and vim.api.nvim_win_is_valid(state.preview_win) + then vim.api.nvim_win_close(state.preview_win, true) end - if state.hover_win ~= nil and vim.api.nvim_win_is_valid(state.hover_win) then + if + state.hover_win ~= nil and vim.api.nvim_win_is_valid(state.hover_win) + then vim.api.nvim_win_close(state.hover_win, true) end end diff --git a/lua/symbols-outline/providers/coc.lua b/lua/symbols-outline/providers/coc.lua index 7bc3abc..f4a95a5 100644 --- a/lua/symbols-outline/providers/coc.lua +++ b/lua/symbols-outline/providers/coc.lua @@ -15,7 +15,15 @@ function M.should_use_provider(_) end function M.hover_info(_, _, on_info) - on_info(nil, { contents = { kind = 'markdown', contents = { 'No extra information availaible!' } } }) + on_info( + nil, + { + contents = { + kind = 'markdown', + contents = { 'No extra information availaible!' }, + }, + } + ) end ---@param on_symbols function diff --git a/lua/symbols-outline/providers/jsx.lua b/lua/symbols-outline/providers/jsx.lua index 626b6ae..5ef2dd2 100644 --- a/lua/symbols-outline/providers/jsx.lua +++ b/lua/symbols-outline/providers/jsx.lua @@ -1,24 +1,33 @@ local M = {} -local parsers = require("nvim-treesitter.parsers") +local parsers = require 'nvim-treesitter.parsers' local SYMBOL_COMPONENT = 27 local SYMBOL_FRAGMENT = 28 function M.should_use_provider(bufnr) - local ft = vim.api.nvim_buf_get_option(bufnr, 'ft') + local ft = vim.api.nvim_buf_get_option(bufnr, 'ft') - return string.match(ft, 'typescriptreact') or string.match(ft, 'javascriptreact') + return string.match(ft, 'typescriptreact') + or string.match(ft, 'javascriptreact') end function M.hover_info(_, _, on_info) - on_info(nil, { contents = { kind = 'nvim-lsp-jsx', contents = { 'No extra information availaible!' } } }) + on_info( + nil, + { + contents = { + kind = 'nvim-lsp-jsx', + contents = { 'No extra information availaible!' }, + }, + } + ) end local function get_open_tag(node) - if node:type() == "jsx_element" then - for _, outer in ipairs(node:field("open_tag")) do - if outer:type() == "jsx_opening_element" then + if node:type() == 'jsx_element' then + for _, outer in ipairs(node:field 'open_tag') do + if outer:type() == 'jsx_opening_element' then return outer end end @@ -30,14 +39,21 @@ end local function jsx_node_detail(node, buf) node = get_open_tag(node) or node - local param_nodes = node:field("attribute") - if #param_nodes == 0 then return nil end + local param_nodes = node:field 'attribute' + if #param_nodes == 0 then + return nil + end - local res = '{ ' .. table.concat(vim.tbl_map(function (el) - local a, b, c, d = el:range() - local text = vim.api.nvim_buf_get_text(buf, a, b, c, d, {}) - return text[1] - end, param_nodes), ' ') .. ' }' + local res = '{ ' + .. table.concat( + vim.tbl_map(function(el) + local a, b, c, d = el:range() + local text = vim.api.nvim_buf_get_text(buf, a, b, c, d, {}) + return text[1] + end, param_nodes), + ' ' + ) + .. ' }' return res end @@ -47,7 +63,7 @@ local function jsx_node_tagname(node, buf) local identifier = nil - for _, val in ipairs(tagnode:field('name')) do + for _, val in ipairs(tagnode:field 'name') do if val:type() == 'identifier' then identifier = val end @@ -62,28 +78,37 @@ local function jsx_node_tagname(node, buf) end local function convert_ts(child, children, bufnr) - local is_frag = (child:type() == 'jsx_fragment') + local is_frag = (child:type() == 'jsx_fragment') - local a, b, c, d = child:range() - local range = { start = { line = a, character = b }, ['end'] = { line = c, character = d } } + local a, b, c, d = child:range() + local range = { + start = { line = a, character = b }, + ['end'] = { line = c, character = d }, + } - local converted = { - name = (not is_frag and (jsx_node_tagname(child, bufnr) or '')) or 'fragment', - children = (#children > 0 and children) or nil, - kind = (is_frag and SYMBOL_FRAGMENT) or SYMBOL_COMPONENT, - detail = jsx_node_detail(child, bufnr), - range = range, - selectionRange = range - } - - return converted + local converted = { + name = (not is_frag and (jsx_node_tagname(child, bufnr) or '')) + or 'fragment', + children = (#children > 0 and children) or nil, + kind = (is_frag and SYMBOL_FRAGMENT) or SYMBOL_COMPONENT, + detail = jsx_node_detail(child, bufnr), + range = range, + selectionRange = range, + } + + return converted end local function parse_ts(root, children, bufnr) children = children or {} for child in root:iter_children() do - if vim.tbl_contains({ 'jsx_element', 'jsx_self_closing_element' }, child:type()) then + if + vim.tbl_contains( + { 'jsx_element', 'jsx_self_closing_element' }, + child:type() + ) + then local new_children = {} parse_ts(child, new_children, bufnr) @@ -105,7 +130,7 @@ function M.request_symbols(on_symbols) local symbols = parse_ts(root, nil, bufnr) -- local symbols = convert_ts(ctree) - on_symbols({ [1000000] = { result = symbols }}) + on_symbols { [1000000] = { result = symbols } } end return M diff --git a/lua/symbols-outline/providers/markdown.lua b/lua/symbols-outline/providers/markdown.lua index f93fe63..1123425 100644 --- a/lua/symbols-outline/providers/markdown.lua +++ b/lua/symbols-outline/providers/markdown.lua @@ -8,7 +8,15 @@ function M.should_use_provider(bufnr) end function M.hover_info(_, _, on_info) - on_info(nil, { contents = { kind = 'markdown', contents = { 'No extra information availaible!' } } }) + on_info( + nil, + { + contents = { + kind = 'markdown', + contents = { 'No extra information availaible!' }, + }, + } + ) end ---@param on_symbols function diff --git a/lua/symbols-outline/providers/nvim-lsp.lua b/lua/symbols-outline/providers/nvim-lsp.lua index 447a8e4..cd2c544 100644 --- a/lua/symbols-outline/providers/nvim-lsp.lua +++ b/lua/symbols-outline/providers/nvim-lsp.lua @@ -23,7 +23,15 @@ function M.hover_info(bufnr, params, on_info) end if not used_client then - on_info(nil, { contents = { kind = 'markdown', content = { 'No extra information availaible!' } } }) + on_info( + nil, + { + contents = { + kind = 'markdown', + content = { 'No extra information availaible!' }, + }, + } + ) end used_client.request('textDocument/hover', params, on_info, bufnr) @@ -51,7 +59,12 @@ end ---@param on_symbols function function M.request_symbols(on_symbols) - vim.lsp.buf_request_all(0, 'textDocument/documentSymbol', getParams(), on_symbols) + vim.lsp.buf_request_all( + 0, + 'textDocument/documentSymbol', + getParams(), + on_symbols + ) end return M diff --git a/lua/symbols-outline/rename.lua b/lua/symbols-outline/rename.lua index 6513934..0d5d4ef 100644 --- a/lua/symbols-outline/rename.lua +++ b/lua/symbols-outline/rename.lua @@ -27,12 +27,17 @@ function M.rename() params.newName = new_name - buf_request(params.bufnr, 'textDocument/rename', params, function(_, result, ctx) - if result ~= nil then - local client = vim.lsp.get_client_by_id(ctx.client_id) - vim.lsp.util.apply_workspace_edit(result, client.offset_encoding) + buf_request( + params.bufnr, + 'textDocument/rename', + params, + function(_, result, ctx) + if result ~= nil then + local client = vim.lsp.get_client_by_id(ctx.client_id) + vim.lsp.util.apply_workspace_edit(result, client.offset_encoding) + end end - end) + ) end return M diff --git a/lua/symbols-outline/ui.lua b/lua/symbols-outline/ui.lua index f46e932..e68b26c 100644 --- a/lua/symbols-outline/ui.lua +++ b/lua/symbols-outline/ui.lua @@ -17,7 +17,14 @@ function M.clear_hover_highlight(bufnr) end function M.add_hover_highlight(bufnr, line, col_start) - vim.api.nvim_buf_add_highlight(bufnr, M.hovered_hl_ns, 'FocusedSymbol', line, col_start, -1) + vim.api.nvim_buf_add_highlight( + bufnr, + M.hovered_hl_ns, + 'FocusedSymbol', + line, + col_start, + -1 + ) end local function highlight_text(name, text, hl_group) @@ -36,18 +43,32 @@ function M.setup_highlights() -- notably making them italic, which messes up the outline connector. Fix -- this by copying the foreground color from the comment hl into a new -- highlight. - local comment_fg_gui = vim.fn.synIDattr(vim.fn.synIDtrans(vim.fn.hlID 'Comment'), 'fg', 'gui') + local comment_fg_gui = vim.fn.synIDattr( + vim.fn.synIDtrans(vim.fn.hlID 'Comment'), + 'fg', + 'gui' + ) if vim.fn.hlexists 'SymbolsOutlineConnector' == 0 then - vim.cmd(string.format('hi SymbolsOutlineConnector guifg=%s', comment_fg_gui)) + vim.cmd( + string.format('hi SymbolsOutlineConnector guifg=%s', comment_fg_gui) + ) end local symbols = config.options.symbols -- 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( + 'marker_vertical', + M.markers.vertical, + 'SymbolsOutlineConnector' + ) + highlight_text( + 'markers_horizontal', + M.markers.horizontal, + 'SymbolsOutlineConnector' + ) highlight_text('markers_bottom', M.markers.bottom, 'SymbolsOutlineConnector') end diff --git a/lua/symbols-outline/utils/init.lua b/lua/symbols-outline/utils/init.lua index fe2ff2a..29465dd 100644 --- a/lua/symbols-outline/utils/init.lua +++ b/lua/symbols-outline/utils/init.lua @@ -8,9 +8,13 @@ function M.nmap(bufnr, keys, action) keys = { keys } end - for _, lhs in ipairs(keys) do - vim.keymap.set('n', lhs, action, { silent = true, noremap = true, buffer = bufnr }) + vim.keymap.set( + 'n', + lhs, + action, + { silent = true, noremap = true, buffer = bufnr } + ) end end diff --git a/lua/symbols-outline/utils/lsp_utils.lua b/lua/symbols-outline/utils/lsp_utils.lua index 0fc15ba..f1f764b 100644 --- a/lua/symbols-outline/utils/lsp_utils.lua +++ b/lua/symbols-outline/utils/lsp_utils.lua @@ -17,7 +17,12 @@ local function mk_handler(fn) local client_id = select(4, ...) local bufnr = select(5, ...) local config = select(6, ...) - fn(err, result, { method = method, client_id = client_id, bufnr = bufnr }, config) + fn( + err, + result, + { method = method, client_id = client_id, bufnr = bufnr }, + config + ) end end end diff --git a/lua/symbols-outline/view.lua b/lua/symbols-outline/view.lua index 848ac6d..1aa912d 100644 --- a/lua/symbols-outline/view.lua +++ b/lua/symbols-outline/view.lua @@ -47,7 +47,10 @@ function View:close() end function View:is_open() - return self.winnr and self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr) and vim.api.nvim_win_is_valid(self.winnr) + return self.winnr + and self.bufnr + and vim.api.nvim_buf_is_valid(self.bufnr) + and vim.api.nvim_win_is_valid(self.winnr) end return View diff --git a/lua/symbols-outline/writer.lua b/lua/symbols-outline/writer.lua index c9afd3a..d80c391 100644 --- a/lua/symbols-outline/writer.lua +++ b/lua/symbols-outline/writer.lua @@ -26,7 +26,14 @@ end function M.add_highlights(bufnr, hl_info) for line, line_hl in ipairs(hl_info) do hl_start, hl_end, hl_type = unpack(line_hl) - vim.api.nvim_buf_add_highlight(bufnr, hlns, hl_type, line - 1, hl_start, hl_end) + vim.api.nvim_buf_add_highlight( + bufnr, + hlns, + hl_type, + line - 1, + hl_start, + hl_end + ) end end diff --git a/stylua.toml b/stylua.toml index 237e120..63ea177 100644 --- a/stylua.toml +++ b/stylua.toml @@ -1,4 +1,4 @@ -column_width = 120 +column_width = 80 line_endings = 'Unix' indent_type = 'Spaces' indent_width = 2 From 1188ed720112d32d033ee4b4bbcff7ef7781e9b0 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Wed, 10 Aug 2022 15:33:23 -0700 Subject: [PATCH 10/28] (breaking_change) feat: Make setup explicit Don't setup on its own --- lua/symbols-outline.lua | 3 +++ plugin/symbols-outline.vim | 14 -------------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index 93af910..3aec829 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -12,6 +12,9 @@ local function setup_global_autocmd() if config.options.highlight_hovered_item then 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()" end ------------------------- diff --git a/plugin/symbols-outline.vim b/plugin/symbols-outline.vim index f8456c9..be0f1b9 100644 --- a/plugin/symbols-outline.vim +++ b/plugin/symbols-outline.vim @@ -1,17 +1,3 @@ -if exists('g:loaded_symbols_outline') - finish -endif -let g:loaded_symbols_outline = 1 - -if exists('g:symbols_outline') - call luaeval('require"symbols-outline".setup(_A[1])', [g:symbols_outline]) -else - call luaeval('require"symbols-outline".setup()') -endif - command! SymbolsOutline :lua require'symbols-outline'.toggle_outline() command! SymbolsOutlineOpen :lua require'symbols-outline'.open_outline() command! SymbolsOutlineClose :lua require'symbols-outline'.close_outline() - -au InsertLeave,WinEnter,BufEnter,BufWinEnter,TabEnter,BufWritePost * :lua require('symbols-outline')._refresh() -au WinEnter * lua require'symbols-outline.preview'.close() From d7542aab11fbf9a35b5e7d18d016ee7962920c10 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Wed, 10 Aug 2022 15:40:38 -0700 Subject: [PATCH 11/28] refresh: Use view.is_open --- lua/symbols-outline.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index 3aec829..68805fc 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -31,7 +31,7 @@ local function wipe_state() end local function __refresh() - if M.view.bufnr ~= nil then + if M.view:is_open() then local function refresh_handler(response) if response == nil or type(response) ~= 'table' then return From 197a9aa45f992bec7fee2807253ce8fb43b1fd1d Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Wed, 10 Aug 2022 16:58:24 -0700 Subject: [PATCH 12/28] refactor: Remove utils.request shim * This is no longer needed as people have probably moved on from 0.5 --- lua/symbols-outline/code_action.lua | 4 +--- lua/symbols-outline/hover.lua | 4 ++-- lua/symbols-outline/preview.lua | 1 - lua/symbols-outline/rename.lua | 3 +-- lua/symbols-outline/utils/lsp_utils.lua | 32 ------------------------- 5 files changed, 4 insertions(+), 40 deletions(-) diff --git a/lua/symbols-outline/code_action.lua b/lua/symbols-outline/code_action.lua index c1c030a..36f8b69 100644 --- a/lua/symbols-outline/code_action.lua +++ b/lua/symbols-outline/code_action.lua @@ -1,7 +1,6 @@ local vim = vim local main = require 'symbols-outline' -local buf_request = require('symbols-outline.utils.lsp_utils').request local M = {} @@ -24,8 +23,7 @@ function M.show_code_actions() local node = main.state.flattened_outline_items[current_line] local params = get_action_params(node, main.state.code_win) - - buf_request( + vim.lsp.buf_request( params.bufnr, 'textDocument/codeAction', params, diff --git a/lua/symbols-outline/hover.lua b/lua/symbols-outline/hover.lua index 92ca455..27f2a01 100644 --- a/lua/symbols-outline/hover.lua +++ b/lua/symbols-outline/hover.lua @@ -1,6 +1,5 @@ local so = require 'symbols-outline' local util = vim.lsp.util -local buf_request = require('symbols-outline.utils.lsp_utils').request local M = {} @@ -22,10 +21,11 @@ function M.show_hover() local hover_params = get_hover_params(node, so.state.code_win) - buf_request( + vim.lsp.buf_request( hover_params.bufnr, 'textDocument/hover', hover_params, +---@diagnostic disable-next-line: param-type-mismatch function(_, result, _, config) if not (result and result.contents) then -- return { 'No information available' } diff --git a/lua/symbols-outline/preview.lua b/lua/symbols-outline/preview.lua index 42ee98a..95da7ec 100644 --- a/lua/symbols-outline/preview.lua +++ b/lua/symbols-outline/preview.lua @@ -1,7 +1,6 @@ local vim = vim local main = require 'symbols-outline' local config = require 'symbols-outline.config' -local buf_request = require('symbols-outline.utils.lsp_utils').request local M = {} diff --git a/lua/symbols-outline/rename.lua b/lua/symbols-outline/rename.lua index 0d5d4ef..b9d413d 100644 --- a/lua/symbols-outline/rename.lua +++ b/lua/symbols-outline/rename.lua @@ -1,5 +1,4 @@ local so = require 'symbols-outline' -local buf_request = require('symbols-outline.utils.lsp_utils').request local M = {} @@ -27,7 +26,7 @@ function M.rename() params.newName = new_name - buf_request( + vim.lsp.buf_request( params.bufnr, 'textDocument/rename', params, diff --git a/lua/symbols-outline/utils/lsp_utils.lua b/lua/symbols-outline/utils/lsp_utils.lua index f1f764b..a0fe87c 100644 --- a/lua/symbols-outline/utils/lsp_utils.lua +++ b/lua/symbols-outline/utils/lsp_utils.lua @@ -1,37 +1,5 @@ -local vim = vim - local M = {} --- callback args changed in Neovim 0.5.1/0.6. See: --- https://github.com/neovim/neovim/pull/15504 -local function mk_handler(fn) - return function(...) - local config_or_client_id = select(4, ...) - local is_new = type(config_or_client_id) ~= 'number' - if is_new then - fn(...) - else - local err = select(1, ...) - local method = select(2, ...) - local result = select(3, ...) - local client_id = select(4, ...) - local bufnr = select(5, ...) - local config = select(6, ...) - fn( - err, - result, - { method = method, client_id = client_id, bufnr = bufnr }, - config - ) - end - end -end - --- from mfussenegger/nvim-lsp-compl@29a81f3 -function M.request(bufnr, method, params, handler) - return vim.lsp.buf_request(bufnr, method, params, mk_handler(handler)) -end - function M.is_buf_attached_to_lsp(bufnr) local clients = vim.lsp.buf_get_clients(bufnr or 0) return clients ~= nil and #clients > 0 From ce21cf3f66c04b57d04537e4ee84f65e0b56ab35 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Wed, 10 Aug 2022 17:05:39 -0700 Subject: [PATCH 13/28] chore: Remove use of global vars + other cleanups --- lua/symbols-outline/parser.lua | 2 +- lua/symbols-outline/ui.lua | 4 ---- lua/symbols-outline/writer.lua | 2 +- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/lua/symbols-outline/parser.lua b/lua/symbols-outline/parser.lua index 4b07b13..9067e32 100644 --- a/lua/symbols-outline/parser.lua +++ b/lua/symbols-outline/parser.lua @@ -217,7 +217,7 @@ function M.get_lines(flattened_outline_items) local hl_start = #string_prefix local hl_end = #string_prefix + #value.icon table.insert(lines, string_prefix .. value.icon .. ' ' .. value.name) - hl_type = config.options.symbols[symbols.kinds[value.kind]].hl + local hl_type = config.options.symbols[symbols.kinds[value.kind]].hl table.insert(hl_info, { hl_start, hl_end, hl_type }) end return lines, hl_info diff --git a/lua/symbols-outline/ui.lua b/lua/symbols-outline/ui.lua index e68b26c..eb54762 100644 --- a/lua/symbols-outline/ui.lua +++ b/lua/symbols-outline/ui.lua @@ -1,6 +1,4 @@ local vim = vim -local config = require 'symbols-outline.config' -local symbol_kinds = require('symbols-outline.symbols').kinds local M = {} M.markers = { @@ -55,8 +53,6 @@ function M.setup_highlights() ) end - local symbols = config.options.symbols - -- markers highlight_text('marker_middle', M.markers.middle, 'SymbolsOutlineConnector') highlight_text( diff --git a/lua/symbols-outline/writer.lua b/lua/symbols-outline/writer.lua index d80c391..cf3606b 100644 --- a/lua/symbols-outline/writer.lua +++ b/lua/symbols-outline/writer.lua @@ -25,7 +25,7 @@ end function M.add_highlights(bufnr, hl_info) for line, line_hl in ipairs(hl_info) do - hl_start, hl_end, hl_type = unpack(line_hl) + local hl_start, hl_end, hl_type = unpack(line_hl) vim.api.nvim_buf_add_highlight( bufnr, hlns, From 094334c1a8b7e308f83aa8d526fdfd68a1b728f4 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Wed, 10 Aug 2022 17:42:50 -0700 Subject: [PATCH 14/28] 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, From 7747a020a626557c833bfa13aa1a0a69baa1838a Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Wed, 10 Aug 2022 20:47:31 -0700 Subject: [PATCH 15/28] feat(parser): Fix guide highlight for multi-width characters --- lua/symbols-outline.lua | 2 ++ lua/symbols-outline/parser.lua | 47 ++++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index 3cea2c4..5f5fcf8 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -169,6 +169,7 @@ local function handler(response) wipe_state() end, }) + setup_keymaps(M.view.bufnr) local items = parser.parse(response) @@ -178,6 +179,7 @@ local function handler(response) writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items) + M._highlight_current_item(M.state.code_win) end diff --git a/lua/symbols-outline/parser.lua b/lua/symbols-outline/parser.lua index c84674a..70b444d 100644 --- a/lua/symbols-outline/parser.lua +++ b/lua/symbols-outline/parser.lua @@ -183,48 +183,73 @@ function M.get_lines(flattened_outline_items) for node_line, node in ipairs(flattened_outline_items) do local line = str_to_table(string.rep(' ', node.depth)) + local running_length = 1 + + local function add_guide_hl(from, to) + table.insert(hl_info, { + node_line, + from, + to, + 'SymbolsOutlineConnector', + }) + end 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, + -- i f index is last, add a bottom marker if current item is last, -- else add a middle marker elseif index == #line then if node.isLast then line[index] = ui.markers.bottom + add_guide_hl( + running_length, + running_length + vim.fn.strlen(ui.markers.bottom) - 1 + ) else line[index] = ui.markers.middle + add_guide_hl( + running_length, + running_length + vim.fn.strlen(ui.markers.middle) - 1 + ) end -- 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 node.hierarchy[index] then line[index] = ui.markers.vertical + add_guide_hl( + running_length, + running_length + vim.fn.strlen(ui.markers.vertical) - 1 + ) 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 } - ) + running_length = running_length + vim.fn.strlen(line[index]) end end local final_prefix = line local string_prefix = table_to_str(final_prefix) + + -- local guide_hl_start = 0 + -- local guide_hl_end = #string_prefix + + -- table.insert( + -- hl_info, + -- { node_line, guide_hl_start, guide_hl_end, 'SymbolsOutlineConnector' } + -- ) + + table.insert(lines, string_prefix .. node.icon .. ' ' .. node.name) + local hl_start = #string_prefix 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 From 40bbb963593a22dcd6e818d751731336e0cdc071 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Thu, 11 Aug 2022 14:31:47 -0700 Subject: [PATCH 16/28] feat(view): Disable the list option --- lua/symbols-outline/view.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lua/symbols-outline/view.lua b/lua/symbols-outline/view.lua index 1aa912d..0202ac6 100644 --- a/lua/symbols-outline/view.lua +++ b/lua/symbols-outline/view.lua @@ -26,6 +26,7 @@ function View:setup_view() vim.api.nvim_win_set_option(self.winnr, 'number', false) vim.api.nvim_win_set_option(self.winnr, 'relativenumber', false) vim.api.nvim_win_set_option(self.winnr, 'winfixwidth', true) + vim.api.nvim_win_set_option(self.winnr, 'list', false) -- buffer stuff vim.api.nvim_buf_set_name(self.bufnr, 'OUTLINE') vim.api.nvim_buf_set_option(self.bufnr, 'filetype', 'Outline') From 65742f3b9306bd6167e3e8561504932a0e3189ee Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Thu, 11 Aug 2022 14:55:54 -0700 Subject: [PATCH 17/28] feat(ui): Improve FocusedSymbol highlight --- lua/symbols-outline/ui.lua | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lua/symbols-outline/ui.lua b/lua/symbols-outline/ui.lua index 5d3936e..325f901 100644 --- a/lua/symbols-outline/ui.lua +++ b/lua/symbols-outline/ui.lua @@ -28,7 +28,14 @@ function M.setup_highlights() -- Setup the FocusedSymbol highlight group if it hasn't been done already by -- a theme or manually set if vim.fn.hlexists 'FocusedSymbol' == 0 then - vim.cmd 'hi FocusedSymbol term=italic,bold cterm=italic ctermbg=yellow ctermfg=darkblue gui=bold,italic guibg=yellow guifg=darkblue' + local cline_hl = vim.api.nvim_get_hl_by_name('CursorLine', true) + local string_hl = vim.api.nvim_get_hl_by_name('String', true) + + vim.api.nvim_set_hl( + 0, + 'FocusedSymbol', + { bg = cline_hl.background, fg = string_hl.foreground } + ) end -- Some colorschemes do some funky things with the comment highlight, most From f33bdb4be90b89c2f8a32630e306dac053e672da Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Mon, 15 Aug 2022 14:08:28 -0700 Subject: [PATCH 18/28] misc: formatting --- lua/symbols-outline.lua | 1 - lua/symbols-outline/providers/coc.lua | 15 ++++++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index 5f5fcf8..83d3572 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -179,7 +179,6 @@ local function handler(response) writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items) - M._highlight_current_item(M.state.code_win) end diff --git a/lua/symbols-outline/providers/coc.lua b/lua/symbols-outline/providers/coc.lua index f4a95a5..1ff7594 100644 --- a/lua/symbols-outline/providers/coc.lua +++ b/lua/symbols-outline/providers/coc.lua @@ -15,15 +15,12 @@ function M.should_use_provider(_) end function M.hover_info(_, _, on_info) - on_info( - nil, - { - contents = { - kind = 'markdown', - contents = { 'No extra information availaible!' }, - }, - } - ) + on_info(nil, { + contents = { + kind = 'markdown', + contents = { 'No extra information availaible!' }, + }, + }) end ---@param on_symbols function From fb2ab3bb1ca867e6e91196778aa01ee9aeed2e97 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Mon, 15 Aug 2022 14:11:50 -0700 Subject: [PATCH 19/28] fix(parser): Make depth/heirarchy optional --- lua/symbols-outline/parser.lua | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/lua/symbols-outline/parser.lua b/lua/symbols-outline/parser.lua index 70b444d..c621a06 100644 --- a/lua/symbols-outline/parser.lua +++ b/lua/symbols-outline/parser.lua @@ -15,8 +15,8 @@ end ---Parses result from LSP into a table of symbols ---@param result table The result from a language server. ----@param depth number The current depth of the symbol in the hierarchy. ----@param hierarchy table A table of booleans which tells if a symbols parent was the last in its group. +---@param depth number? The current depth of the symbol in the hierarchy. +---@param hierarchy table? A table of booleans which tells if a symbols parent was the last in its group. ---@return table local function parse_result(result, depth, hierarchy) local ret = {} @@ -144,7 +144,7 @@ function M.parse(response) local sorted = sort_result(all_results) - return parse_result(sorted) + return parse_result(sorted, nil, nil) end function M.flatten(outline_items) @@ -238,14 +238,6 @@ function M.get_lines(flattened_outline_items) local string_prefix = table_to_str(final_prefix) - -- local guide_hl_start = 0 - -- local guide_hl_end = #string_prefix - - -- table.insert( - -- hl_info, - -- { node_line, guide_hl_start, guide_hl_end, 'SymbolsOutlineConnector' } - -- ) - table.insert(lines, string_prefix .. node.icon .. ' ' .. node.name) local hl_start = #string_prefix From 0250f77085d359fe58ea558cabea7e75fcec0dc0 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Mon, 15 Aug 2022 14:18:48 -0700 Subject: [PATCH 20/28] refactor(parser): Move utility functions to table utils --- lua/symbols-outline/parser.lua | 32 ++++------------------------- lua/symbols-outline/utils/table.lua | 31 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 28 deletions(-) create mode 100644 lua/symbols-outline/utils/table.lua diff --git a/lua/symbols-outline/parser.lua b/lua/symbols-outline/parser.lua index c621a06..251711e 100644 --- a/lua/symbols-outline/parser.lua +++ b/lua/symbols-outline/parser.lua @@ -1,18 +1,10 @@ local symbols = require 'symbols-outline.symbols' local ui = require 'symbols-outline.ui' local config = require 'symbols-outline.config' +local t_utils = require 'symbols-outline.utils.table' local M = {} --- copies an array and returns it because lua usually does references -local function array_copy(t) - local ret = {} - for _, value in ipairs(t) do - table.insert(ret, value) - end - return ret -end - ---Parses result from LSP into a table of symbols ---@param result table The result from a language server. ---@param depth number? The current depth of the symbol in the hierarchy. @@ -35,7 +27,7 @@ local function parse_result(result, depth, hierarchy) local children = nil if value.children ~= nil then -- copy by value because we dont want it messing with the hir table - local child_hir = array_copy(hir) + local child_hir = t_utils.array_copy(hir) table.insert(child_hir, isLast) children = parse_result(value.children, level + 1, child_hir) end @@ -161,28 +153,12 @@ function M.flatten(outline_items) return ret end -local function table_to_str(t) - local ret = '' - for _, value in ipairs(t) do - ret = ret .. tostring(value) - end - return ret -end - -local function str_to_table(str) - local t = {} - for i = 1, #str do - t[i] = str:sub(i, i) - end - return t -end - function M.get_lines(flattened_outline_items) local lines = {} local hl_info = {} for node_line, node in ipairs(flattened_outline_items) do - local line = str_to_table(string.rep(' ', node.depth)) + local line = t_utils.str_to_table(string.rep(' ', node.depth)) local running_length = 1 local function add_guide_hl(from, to) @@ -236,7 +212,7 @@ function M.get_lines(flattened_outline_items) local final_prefix = line - local string_prefix = table_to_str(final_prefix) + local string_prefix = t_utils.table_to_str(final_prefix) table.insert(lines, string_prefix .. node.icon .. ' ' .. node.name) diff --git a/lua/symbols-outline/utils/table.lua b/lua/symbols-outline/utils/table.lua new file mode 100644 index 0000000..93247d1 --- /dev/null +++ b/lua/symbols-outline/utils/table.lua @@ -0,0 +1,31 @@ +local M = {} + +function M.table_to_str(t) + local ret = '' + for _, value in ipairs(t) do + ret = ret .. tostring(value) + end + return ret +end + +function M.str_to_table(str) + local t = {} + for i = 1, #str do + t[i] = str:sub(i, i) + end + return t +end + +--- Copies an array and returns it because lua usually does references +---@generic T +---@param t T[] +---@return T[] +function M.array_copy(t) + local ret = {} + for _, value in ipairs(t) do + table.insert(ret, value) + end + return ret +end + +return M From 609d630a961f68c17c9fb0be91e3baadd1590780 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Mon, 15 Aug 2022 14:21:23 -0700 Subject: [PATCH 21/28] chore: Add help tags to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..926ccaa --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +doc/tags From 776ddb393024d1a0d9e530c10c4df08ad0c4b524 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Mon, 15 Aug 2022 14:31:19 -0700 Subject: [PATCH 22/28] feat(parser): Flatten outline items while parsing * Removes the need for flatten_outline_items * Improves performance --- lua/symbols-outline.lua | 13 +++++-------- lua/symbols-outline/code_action.lua | 2 +- lua/symbols-outline/hover.lua | 2 +- lua/symbols-outline/parser.lua | 30 ++++++++++------------------- lua/symbols-outline/preview.lua | 2 +- lua/symbols-outline/rename.lua | 2 +- lua/symbols-outline/writer.lua | 6 +++--- 7 files changed, 22 insertions(+), 35 deletions(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index 83d3572..43acff3 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -22,12 +22,11 @@ end ------------------------- M.state = { outline_items = {}, - flattened_outline_items = {}, code_win = 0, } local function wipe_state() - M.state = { outline_items = {}, flattened_outline_items = {}, code_win = 0 } + M.state = { outline_items = {}, code_win = 0 } end local function __refresh() @@ -41,9 +40,8 @@ local function __refresh() M.state.code_win = vim.api.nvim_get_current_win() M.state.outline_items = 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.outline_items) end providers.request_symbols(refresh_handler) @@ -54,7 +52,7 @@ M._refresh = utils.debounce(__refresh, 100) local function goto_location(change_focus) local current_line = vim.api.nvim_win_get_cursor(M.view.winnr)[1] - local node = M.state.flattened_outline_items[current_line] + local node = M.state.outline_items[current_line] vim.api.nvim_win_set_cursor( M.state.code_win, { node.line + 1, node.character } @@ -95,7 +93,7 @@ function M._highlight_current_item(winnr) local hovered_line = vim.api.nvim_win_get_cursor(win)[1] - 1 local nodes = {} - for index, value in ipairs(M.state.flattened_outline_items) do + for index, value in ipairs(M.state.outline_items) do if value.line == hovered_line or (hovered_line > value.range_start and hovered_line < value.range_end) @@ -175,9 +173,8 @@ local function handler(response) local items = parser.parse(response) M.state.outline_items = 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.outline_items) M._highlight_current_item(M.state.code_win) end diff --git a/lua/symbols-outline/code_action.lua b/lua/symbols-outline/code_action.lua index 36f8b69..72e7228 100644 --- a/lua/symbols-outline/code_action.lua +++ b/lua/symbols-outline/code_action.lua @@ -20,7 +20,7 @@ end function M.show_code_actions() local current_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1] - local node = main.state.flattened_outline_items[current_line] + local node = main.state.outline_items[current_line] local params = get_action_params(node, main.state.code_win) vim.lsp.buf_request( diff --git a/lua/symbols-outline/hover.lua b/lua/symbols-outline/hover.lua index 27f2a01..a6e68aa 100644 --- a/lua/symbols-outline/hover.lua +++ b/lua/symbols-outline/hover.lua @@ -17,7 +17,7 @@ end -- handler yoinked from the default implementation function M.show_hover() local current_line = vim.api.nvim_win_get_cursor(so.view.winnr)[1] - local node = so.state.flattened_outline_items[current_line] + local node = so.state.outline_items[current_line] local hover_params = get_hover_params(node, so.state.code_win) diff --git a/lua/symbols-outline/parser.lua b/lua/symbols-outline/parser.lua index 251711e..6470b9c 100644 --- a/lua/symbols-outline/parser.lua +++ b/lua/symbols-outline/parser.lua @@ -24,7 +24,7 @@ local function parse_result(result, depth, hierarchy) -- whether this node is the last in its group local isLast = index == #result - local children = nil + local children = {} if value.children ~= nil then -- copy by value because we dont want it messing with the hir table local child_hir = t_utils.array_copy(hir) @@ -54,11 +54,15 @@ local function parse_result(result, depth, hierarchy) character = selectionRange.start.character, range_start = range.start.line, range_end = range['end'].line, - children = children, + -- children = children, depth = level, isLast = isLast, hierarchy = hir, }) + + for _, node in pairs(children) do + table.insert(ret, node) + end end end return ret @@ -139,25 +143,11 @@ function M.parse(response) return parse_result(sorted, nil, nil) end -function M.flatten(outline_items) - local ret = {} - for _, value in ipairs(outline_items) do - table.insert(ret, value) - if value.children ~= nil then - local inner = M.flatten(value.children) - for _, value_inner in ipairs(inner) do - table.insert(ret, value_inner) - end - end - end - return ret -end - -function M.get_lines(flattened_outline_items) +function M.get_lines(outline_items) local lines = {} local hl_info = {} - for node_line, node in ipairs(flattened_outline_items) do + for node_line, node in ipairs(outline_items) do local line = t_utils.str_to_table(string.rep(' ', node.depth)) local running_length = 1 @@ -224,9 +214,9 @@ function M.get_lines(flattened_outline_items) return lines, hl_info end -function M.get_details(flattened_outline_items) +function M.get_details(outline_items) local lines = {} - for _, value in ipairs(flattened_outline_items) do + for _, value in ipairs(outline_items) do table.insert(lines, value.detail or '') end return lines diff --git a/lua/symbols-outline/preview.lua b/lua/symbols-outline/preview.lua index 95da7ec..8b384dd 100644 --- a/lua/symbols-outline/preview.lua +++ b/lua/symbols-outline/preview.lua @@ -50,7 +50,7 @@ end local function get_hovered_node() local hovered_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1] - local node = main.state.flattened_outline_items[hovered_line] + local node = main.state.outline_items[hovered_line] return node end diff --git a/lua/symbols-outline/rename.lua b/lua/symbols-outline/rename.lua index b9d413d..1378eee 100644 --- a/lua/symbols-outline/rename.lua +++ b/lua/symbols-outline/rename.lua @@ -15,7 +15,7 @@ end function M.rename() local current_line = vim.api.nvim_win_get_cursor(so.view.winnr)[1] - local node = so.state.flattened_outline_items[current_line] + local node = so.state.outline_items[current_line] local params = get_rename_params(node, so.state.code_win) diff --git a/lua/symbols-outline/writer.lua b/lua/symbols-outline/writer.lua index 69c6fd8..8b16308 100644 --- a/lua/symbols-outline/writer.lua +++ b/lua/symbols-outline/writer.lua @@ -62,12 +62,12 @@ end -- runs the whole writing routine where the text is cleared, new data is parsed -- and then written -function M.parse_and_write(bufnr, flattened_outline_items) - local lines, hl_info = parser.get_lines(flattened_outline_items) +function M.parse_and_write(bufnr, outline_items) + local lines, hl_info = parser.get_lines(outline_items) M.write_outline(bufnr, lines) clear_virt_text(bufnr) - local details = parser.get_details(flattened_outline_items) + local details = parser.get_details(outline_items) M.add_highlights(bufnr, hl_info) M.write_details(bufnr, details) end From 926a394e7efb28feaf3a860ae61ba8881ca81213 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Mon, 15 Aug 2022 14:40:47 -0700 Subject: [PATCH 23/28] writer: Remove useless vim = vim declaration --- lua/symbols-outline/writer.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/lua/symbols-outline/writer.lua b/lua/symbols-outline/writer.lua index 8b16308..c25c64e 100644 --- a/lua/symbols-outline/writer.lua +++ b/lua/symbols-outline/writer.lua @@ -1,5 +1,3 @@ -local vim = vim - local parser = require 'symbols-outline.parser' local config = require 'symbols-outline.config' From ab3c25c3d911de2e22078353f9d620ebbd713d06 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Mon, 15 Aug 2022 14:53:29 -0700 Subject: [PATCH 24/28] refactor: Switch to new autocmd apis --- lua/symbols-outline.lua | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index 43acff3..814e492 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -10,11 +10,30 @@ local M = {} local function setup_global_autocmd() if config.options.highlight_hovered_item then - vim.cmd "au CursorHold * :lua require('symbols-outline')._highlight_current_item()" + vim.api.nvim_create_autocmd('CursorHold', { + pattern = '*', + callback = function() + M._highlight_current_item(nil) + end, + }) 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.api.nvim_create_autocmd({ + 'InsertLeave', + 'WinEnter', + 'BufEnter', + 'BufWinEnter', + 'TabEnter', + 'BufWritePost', + }, { + pattern = '*', + callback = M._refresh, + }) + + vim.api.nvim_create_autocmd('WinEnter', { + pattern = '*', + callback = require('symbols-outline.preview').close, + }) end ------------------------- From 47cbb20f5203547957e2eb8daca266ed8953bb3a Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Mon, 15 Aug 2022 14:59:40 -0700 Subject: [PATCH 25/28] feat: Bring back auto_preview --- lua/symbols-outline.lua | 15 +++++++++++++++ lua/symbols-outline/preview.lua | 23 +++++++++++------------ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index 814e492..83038ed 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -36,6 +36,20 @@ local function setup_global_autocmd() }) end +local function setup_buffer_autocmd() + if config.options.auto_preview then + vim.api.nvim_create_autocmd('CursorHold', { + buffer = 0, + callback = require('symbols-outline.preview').show, + }) + else + vim.api.nvim_create_autocmd('CursorMoved', { + buffer = 0, + callback = require('symbols-outline.preview').close, + }) + end +end + ------------------------- -- STATE ------------------------- @@ -188,6 +202,7 @@ local function handler(response) }) setup_keymaps(M.view.bufnr) + setup_buffer_autocmd() local items = parser.parse(response) diff --git a/lua/symbols-outline/preview.lua b/lua/symbols-outline/preview.lua index 8b384dd..92556c2 100644 --- a/lua/symbols-outline/preview.lua +++ b/lua/symbols-outline/preview.lua @@ -1,5 +1,4 @@ -local vim = vim -local main = require 'symbols-outline' +local so = require 'symbols-outline' local config = require 'symbols-outline.config' local M = {} @@ -13,21 +12,21 @@ local state = { local function is_current_win_outline() local curwin = vim.api.nvim_get_current_win() - return curwin == main.state.outline_win + return curwin == so.view.winnr end local function has_code_win() - local isWinValid = vim.api.nvim_win_is_valid(main.state.code_win) + local isWinValid = vim.api.nvim_win_is_valid(so.state.code_win) if not isWinValid then return false end - local bufnr = vim.api.nvim_win_get_buf(main.state.code_win) + local bufnr = vim.api.nvim_win_get_buf(so.state.code_win) local isBufValid = vim.api.nvim_buf_is_valid(bufnr) return isBufValid end local function get_offset() - local outline_winnr = main.state.outline_win + local outline_winnr = so.view.winnr local width = 53 local height = 0 @@ -49,13 +48,13 @@ local function get_height() end local function get_hovered_node() - local hovered_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1] - local node = main.state.outline_items[hovered_line] + local hovered_line = vim.api.nvim_win_get_cursor(so.view.winnr)[1] + local node = so.state.outline_items[hovered_line] return node end local function update_preview(code_buf) - code_buf = code_buf or vim.api.nvim_win_get_buf(main.state.code_win) + code_buf = code_buf or vim.api.nvim_win_get_buf(so.state.code_win) local node = get_hovered_node() if not node then @@ -73,7 +72,7 @@ local function update_preview(code_buf) end local function setup_preview_buf() - local code_buf = vim.api.nvim_win_get_buf(main.state.code_win) + local code_buf = vim.api.nvim_win_get_buf(so.state.code_win) local ft = vim.api.nvim_buf_get_option(code_buf, 'filetype') local function treesitter_attach() @@ -113,7 +112,7 @@ local function update_hover() end local provider = _G._symbols_outline_current_provider - local params = get_hover_params(node, main.state.code_win) + local params = get_hover_params(node, so.state.code_win) provider.hover_info(params.bufnr, params, function(err, result) if err then @@ -146,7 +145,7 @@ local function setup_hover_buf() if not has_code_win() then return end - local code_buf = vim.api.nvim_win_get_buf(main.state.code_win) + local code_buf = vim.api.nvim_win_get_buf(so.state.code_win) local ft = vim.api.nvim_buf_get_option(code_buf, 'filetype') vim.api.nvim_buf_set_option(state.hover_buf, 'syntax', ft) vim.api.nvim_buf_set_option(state.hover_buf, 'bufhidden', 'delete') From 66b242245578dec5cf8ae1931f3040c74434493a Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Mon, 15 Aug 2022 15:06:37 -0700 Subject: [PATCH 26/28] feat: Bring back auto_preview keymap --- lua/symbols-outline.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lua/symbols-outline.lua b/lua/symbols-outline.lua index 83038ed..40bbe54 100644 --- a/lua/symbols-outline.lua +++ b/lua/symbols-outline.lua @@ -164,7 +164,9 @@ local function setup_keymaps(bufnr) map( config.options.keymaps.hover_symbol, require('symbols-outline.hover').show_hover - ) + ) + -- preview symbol + map(config.options.keymaps.toggle_preview, require('symbols-outline.preview').toggle) -- rename symbol map( config.options.keymaps.rename_symbol, From eec01dc1ebb4bea1136439e690388c115a0537ac Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Mon, 15 Aug 2022 15:18:14 -0700 Subject: [PATCH 27/28] docs(readme): Update readme with new config instructions --- README.md | 144 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 74 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index a0813d2..5211d63 100644 --- a/README.md +++ b/README.md @@ -5,79 +5,83 @@ Supports all your favourite languages.** ![demo](https://github.com/simrat39/rust-tools-demos/raw/master/symbols-demo.gif) -### Prerequisites +## Prerequisites -- `neovim 0.5+` +- `neovim 0.7+` - Properly configured Neovim LSP client -### Installation +## Installation -Using `vim-plug` - -```vim -Plug 'simrat39/symbols-outline.nvim' -``` - -### Configuration - -Define a global variable `symbols_outline` as follows: - -Only add stuff that you want to override (even in nested tables), or skip this section entirely if you -want to roll with the defaults. +Using `packer.nvim` ```lua --- init.lua -vim.g.symbols_outline = { - highlight_hovered_item = true, - show_guides = true, - auto_preview = true, - position = 'right', - relative_width = true, - width = 25, - auto_close = false, - show_numbers = false, - show_relative_numbers = false, - show_symbol_details = true, - preview_bg_highlight = 'Pmenu', - keymaps = { -- These keymaps can be a string or a table for multiple keys - close = {"", "q"}, - goto_location = "", - focus_location = "o", - hover_symbol = "", - toggle_preview = "K", - rename_symbol = "r", - code_actions = "a", - }, - lsp_blacklist = {}, - symbol_blacklist = {}, - symbols = { - File = {icon = "", hl = "TSURI"}, - Module = {icon = "", hl = "TSNamespace"}, - Namespace = {icon = "", hl = "TSNamespace"}, - Package = {icon = "", hl = "TSNamespace"}, - Class = {icon = "𝓒", hl = "TSType"}, - Method = {icon = "ƒ", hl = "TSMethod"}, - Property = {icon = "", hl = "TSMethod"}, - Field = {icon = "", hl = "TSField"}, - Constructor = {icon = "", hl = "TSConstructor"}, - Enum = {icon = "ℰ", hl = "TSType"}, - Interface = {icon = "ﰮ", hl = "TSType"}, - Function = {icon = "", hl = "TSFunction"}, - Variable = {icon = "", hl = "TSConstant"}, - Constant = {icon = "", hl = "TSConstant"}, - String = {icon = "𝓐", hl = "TSString"}, - Number = {icon = "#", hl = "TSNumber"}, - Boolean = {icon = "⊨", hl = "TSBoolean"}, - Array = {icon = "", hl = "TSConstant"}, - Object = {icon = "⦿", hl = "TSType"}, - Key = {icon = "🔐", hl = "TSType"}, - Null = {icon = "NULL", hl = "TSType"}, - EnumMember = {icon = "", hl = "TSField"}, - Struct = {icon = "𝓢", hl = "TSType"}, - Event = {icon = "🗲", hl = "TSType"}, - Operator = {icon = "+", hl = "TSOperator"}, - TypeParameter = {icon = "𝙏", hl = "TSParameter"} - } +use 'simrat39/symbols-outline.nvim' +``` + +## Setup + +Put the setup call in your init.lua or any lua file that is sourced. + +```lua +require("symbols-outline").setup() +``` + +## Configuration + +Pass a table to the setup call above with your configuration options. + +```lua +local opts = { + highlight_hovered_item = true, + show_guides = true, + auto_preview = true, + position = 'right', + relative_width = true, + width = 25, + auto_close = false, + show_numbers = false, + show_relative_numbers = false, + show_symbol_details = true, + preview_bg_highlight = 'Pmenu', + keymaps = { -- These keymaps can be a string or a table for multiple keys + close = {"", "q"}, + goto_location = "", + focus_location = "o", + hover_symbol = "", + toggle_preview = "K", + rename_symbol = "r", + code_actions = "a", + }, + lsp_blacklist = {}, + symbol_blacklist = {}, + symbols = { + File = {icon = "", hl = "TSURI"}, + Module = {icon = "", hl = "TSNamespace"}, + Namespace = {icon = "", hl = "TSNamespace"}, + Package = {icon = "", hl = "TSNamespace"}, + Class = {icon = "𝓒", hl = "TSType"}, + Method = {icon = "ƒ", hl = "TSMethod"}, + Property = {icon = "", hl = "TSMethod"}, + Field = {icon = "", hl = "TSField"}, + Constructor = {icon = "", hl = "TSConstructor"}, + Enum = {icon = "ℰ", hl = "TSType"}, + Interface = {icon = "ﰮ", hl = "TSType"}, + Function = {icon = "", hl = "TSFunction"}, + Variable = {icon = "", hl = "TSConstant"}, + Constant = {icon = "", hl = "TSConstant"}, + String = {icon = "𝓐", hl = "TSString"}, + Number = {icon = "#", hl = "TSNumber"}, + Boolean = {icon = "⊨", hl = "TSBoolean"}, + Array = {icon = "", hl = "TSConstant"}, + Object = {icon = "⦿", hl = "TSType"}, + Key = {icon = "🔐", hl = "TSType"}, + Null = {icon = "NULL", hl = "TSType"}, + EnumMember = {icon = "", hl = "TSField"}, + Struct = {icon = "𝓢", hl = "TSType"}, + Event = {icon = "🗲", hl = "TSType"}, + Operator = {icon = "+", hl = "TSOperator"}, + TypeParameter = {icon = "𝙏", hl = "TSParameter"} + } } ``` @@ -100,7 +104,7 @@ vim.g.symbols_outline = { | lsp_blacklist | Which lsp clients to ignore | table (array) | {} | | symbol_blacklist | Which symbols to ignore ([possible values](./lua/symbols-outline/symbols.lua)) | table (array) | {} | -### Commands +## Commands | Command | Description | | ---------------------- | ---------------------- | @@ -108,7 +112,7 @@ vim.g.symbols_outline = { | `:SymbolsOutlineOpen` | Open symbols outline | | `:SymbolsOutlineClose` | Close symbols outline | -### Default keymaps +## Default keymaps | Key | Action | | ---------- | -------------------------------------------------- | @@ -121,7 +125,7 @@ vim.g.symbols_outline = { | a | Code actions | | ? | Show help message | -### Highlights +## Highlights | Highlight | Purpose | | ----------------------- | -------------------------------------- | From fac1cb602cb9775ca9afd0d3dfb18c3e68a14f12 Mon Sep 17 00:00:00 2001 From: Simrat Grewal Date: Mon, 15 Aug 2022 15:21:38 -0700 Subject: [PATCH 28/28] feat(config): Disable auto_preview by default --- README.md | 4 ++-- lua/symbols-outline/config.lua | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5211d63..66e8e69 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Pass a table to the setup call above with your configuration options. local opts = { highlight_hovered_item = true, show_guides = true, - auto_preview = true, + auto_preview = false, position = 'right', relative_width = true, width = 25, @@ -93,7 +93,7 @@ local opts = { | relative_width | Whether width of window is set relative to existing windows | boolean | true | | width | Width of window (as a % or columns based on `relative_width`) | int | 25 | | auto_close | Whether to automatically close the window after selection | boolean | false | -| auto_preview | Show a preview of the code on hover | boolean | true | +| auto_preview | Show a preview of the code on hover | boolean | false | | show_numbers | Shows numbers with the outline | boolean | false | | show_relative_numbers | Shows relative numbers with the outline | boolean | false | | show_symbol_details | Shows extra details with the symbols (lsp dependent) | boolean | true | diff --git a/lua/symbols-outline/config.lua b/lua/symbols-outline/config.lua index e61bd5e..5376b91 100644 --- a/lua/symbols-outline/config.lua +++ b/lua/symbols-outline/config.lua @@ -10,7 +10,7 @@ M.defaults = { relative_width = true, width = 25, auto_close = false, - auto_preview = true, + auto_preview = false, show_numbers = false, show_relative_numbers = false, show_symbol_details = true,