Skip to content

Commit

Permalink
feat: add filepath to action variables (#424)
Browse files Browse the repository at this point in the history
* feature: add `filepath`

* fix: use relative path
  • Loading branch information
jack-michaud authored Jun 22, 2024
1 parent 4cf2bf6 commit b24eefe
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ An example of custom action may look like this: (`#` marks comments)
"action_name": {
"type": "chat", # or "completion" or "edit"
"opts": {
"template": "A template using possible variable: {{filetype}} (neovim filetype), {{input}} (the selected text) an {{argument}} (provided on the command line)",
"template": "A template using possible variable: {{filetype}} (neovim filetype), {{input}} (the selected text) an {{argument}} (provided on the command line), {{filepath}} (the relative path to the file)",
"strategy": "replace", # or "display" or "append" or "edit"
"params": { # parameters according to the official OpenAI API
"model": "gpt-3.5-turbo", # or any other model supported by `"type"` in the OpenAI API, use the playground for reference
Expand Down
14 changes: 14 additions & 0 deletions lua/chatgpt/flows/actions/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ function BaseAction:get_filetype()
return vim.api.nvim_buf_get_option(bufnr, "filetype")
end

function BaseAction:get_filepath()
local bufnr = self:get_bufnr()
local full_path = vim.api.nvim_buf_get_name(bufnr)
-- Get relative path
local cwd = vim.fn.getcwd()
local rel_path = vim.fn.fnamemodify(full_path, ":~:.")

if string.find(rel_path, cwd, 1, true) == 1 then
return string.sub(rel_path, #cwd + 2)
end

return rel_path
end

function BaseAction:get_visual_selection()
-- return lines and selection, but caches them, so they always are the ones used
-- when the action was started, even if the user has changed buffer/selection
Expand Down
1 change: 1 addition & 0 deletions lua/chatgpt/flows/actions/chat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function ChatAction:render_template()
or self:get_selected_text()
local data = {
filetype = self:get_filetype(),
filepath = self:get_filepath(),
input = input,
}
data = vim.tbl_extend("force", {}, data, self.variables)
Expand Down

0 comments on commit b24eefe

Please sign in to comment.