Skip to content
This repository was archived by the owner on Mar 15, 2024. It is now read-only.

Commit cba95bf

Browse files
committed
fix: make it work.
1 parent 2fa3ddf commit cba95bf

File tree

5 files changed

+35
-25
lines changed

5 files changed

+35
-25
lines changed

lua/commit-msg-sg/init.lua

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ local utils = require('commit-msg-sg.utils')
55
local writters = {}
66

77
local prompt_str = [[
8-
You are a git expert and experienced programmer that are here to help me write a git commit message in a git repo.\n
9-
1. Generate concise and accurate git commit message based on the git diff that provided later with focus on the changed lines that start with "+","-".\n
10-
2. Do not add additional information about the response, like how the generated content is better and follow the rules/standards.\n
11-
4. The generated git commit message follow the Conventional commits standard.\n
12-
6. Avoid duplicating the diff content.\n
13-
The diff output:\n
14-
```diff\n%s```
8+
You are a git expert and experienced programmer that are here to help me write a git commit message in a git repo.
9+
1. Generate concise and accurate git commit message based on the git diff that provided later with focus on the changed lines that start with "+","-".
10+
2. Do not add additional information about the response, like how the generated content is better and follow the rules/standards.
11+
4. The generated git commit message follow the Conventional commits standard.
12+
6. Avoid duplicating the diff content.
13+
The diff output:
14+
```diff%s```
1515
]]
1616

1717
local function on_attach(client, bufnr, opts)
@@ -26,13 +26,13 @@ function M.setup(opts)
2626
opts = opts or {}
2727
local on_attach_ = opts.on_attach
2828
opts.on_attach = function(client, bufnr)
29-
on_attach(client, bufnr, opts)
29+
on_attach(client, bufnr, config.options)
3030
if on_attach_ then
3131
on_attach_(client, bufnr)
3232
end
3333
end
3434
config.setup(opts)
35-
if config.options.default_prompt and config.options.default_prompt ~= '' then
35+
if config.options.default_prompt and config.options.default_prompt and config.options.default_prompt ~= '' then
3636
prompt_str = config.options.default_prompt
3737
end
3838
if config.options.auto_setup_gitcommit then
@@ -81,32 +81,36 @@ end
8181

8282
function M.write()
8383
local bufnr = vim.api.nvim_get_current_buf()
84+
local Writter = require('commit-msg-sg.simple_writter')
8485
local writter = writters[bufnr]
85-
if not writter or writter:invalid() then
86-
local Writter = require('commit-msg-sg.simple_writter')
87-
writters[bufnr] = Writter.init(bufnr)
88-
writter = writters[bufnr]
86+
if writter then
87+
writter:reset()
88+
end
89+
writter = Writter.init(bufnr)
90+
writters[bufnr] = writter
91+
92+
--- NOTE: maybe move to hook.
93+
if config.options.ghost_text then
94+
utils.update_ghost_text(bufnr, nil)
95+
utils.update_ghost_text(bufnr, config.options.ghost_text)
8996
end
9097

9198
local executor = require('commit-msg-sg.executor')
9299
gen_snippet(config.options, function(err, snippet)
100+
print(snippet)
93101
if err then
94102
vim.notify(err, vim.log.levels.ERROR)
95103
return
96104
end
97105
writter:reset()
98-
--- NOTE: maybe move to hook.
99-
if config.options.ghost_text then
100-
utils.update_ghost_text(config.options.ghost_text)
101-
end
102106
executor.execute(bufnr, snippet, function(err_, text)
103107
if writter:invalid() then return end
104108
if err_ then
105109
vim.notify(err_, vim.log.levels.ERROR)
106110
return
107111
end
108-
if text and config.options.ghost_text then
109-
utils.update_ghost_text(nil)
112+
if text and text ~= "" and config.options.ghost_text then
113+
utils.update_ghost_text(bufnr, nil)
110114
end
111115
writter:update(text)
112116
end)

lua/commit-msg-sg/simple_writter.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ end
3636
function SimpleWritter:update(text)
3737
if self:invalid() then return end
3838

39+
self.text = text or ''
40+
3941
local lines = vim.split(text, '\n')
4042
-- iterate the lines, if vim.trim(line) is ```, ignore it
4143
local new_lines = {}
@@ -60,3 +62,5 @@ function SimpleWritter:reset()
6062
end
6163
vim.api.nvim_buf_set_lines(self.bufnr, marker:start_pos().row, end_row, false, {})
6264
end
65+
66+
return SimpleWritter
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local utils = require('lua.commit-msg-sg.utils')
1+
local utils = require('commit-msg-sg.utils')
22

33
local M = {}
44

@@ -23,6 +23,7 @@ end
2323

2424
function M.setup(bufnr, config)
2525
utils.throws_if_deps_is_missing()
26+
if clients[bufnr] then return end
2627

2728
require('sg.cody.rpc').start({
2829
force = false,
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,15 @@ function M.update_ghost_text(bufnr, text)
6363
bufnr = vim.api.nvim_get_current_buf()
6464
end
6565
if not bufnr then
66-
if ghost_text_ns ~= nil then
67-
text = nil
68-
else
69-
error("bufnr is required", vim.log.levels.ERROR)
70-
end
66+
error("bufnr is required", vim.log.levels.ERROR)
67+
return
7168
end
7269
if text == nil and ghost_text_ns ~= nil then
7370
vim.api.nvim_buf_clear_namespace(bufnr, ghost_text_ns, 0, -1);
7471
ghost_text_ns = nil
7572
return
73+
elseif text == nil then
74+
return
7675
end
7776

7877
if not ghost_text_ns then
@@ -85,3 +84,5 @@ function M.update_ghost_text(bufnr, text)
8584
virt_text_hide = false,
8685
})
8786
end
87+
88+
return M

0 commit comments

Comments
 (0)