Skip to content

Commit

Permalink
Highlight whole node on move_out even if node hasn't changed
Browse files Browse the repository at this point in the history
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
  • Loading branch information
aaronik committed Feb 8, 2025
1 parent 5195f9b commit bbdcc38
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 35 deletions.
1 change: 0 additions & 1 deletion checks/rogue_util_calls_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ local load_fixture = require "tests.load_fixture"
local assert = require 'luassert'
local stub = require 'luassert.stub'
local util = require 'treewalker.util'
local nodes = require 'treewalker.nodes'

local commands = {
"Treewalker Up",
Expand Down
6 changes: 5 additions & 1 deletion lua/treewalker/movement.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ function M.move_out()
vim.cmd("normal! ^")
local node = nodes.get_current()
local target, row = targets.out(node)
if not (target and row) then return end
if not (target and row) then
-- highlight the node anyways, for when normal! ^ does something
operations.jump(node, nodes.get_srow(node))
return
end
vim.cmd("normal! m'") -- Add originating node to jump list
operations.jump(target, row)
vim.cmd("normal! m'") -- Add destination node to jump list
Expand Down
3 changes: 2 additions & 1 deletion lua/treewalker/nodes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ end
---@param node TSNode
---@return TSNode
function M.get_highest_row_coincident(node)
local iter = node:parent()
---@type TSNode | nil
local iter = node
while iter and M.have_same_srow(node, iter) do
if M.is_highlight_target(iter) then node = iter end
iter = iter:parent()
Expand Down
9 changes: 9 additions & 0 deletions tests/treewalker/highlight_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ local load_fixture = require "tests.load_fixture"
local stub = require 'luassert.stub'
local assert = require "luassert"
local tw = require 'treewalker'
local h = require 'tests.treewalker.helpers'
local operations = require 'treewalker.operations'

local highlight_stub = stub(operations, "highlight")

-- use with rows as they're numbered in vim lines (1-indexed)
local function assert_highlighted(srow, scol, erow, ecol, desc)
assert(highlight_stub.calls[1], "highlight was not called at all")
assert.same(
{ srow - 1, scol - 1, erow - 1, ecol },
highlight_stub.calls[1].refs[1],
Expand Down Expand Up @@ -115,4 +117,11 @@ describe("Highlights in a regular lua file: ", function()
tw.move_up()
assert_highlighted(22, 3, 26, 5, "child = iter()")
end)

it("given in a line with no parent, move_out highlights the whole node", function()
vim.fn.cursor(21, 16) -- |is_jump_target
tw.move_out()
h.assert_cursor_at(21, 1)
assert_highlighted(21, 1, 28, 3, "is_jump_target function")
end)
end)
64 changes: 32 additions & 32 deletions tests/treewalker/movement_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local load_fixture = require "tests.load_fixture"
local tw = require 'treewalker'
local helpers = require 'tests.treewalker.helpers'
local h = require 'tests.treewalker.helpers'

describe("Movement in a regular lua file: ", function()
before_each(function()
Expand All @@ -10,80 +10,80 @@ describe("Movement in a regular lua file: ", function()
it("moves up and down at the same pace", function()
vim.fn.cursor(1, 1) -- Reset cursor
tw.move_down()
helpers.assert_cursor_at(3, 1)
h.assert_cursor_at(3, 1)
tw.move_down()
helpers.assert_cursor_at(5, 1)
h.assert_cursor_at(5, 1)
tw.move_down()
helpers.assert_cursor_at(10, 1)
h.assert_cursor_at(10, 1)
tw.move_down()
helpers.assert_cursor_at(21, 1)
h.assert_cursor_at(21, 1)
tw.move_up()
helpers.assert_cursor_at(10, 1)
h.assert_cursor_at(10, 1)
tw.move_up()
helpers.assert_cursor_at(5, 1)
h.assert_cursor_at(5, 1)
tw.move_up()
helpers.assert_cursor_at(3, 1)
h.assert_cursor_at(3, 1)
tw.move_up()
helpers.assert_cursor_at(1, 1)
h.assert_cursor_at(1, 1)
end)

it("doesn't consider empty lines to be outer scopes", function()
vim.fn.cursor(85, 1)
tw.move_down()
helpers.assert_cursor_at(88, 3, "local")
h.assert_cursor_at(88, 3, "local")
vim.fn.cursor(85, 1)
tw.move_up()
helpers.assert_cursor_at(84, 3, "end")
h.assert_cursor_at(84, 3, "end")
end)

it("goes into functions eagerly", function()
vim.fn.cursor(143, 1) -- In a bigger function
tw.move_in()
helpers.assert_cursor_at(144, 3)
h.assert_cursor_at(144, 3)
tw.move_in()
helpers.assert_cursor_at(147, 5)
h.assert_cursor_at(147, 5)
tw.move_in()
helpers.assert_cursor_at(149, 7)
h.assert_cursor_at(149, 7)
end)

it("doesn't jump into a comment", function()
vim.fn.cursor(177, 1)
tw.move_in()
helpers.assert_cursor_at(179, 3, "local")
h.assert_cursor_at(179, 3, "local")
end)

it("goes out of functions", function()
vim.fn.cursor(149, 7)
tw.move_out()
helpers.assert_cursor_at(148, 5, "if")
h.assert_cursor_at(148, 5, "if")
tw.move_out()
helpers.assert_cursor_at(146, 3, "while")
h.assert_cursor_at(146, 3, "while")
tw.move_out()
helpers.assert_cursor_at(143, 1, "function")
h.assert_cursor_at(143, 1, "function")
end)

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

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

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

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

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

-- go to first load_buf
local function go_to_load_buf()
go_to_describe()
tw.move_in(); tw.move_in()
helpers.assert_cursor_at(19, 5, "load_buf")
h.assert_cursor_at(19, 5, "load_buf")
end

it("moves up and down at the same pace", function()
go_to_load_buf()
tw.move_down(); tw.move_down()
helpers.assert_cursor_at(41, 5, "it")
h.assert_cursor_at(41, 5, "it")
tw.move_up(); tw.move_up()
helpers.assert_cursor_at(19, 5, "load_buf")
h.assert_cursor_at(19, 5, "load_buf")
end)

it("always moves down at least one line", function()
go_to_load_buf()
tw.move_down()
helpers.assert_cursor_at(21, 5, "it")
h.assert_cursor_at(21, 5, "it")
end)
end)

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

Expand All @@ -143,12 +143,12 @@ describe("Movement in a python file: ", function()
it("You can get into the body of a function with multiline signature", function()
vim.fn.cursor(131, 3) -- de|f
tw.move_in()
helpers.assert_cursor_at(132, 5)
h.assert_cursor_at(132, 5)
tw.move_down()
helpers.assert_cursor_at(133, 5)
h.assert_cursor_at(133, 5)
tw.move_down()
helpers.assert_cursor_at(134, 5)
h.assert_cursor_at(134, 5)
tw.move_down()
helpers.assert_cursor_at(136, 5, "|print")
h.assert_cursor_at(136, 5, "|print")
end)
end)

0 comments on commit bbdcc38

Please sign in to comment.