Skip to content

Commit 7b21e21

Browse files
committed
refactor(gitcommit): improve readability of async handlers
- expand multi-statement anonymous functions for clarity - format error handling and data appending in uv.read_start callbacks - split long return statements for better maintainability - enhance error message formatting in git merge conflict handling - reformat long table returns for staged changes check
1 parent 658a62c commit 7b21e21

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

lua/codecompanion/_extensions/gitcommit/generator.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function Generator._handle_response(err, data, _adapter, callback)
117117
local error_msg = "Error generating commit message: " .. (err.stderr or err.message or "Unknown error")
118118
return callback(nil, error_msg)
119119
end
120-
120+
121121
-- Check for empty or invalid data
122122
if not data then
123123
return callback(nil, "No response received from LLM")

lua/codecompanion/_extensions/gitcommit/init.lua

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,21 @@ local function setup_slash_commands(opts)
169169
end)
170170

171171
vim.uv.read_start(stdout, function(err, data)
172-
if err then return end
173-
if data then output = output .. data end
172+
if err then
173+
return
174+
end
175+
if data then
176+
output = output .. data
177+
end
174178
end)
175179

176180
vim.uv.read_start(stderr, function(err, data)
177-
if err then return end
178-
if data then error_output = error_output .. data end
181+
if err then
182+
return
183+
end
184+
if data then
185+
error_output = error_output .. data
186+
end
179187
end)
180188
end
181189

@@ -219,19 +227,31 @@ local function setup_slash_commands(opts)
219227
end
220228
select_commit(chat, items)
221229
else
222-
chat:add_reference({ role = "user", content = "Error: Failed to get git log\n" .. error_output }, "git", "<git_error>")
230+
chat:add_reference(
231+
{ role = "user", content = "Error: Failed to get git log\n" .. error_output },
232+
"git",
233+
"<git_error>"
234+
)
223235
end
224236
end)
225237
end)
226238

227239
vim.uv.read_start(stdout, function(err, data)
228-
if err then return end
229-
if data then output = output .. data end
240+
if err then
241+
return
242+
end
243+
if data then
244+
output = output .. data
245+
end
230246
end)
231247

232248
vim.uv.read_start(stderr, function(err, data)
233-
if err then return end
234-
if data then error_output = error_output .. data end
249+
if err then
250+
return
251+
end
252+
if data then
253+
error_output = error_output .. data
254+
end
235255
end)
236256
end
237257

lua/codecompanion/_extensions/gitcommit/tools/git.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,8 @@ function GitTool.merge(branch)
540540
return true, output
541541
else
542542
if output:match("CONFLICT") then
543-
return false, "Merge conflict detected. Please resolve the conflicts manually. You can use 'git merge --abort' to cancel."
543+
return false,
544+
"Merge conflict detected. Please resolve the conflicts manually. You can use 'git merge --abort' to cancel."
544545
else
545546
return false, output
546547
end

lua/codecompanion/_extensions/gitcommit/tools/git_edit.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,12 @@ Available write-access Git operations:
211211
-- Check if there are staged changes
212212
local diff_success, diff_output = GitTool.get_diff(true) -- staged changes
213213
if not diff_success or not diff_output or vim.trim(diff_output) == "" then
214-
return { status = "error", data = "No staged changes found for commit. Please stage your changes first using the stage operation." }
214+
return {
215+
status = "error",
216+
data = "No staged changes found for commit. Please stage your changes first using the stage operation.",
217+
}
215218
end
216-
219+
217220
-- Return success with instruction for AI to use the diff tool
218221
return {
219222
status = "success",

0 commit comments

Comments
 (0)