Skip to content

Commit a9080f1

Browse files
fix: empty struct returns being erroneously wrapped in parens (#17)
1 parent 647cb16 commit a9080f1

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

lua/auto-fix-return/fix.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ function M.build_fixed_definition(line, cursor_col)
136136
or curr_word:find("chan$") ~= nil
137137
or curr_word:find("^func")
138138
or curr_word:find("^interface")
139+
or curr_word:find("^struct")
139140
)
140141
then
141142
-- Peek ahead to find the next non-space character

lua/test/declaration_test_spec.lua

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,31 @@ describe("test functions with body defined", function()
10991099
eq("{", char)
11001100
end)
11011101
end)
1102+
1103+
describe(
1104+
"when a struct with a space between the struct keyword and the braces with cursor at the end of the braces",
1105+
function()
1106+
local winid = 0
1107+
before_each(function()
1108+
winid = utils.set_test_window_value("func Foo() struct {}| {}")
1109+
vim.cmd("AutoFixReturn")
1110+
end)
1111+
1112+
after_each(function()
1113+
utils.cleanup_test(winid)
1114+
end)
1115+
1116+
it("should not add parentheses around the return type", function()
1117+
local lines = utils.get_win_lines(winid)
1118+
eq("func Foo() struct {} {}", lines[1])
1119+
end)
1120+
1121+
it("should not touch the cursor", function()
1122+
local char = utils.get_cursor_char(winid)
1123+
eq("}", char)
1124+
end)
1125+
end
1126+
)
11021127
end)
11031128

11041129
describe("test functions without a body defined", function()
@@ -1834,6 +1859,31 @@ describe("test functions without a body defined", function()
18341859
end)
18351860
end
18361861
)
1862+
1863+
describe(
1864+
"when a struct with a space between the struct keyword and the braces with cursor at the end of the braces",
1865+
function()
1866+
local winid = 0
1867+
before_each(function()
1868+
winid = utils.set_test_window_value("func Foo() struct {}|")
1869+
vim.cmd("AutoFixReturn")
1870+
end)
1871+
1872+
after_each(function()
1873+
utils.cleanup_test(winid)
1874+
end)
1875+
1876+
it("should not add parentheses around the return type", function()
1877+
local lines = utils.get_win_lines(winid)
1878+
eq("func Foo() struct {}", lines[1])
1879+
end)
1880+
1881+
it("should not touch the cursor", function()
1882+
local char = utils.get_cursor_char(winid)
1883+
eq("}", char)
1884+
end)
1885+
end
1886+
)
18371887
end)
18381888

18391889
describe("test methods with body defined", function()

lua/test/interface_test_spec.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,4 +1152,38 @@ describe("test interface method declarations", function()
11521152
end)
11531153
end
11541154
)
1155+
1156+
describe(
1157+
"when a struct return with space between struct keyword and braces is started",
1158+
function()
1159+
local winid = 0
1160+
before_each(function()
1161+
winid = utils.set_test_window_value({
1162+
"type Foo interface {",
1163+
" Bar() struct {}|",
1164+
"}",
1165+
})
1166+
vim.cmd("AutoFixReturn")
1167+
end)
1168+
1169+
after_each(function()
1170+
utils.cleanup_test(winid)
1171+
end)
1172+
1173+
it("should not add parentheses around the return type", function()
1174+
local lines = utils.get_win_lines(winid)
1175+
local expected = {
1176+
"type Foo interface {",
1177+
" Bar() struct {}",
1178+
"}",
1179+
}
1180+
eq(expected, lines)
1181+
end)
1182+
1183+
it("should not touch the cursor", function()
1184+
local char = utils.get_cursor_char(winid)
1185+
eq("}", char)
1186+
end)
1187+
end
1188+
)
11551189
end)

0 commit comments

Comments
 (0)