Skip to content

Commit ea5bbd8

Browse files
committed
feat: dropdown chevron
1 parent c8e2795 commit ea5bbd8

6 files changed

Lines changed: 138 additions & 13 deletions

File tree

lua/input-form/form.lua

Lines changed: 12 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
@@ -183,8 +188,15 @@ function M:show()
183188
border = border,
184189
})
185190
self:_install_keymaps(input)
191+
self:_install_validation(input)
186192
end
187193

194+
-- Default highlight groups for validation error state. `default = true`
195+
-- means user overrides take precedence.
196+
pcall(vim.api.nvim_set_hl, 0, "InputFormFieldError", { fg = "Red", default = true })
197+
pcall(vim.api.nvim_set_hl, 0, "InputFormFieldErrorBorder", { fg = "Red", default = true })
198+
pcall(vim.api.nvim_set_hl, 0, "InputFormFieldErrorTitle", { fg = "Red", default = true })
199+
188200
self:_focus(1)
189201
return self
190202
end

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: 31 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,46 @@ 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_CLOSED = ""
52+
local CHEVRON_OPEN = ""
53+
54+
local function format_display(options, id, width, open)
55+
local label = label_for(options, id)
56+
local chevron = open and CHEVRON_OPEN or CHEVRON_CLOSED
57+
if not width or width <= 0 then
58+
return label .. chevron
59+
end
60+
local label_w = vim.fn.strdisplaywidth(label)
61+
local chev_w = vim.fn.strdisplaywidth(chevron)
62+
local pad = width - label_w - chev_w
63+
if pad < 1 then
64+
pad = 1
65+
end
66+
return label .. string.rep(" ", pad) .. chevron
5267
end
5368

5469
function M:_render_display()
5570
if self.buf and vim.api.nvim_buf_is_valid(self.buf) then
71+
local line = format_display(self.options, self._selected_id, self._width, self._open)
5672
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-
)
73+
vim.api.nvim_buf_set_lines(self.buf, 0, -1, false, { line })
6474
vim.bo[self.buf].modifiable = false
6575
end
6676
end
6777

6878
function M:mount(layout)
79+
self._width = layout.width
6980
self.buf = vim.api.nvim_create_buf(false, true)
7081
vim.bo[self.buf].buftype = "nofile"
7182
vim.bo[self.buf].bufhidden = "wipe"
7283
vim.bo[self.buf].swapfile = false
84+
utils.mark_form_buffer(self.buf)
7385
vim.api.nvim_buf_set_lines(
7486
self.buf,
7587
0,
7688
-1,
7789
false,
78-
{ format_display(self.options, self._selected_id) }
90+
{ format_display(self.options, self._selected_id, self._width, self._open) }
7991
)
8092
vim.bo[self.buf].modifiable = false
8193

@@ -119,6 +131,10 @@ end
119131
function M:focus()
120132
if self.win and vim.api.nvim_win_is_valid(self.win) then
121133
vim.api.nvim_set_current_win(self.win)
134+
-- Park the cursor at col 0 so the terminal cursor block sits on the label
135+
-- (clean state) or on the dirty-shifted chevron's left neighbour (dirty
136+
-- state), never on top of the chevron itself.
137+
pcall(vim.api.nvim_win_set_cursor, self.win, { 1, 0 })
122138
end
123139
end
124140

@@ -140,6 +156,7 @@ function M:open_dropdown()
140156
self.dropdown_buf = vim.api.nvim_create_buf(false, true)
141157
vim.bo[self.dropdown_buf].buftype = "nofile"
142158
vim.bo[self.dropdown_buf].bufhidden = "wipe"
159+
utils.mark_form_buffer(self.dropdown_buf)
143160
vim.api.nvim_buf_set_lines(self.dropdown_buf, 0, -1, false, lines)
144161
vim.bo[self.dropdown_buf].modifiable = false
145162

@@ -160,6 +177,8 @@ function M:open_dropdown()
160177
focusable = true,
161178
zindex = 100,
162179
})
180+
self._open = true
181+
self:_render_display()
163182
vim.wo[self.dropdown_win].cursorline = true
164183
vim.wo[self.dropdown_win].winhl =
165184
"NormalFloat:InputFormDropdown,CursorLine:InputFormDropdownActive"
@@ -192,6 +211,8 @@ function M:close_dropdown()
192211
end
193212
self.dropdown_win = nil
194213
self.dropdown_buf = nil
214+
self._open = false
215+
self:_render_display()
195216
if self.win and vim.api.nvim_win_is_valid(self.win) then
196217
vim.api.nvim_set_current_win(self.win)
197218
end

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)