Skip to content
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ to consider a visual selection within an API request.
-- Type of spinner animation to display while loading
-- Available options: "dots", "line", "star", "bouncing_bar", "bouncing_ball"
spinner_type = "star",
-- Show hints for context added through completion with @file, @buffer or @directory
show_context_hints = true
}
```

Expand Down Expand Up @@ -522,6 +524,29 @@ require("parrot").setup {
}
```

## Completion

Instead of using the [template placeholders](#template-placeholders),
`parrot.nvim` supports inline completion via [nvim-cmp](https://github.com/hrsh7th/nvim-cmp) for additional contexts:

- `@buffer:foo.txt` - Includes the content of the open buffer `foo.txt`
- `@file:test.lua` - Includes the content of the file `test.lua`
- `@directory:src/` - Includes all file contents from the directory `src/`

> Hint: The option `show_context_hints` allows you to transparently see notifications about the
actual file contents considered by the request. The completion keywords (e.g., `@file`) need to be placed
on a **new line**!

### Setup for nvim-cmp

To enable `parrot.nvim` completions, add the source to your nvim-cmp configuration:

```lua
sources = cmp.config.sources({
{ name = "parrot_completion" },
}),
```

## Statusline Support

Knowing the current chat or command model can be shown using your favorite statusline plugin.
Expand Down
13 changes: 12 additions & 1 deletion lua/parrot/chat_handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local Spinner = require("parrot.spinner")
local Job = require("plenary.job")
local pft = require("plenary.filetype")
local ResponseHandler = require("parrot.response_handler")
local completion = require("parrot.completion")

local ChatHandler = {}

Expand Down Expand Up @@ -43,6 +44,7 @@ function ChatHandler:new(options, providers, available_providers, available_mode
last_line1 = nil,
last_line2 = nil,
},
completion = completion,
}, self)
end

Expand Down Expand Up @@ -86,7 +88,6 @@ function ChatHandler:get_provider(is_chat)
end

--- Retrieves the current provider for chat or command.
---@param is_chat boolean True for chat provider, false for command provider.
---@return table | nil Provider table or nil if not found.
function ChatHandler:buf_handler()
local gid = utils.create_augroup("PrtBufHandler", { clear = true })
Expand Down Expand Up @@ -747,6 +748,11 @@ function ChatHandler:_chat_respond(params)
spinner:start("calling API...")
end

-- add completion context
for _, message in ipairs(messages) do
message.content = self.completion.context.insert_contexts(message.content)
end

-- call the model and write response
self:query(
buf,
Expand Down Expand Up @@ -1511,6 +1517,11 @@ function ChatHandler:prompt(params, target, model_obj, prompt, template, reset_h
spinner = Spinner:new(self.options.spinner_type)
spinner:start("calling API...")
end

-- add completion context
for _, message in ipairs(messages) do
message.content = self.completion.context.insert_contexts(message.content)
end
self:query(
buf,
prov,
Expand Down
Loading