Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filepath to action variables #424

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,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