fix(highlight-hover): Set cursor in parent if child is folded

Hopefully this commit fixes #1.

- Improved algorithm that finds the items to set hover-highlight
- Now we can set cursor on the nearest parent if the leaf node is folded
  in the outline.
- Set cursor column appropriate depending on whether lineno is enabled.

API:
- parser.preorder_iter now supports an optional argument as function,
  which determines whether to explore children. By default, folded
  parents will not explore children. This can now be overridden.

Behaviour:
- If you fold a nested node and hit <C-g>, you can go back to the
  nearest parent
- The column that cursor goes to is no longer arbitrarily chosen

It appears that simrat or whoever wrote this code thought the column was
1-indexed, however it is 0-indexed, so the old code was always putting
the cursor on the 2nd column.

Now, we put it in the first column. If lineno is enabled, we set the
cursor the be at the column of lineno padding, this makes both the
lineno and the markers visible.

Unfortunately the so-called 'improved' algorithm for
_highlight_current_item is still not the best. The most optimal would be
O(n). However, to make sure we stop refactoring now that it works OK and
can already fix an issue, I will leave this to posterity.

Tested to work (for me).
This commit is contained in:
hedy
2023-11-13 21:07:30 +08:00
parent 3f0025af3d
commit 9c70b96b36
3 changed files with 50 additions and 15 deletions

View File

@@ -100,11 +100,11 @@ end
-- Handles highlights, virtual text, and of course lines of outline to write
---@param bufnr integer Nothing is done if is_buffer_outline(bufnr) is not true
---@param items outline.SymbolNode[] Tree of symbols after being parsed by parser.parse_result
---@return outline.FlatSymbolNode[]? flattened_items No return value if bufnr is invalid
---@return outline.FlatSymbolNode[] flattened_items Empty table returned if bufnr is invalid
---@param codewin integer code window
function M.make_outline(bufnr, items, codewin)
if not M.is_buffer_outline(bufnr) then
return
return {}
end
local codebuf = vim.api.nvim_win_get_buf(codewin)