From 1713bac51eabe1c884e190236a5b5e67fcfaf546 Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 5 May 2026 14:45:19 +0300 Subject: [PATCH 1/2] fix: use prev_node to correctly resolve array index in nested arrays 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 --- lua/jsonpath.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lua/jsonpath.lua b/lua/jsonpath.lua index c0298ed0..4940c71b 100644 --- a/lua/jsonpath.lua +++ b/lua/jsonpath.lua @@ -61,6 +61,7 @@ M.get = function(start_node, bufnr) local accessors = {} local node = current_node + local prev_node = nil while node do local accessor = "" @@ -80,8 +81,7 @@ M.get = function(start_node, bufnr) accessor = "[]" for i, child in ipairs(get_children(node)) do - local parent = current_node:parent() - if parent == child then + if prev_node == child then accessor = string.format("[%d]", i - 1) end end @@ -91,6 +91,7 @@ M.get = function(start_node, bufnr) table.insert(accessors, 1, accessor) end + prev_node = node node = node:parent() end From d074c61871dea5f694017139b346aee86a2d3cd3 Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 5 May 2026 16:28:27 +0300 Subject: [PATCH 2/2] fix: use ancestor traversal to resolve array index Co-Authored-By: Claude Sonnet 4.6 --- lua/jsonpath.lua | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lua/jsonpath.lua b/lua/jsonpath.lua index 4940c71b..eb2798d7 100644 --- a/lua/jsonpath.lua +++ b/lua/jsonpath.lua @@ -61,7 +61,6 @@ M.get = function(start_node, bufnr) local accessors = {} local node = current_node - local prev_node = nil while node do local accessor = "" @@ -78,11 +77,19 @@ M.get = function(start_node, bufnr) end if node:type() == "array" then - accessor = "[]" - - for i, child in ipairs(get_children(node)) do - if prev_node == child then - accessor = string.format("[%d]", i - 1) + local index = 0 + for child in node:iter_children() do + if child:named() then + local n = current_node + while n and n ~= node do + if n == child then + accessor = string.format("[%d]", index) + break + end + n = n:parent() + end + if accessor ~= "" then break end + index = index + 1 end end end @@ -91,7 +98,6 @@ M.get = function(start_node, bufnr) table.insert(accessors, 1, accessor) end - prev_node = node node = node:parent() end