Skip to content

Commit bf7b5c8

Browse files
committed
feat: add initial support for closure returns
1 parent 3234b33 commit bf7b5c8

File tree

2 files changed

+649
-7
lines changed

2 files changed

+649
-7
lines changed

lua/auto-fix-return/fix.lua

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,22 @@ function M.build_fixed_definition(line, cursor_col)
7777
--
7878
-- NOTE: `chan` syntax is unique in that a single return that is a channel type DOES NOT
7979
-- require parenthesis, this is for all forms of channel including `chan` `<-chan` and `chan<-`
80+
-- NOTE: `func()` syntax is also unique in that it can contain arbitrary amounts of spaces that
81+
-- should not be considered a named return
8082
-- TODO: This should be rewritten into a more robust parser
8183
for i = 1, #trimmed do
8284
local c = trimmed:sub(i, i)
8385

84-
if c == "{" or c == "[" then
86+
if c == "{" or c == "[" or c == "(" then
8587
table.insert(bracket_stack, #bracket_stack + 1, c)
8688
end
87-
if c == "}" or c == "]" then
89+
if c == "}" or c == "]" or c == ")" then
8890
table.remove(bracket_stack, #bracket_stack)
8991
end
9092

9193
-- This technically does not handle a case like `func foo() chan<- int a b c` but as this will never be syntactically valid go code we can ignore it
92-
if c == " " and not (curr_word:find("^chan") ~= nil or curr_word:find("chan$") ~= nil) then
94+
if c == " " and not (curr_word:find("^chan") ~= nil or curr_word:find("chan$") ~= nil or curr_word:find("^func")) then
95+
-- vim.print(curr_word)
9396
-- Peek ahead to find the next non-space character
9497
-- so that we can ignore whitespace in return definitions like `interface {}`
9598
-- we then do a stack approach to ensure we only add a return definition if we have validated
@@ -103,7 +106,7 @@ function M.build_fixed_definition(line, cursor_col)
103106
end
104107
end
105108

106-
if next_char == "{" or next_char == "[" then
109+
if next_char == "{" or next_char == "[" or next_char == "(" then
107110
table.insert(bracket_stack, #bracket_stack + 1, next_char)
108111
end
109112

0 commit comments

Comments
 (0)