Skip to content

Commit aadaa48

Browse files
committed
feat: add support for start and end paren backspace in interface methods
1 parent d933021 commit aadaa48

File tree

4 files changed

+193
-2
lines changed

4 files changed

+193
-2
lines changed

lua/auto-fix-return/fix.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ function M.parse_return()
208208
return
209209
end
210210

211-
212211
-- Here we rebuild the entire return statement to a syntactically correct version
213212
-- splitting on commas to decide if there is a parameter list or a single value
214213
local fixed_def = M.build_fixed_definition(line, cursor_col)

lua/auto-fix-return/parsers/declaration.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ function M.parse_declaration(cursor_row)
103103
;; with the final error token for the , outside of the method_declaration
104104
(ERROR)? @outside_error_end
105105
)
106+
106107
;; For the following code
107108
;; func (b *Bar) Foo() (string, int| {}
108109
;; There is no 'result' node parsed, the error token contains the entire thing

lua/auto-fix-return/parsers/interface.lua

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,46 @@ function M.parse_interface(cursor_row)
5050
.
5151
(ERROR)? @outside_error_end
5252
)
53+
54+
;; Special parsing cases for the following code
55+
;;
56+
;; type baz interface {
57+
;; Baz() i|,k)
58+
;; }
59+
;; ||
60+
;; type baz interface {
61+
;; Baz() (i,k
62+
;; }
63+
;; This is parsed as either the entire type return in the outside error or
64+
;; the initial type identifier found in result
65+
(
66+
(method_elem
67+
name: (_)
68+
parameters: (_)
69+
result: (_)? @result
70+
) @func
71+
.
72+
(ERROR) @outside_error_end
73+
)
74+
75+
;; This is a special case for a multi method interface type with a missing end parentheses
76+
;; type Foo interface {
77+
;; Bax() int
78+
;; Baz() (i,k,l|
79+
;; Bar() string
80+
;; }
81+
(
82+
(method_elem
83+
name: (_)
84+
parameters: (_)
85+
result: (parameter_list
86+
(parameter_declaration
87+
name: (identifier) @name
88+
)
89+
(ERROR)?
90+
) @result_start
91+
) @func
92+
)
5393
]
5494
]]
5595
)
@@ -86,6 +126,10 @@ function M.parse_interface(cursor_row)
86126
if final_end_col == 0 then
87127
final_end_col = end_col
88128
end
129+
elseif capture_name == "name" then
130+
if end_col > final_end_col then
131+
final_end_col = end_col
132+
end
89133
elseif capture_name == "error_end" then
90134
final_end_col = end_col
91135
elseif capture_name == "outside_error_end" then
@@ -98,11 +142,14 @@ function M.parse_interface(cursor_row)
98142

99143
::continue::
100144

101-
-- The outside_error_above token stretches across multiple rows so
145+
-- These capture edge cases stretch token stretches across multiple rows so
102146
-- we need to check it every time even if the continue check above fires
103147
if capture_name == "outside_error_above" then
104148
final_end_col = end_col
105149
end
150+
if capture_name == "result_start" then
151+
final_start_col = start_col
152+
end
106153
end
107154

108155
return {

lua/test/interface_test_spec.lua

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,4 +813,148 @@ describe("test interface method declarations", function()
813813
end)
814814
end
815815
)
816+
817+
describe(
818+
"when a single function interface has multi return with only one starting parentheses",
819+
function()
820+
local winid = 0
821+
before_each(function()
822+
winid = utils.set_test_window_value({
823+
"type Foo interface {",
824+
" Bar() (i,k|",
825+
"}",
826+
})
827+
vim.cmd("AutoFixReturn")
828+
end)
829+
830+
after_each(function()
831+
utils.cleanup_test(winid)
832+
end)
833+
834+
it("should add parentheses around the return type", function()
835+
local lines = utils.get_win_lines(winid)
836+
local expected = {
837+
"type Foo interface {",
838+
" Bar() (i,k)",
839+
"}",
840+
}
841+
eq(expected, lines)
842+
end)
843+
844+
it("should set the cursor to inside the parens", function()
845+
local char = utils.get_cursor_char(winid)
846+
eq("k", char)
847+
end)
848+
end
849+
)
850+
851+
describe(
852+
"when a single function interface has multi return with only one ending parentheses",
853+
function()
854+
local winid = 0
855+
before_each(function()
856+
winid = utils.set_test_window_value({
857+
"type Foo interface {",
858+
" Bar() i|,k)",
859+
"}",
860+
})
861+
vim.cmd("AutoFixReturn")
862+
end)
863+
864+
after_each(function()
865+
utils.cleanup_test(winid)
866+
end)
867+
868+
it("should add parentheses around the return type", function()
869+
local lines = utils.get_win_lines(winid)
870+
local expected = {
871+
"type Foo interface {",
872+
" Bar() (i,k)",
873+
"}",
874+
}
875+
eq(expected, lines)
876+
end)
877+
878+
it("should set the cursor to inside the parens", function()
879+
local char = utils.get_cursor_char(winid)
880+
eq("(", char)
881+
end)
882+
end
883+
)
884+
885+
describe(
886+
"when a multiple function interface has multi return with only one starting parentheses",
887+
function()
888+
local winid = 0
889+
before_each(function()
890+
winid = utils.set_test_window_value({
891+
"type Foo interface {",
892+
" Bax() int",
893+
" Bar() (i,k|",
894+
" Baz() string",
895+
"}",
896+
})
897+
vim.cmd("AutoFixReturn")
898+
end)
899+
900+
after_each(function()
901+
utils.cleanup_test(winid)
902+
end)
903+
904+
it("should add parentheses around the return type", function()
905+
local lines = utils.get_win_lines(winid)
906+
local expected = {
907+
"type Foo interface {",
908+
" Bax() int",
909+
" Bar() (i,k)",
910+
" Baz() string",
911+
"}",
912+
}
913+
eq(expected, lines)
914+
end)
915+
916+
it("should set the cursor to inside the parens", function()
917+
local char = utils.get_cursor_char(winid)
918+
eq("k", char)
919+
end)
920+
end
921+
)
922+
923+
describe(
924+
"when a multiple function interface has multi return with only one ending parentheses",
925+
function()
926+
local winid = 0
927+
before_each(function()
928+
winid = utils.set_test_window_value({
929+
"type Foo interface {",
930+
" Bax() int",
931+
" Bar() i|,k)",
932+
" Baz() string",
933+
"}",
934+
})
935+
vim.cmd("AutoFixReturn")
936+
end)
937+
938+
after_each(function()
939+
utils.cleanup_test(winid)
940+
end)
941+
942+
it("should add parentheses around the return type", function()
943+
local lines = utils.get_win_lines(winid)
944+
local expected = {
945+
"type Foo interface {",
946+
" Bax() int",
947+
" Bar() (i,k)",
948+
" Baz() string",
949+
"}",
950+
}
951+
eq(expected, lines)
952+
end)
953+
954+
it("should set the cursor to inside the parens", function()
955+
local char = utils.get_cursor_char(winid)
956+
eq("(", char)
957+
end)
958+
end
959+
)
816960
end)

0 commit comments

Comments
 (0)