fix: correctly resolve array index in nested arrays#17
Open
Nikita-90 wants to merge 2 commits into
Open
Conversation
When traversing the treesitter tree upward, `current_node:parent()` is always the direct parent of the cursor node. This means array index detection only worked when the array was the immediate parent of the cursor, producing `[]` for all outer/nested arrays. Track `prev_node` across iterations so that when an `array` node is reached, we compare against the actual direct child of that array containing the cursor. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When the cursor is inside a deeply nested array, the array index is shown as
[]instead of the actual index (e.g.[0]).Example: with this JSON:
{ "users": [ { "orders": [ { "id": 1 } ] } ] }With cursor on
"id", the winbar shows.users[].orders[]instead of.users[0].orders[0].Root cause
When traversing the treesitter tree upward, the original code used
current_node:parent()to find which child of an array contains the cursor. This is always the direct parent of the initial cursor node, so it only matched when the array was the immediate parent. For any outer/nested array, the comparison failed and the index stayed as[]. Additionally, iterating all children withipairscounted anonymous nodes (brackets, commas), causing an off-by-one error.Fix
For each named child of the array, walk up from
current_nodeto check if the cursor is inside that child. This correctly handles nested arrays at any depth.