Skip to content

Commit 62e85e7

Browse files
committed
feat: dropdown chevron
1 parent c8e2795 commit 62e85e7

6 files changed

Lines changed: 125 additions & 13 deletions

File tree

lua/input-form/form.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ function M:show()
118118
end
119119
self._visible = true
120120

121+
-- Lazy: teach known UI plugins (nvim-scrollbar, satellite, ...) to skip
122+
-- form buffers. Runs once per nvim session.
123+
utils.register_ui_exclusions()
124+
121125
local layout = self:_compute_layout()
122126
self._layout = layout
123127

@@ -126,6 +130,7 @@ function M:show()
126130
vim.bo[self._parent_buf].buftype = "nofile"
127131
vim.bo[self._parent_buf].bufhidden = "wipe"
128132
vim.bo[self._parent_buf].swapfile = false
133+
utils.mark_form_buffer(self._parent_buf)
129134

130135
local parent_lines = {}
131136
for _ = 1, layout.parent_inner_h do

lua/input-form/inputs/multiline.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
--- Multi-line text input component.
22

33
local config = require("input-form.config")
4+
local utils = require("input-form.utils")
45

56
local M = {}
67
M.__index = M
@@ -31,6 +32,7 @@ function M:mount(layout)
3132
vim.bo[self.buf].buftype = "nofile"
3233
vim.bo[self.buf].bufhidden = "wipe"
3334
vim.bo[self.buf].swapfile = false
35+
utils.mark_form_buffer(self.buf)
3436
local lines = vim.split(self._value, "\n", { plain = true })
3537
vim.api.nvim_buf_set_lines(self.buf, 0, -1, false, lines)
3638

lua/input-form/inputs/select.lua

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
--- window; j/k/arrows navigate, <CR> confirms, <Esc> cancels.
66

77
local config = require("input-form.config")
8+
local utils = require("input-form.utils")
89

910
local M = {}
1011
M.__index = M
@@ -47,35 +48,44 @@ local function label_for(options, id)
4748
return ""
4849
end
4950

50-
local function format_display(options, id)
51-
return label_for(options, id)
51+
local CHEVRON = ""
52+
53+
local function format_display(options, id, width)
54+
local label = label_for(options, id)
55+
if not width or width <= 0 then
56+
return label .. CHEVRON
57+
end
58+
local label_w = vim.fn.strdisplaywidth(label)
59+
local chev_w = vim.fn.strdisplaywidth(CHEVRON)
60+
local pad = width - label_w - chev_w
61+
if pad < 1 then
62+
pad = 1
63+
end
64+
return label .. string.rep(" ", pad) .. CHEVRON
5265
end
5366

5467
function M:_render_display()
5568
if self.buf and vim.api.nvim_buf_is_valid(self.buf) then
69+
local line = format_display(self.options, self._selected_id, self._width)
5670
vim.bo[self.buf].modifiable = true
57-
vim.api.nvim_buf_set_lines(
58-
self.buf,
59-
0,
60-
-1,
61-
false,
62-
{ format_display(self.options, self._selected_id) }
63-
)
71+
vim.api.nvim_buf_set_lines(self.buf, 0, -1, false, { line })
6472
vim.bo[self.buf].modifiable = false
6573
end
6674
end
6775

6876
function M:mount(layout)
77+
self._width = layout.width
6978
self.buf = vim.api.nvim_create_buf(false, true)
7079
vim.bo[self.buf].buftype = "nofile"
7180
vim.bo[self.buf].bufhidden = "wipe"
7281
vim.bo[self.buf].swapfile = false
82+
utils.mark_form_buffer(self.buf)
7383
vim.api.nvim_buf_set_lines(
7484
self.buf,
7585
0,
7686
-1,
7787
false,
78-
{ format_display(self.options, self._selected_id) }
88+
{ format_display(self.options, self._selected_id, self._width) }
7989
)
8090
vim.bo[self.buf].modifiable = false
8191

