Skip to content

Commit e339e81

Browse files
committed
Find better way to fix csharp block issues
Closes #52
1 parent bccd819 commit e339e81

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

lua/treewalker/nodes.lua

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@ 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 - these are structural containers
13+
"declaration_list", -- C# class blocks
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",
15+
"c_sharp:block", -- C# block nodes (language-specific)
2016
}
2117

2218
local HIGHLIGHT_BLACKLIST_TYPE_MATCHERS = {
@@ -33,22 +29,38 @@ local AUGMENT_TARGET_TYPE_MATCHERS = {
3329

3430
local M = {}
3531

32+
---Get the parser name for the current buffer
33+
---@return string|nil
34+
local function get_parser_name()
35+
local ok, parser = pcall(vim.treesitter.get_parser, 0)
36+
if not ok or not parser then
37+
return nil
38+
end
39+
40+
local ok_lang, lang = pcall(parser.lang, parser)
41+
if not ok_lang then
42+
return nil
43+
end
44+
45+
return lang
46+
end
47+
3648
---@param node TSNode
3749
---@param matchers string[]
3850
---@return boolean
3951
local function is_matched_in(node, matchers)
52+
local parser_name = get_parser_name()
53+
4054
for _, matcher in ipairs(matchers) do
41-
-- Check for parent-child pattern: "child_type:parent_type"
55+
-- Check if matcher is language-specific (contains ':')
4256
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
57+
local lang, node_type = matcher:match("([^:]+):(.+)")
58+
-- Only apply this matcher if we're in the specified language
59+
if parser_name and lang == parser_name and node:type():match(node_type) then
60+
return true
4961
end
5062
else
51-
-- Simple type matching
63+
-- Regular matcher, apply to all languages
5264
if node:type():match(matcher) then
5365
return true
5466
end

tests/treewalker/c_sharp_spec.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,26 @@ describe("In a C Sharp file", function()
3434
h.assert_cursor_at(27, 5)
3535
end)
3636

37+
it("moves through foreach/try/catch blocks", function()
38+
vim.fn.cursor(113, 13)
39+
tw.move_down()
40+
h.assert_cursor_at(114, 13)
41+
tw.move_down()
42+
h.assert_cursor_at(116, 13)
43+
tw.move_down()
44+
h.assert_cursor_at(117, 13)
45+
tw.move_down()
46+
h.assert_cursor_at(122, 13)
47+
tw.move_down()
48+
h.assert_cursor_at(130, 13)
49+
tw.move_down()
50+
h.assert_cursor_at(135, 13)
51+
tw.move_down()
52+
h.assert_cursor_at(143, 13)
53+
tw.move_down()
54+
h.assert_cursor_at(144, 13)
55+
end)
56+
3757
it("swaps down classes", function()
3858
vim.fn.cursor(7, 5)
3959
local first_block = lines.get_lines(6, 24)
@@ -74,4 +94,13 @@ describe("In a C Sharp file", function()
7494
h.assert_cursor_at(31, 9)
7595
end)
7696

97+
it("swaps inner blocks across functions", function()
98+
vim.fn.cursor(122, 13)
99+
local first_block = lines.get_lines(122, 125)
100+
local second_block = lines.get_lines(130, 138)
101+
tw.swap_down()
102+
assert.same(second_block, lines.get_lines(122, 130))
103+
assert.same(first_block, lines.get_lines(135, 138))
104+
h.assert_cursor_at(135, 13)
105+
end)
77106
end)

0 commit comments

Comments
 (0)