One table of keys binds every action in two forms (the terminal's chords, and keys that always arrive), and the bottom bar shows the form that works for the bar that has the focus. The prompt says what Enter will do (ask, rewrite, add; Tab switches in an empty prompt) and carries the selection or the cursor's line as its attachment. Mode, effort, suggestions, AI on or off and the bars are remembered per directory with a machine-wide default. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
162 lines
5.9 KiB
Lua
162 lines
5.9 KiB
Lua
-- The settings lines: what holds now — model, mode, effort, autocomplete — one line each, at the
|
|
-- top of the right bar. Enter on a line opens its choices right under it, in the bar; Enter on a
|
|
-- choice takes it. The values and the choices are the plugin's public ones (`setting_lines`,
|
|
-- `choices`, `set`): this module only draws them.
|
|
local M = {}
|
|
local ns = vim.api.nvim_create_namespace("llembas-lines")
|
|
|
|
local P = { buf = nil, open = nil, choices = nil, note = nil, rows = {} }
|
|
M.LABEL_WIDTH = 14 -- every label is cut or padded to this, so the values share one edge
|
|
M.MAX_CHOICES = 8 -- rows the bar gives a list of choices before it scrolls
|
|
|
|
local function L() return require("llembas") end
|
|
local function valid() return P.buf and vim.api.nvim_buf_is_valid(P.buf) end
|
|
|
|
function M.win()
|
|
if not valid() then return nil end
|
|
for _, w in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
|
|
if vim.api.nvim_win_get_buf(w) == P.buf then return w end
|
|
end
|
|
end
|
|
|
|
local function one_line(text) return (tostring(text or ""):gsub("[%c]+", " ")) end
|
|
|
|
--- How many rows the lines want now: the settings, and the open list up to its limit.
|
|
function M.height()
|
|
local settings = #L().setting_lines()
|
|
if not P.open then return settings end
|
|
local extra = P.choices and #P.choices or 1
|
|
return settings + math.min(math.max(extra, 1), M.MAX_CHOICES)
|
|
end
|
|
|
|
function M.render()
|
|
if not valid() then return end
|
|
local lines, rows, marks = {}, {}, {}
|
|
for _, s in ipairs(L().setting_lines()) do
|
|
local label = vim.fn.strcharpart(s.label, 0, M.LABEL_WIDTH - 1)
|
|
lines[#lines + 1] = label .. (" "):rep(M.LABEL_WIDTH - vim.fn.strdisplaywidth(label)) .. one_line(s.value)
|
|
rows[#rows + 1] = { kind = "setting", id = s.id }
|
|
marks[#marks + 1] = { #lines - 1, 0, #label, "LLeMbasLineLabel" }
|
|
marks[#marks + 1] = { #lines - 1, M.LABEL_WIDTH, #lines[#lines], s.off and "LLeMbasLineOff" or "LLeMbasLineValue" }
|
|
if P.open == s.id then
|
|
if P.choices then
|
|
for _, c in ipairs(P.choices) do
|
|
lines[#lines + 1] = (c.current and " ● " or " ○ ") .. one_line(c.label)
|
|
rows[#rows + 1] = { kind = "choice", id = s.id, value = c.value }
|
|
if c.current then marks[#marks + 1] = { #lines - 1, 0, #lines[#lines], "LLeMbasLineCurrent" } end
|
|
end
|
|
if #P.choices == 0 then
|
|
lines[#lines + 1] = " (nothing to choose from)"
|
|
rows[#rows + 1] = { kind = "note" }
|
|
end
|
|
else
|
|
lines[#lines + 1] = " " .. one_line(P.note or "…")
|
|
rows[#rows + 1] = { kind = "note" }
|
|
marks[#marks + 1] = { #lines - 1, 0, #lines[#lines], "LLeMbasNote" }
|
|
end
|
|
end
|
|
end
|
|
P.rows = rows
|
|
vim.bo[P.buf].modifiable = true
|
|
vim.api.nvim_buf_set_lines(P.buf, 0, -1, false, lines)
|
|
vim.bo[P.buf].modifiable = false
|
|
vim.api.nvim_buf_clear_namespace(P.buf, ns, 0, -1)
|
|
for _, m in ipairs(marks) do
|
|
pcall(vim.api.nvim_buf_set_extmark, P.buf, ns, m[1], m[2], { end_col = m[3], hl_group = m[4] })
|
|
end
|
|
local win = M.win()
|
|
if win then pcall(vim.api.nvim_win_set_height, win, M.height()) end
|
|
end
|
|
|
|
local function row_here()
|
|
local win = M.win()
|
|
if not win then return nil end
|
|
return P.rows[vim.api.nvim_win_get_cursor(win)[1]], win
|
|
end
|
|
|
|
local function cursor_to(pred)
|
|
local win = M.win()
|
|
if not win then return end
|
|
for i, r in ipairs(P.rows) do
|
|
if pred(r) then return pcall(vim.api.nvim_win_set_cursor, win, { i, 0 }) end
|
|
end
|
|
end
|
|
|
|
--- Close the open list. True when one was open.
|
|
function M.close_choices()
|
|
if not P.open then return false end
|
|
local id = P.open
|
|
P.open, P.choices, P.note = nil, nil, nil
|
|
M.render()
|
|
cursor_to(function(r) return r.kind == "setting" and r.id == id end)
|
|
return true
|
|
end
|
|
|
|
--- Open the choices of the setting `id` under its line.
|
|
function M.open_choices(id)
|
|
P.open, P.choices, P.note = id, nil, nil
|
|
M.render()
|
|
L().choices(id, function(choices, why)
|
|
if P.open ~= id then return end
|
|
if not choices then
|
|
P.note = why or "not available now"
|
|
else
|
|
P.choices = choices
|
|
end
|
|
M.render()
|
|
cursor_to(function(r) return r.kind == "choice" and r.id == id and (function()
|
|
for _, c in ipairs(P.choices or {}) do if c.current and c.value == r.value then return true end end
|
|
return false
|
|
end)() end)
|
|
end)
|
|
end
|
|
|
|
--- Enter: on a setting, its choices open (or close, when they are open); on a choice, it is taken.
|
|
function M.enter()
|
|
local r = row_here()
|
|
if not r then return end
|
|
if r.kind == "setting" then
|
|
if P.open == r.id then return M.close_choices() end
|
|
return M.open_choices(r.id)
|
|
elseif r.kind == "choice" then
|
|
local id, value = r.id, r.value
|
|
P.open, P.choices, P.note = nil, nil, nil
|
|
M.render()
|
|
cursor_to(function(x) return x.kind == "setting" and x.id == id end)
|
|
L().set(id, value)
|
|
end
|
|
end
|
|
|
|
--- Esc: the open list closes; with none open, back to the code.
|
|
function M.back()
|
|
if M.close_choices() then return end
|
|
pcall(function() require("llembas.layout").focus("code") end)
|
|
end
|
|
|
|
function M.buffer()
|
|
if valid() then return P.buf end
|
|
P.buf = vim.api.nvim_create_buf(false, true)
|
|
vim.bo[P.buf].buftype = "nofile"
|
|
vim.bo[P.buf].bufhidden = "hide"
|
|
vim.bo[P.buf].swapfile = false
|
|
vim.bo[P.buf].modeline = false
|
|
vim.bo[P.buf].undolevels = -1
|
|
vim.bo[P.buf].filetype = "llembas-settings"
|
|
pcall(vim.api.nvim_buf_set_name, P.buf, "llembas://settings")
|
|
vim.bo[P.buf].modifiable = false
|
|
require("llembas.keys").bind("settings", P.buf, { settings_change = M.enter, settings_back = M.back })
|
|
vim.api.nvim_create_autocmd("User", {
|
|
group = vim.api.nvim_create_augroup("llembas-lines", { clear = true }),
|
|
pattern = "LLeMbasChanged",
|
|
callback = function() if M.win() then M.render() end end,
|
|
})
|
|
return P.buf
|
|
end
|
|
|
|
--- The lines as text, and which setting's list is open: for the tests.
|
|
function M.lines() return valid() and vim.api.nvim_buf_get_lines(P.buf, 0, -1, false) or {} end
|
|
function M.opened() return P.open end
|
|
function M.buf() return valid() and P.buf or nil end
|
|
|
|
return M
|