Skip to content

Commit cf73840

Browse files
committed
fix: c files with wonky formatting now still work
.. where wonky means putting your curly on a newline after your function declaration. Aka you operate within a religion that my religion does not tolerate. Why not just switch to emacs? It probably has better tree walking natively. Closes #34
1 parent f2b4b3b commit cf73840

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

lua/treewalker/nodes.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ local util = require "treewalker.util"
44
-- These are regexes but just happen to be real simple so far
55
local TARGET_BLACKLIST_TYPE_MATCHERS = {
66
"comment",
7-
"attribute_item", -- decorators (rust)
8-
"decorat", -- decorators (py)
9-
"else", -- else/elseif statements (lua)
10-
"elif", -- else/elseif statements (py)
11-
"end_tag", -- html closing tags
12-
"block", -- C# puts their blocks under their fn names like a psycho
13-
"declaration_list", -- C# class blocks
7+
"attribute_item", -- decorators (rust)
8+
"decorat", -- decorators (py)
9+
"else", -- else/elseif statements (lua)
10+
"elif", -- else/elseif statements (py)
11+
"end_tag", -- html closing tags
12+
"block", -- C# puts their blocks under their fn names like a psycho
13+
"declaration_list", -- C# class blocks
14+
"compound_statement", -- C blocks when defined under their fn names like a psycho
1415
}
1516

1617
local HIGHLIGHT_BLACKLIST_TYPE_MATCHERS = {

tests/fixtures/c.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,21 @@ int main() {
6060
return 0;
6161
}
6262

63+
// Some psychos actually structure their code like this
64+
void doOtherThings(Account* account)
65+
{
66+
printf("Account Number: %d\nBalance: $%.2f\n", account->accountNumber, account->balance);
67+
}
68+
69+
void doOtherThingsAgain(Account* account)
70+
{
71+
printf("Account Number: %d\nBalance: $%.2f\n", account->accountNumber, account->balance);
72+
}
73+
74+
#define PI 3.14159 // Define a constant PI (preprocessor directive)
75+
76+
void doOtherThingsAgainAgain(Account* account)
77+
{
78+
printf("Account Number: %d\nBalance: $%.2f\n", account->accountNumber, account->balance);
79+
}
80+

tests/treewalker/c_spec.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ local tw = require 'treewalker'
44
local h = require 'tests.treewalker.helpers'
55
local lines = require 'treewalker.lines'
66

7+
describe("Movement in a c file:", function()
8+
before_each(function()
9+
load_fixture("/c.c")
10+
end)
11+
12+
h.ensure_has_parser()
13+
14+
it("Moves around", function()
15+
vim.fn.cursor(46, 1)
16+
tw.move_down()
17+
h.assert_cursor_at(50, 1, "int main")
18+
tw.move_down()
19+
h.assert_cursor_at(64, 1)
20+
tw.move_down()
21+
h.assert_cursor_at(69, 1)
22+
end)
23+
end)
24+
725
describe("Swapping in a c file:", function()
826
before_each(function()
927
load_fixture("/c.c")
@@ -24,4 +42,22 @@ describe("Swapping in a c file:", function()
2442
tw.swap_left()
2543
assert.same(' printf("\\ntwo\\n", "one\\n");', lines.get_line(17))
2644
end)
45+
46+
it("swaps down on next line bracket structured functions", function()
47+
local first_block = lines.get_lines(63, 67)
48+
local second_block = lines.get_lines(69, 72)
49+
vim.fn.cursor(64, 1)
50+
tw.swap_down()
51+
assert.same(first_block, lines.get_lines(68, 72))
52+
assert.same(second_block, lines.get_lines(63, 66))
53+
end)
54+
55+
it("swaps up on next line bracket structured functions", function()
56+
local first_block = lines.get_lines(63, 67)
57+
local second_block = lines.get_lines(69, 72)
58+
vim.fn.cursor(69, 1)
59+
tw.swap_up()
60+
assert.same(first_block, lines.get_lines(68, 72))
61+
assert.same(second_block, lines.get_lines(63, 66))
62+
end)
2763
end)

0 commit comments

Comments
 (0)