Skip to content

Commit

Permalink
feat: #20 inline editing is now smarter
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Mar 12, 2024
1 parent 5b85b4f commit 690bcae
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 98 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ You can use the plugin to create inline code directly into a Neovim buffer. This
> [!NOTE]
> The command can detect if you've made a visual selection and send any code as context to the API alongside the filetype of the buffer.
One of the challenges with inline editing is determining how the generative AI's response should be handled in the buffer. If you've prompted the API to "create a table of 5 fruits" then you may wish for the response to be placed after the cursor in the buffer. However, if you asked the API to "refactor this function" then you'd expect the response to overwrite a visual selection. If this placement isn't specified then the plugin will use generative AI itself to determine if the response should follow any of the placements below:

- _after_ - after the visual selection
- _before_ - before the visual selection
- _cursor_ - one column after the cursor position
- _new_ - in a new buffer
- _replace_ - replacing the visual selection

As a final example, specifying a prompt like _"create a test for this code in a new buffer"_ would result in a new Neovim buffer being created.

### In-Built Actions

The plugin comes with a number of [in-built actions](https://github.com/olimorris/codecompanion.nvim/blob/main/lua/codecompanion/actions.lua) which aim to improve your Neovim workflow. Actions make use of either a _chat_ or an _inline_ strategy. The chat strategy opens up a chat buffer whilst an inline strategy will write output from the generative AI service into the Neovim buffer.
Expand Down
19 changes: 10 additions & 9 deletions lua/codecompanion/adapters/anthropic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ local function get_system_prompt(tbl)
return i
end
end
return nil
end

---@class CodeCompanion.Adapter
Expand Down Expand Up @@ -124,16 +123,18 @@ return {
---@param context table Useful context about the buffer to inline to
---@return table|nil
inline_output = function(data, context)
data = data:sub(6)
local ok, json = pcall(vim.json.decode, data, { luanil = { object = true } })
if data and data ~= "" then
data = data:sub(6)
local ok, json = pcall(vim.json.decode, data, { luanil = { object = true } })

if not ok then
return
end
if not ok then
return
end

log:trace("INLINE JSON: %s", json)
if json.type == "content_block_delta" then
return json.delta.text
-- log:trace("INLINE JSON: %s", json)
if json.type == "content_block_delta" then
return json.delta.text
end
end
end,
},
Expand Down
12 changes: 7 additions & 5 deletions lua/codecompanion/adapters/ollama.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@ return {
---@param context table Useful context about the buffer to inline to
---@return table|nil
inline_output = function(data, context)
local ok, json = pcall(vim.json.decode, data, { luanil = { object = true } })
if data and data ~= "" then
local ok, json = pcall(vim.json.decode, data, { luanil = { object = true } })

if not ok then
return
end
if not ok then
return
end

return json.message.content
return json.message.content
end
end,
},
schema = {
Expand Down
19 changes: 12 additions & 7 deletions lua/codecompanion/adapters/openai.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,21 @@ return {
---Output the data from the API ready for inlining into the current buffer
---@param data table The streamed JSON data from the API, also formatted by the format_data callback
---@param context table Useful context about the buffer to inline to
---@return table|nil
---@return string|nil
inline_output = function(data, context)
data = data:sub(7)
local ok, json = pcall(vim.json.decode, data, { luanil = { object = true } })
if data and data ~= "" then
data = data:sub(7)
local ok, json = pcall(vim.json.decode, data, { luanil = { object = true } })

if not ok then
return
end
if not ok then
return
end

return json.choices[1].delta.content
local content = json.choices[1].delta.content
if content then
return content
end
end
end,
},
schema = {
Expand Down
6 changes: 4 additions & 2 deletions lua/codecompanion/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ function Client:stream(adapter, payload, bufnr, cb)
raw = adapter.raw or { "--no-buffer" },
headers = headers,
body = body,
stream = self.opts.schedule(function(_, data, _)
log:trace("Chat data: %s", data)
stream = self.opts.schedule(function(_, data)
if data then
log:trace("Chat data: %s", data)
end
-- log:trace("----- For Adapter test creation -----\nRequest: %s\n ---------- // END ----------", data)

if adapter.callbacks.is_complete(data) then
Expand Down
Loading

0 comments on commit 690bcae

Please sign in to comment.