diff --git a/README.md b/README.md index 533250a7..29f25aa1 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lua/chatgpt/flows/actions/base.lua b/lua/chatgpt/flows/actions/base.lua index 29b80bd9..d322191e 100644 --- a/lua/chatgpt/flows/actions/base.lua +++ b/lua/chatgpt/flows/actions/base.lua @@ -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 diff --git a/lua/chatgpt/flows/actions/chat/init.lua b/lua/chatgpt/flows/actions/chat/init.lua index b9c61b68..07a77bfa 100644 --- a/lua/chatgpt/flows/actions/chat/init.lua +++ b/lua/chatgpt/flows/actions/chat/init.lua @@ -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)