Files
outline.nvim/lua/outline/view.lua
hedy d35ee70f95 feat: Better highlight-hover/follow-cursor procedures
Previously on each outline open, the `writer.make_outline` function
might be called at least 4 times(!), after this refactor it will only be
called once. And on update cursor autocmds, also called once (previously
at least twice).

behaviour:
- Now the outline window focus and highlight can update on each cursor
  move (previously CursorHold, dependent on updatetime). This is now
  configurable as well.

- During fold-all/unfold-all operations, now the cursor will remain on
  the same node (rather than same line in outline buffer).

- The performance improvement is not significantly observable since even
  the old implementation can appear instant. One may even argue I am
  fixing a problem that did not exist, but implementation-wise it's just
  so much better now.

config:
- outline_window.auto_update_events, list of events to be passed to
  create_user_autocmd for updating cursor focus in outline, and updating
  outline items (refetching symbols), using keys cursor and items
  respectively.

- outline_window.show_cursorline now supports 2 other string values:
  'focus_in_outline'/'focus_in_code' which controls when to enable
  cursorline. Setting to true retains the default behaviour of always
  showing the cursorline. This was added because now that the cursor
  focus on the outline could change on each CursorMoved, the cursorline
  may pose to be qute attention-seeking during the outline cursor
  updates. Hence `focus_in_outline` is added so that when focus is in
  code, the cursorline for outline window is not shown.

  'focus_in_code' is added so that a user who disabled
  highlight_hovered_item can keep track of position in outline when
  focus is in code, disabling cursorline when focus is in outline.

  At any given time, if hide cursor is enabled and show_cursorline is a
  string value, hiding of cursor will not be done if cursorline is not
  shown in the the given situation.

implementation:
- The reason for the improvement in performance as described in the
  first paragraph is due to merging of finding hover item and finding
  the deepest matched node to put cursor, into writer.make_outline. This
  done, when previously done in separate function, because after the
  separate function (namely _highlight_hovered_item) finishes,
  writer.make_outline is called *again* anyway.

- Autocmds to update cursor position in outline is now done per buffer
  rather than global.

Somehow the auto unfold and unfold depth options still work perfectly,
for this we should thank simrat or which ever contributor that
modularized the folding module and made it adaptable :)
2023-11-18 09:34:16 +08:00

77 lines
2.8 KiB
Lua

local cfg = require('outline.config')
local View = {}
function View:new()
return setmetatable({ bufnr = nil, winnr = nil }, { __index = View })
end
---creates the outline window and sets it up
function View:setup_view()
-- create a scratch unlisted buffer
self.bufnr = vim.api.nvim_create_buf(false, true)
-- delete buffer when window is closed / buffer is hidden
vim.api.nvim_buf_set_option(self.bufnr, 'bufhidden', 'delete')
-- create a split
vim.cmd(cfg.get_split_command())
-- resize to a % of the current window size
vim.cmd('vertical resize ' .. cfg.get_window_width())
-- get current (outline) window and attach our buffer to it
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(self.winnr, 'spell', false)
vim.api.nvim_win_set_option(self.winnr, 'signcolumn', 'no')
vim.api.nvim_win_set_option(self.winnr, 'foldcolumn', '0')
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)
vim.api.nvim_win_set_option(self.winnr, 'wrap', cfg.o.outline_window.wrap)
vim.api.nvim_win_set_option(self.winnr, 'winhl', cfg.o.outline_window.winhl)
vim.api.nvim_win_set_option(self.winnr, 'linebreak', true) -- only has effect when wrap=true
vim.api.nvim_win_set_option(self.winnr, 'breakindent', true) -- only has effect when wrap=true
-- Would be nice to use guides.markers.vertical as part of showbreak to keep
-- continuity of the tree UI, but there's currently no way to style the
-- color, apart from globally overriding hl-NonText, which will potentially
-- mess with other theme/user settings. So just use empty spaces for now.
vim.api.nvim_win_set_option(self.winnr, 'showbreak', ' ') -- only has effect when wrap=true.
-- buffer stuff
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 cfg.o.outline_window.show_numbers or cfg.o.outline_window.show_relative_numbers then
vim.api.nvim_win_set_option(self.winnr, 'nu', true)
end
if cfg.o.outline_window.show_relative_numbers then
vim.api.nvim_win_set_option(self.winnr, 'rnu', true)
end
local cl = cfg.o.outline_window.show_cursorline
if cl == true or cl == 'focus_in_outline' then
vim.api.nvim_win_set_option(self.winnr, 'cursorline', true)
end
end
function View:close()
if self.winnr then
vim.api.nvim_win_close(self.winnr, true)
self.winnr = nil
self.bufnr = nil
end
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