Skip to content

Commit

Permalink
feat: floating input (yetone#721)
Browse files Browse the repository at this point in the history
* feat: add floating input to ask method

Open a floating input similar to the "edit" input for the "ask" input.
Enabled in config via `Config.windows.ask.floating` or by passing
`{ floating = true }` to the `api.ask` method.

Includes logic to ensure the sidebar uses the correct buffer and selection
if an existing sidebar is open for another code buffer.

Also refactored the `selection` module to extract the floating input
logic into a new `PromptInput` class.

* docs: update config options

* feat: more accurate annotations to prevent user misunderstandings

---------

Co-authored-by: yetone <yetoneful@gmail.com>
  • Loading branch information
b0o and yetone authored Oct 15, 2024
1 parent b19573c commit 964715b
Show file tree
Hide file tree
Showing 7 changed files with 443 additions and 286 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,18 @@ _See [config.lua#L9](./lua/avante/config.lua) for the full config_
align = "center", -- left, center, right for title
rounded = true,
},
input = {
prefix = "> ",
},
edit = {
border = "rounded",
start_insert = true, -- Start insert mode when opening the edit window
},
ask = {
floating = false, -- Open the 'AvanteAsk' prompt in a floating window
start_insert = true, -- Start insert mode when opening the ask window, only effective if floating = true.
border = "rounded",
},
},
highlights = {
---@type AvanteConflictHighlights
Expand Down
42 changes: 38 additions & 4 deletions lua/avante/api.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local Config = require("avante.config")
local Utils = require("avante.utils")
local PromptInput = require("avante.prompt_input")

---@class avante.ApiToggle
---@operator call(): boolean
Expand Down Expand Up @@ -88,6 +89,7 @@ end
---@field question? string optional questions
---@field win? table<string, any> windows options similar to |nvim_open_win()|
---@field ask? boolean
---@field floating? boolean whether to open a floating input to enter the question

---@param opts? AskOptions
M.ask = function(opts)
Expand All @@ -97,10 +99,42 @@ M.ask = function(opts)
opts = { question = opts }
end

if not require("avante").toggle_sidebar(opts) then return false end
if opts.question == nil or opts.question == "" then return true end
vim.api.nvim_exec_autocmds("User", { pattern = "AvanteInputSubmitted", data = { request = opts.question } })
return true
local has_question = opts.question ~= nil and opts.question ~= ""

if Utils.is_sidebar_buffer(0) and not has_question then
require("avante").close_sidebar()
return false
end

opts = vim.tbl_extend("force", { selection = Utils.get_visual_selection_and_range() }, opts)

local function ask(input)
if input == nil or input == "" then input = opts.question end
local sidebar = require("avante").get()
if sidebar and sidebar:is_open() and sidebar.code.bufnr ~= vim.api.nvim_get_current_buf() then
sidebar:close({ goto_code_win = false })
end
require("avante").open_sidebar(opts)
if input == nil or input == "" then return true end
vim.api.nvim_exec_autocmds("User", { pattern = "AvanteInputSubmitted", data = { request = input } })
return true
end

if opts.floating == true or (Config.windows.ask.floating == true and not has_question and opts.floating == nil) then
local prompt_input = PromptInput:new({
submit_callback = function(input) ask(input) end,
close_on_submit = true,
win_opts = {
border = Config.options.windows.ask.border,
title = { { "ask", "FloatTitle" } },
},
start_insert = Config.options.windows.ask.start_insert,
})
prompt_input:open()
return true
end

return ask()
end

---@param question? string
Expand Down
6 changes: 6 additions & 0 deletions lua/avante/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ Respect and use existing conventions, libraries, etc that are already present in
},
edit = {
border = "rounded",
start_insert = true, -- Start insert mode when opening the edit window
},
ask = {
floating = false, -- Open the 'AvanteAsk' prompt in a floating window
border = "rounded",
start_insert = true, -- Start insert mode when opening the ask window
},
},
--- @class AvanteConflictConfig
Expand Down
21 changes: 21 additions & 0 deletions lua/avante/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,27 @@ M.toggle_sidebar = function(opts)
return sidebar:toggle(opts)
end

M.is_sidebar_open = function()
local sidebar = M.get()
if not sidebar then return false end
return sidebar:is_open()
end

---@param opts? AskOptions
M.open_sidebar = function(opts)
opts = opts or {}
if opts.ask == nil then opts.ask = true end
local sidebar = M.get()
if not sidebar then M._init(api.nvim_get_current_tabpage()) end
M.current.sidebar:open(opts)
end

M.close_sidebar = function()
local sidebar = M.get()
if not sidebar then return end
sidebar:close()
end

M.toggle.debug = H.api(Utils.toggle_wrap({
name = "debug",
get = function() return Config.debug end,
Expand Down
Loading

0 comments on commit 964715b

Please sign in to comment.