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

@@ -99,11 +99,19 @@ end
---Iterator that traverses the tree parent first before children, returning each node.
-- Essentailly 'flatten' items, but returns an iterator.
---@param items outline.SymbolNode[] Tree of symbols parsed by parse_result
function M.preorder_iter(items)
local node = { children = items, traversal_child = 1, depth = 1, folded = false }
---@param children_check function? Takes a node and return whether the children should be explored.
---Note that the root node (param items) is always explored regardless of children_check.
function M.preorder_iter(items, children_check)
local node = { children = items, traversal_child = 1, depth = 1, is_root = true }
local prev
local visited = {}
if children_check == nil then
children_check = function(n)
return not folding.is_folded(n)
end
end
return function()
while node do
if node.name and not visited[node] then
@@ -113,7 +121,7 @@ function M.preorder_iter(items)
if
node.children and node.traversal_child <= #node.children
and not folding.is_folded(node)
and (node.is_root or children_check(node))
then
prev = node
if node.children[node.traversal_child] then