Skip to content

Commit

Permalink
(nvim) Add custom script that modifies commit message using OpenAI API
Browse files Browse the repository at this point in the history
  • Loading branch information
malkoG committed Sep 25, 2023
1 parent 5ff8ead commit 07c0e00
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
18 changes: 18 additions & 0 deletions private_dot_config/nvim/after/plugin/neoai-extended.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ local inject_commit_message = function()
)
end

local textify_commit_message = function()
vim.ui.select(
{ "korean", "english" },
{
prompt = "Select language: ",
},
function(language)
if language ~= nil then
local prompt = require('utilities.prompt-engineering').generate_modified_commit_message(language)
local start_line = vim.fn.line("'<")
local end_line = vim.fn.line("'>")
require("neoai").context_inject(prompt, nil, start_line, end_line)
end
end
)
end

local inject_docstring = function()
local docstring_formats = {
["python"] = "reST" ,
Expand Down Expand Up @@ -65,6 +82,7 @@ local inject_docstring = function()
end

vim.api.nvim_create_user_command("InjectCommitMessage", inject_commit_message, {})
vim.api.nvim_create_user_command("TextifyCommitMessage", textify_commit_message, { range = true })
vim.api.nvim_create_user_command("InjectDocstring", inject_docstring, { range = true })


37 changes: 37 additions & 0 deletions private_dot_config/nvim/lua/utilities/prompt-engineering.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,43 @@ M.generate_commit_message = function(language)
return prompt
end

M.generate_modified_commit_message = function(language)
local buffer = vim.api.nvim_get_current_buf()
local lines = vim.api.nvim_buf_get_lines(buffer, 0, -1, false)

local start_line = vim.fn.line("'<")
local end_line = vim.fn.line("'>")

local lines_to_modify = {}
for i = start_line, end_line do
table.insert(lines_to_modify, lines[i])
end

local prompt = [[
Here is an incomplete commit message that you need to modify:
```
]] .. table.concat(lines_to_modify, "\n") .. [[
```
Using the following git diff generate a consise and
clear and appropriate git commit message, with a short title summary
that is 75 characters or less
]] .. " and you should generate commit message in " .. language .. [[:
]] .. [[
```
]] .. vim.fn.system("git diff --cached")
.. [[
```
* You SHOULD give me commit message immediately.
* You DON'T NEED TO explain unnecessary things.
]]

return prompt
end

M.generate_docstring = function(language, docstring_format)
local buffer = vim.api.nvim_get_current_buf()
local filetype = vim.api.nvim_buf_get_option(buffer, "filetype")
Expand Down

0 comments on commit 07c0e00

Please sign in to comment.