Skip to content

Commit 2f6fc49

Browse files
committed
Update comment detection logic for better jumping from comments
Also: * Simplify the butts off of target's current node identification, making it more node wise than line start number wise * Move some errant print statements in assert_highlighted so they only show on failures * Fix up some comments and error strings
1 parent e339e81 commit 2f6fc49

File tree

5 files changed

+21
-26
lines changed

5 files changed

+21
-26
lines changed

lua/treewalker/nodes.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ function M.get_lines(node)
282282
return vim.split(text, "\n")
283283
end
284284

285-
-- get 1-indexed row of given node
285+
-- get 1-indexed start row of given node
286286
-- (so will work directly with vim.fn.cursor,
287287
-- and will reflect row as seen in the vim status line)
288288
---@param node TSNode
@@ -302,7 +302,7 @@ function M.get_erow(node)
302302
return row + 1
303303
end
304304

305-
-- get 1-indexed column of given node
305+
-- get 1-indexed start column of given node
306306
-- (so will work directly with vim.fn.cursor,
307307
-- and will reflect col as seen in the vim status line)
308308
---@param node TSNode
@@ -340,7 +340,7 @@ end
340340
---@return TSNode
341341
function M.get_current()
342342
local current = vim.treesitter.get_node({ ignore_injections = false })
343-
assert(current, "Treewalker: Treesitter node not found under cursor. This shouldn't happen!")
343+
assert(current, "Treewalker: Treesitter node not found under cursor. Missing parser?")
344344
return current
345345
end
346346

lua/treewalker/targets.lua

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local lines = require "treewalker.lines"
21
local nodes = require "treewalker.nodes"
32
local strategies = require "treewalker.strategies"
43
local util = require "treewalker.util"
@@ -7,25 +6,12 @@ local markdown_targets = require "treewalker.markdown.targets"
76
local M = {}
87

98
-- Gets a bunch of information about where the user currently is.
10-
-- I don't really like this here, I wish everything ran on nodes.
11-
-- But the node information is often wrong, like the current node
12-
-- could come back as a bigger containing scope, and the behavior
13-
-- would be unintuitive.
149
---@return integer, integer
1510
local function current()
1611
local current_row = vim.fn.line(".")
17-
local current_line = lines.get_line(current_row)
18-
assert(current_line, "Treewalker: cursor is on invalid line number")
19-
20-
-- When on a non-jump-target node (like a comment), use actual cursor column
21-
-- instead of the line's start column to allow movement to differently indented code
2212
local current_node = nodes.get_current()
23-
if current_node and not nodes.is_jump_target(current_node) then
24-
return current_row, vim.fn.col(".")
25-
end
26-
27-
local current_col = lines.get_start_col(current_line)
28-
return current_row, current_col
13+
local highest_coincident = nodes.get_highest_row_coincident(current_node)
14+
return current_row, nodes.get_scol(highest_coincident)
2915
end
3016

3117
---Get the highest coincident; helper

tests/treewalker/helpers.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,18 @@ function M.assert_highlighted(srow, scol, erow, ecol)
106106
local actual_erow = highlight[4].end_row + 1
107107
local actual_ecol = highlight[4].end_col
108108

109-
print("actual_srow:", actual_srow)
110-
print("actual_scol:", actual_scol)
111-
print("actual_erow:", actual_erow)
112-
print("actual_ecol:", actual_ecol)
113-
114109
if
115110
srow == actual_srow
116111
and scol == actual_scol
117112
and erow == actual_erow
118113
and ecol == actual_ecol
119114
then
120115
return true
116+
else
117+
print("actual_srow:", actual_srow)
118+
print("actual_scol:", actual_scol)
119+
print("actual_erow:", actual_erow)
120+
print("actual_ecol:", actual_ecol)
121121
end
122122
end
123123

tests/treewalker/missing_parser_spec.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ describe("For a file in which there is a missing parser", function()
3131
end)
3232

3333
describe("For a file in which there is a parser present", function()
34-
load_fixture("/lua.lua")
34+
before_each(function()
35+
load_fixture("/lua.lua")
36+
end)
3537

3638
for nam, command in pairs(commands) do
3739
it(string.format("does not notify when %s is called", nam), function()
@@ -59,4 +61,3 @@ describe("For a file in which there is a missing parser, when notifications are
5961
end)
6062
end
6163
end)
62-

tests/treewalker/typescript_spec.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,13 @@ describe("In a typescript file:", function()
3131
vim.fn.cursor(115, 1)
3232
tw.move_down()
3333
h.assert_cursor_at(117, 1)
34+
35+
vim.fn.cursor(115, 3)
36+
tw.move_down()
37+
h.assert_cursor_at(117, 1)
38+
39+
vim.fn.cursor(115, 7)
40+
tw.move_down()
41+
h.assert_cursor_at(117, 1)
3442
end)
3543
end)

0 commit comments

Comments
 (0)