From db0682fe50d863328c7ff335486545fd71df02ae Mon Sep 17 00:00:00 2001 From: Evgeni Chasnovski Date: Sun, 17 May 2026 18:34:06 +0300 Subject: [PATCH] feat(ui): vim.ui.input(opts.scope) #39570 Problem: There is no way for a `vim.ui.input` caller to indicate for which scope the input is. As in "This input is for something at cursor scope". This information can be useful for `vim.ui.input` implementation to tweak its behavior and presentation: - Show different floating window depending on the scope. For example: - Near cursor for "cursor" scope. - At line start for "line" scope. - In window corner for "buffer" and "window" scopes. - In whole editor corner for "tabpage", "editor", "project" scopes. - Navigate through history only for inputs with the same scope. Solution: Document new `opts.scope` for `vim.ui.input`. Use it in the codebase. --- runtime/doc/lua.txt | 11 ++++++++++- runtime/doc/news.txt | 1 + runtime/lua/nvim/spellfile.lua | 2 +- runtime/lua/vim/lsp/buf.lua | 2 ++ runtime/lua/vim/ui.lua | 9 ++++++++- 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt index 8314f5a768..2f935f0882 100644 --- a/runtime/doc/lua.txt +++ b/runtime/doc/lua.txt @@ -5334,7 +5334,8 @@ vim.ui.input({opts}, {on_confirm}) *vim.ui.input()* work until `on_confirm`. Example: >lua - vim.ui.input({ prompt = 'Enter value for shiftwidth: ' }, function(input) + local opts = { prompt = 'Enter value for shiftwidth: ', scope = 'buffer' } + vim.ui.input(opts, function(input) vim.o.shiftwidth = tonumber(input) end) < @@ -5349,6 +5350,14 @@ vim.ui.input({opts}, {on_confirm}) *vim.ui.input()* • {highlight}? (`function`) Function that will be used for highlighting user inputs. • {prompt}? (`string`) Text of the prompt + • {scope}? + (`'cursor'|'line'|'buffer'|'window'|'tabpage'|'editor'|'project'`) + Input scope, as in "This input is for something at + cursor/line/etc scope". Can be used by `vim.ui.input` + implementations to tweak behavior and presentation. + For example, the input may adjust the floating window + position: near the cursor if `cursor`, in window + corner if `buffer` or `window`, etc. • {on_confirm} (`fun(input?: string)`) Called once the user confirms or abort the input. `input` is what the user typed (it might be an empty string if nothing was entered), or diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index ae432002ca..c53d13e738 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -199,6 +199,7 @@ LUA • |vim.npcall()| calls the function `fn` in protected-mode like |pcall()|, but returns `nil` on error. • |vim.pos| can now convert between positions and buffer offsets. +• |vim.ui.input()| now allows setting input scope. • |vim.log| for easily creating loggers. OPTIONS diff --git a/runtime/lua/nvim/spellfile.lua b/runtime/lua/nvim/spellfile.lua index f964d78a2f..5a33e7ee9e 100644 --- a/runtime/lua/nvim/spellfile.lua +++ b/runtime/lua/nvim/spellfile.lua @@ -281,7 +281,7 @@ function M.get(lang) info.lang, info.encoding ) - vim.ui.input({ prompt = prompt }, function(input) + vim.ui.input({ prompt = prompt, scope = 'editor' }, function(input) -- properly clear the message window vim.api.nvim_echo({ { ' ' } }, false, { kind = 'empty' }) if not input or input:lower() ~= 'y' then diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua index af339a6244..94fc21dd27 100644 --- a/runtime/lua/vim/lsp/buf.lua +++ b/runtime/lua/vim/lsp/buf.lua @@ -809,6 +809,7 @@ function M.rename(new_name, opts) local prompt_opts = { prompt = 'New Name: ', + scope = 'cursor', } if result.placeholder then prompt_opts.default = result.placeholder @@ -839,6 +840,7 @@ function M.rename(new_name, opts) local prompt_opts = { prompt = 'New Name: ', default = cword, + scope = 'cursor', } vim.ui.input(prompt_opts, function(input) if not input or #input == 0 then diff --git a/runtime/lua/vim/ui.lua b/runtime/lua/vim/ui.lua index 665fd19e9e..fd065d1ad5 100644 --- a/runtime/lua/vim/ui.lua +++ b/runtime/lua/vim/ui.lua @@ -98,6 +98,12 @@ end ---Function that will be used for highlighting ---user inputs. ---@field highlight? function +--- +---Input scope, as in "This input is for something at cursor/line/etc scope". +---Can be used by `vim.ui.input` implementations to tweak behavior and presentation. +---For example, the input may adjust the floating window position: near the cursor if +---`cursor`, in window corner if `buffer` or `window`, etc. +---@field scope? 'cursor'|'line'|'buffer'|'window'|'tabpage'|'editor'|'project' --- Prompts the user for input, allowing arbitrary (potentially asynchronous) work until --- `on_confirm`. @@ -105,7 +111,8 @@ end --- Example: --- --- ```lua ---- vim.ui.input({ prompt = 'Enter value for shiftwidth: ' }, function(input) +--- local opts = { prompt = 'Enter value for shiftwidth: ', scope = 'buffer' } +--- vim.ui.input(opts, function(input) --- vim.o.shiftwidth = tonumber(input) --- end) --- ```