Skip to content

Commit 501cb86

Browse files
ahdzib-mayaaaronik
authored andcommitted
fix: use visual column for navigation comparison
The navigation functions compare columns using lines.get_start_col() which returns visual column (with tab expansion). The current() function in targets.lua was returning byte column from nodes.get_scol(), causing sibling detection to fail when files use tabs for indentation. This fix changes current() to use visual column for the comparison, using the node's starting row (not cursor row) to handle cases where cursor is inside a multi-line node that starts on a different line. Fixes sibling navigation (Up/Down) in files with tab indentation.
1 parent 85e79f6 commit 501cb86

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lua/treewalker/targets.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
local nodes = require "treewalker.nodes"
22
local strategies = require "treewalker.strategies"
33
local util = require "treewalker.util"
4+
local lines = require "treewalker.lines"
45
local markdown_targets = require "treewalker.markdown.targets"
56

67
local M = {}
78

9+
-- Get visual column for node's starting position
10+
-- This matches lines.get_start_col() used in strategies.lua
11+
-- Needed because get_scol() returns byte column, but strategies uses visual column
12+
local function get_visual_col(node)
13+
local node_srow = nodes.get_srow(node)
14+
local line = lines.get_line(node_srow)
15+
return line and lines.get_start_col(line) or nodes.get_scol(node)
16+
end
17+
818
---@param node TSNode
919
---@return TSNode | nil, integer | nil
1020
function M.out(node)
@@ -45,7 +55,7 @@ function M.up(current_node, current_row)
4555
if util.is_markdown_file() then
4656
return markdown_targets.up()
4757
end
48-
local current_col = nodes.get_scol(current_node)
58+
local current_col = get_visual_col(current_node)
4959
local candidate, candidate_row = strategies.get_neighbor_at_same_col("up", current_row, current_col, nil, nil)
5060
return candidate, candidate_row
5161
end
@@ -57,7 +67,7 @@ function M.down(current_node, current_row)
5767
if util.is_markdown_file() then
5868
return markdown_targets.down()
5969
end
60-
local current_col = nodes.get_scol(current_node)
70+
local current_col = get_visual_col(current_node)
6171
local candidate, candidate_row = strategies.get_neighbor_at_same_col("down", current_row, current_col, nil, nil)
6272
return candidate, candidate_row
6373
end

0 commit comments

Comments
 (0)