@@ -119,6 +129,10 @@ end
119129
function M:focus()
120130
if self.win and vim.api.nvim_win_is_valid(self.win) then
121131
vim.api.nvim_set_current_win(self.win)
132+
-- Park the cursor at col 0 so the terminal cursor block sits on the label
133+
-- (clean state) or on the dirty-shifted chevron's left neighbour (dirty
134+
-- state), never on top of the chevron itself.
135+
pcall(vim.api.nvim_win_set_cursor, self.win, { 1, 0 })
122136
end
123137
end
124138

@@ -140,6 +154,7 @@ function M:open_dropdown()
140154
self.dropdown_buf = vim.api.nvim_create_buf(false, true)
141155
vim.bo[self.dropdown_buf].buftype = "nofile"
142156
vim.bo[self.dropdown_buf].bufhidden = "wipe"
157+
utils.mark_form_buffer(self.dropdown_buf)
143158
vim.api.nvim_buf_set_lines(self.dropdown_buf, 0, -1, false, lines)
144159
vim.bo[self.dropdown_buf].modifiable = false
145160

lua/input-form/inputs/text.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
--- Single-line text input component.
22

3+
local utils = require("input-form.utils")
4+
35
local M = {}
46
M.__index = M
57

@@ -29,6 +31,7 @@ function M:mount(layout)
2931
vim.bo[self.buf].buftype = "nofile"
3032
vim.bo[self.buf].bufhidden = "wipe"
3133
vim.bo[self.buf].swapfile = false
34+
utils.mark_form_buffer(self.buf)
3235
vim.api.nvim_buf_set_lines(self.buf, 0, -1, false, { self._value })
3336

