Skip to content
This repository was archived by the owner on Jan 14, 2026. It is now read-only.

Commit 40af794

Browse files
committed
style(gitcommit): improve code comments and docstrings
- update comments and docstrings for clarity and consistency across buffer, generator, git, init, and ui modules - rephrase and simplify inline comments to enhance readability - standardize parameter descriptions and function documentation for better maintainability
1 parent 0fcf286 commit 40af794

5 files changed

Lines changed: 86 additions & 86 deletions

File tree

lua/codecompanion/_extensions/gitcommit/buffer.lua

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,35 @@ local default_config = {
1717
local config = {}
1818

1919
---Setup buffer keymaps for gitcommit filetype
20-
---@param opts? CodeCompanion.GitCommit.ExtensionOpts.Buffer Configuration options
20+
---@param opts? CodeCompanion.GitCommit.ExtensionOpts.Buffer Config options
2121
function Buffer.setup(opts)
2222
config = vim.tbl_deep_extend("force", default_config, opts or {})
2323

2424
if not config.enabled then
2525
return
2626
end
2727

28-
-- Create autocommand for gitcommit filetype
28+
-- Create autocommand for gitcommit
2929
vim.api.nvim_create_autocmd("FileType", {
3030
pattern = "gitcommit",
3131
callback = function(event)
3232
Buffer._setup_gitcommit_keymap(event.buf)
3333

3434
if config.auto_generate then
35-
-- Auto-generation autocommand triggers once when entering gitcommit window
36-
-- This avoids race conditions with plugins like neogit that manage multiple windows
37-
-- Skip auto-generation during git amend to preserve user's intent to modify existing commit
35+
-- Auto-generation triggers once when entering gitcommit window
36+
-- Avoids race conditions with plugins like neogit
37+
-- Skip auto-generation during git amend to preserve user intent
3838
vim.api.nvim_create_autocmd("WinEnter", {
3939
buffer = event.buf,
4040
once = true,
4141
callback = function(args)
42-
-- Defer execution to ensure other plugins (like neogit) have finished UI setup
42+
-- Defer execution to ensure other plugins finish UI setup
4343
vim.defer_fn(function()
4444
if not vim.api.nvim_buf_is_valid(args.buf) then
4545
return
4646
end
4747

48-
-- Check if buffer already has a commit message
48+
-- Check if buffer already has commit message
4949
local lines = vim.api.nvim_buf_get_lines(args.buf, 0, -1, false)
5050
local has_message = false
5151
for _, line in ipairs(lines) do
@@ -55,9 +55,9 @@ function Buffer.setup(opts)
5555
end
5656
end
5757

58-
-- Skip auto-generation in the following cases:
59-
-- 1. Buffer already has a commit message
60-
-- 2. We are in a git amend operation (user may want to keep existing message)
58+
-- Skip auto-generation if:
59+
-- 1. Buffer already has commit message
60+
-- 2. In git amend operation (user may want to keep existing message)
6161
local should_skip_amend = config.skip_auto_generate_on_amend and Git.is_amending()
6262
if not has_message and not should_skip_amend then
6363
Buffer._generate_and_insert_commit_message(args.buf)
@@ -72,10 +72,10 @@ function Buffer.setup(opts)
7272
})
7373
end
7474

75-
---Setup keymap for specific gitcommit buffer
75+
---Setup keymap for gitcommit buffer
7676
---@param bufnr number Buffer number
7777
function Buffer._setup_gitcommit_keymap(bufnr)
78-
-- Only set keymap if buffer is modifiable and in gitcommit filetype
78+
-- Only set keymap if buffer is valid
7979
if not vim.api.nvim_buf_is_valid(bufnr) then
8080
return
8181
end
@@ -93,13 +93,13 @@ end
9393
---Generate commit message and insert into gitcommit buffer
9494
---@param bufnr number Buffer number
9595
function Buffer._generate_and_insert_commit_message(bufnr)
96-
-- Check if we're in a git repository
96+
-- Check git repository
9797
if not Git.is_repository() then
9898
vim.notify("Not in a git repository", vim.log.levels.ERROR)
9999
return
100100
end
101101

102-
-- Get relevant changes (staged or amend)
102+
-- Get changes for commit
103103
local diff, context = Git.get_contextual_diff()
104104
if not diff then
105105
local msg
@@ -115,14 +115,14 @@ function Buffer._generate_and_insert_commit_message(bufnr)
115115
Langs.select_lang(function(lang)
116116
vim.notify("Generating commit message...", vim.log.levels.INFO)
117117

118-
-- Get commit history if enabled
118+
-- Get commit history for context
119119
local commit_history = nil
120120
local git_config = Git.get_config and Git.get_config() or {}
121121
if git_config.use_commit_history then
122122
commit_history = Git.get_commit_history(git_config.commit_history_count)
123123
end
124124

125-
-- Generate commit message using LLM
125+
-- Generate commit message
126126
Generator.generate_commit_message(diff, lang, commit_history, function(result, error)
127127
if error then
128128
vim.notify("Failed to generate commit message: " .. error, vim.log.levels.ERROR)
@@ -150,15 +150,15 @@ function Buffer._insert_commit_message(bufnr, message)
150150
-- Get current buffer content
151151
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
152152

153-
-- Find the first line that doesn't start with # (comment)
154-
-- This is where we'll insert the commit message
153+
-- Find first line that doesn't start with # (comment)
154+
-- This is where we'll insert commit message
155155
local insert_line = 0
156156
for i, line in ipairs(lines) do
157157
if not line:match("^%s*#") and vim.trim(line) == "" then
158158
insert_line = i - 1
159159
break
160160
elseif not line:match("^%s*#") and vim.trim(line) ~= "" then
161-
-- Found non-comment, non-empty line, insert before it
161+
-- Found non-comment, non-empty line, insert before
162162
insert_line = i - 1
163163
break
164164
end
@@ -167,7 +167,7 @@ function Buffer._insert_commit_message(bufnr, message)
167167
-- Split message into lines
168168
local message_lines = vim.split(message, "\n")
169169

170-
-- Remove existing commit message if present (before first comment line)
170+
-- Remove existing commit message if present
171171
local first_comment_line = nil
172172
for i, line in ipairs(lines) do
173173
if line:match("^%s*#") then
@@ -177,29 +177,29 @@ function Buffer._insert_commit_message(bufnr, message)
177177
end
178178

179179
if first_comment_line then
180-
-- Remove non-comment lines before the first comment
180+
-- Remove non-comment lines before first comment
181181
local non_comment_lines = {}
182182
for i = 1, first_comment_line do
183183
if not lines[i]:match("^%s*#") and vim.trim(lines[i]) ~= "" then
184-
-- This is a non-comment line, it might be an existing commit message
184+
-- This is non-comment line, might be existing commit message
185185
else
186186
table.insert(non_comment_lines, lines[i])
187187
end
188188
end
189189

190-
-- Clear the buffer and insert new content
190+
-- Clear buffer and insert new content
191191
vim.api.nvim_buf_set_lines(bufnr, 0, first_comment_line, false, {})
192192
end
193193

194-
-- Insert the new commit message at the beginning
194+
-- Insert new commit message at beginning
195195
vim.api.nvim_buf_set_lines(bufnr, 0, 0, false, message_lines)
196196

197-
-- Add an empty line after the commit message if it doesn't end with one
197+
-- Add empty line after commit message if needed
198198
if #message_lines > 0 and message_lines[#message_lines] ~= "" then
199199
vim.api.nvim_buf_set_lines(bufnr, #message_lines, #message_lines, false, { "" })
200200
end
201201

202-
-- Move cursor to the beginning of the commit message
202+
-- Move cursor to beginning of commit message
203203
vim.api.nvim_win_set_cursor(0, { 1, 0 })
204204

205205
vim.notify("Commit message generated and inserted!", vim.log.levels.INFO)

lua/codecompanion/_extensions/gitcommit/generator.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,17 @@ end
6464

6565
---Create prompt for commit message generation
6666
---@param diff string The git diff to include in prompt
67-
---@param commit_history? string[] Array of recent commit messages for context (optional)
67+
---@param commit_history? string[] Recent commit messages for context (optional)
6868
function Generator._create_prompt(diff, lang, commit_history)
69-
-- Build the history context section
69+
-- Build history context section
7070
local history_context = ""
7171
if commit_history and #commit_history > 0 then
7272
history_context = "\nRECENT COMMIT HISTORY (for style reference):\n"
7373
for i, commit_msg in ipairs(commit_history) do
7474
history_context = history_context .. string.format("%d. %s\n", i, commit_msg)
7575
end
7676
history_context = history_context
77-
.. "\nAnalyze the above commit history to understand the project's commit style, tone, and format patterns. Use this as guidance to maintain consistency.\n"
77+
.. "\nAnalyze commit history to understand project style, tone, and format patterns. Use this for consistency.\n"
7878
end
7979

8080
return string.format(
@@ -127,7 +127,7 @@ Return ONLY the commit message in the exact format shown above.]],
127127
end
128128

129129
---Handle LLM response
130-
---@param err table|nil Error from request
130+
---@param err table|nil Request error
131131
---@param data table|nil Response data
132132
---@param adapter table The adapter used
133133
---@param callback fun(result: string|nil, error: string|nil) Callback function
@@ -138,12 +138,12 @@ function Generator._handle_response(err, data, adapter, callback)
138138
return callback(nil, error_msg)
139139
end
140140

141-
-- Check for empty or invalid data
141+
-- Check for empty/invalid data
142142
if not data then
143143
return callback(nil, "No response received from LLM")
144144
end
145145

146-
-- Process successful response
146+
-- Process response
147147
if data then
148148
local result = adapter.handlers.chat_output(adapter, data)
149149
if result and result.status then

0 commit comments

Comments
 (0)