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

Commit

Permalink
fix: make it work.
Browse files Browse the repository at this point in the history
  • Loading branch information
towry committed Dec 25, 2023
1 parent 2fa3ddf commit cba95bf
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
42 changes: 23 additions & 19 deletions lua/commit-msg-sg/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ local utils = require('commit-msg-sg.utils')
local writters = {}

local prompt_str = [[
You are a git expert and experienced programmer that are here to help me write a git commit message in a git repo.\n
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
2. Do not add additional information about the response, like how the generated content is better and follow the rules/standards.\n
4. The generated git commit message follow the Conventional commits standard.\n
6. Avoid duplicating the diff content.\n
The diff output:\n
```diff\n%s```
You are a git expert and experienced programmer that are here to help me write a git commit message in a git repo.
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 "+","-".
2. Do not add additional information about the response, like how the generated content is better and follow the rules/standards.
4. The generated git commit message follow the Conventional commits standard.
6. Avoid duplicating the diff content.
The diff output:
```diff%s```
]]

local function on_attach(client, bufnr, opts)
Expand All @@ -26,13 +26,13 @@ function M.setup(opts)
opts = opts or {}
local on_attach_ = opts.on_attach
opts.on_attach = function(client, bufnr)
on_attach(client, bufnr, opts)
on_attach(client, bufnr, config.options)
if on_attach_ then
on_attach_(client, bufnr)
end
end
config.setup(opts)
if config.options.default_prompt and config.options.default_prompt ~= '' then
if config.options.default_prompt and config.options.default_prompt and config.options.default_prompt ~= '' then
prompt_str = config.options.default_prompt
end
if config.options.auto_setup_gitcommit then
Expand Down Expand Up @@ -81,32 +81,36 @@ end

function M.write()
local bufnr = vim.api.nvim_get_current_buf()
local Writter = require('commit-msg-sg.simple_writter')
local writter = writters[bufnr]
if not writter or writter:invalid() then
local Writter = require('commit-msg-sg.simple_writter')
writters[bufnr] = Writter.init(bufnr)
writter = writters[bufnr]
if writter then
writter:reset()
end
writter = Writter.init(bufnr)
writters[bufnr] = writter

--- NOTE: maybe move to hook.
if config.options.ghost_text then
utils.update_ghost_text(bufnr, nil)
utils.update_ghost_text(bufnr, config.options.ghost_text)
end

local executor = require('commit-msg-sg.executor')
gen_snippet(config.options, function(err, snippet)
print(snippet)
if err then
vim.notify(err, vim.log.levels.ERROR)
return
end
writter:reset()
--- NOTE: maybe move to hook.
if config.options.ghost_text then
utils.update_ghost_text(config.options.ghost_text)
end
executor.execute(bufnr, snippet, function(err_, text)
if writter:invalid() then return end
if err_ then
vim.notify(err_, vim.log.levels.ERROR)
return
end
if text and config.options.ghost_text then
utils.update_ghost_text(nil)
if text and text ~= "" and config.options.ghost_text then
utils.update_ghost_text(bufnr, nil)
end
writter:update(text)
end)
Expand Down
4 changes: 4 additions & 0 deletions lua/commit-msg-sg/simple_writter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ end
function SimpleWritter:update(text)
if self:invalid() then return end

self.text = text or ''

local lines = vim.split(text, '\n')
-- iterate the lines, if vim.trim(line) is ```, ignore it
local new_lines = {}
Expand All @@ -60,3 +62,5 @@ function SimpleWritter:reset()
end
vim.api.nvim_buf_set_lines(self.bufnr, marker:start_pos().row, end_row, false, {})
end

return SimpleWritter
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local utils = require('lua.commit-msg-sg.utils')
local utils = require('commit-msg-sg.utils')

local M = {}

Expand All @@ -23,6 +23,7 @@ end

function M.setup(bufnr, config)
utils.throws_if_deps_is_missing()
if clients[bufnr] then return end

require('sg.cody.rpc').start({
force = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,15 @@ function M.update_ghost_text(bufnr, text)
bufnr = vim.api.nvim_get_current_buf()
end
if not bufnr then
if ghost_text_ns ~= nil then
text = nil
else
error("bufnr is required", vim.log.levels.ERROR)
end
error("bufnr is required", vim.log.levels.ERROR)
return
end
if text == nil and ghost_text_ns ~= nil then
vim.api.nvim_buf_clear_namespace(bufnr, ghost_text_ns, 0, -1);
ghost_text_ns = nil
return
elseif text == nil then
return
end

if not ghost_text_ns then
Expand All @@ -85,3 +84,5 @@ function M.update_ghost_text(bufnr, text)
virt_text_hide = false,
})
end

return M

0 comments on commit cba95bf

Please sign in to comment.