Skip to content

Commit bccd819

Browse files
committed
Fix issue with C# node identification
Finally we need some contextual matchers, bummer. Closes #51
1 parent 51aedba commit bccd819

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

lua/treewalker/nodes.lua

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ local TARGET_BLACKLIST_TYPE_MATCHERS = {
1010
"else", -- else/elseif statements (lua)
1111
"elif", -- else/elseif statements (py)
1212
"end_tag", -- html closing tags
13-
"declaration_list", -- C# class blocks
13+
"declaration_list", -- C# class blocks - these are structural containers
1414
"compound_statement", -- C blocks when defined under their fn names like a psycho
15+
-- Parent-child patterns: "child_type:parent_type"
16+
"block:method_declaration",
17+
"block:constructor_declaration",
18+
"block:destructor_declaration",
19+
"block:class_declaration",
1520
}
1621

1722
local HIGHLIGHT_BLACKLIST_TYPE_MATCHERS = {
@@ -33,8 +38,20 @@ local M = {}
3338
---@return boolean
3439
local function is_matched_in(node, matchers)
3540
for _, matcher in ipairs(matchers) do
36-
if node:type():match(matcher) then
37-
return true
41+
-- Check for parent-child pattern: "child_type:parent_type"
42+
if matcher:find(":") then
43+
local child_type, parent_type = matcher:match("^([^:]+):([^:]+)$")
44+
if child_type and parent_type then
45+
local parent = node:parent()
46+
if node:type():match(child_type) and parent and parent:type():match(parent_type) then
47+
return true
48+
end
49+
end
50+
else
51+
-- Simple type matching
52+
if node:type():match(matcher) then
53+
return true
54+
end
3855
end
3956
end
4057
return false

tests/treewalker/c_sharp_spec.lua

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,25 @@ describe("In a C Sharp file", function()
1616
h.assert_cursor_at(27, 5)
1717
tw.move_down()
1818
h.assert_cursor_at(50, 5)
19+
tw.move_up()
20+
h.assert_cursor_at(27, 5)
1921
tw.move_in()
20-
h.assert_cursor_at(52, 9)
22+
h.assert_cursor_at(29, 9)
23+
tw.move_down()
24+
h.assert_cursor_at(31, 9)
25+
tw.move_down()
26+
h.assert_cursor_at(36, 9)
27+
tw.move_down()
28+
h.assert_cursor_at(41, 9)
29+
tw.move_up()
30+
h.assert_cursor_at(36, 9)
31+
tw.move_up()
32+
h.assert_cursor_at(31, 9)
2133
tw.move_out()
22-
h.assert_cursor_at(50, 5)
34+
h.assert_cursor_at(27, 5)
2335
end)
2436

25-
it("swaps down", function()
37+
it("swaps down classes", function()
2638
vim.fn.cursor(7, 5)
2739
local first_block = lines.get_lines(6, 24)
2840
local second_block = lines.get_lines(26, 48)
@@ -32,7 +44,17 @@ describe("In a C Sharp file", function()
3244
h.assert_cursor_at(31, 5)
3345
end)
3446

35-
it("swaps up", function()
47+
it("swaps down functions", function()
48+
vim.fn.cursor(31, 9)
49+
local first_block = lines.get_lines(31, 34)
50+
local second_block = lines.get_lines(36, 39)
51+
tw.swap_down()
52+
assert.same(second_block, lines.get_lines(31, 34))
53+
assert.same(first_block, lines.get_lines(36, 39))
54+
h.assert_cursor_at(36, 9)
55+
end)
56+
57+
it("swaps up classes", function()
3658
vim.fn.cursor(27, 5)
3759
local first_block = lines.get_lines(6, 24)
3860
local second_block = lines.get_lines(26, 48)
@@ -41,4 +63,15 @@ describe("In a C Sharp file", function()
4163
assert.same(first_block, lines.get_lines(30, 48))
4264
h.assert_cursor_at(7, 5)
4365
end)
66+
67+
it("swaps up functions", function()
68+
vim.fn.cursor(36, 9)
69+
local first_block = lines.get_lines(31, 34)
70+
local second_block = lines.get_lines(36, 39)
71+
tw.swap_up()
72+
assert.same(second_block, lines.get_lines(31, 34))
73+
assert.same(first_block, lines.get_lines(36, 39))
74+
h.assert_cursor_at(31, 9)
75+
end)
76+
4477
end)

0 commit comments

Comments
 (0)