3437
local win_cfg = {

lua/input-form/utils.lua

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,85 @@ function M.clamp(v, lo, hi)
3939
return v
4040
end
4141

42+
--- The filetype set on every buffer the plugin owns. Users can add this to
43+
--- their UI plugins' exclusion lists as a fallback.
44+
M.FORM_FILETYPE = "input-form"
45+
46+
local _excluded_registered = false
47+
48+
-- Append `ft` to a list-shaped config field if missing.
49+
local function ensure_excluded(cfg, key, ft)
50+
if type(cfg) ~= "table" then
51+
return
52+
end
53+
cfg[key] = cfg[key] or {}
54+
if not vim.tbl_contains(cfg[key], ft) then
55+
table.insert(cfg[key], ft)
56+
end
57+
end
58+
59+
--- Tell known UI plugins (scrollbars, indent guides, etc.) to skip buffers
60+
--- with filetype `input-form`. Idempotent. Called lazily on the first
61+
--- `form:show()` so it works whether or not the user called `setup()`.
62+
function M.register_ui_exclusions()
63+
if _excluded_registered then
64+
return
65+
end
66+
_excluded_registered = true
67+
68+
-- nvim-scrollbar (petertriho/nvim-scrollbar).
69+
-- Its real config lives at `require("scrollbar.config")` — the module stores
70+
-- the active table under `.config` and exposes it via `.get()`. We patch
71+
-- both paths defensively in case the module layout differs across versions.
72+
local ok, sbar_cfg = pcall(require, "scrollbar.config")
73+
if ok and sbar_cfg then
74+
if type(sbar_cfg.get) == "function" then
75+
local cfg = sbar_cfg.get()
76+
ensure_excluded(cfg, "excluded_filetypes", M.FORM_FILETYPE)
77+
ensure_excluded(cfg, "excluded_buftypes", "nofile")
78+
end
79+
if type(sbar_cfg.config) == "table" then
80+
ensure_excluded(sbar_cfg.config, "excluded_filetypes", M.FORM_FILETYPE)
81+
end
82+
end
83+
-- Some older layouts exposed the config directly on the main module.
84+
local ok_top, sbar = pcall(require, "scrollbar")
85+
if ok_top and type(sbar) == "table" and type(sbar.config) == "table" then
86+
ensure_excluded(sbar.config, "excluded_filetypes", M.FORM_FILETYPE)
87+
end
88+
89+
-- satellite.nvim (lewis6991/satellite.nvim).
90+
local ok2, sat_cfg = pcall(require, "satellite.config")
91+
if ok2 and sat_cfg then
92+
if type(sat_cfg.user_config) == "table" then
93+
ensure_excluded(sat_cfg.user_config, "excluded_filetypes", M.FORM_FILETYPE)
94+
end
95+
if type(sat_cfg.config) == "table" then
96+
ensure_excluded(sat_cfg.config, "excluded_filetypes", M.FORM_FILETYPE)
97+
end
98+
end
99+
end
100+
101+
--- Mark a buffer as an internal form buffer so third-party UI plugins
102+
--- (scrollbars, indent guides, git signs, etc.) skip it.
103+
---
104+
--- Sets `filetype = "input-form"` plus the opt-out buffer variables
105+
--- recognized by common plugins. Users whose plugins don't honour these can
106+
--- add `input-form` to their plugin's exclusion list.
107+
function M.mark_form_buffer(buf)
108+
if not (buf and vim.api.nvim_buf_is_valid(buf)) then
109+
return
110+
end
111+
vim.bo[buf].filetype = M.FORM_FILETYPE
112+
-- nvim-scrollbar (petertriho/nvim-scrollbar)
113+
vim.b[buf].scrollbar_disabled = true
114+
-- satellite.nvim (lewis6991/satellite.nvim)
115+
vim.b[buf].satellite_disable = true
116+
-- mini.indentscope / mini.map
117+
vim.b[buf].miniindentscope_disable = true
118+
vim.b[buf].minimap_disable = true
119+
-- gitsigns (defensive; unlikely on a nofile buf but cheap)
120+
vim.b[buf].gitsigns_disable = true
121+
end
122+
42123
return M

tests/test_inputs_select.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ T["select input"] = MiniTest.new_set()
3232
T["select input"]["defaults to first option when none given"] = function()
3333
child.lua([[_G.t = _G.mk(nil); _G.t:mount({ row = 5, col = 5, width = 30 })]])
3434
eq(child.lua_get([[_G.t:value()]]), "a")
35-
eq(child.lua_get([[vim.api.nvim_buf_get_lines(_G.t.buf, 0, -1, false)]]), { "Alpha" })
35+
local line = child.lua_get([==[vim.api.nvim_buf_get_lines(_G.t.buf, 0, -1, false)[1]]==])
36+
helpers.expect.match(line, "^Alpha")
37+
helpers.expect.match(line, "")
3638
end
3739

3840
T["select input"]["honors explicit default"] = function()
3941
child.lua([[_G.t = _G.mk('b'); _G.t:mount({ row = 5, col = 5, width = 30 })]])
4042
eq(child.lua_get([[_G.t:value()]]), "b")
41-
eq(child.lua_get([[vim.api.nvim_buf_get_lines(_G.t.buf, 0, -1, false)]]), { "Beta" })
43+
local line = child.lua_get([==[vim.api.nvim_buf_get_lines(_G.t.buf, 0, -1, false)[1]]==])
44+
helpers.expect.match(line, "^Beta")
45+
helpers.expect.match(line, "")
4246
end
4347

4448
T["select input"]["display buffer is read-only"] = function()
@@ -54,7 +58,9 @@ T["select input"]["select_id updates value and display"] = function()
5458
]])
5559
eq(child.lua_get([[_G.ok]]), true)
5660
eq(child.lua_get([[_G.t:value()]]), "c")
57-
eq(child.lua_get([[vim.api.nvim_buf_get_lines(_G.t.buf, 0, -1, false)]]), { "Gamma" })
61+
local line = child.lua_get([==[vim.api.nvim_buf_get_lines(_G.t.buf, 0, -1, false)[1]]==])
62+
helpers.expect.match(line, "^Gamma")
63+
helpers.expect.match(line, "")
5864
end
5965

6066
T["select input"]["open_dropdown shows all options and <CR> confirms"] = function()

0 commit comments

Comments
 (0)