Skip to content

Commit bbdcc38

Browse files
committed
Highlight whole node on move_out even if node hasn't changed
Originally this is so the normal! ^ looks like it's having an effect with the highlight, but it actually is kinda cool on its own imho
1 parent 5195f9b commit bbdcc38

File tree

5 files changed

+48
-35
lines changed

5 files changed

+48
-35
lines changed

checks/rogue_util_calls_spec.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ local load_fixture = require "tests.load_fixture"
66
local assert = require 'luassert'
77
local stub = require 'luassert.stub'
88
local util = require 'treewalker.util'
9-
local nodes = require 'treewalker.nodes'
109

1110
local commands = {
1211
"Treewalker Up",

lua/treewalker/movement.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ function M.move_out()
99
vim.cmd("normal! ^")
1010
local node = nodes.get_current()
1111
local target, row = targets.out(node)
12-
if not (target and row) then return end
12+
if not (target and row) then
13+
-- highlight the node anyways, for when normal! ^ does something
14+
operations.jump(node, nodes.get_srow(node))
15+
return
16+
end
1317
vim.cmd("normal! m'") -- Add originating node to jump list
1418
operations.jump(target, row)
1519
vim.cmd("normal! m'") -- Add destination node to jump list

lua/treewalker/nodes.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ end
180180
---@param node TSNode
181181
---@return TSNode
182182
function M.get_highest_row_coincident(node)
183-
local iter = node:parent()
183+
---@type TSNode | nil
184+
local iter = node
184185
while iter and M.have_same_srow(node, iter) do
185186
if M.is_highlight_target(iter) then node = iter end
186187
iter = iter:parent()

tests/treewalker/highlight_spec.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ local load_fixture = require "tests.load_fixture"
22
local stub = require 'luassert.stub'
33
local assert = require "luassert"
44
local tw = require 'treewalker'
5+
local h = require 'tests.treewalker.helpers'
56
local operations = require 'treewalker.operations'
67

78
local highlight_stub = stub(operations, "highlight")
89

910
-- use with rows as they're numbered in vim lines (1-indexed)
1011
local function assert_highlighted(srow, scol, erow, ecol, desc)
12+
assert(highlight_stub.calls[1], "highlight was not called at all")
1113
assert.same(
1214
{ srow - 1, scol - 1, erow - 1, ecol },
1315
highlight_stub.calls[1].refs[1],
@@ -115,4 +117,11 @@ describe("Highlights in a regular lua file: ", function()
115117
tw.move_up()
116118
assert_highlighted(22, 3, 26, 5, "child = iter()")
117119
end)
120+
121+
it("given in a line with no parent, move_out highlights the whole node", function()
122+
vim.fn.cursor(21, 16) -- |is_jump_target
123+
tw.move_out()
124+
h.assert_cursor_at(21, 1)
125+
assert_highlighted(21, 1, 28, 3, "is_jump_target function")
126+
end)
118127
end)

tests/treewalker/movement_spec.lua

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local load_fixture = require "tests.load_fixture"
22
local tw = require 'treewalker'
3-
local helpers = require 'tests.treewalker.helpers'
3+
local h = require 'tests.treewalker.helpers'
44

55
describe("Movement in a regular lua file: ", function()
66
before_each(function()
@@ -10,80 +10,80 @@ describe("Movement in a regular lua file: ", function()
1010
it("moves up and down at the same pace", function()
1111
vim.fn.cursor(1, 1) -- Reset cursor
1212
tw.move_down()
13-
helpers.assert_cursor_at(3, 1)
13+
h.assert_cursor_at(3, 1)
1414
tw.move_down()
15-
helpers.assert_cursor_at(5, 1)
15+
h.assert_cursor_at(5, 1)
1616
tw.move_down()
17-
helpers.assert_cursor_at(10, 1)
17+
h.assert_cursor_at(10, 1)
1818
tw.move_down()
19-
helpers.assert_cursor_at(21, 1)
19+
h.assert_cursor_at(21, 1)
2020
tw.move_up()
21-
helpers.assert_cursor_at(10, 1)
21+
h.assert_cursor_at(10, 1)
2222
tw.move_up()
23-
helpers.assert_cursor_at(5, 1)
23+
h.assert_cursor_at(5, 1)
2424
tw.move_up()
25-
helpers.assert_cursor_at(3, 1)
25+
h.assert_cursor_at(3, 1)
2626
tw.move_up()
27-
helpers.assert_cursor_at(1, 1)
27+
h.assert_cursor_at(1, 1)
2828
end)
2929

3030
it("doesn't consider empty lines to be outer scopes", function()
3131
vim.fn.cursor(85, 1)
3232
tw.move_down()
33-
helpers.assert_cursor_at(88, 3, "local")
33+
h.assert_cursor_at(88, 3, "local")
3434
vim.fn.cursor(85, 1)
3535
tw.move_up()
36-
helpers.assert_cursor_at(84, 3, "end")
36+
h.assert_cursor_at(84, 3, "end")
3737
end)
3838

3939
it("goes into functions eagerly", function()
4040
vim.fn.cursor(143, 1) -- In a bigger function
4141
tw.move_in()
42-
helpers.assert_cursor_at(144, 3)
42+
h.assert_cursor_at(144, 3)
4343
tw.move_in()
44-
helpers.assert_cursor_at(147, 5)
44+
h.assert_cursor_at(147, 5)
4545
tw.move_in()
46-
helpers.assert_cursor_at(149, 7)
46+
h.assert_cursor_at(149, 7)
4747
end)
4848

4949
it("doesn't jump into a comment", function()
5050
vim.fn.cursor(177, 1)
5151
tw.move_in()
52-
helpers.assert_cursor_at(179, 3, "local")
52+
h.assert_cursor_at(179, 3, "local")
5353
end)
5454

5555
it("goes out of functions", function()
5656
vim.fn.cursor(149, 7)
5757
tw.move_out()
58-
helpers.assert_cursor_at(148, 5, "if")
58+
h.assert_cursor_at(148, 5, "if")
5959
tw.move_out()
60-
helpers.assert_cursor_at(146, 3, "while")
60+
h.assert_cursor_at(146, 3, "while")
6161
tw.move_out()
62-
helpers.assert_cursor_at(143, 1, "function")
62+
h.assert_cursor_at(143, 1, "function")
6363
end)
6464

6565
-- aka doesn't error
6666
it("is chill when down is invoked from empty last line", function()
67-
helpers.feed_keys('G')
67+
h.feed_keys('G')
6868
tw.move_down()
6969
end)
7070

7171
it("moves up from inside a function", function()
7272
vim.fn.cursor(21, 16) -- |is_jump_target
7373
tw.move_up()
74-
helpers.assert_cursor_at(10, 1, "local TARGET_DESCENDANT_TYPES")
74+
h.assert_cursor_at(10, 1, "local TARGET_DESCENDANT_TYPES")
7575
end)
7676

7777
it("moves down from inside a function", function()
7878
vim.fn.cursor(21, 16) -- |is_jump_target
7979
tw.move_down()
80-
helpers.assert_cursor_at(30, 1, "local function is_descendant_jump_target")
80+
h.assert_cursor_at(30, 1, "local function is_descendant_jump_target")
8181
end)
8282

8383
it("moves to true outer node when invoked from inside a line", function()
8484
vim.fn.cursor(22, 28) -- |NON_
8585
tw.move_out()
86-
helpers.assert_cursor_at(21, 1)
86+
h.assert_cursor_at(21, 1)
8787
end)
8888
end)
8989

@@ -98,28 +98,28 @@ describe("Movement in a lua spec file: ", function()
9898
for _ = 1, 6 do
9999
tw.move_down()
100100
end
101-
helpers.assert_cursor_at(17, 1, "describe")
101+
h.assert_cursor_at(17, 1, "describe")
102102
end
103103

104104
-- go to first load_buf
105105
local function go_to_load_buf()
106106
go_to_describe()
107107
tw.move_in(); tw.move_in()
108-
helpers.assert_cursor_at(19, 5, "load_buf")
108+
h.assert_cursor_at(19, 5, "load_buf")
109109
end
110110

111111
it("moves up and down at the same pace", function()
112112
go_to_load_buf()
113113
tw.move_down(); tw.move_down()
114-
helpers.assert_cursor_at(41, 5, "it")
114+
h.assert_cursor_at(41, 5, "it")
115115
tw.move_up(); tw.move_up()
116-
helpers.assert_cursor_at(19, 5, "load_buf")
116+
h.assert_cursor_at(19, 5, "load_buf")
117117
end)
118118

119119
it("always moves down at least one line", function()
120120
go_to_load_buf()
121121
tw.move_down()
122-
helpers.assert_cursor_at(21, 5, "it")
122+
h.assert_cursor_at(21, 5, "it")
123123
end)
124124
end)
125125

@@ -131,7 +131,7 @@ describe("Movement in a haskell file: ", function()
131131
it("moves out of a nested node", function()
132132
vim.fn.cursor(22, 3)
133133
tw.move_out()
134-
helpers.assert_cursor_at(19, 1, "|randomList")
134+
h.assert_cursor_at(19, 1, "|randomList")
135135
end)
136136
end)
137137

@@ -143,12 +143,12 @@ describe("Movement in a python file: ", function()
143143
it("You can get into the body of a function with multiline signature", function()
144144
vim.fn.cursor(131, 3) -- de|f
145145
tw.move_in()
146-
helpers.assert_cursor_at(132, 5)
146+
h.assert_cursor_at(132, 5)
147147
tw.move_down()
148-
helpers.assert_cursor_at(133, 5)
148+
h.assert_cursor_at(133, 5)
149149
tw.move_down()
150-
helpers.assert_cursor_at(134, 5)
150+
h.assert_cursor_at(134, 5)
151151
tw.move_down()
152-
helpers.assert_cursor_at(136, 5, "|print")
152+
h.assert_cursor_at(136, 5, "|print")
153153
end)
154154
end)

0 commit comments

Comments
 (0)