local format = require 'bugabinga.cmdline.format' local position = require 'bugabinga.cmdline.position' local cursor = require 'bugabinga.cmdline.cursor' local highlight = require 'bugabinga.cmdline.highlight' local table = require 'std.table' local ns = vim.api.nvim_create_namespace 'bugabinga.cmdline' local hl_ns = vim.api.nvim_create_namespace 'bugabinga.cmdline.hl' local state = { buffer = -1, window = -1, current_fmt = nil, text = '', } local ICON_HIGHLIGHT_MAP = { search_down = 'CmdlineIconSearch', search_up = 'CmdlineIconSearch', shell = 'CmdlineIconShell', help = 'CmdlineIconHelp', lua = 'CmdlineIconLua', input = 'CmdlineIconInput', cmdline = 'CmdlineIconCmdline', } local function get_winhighlight(type) local hl = format.border_highlight_map[type] or format.border_highlight_map.cmdline return string.format('Normal:NormalFloat,FloatBorder:%s,FloatBorderCorner:%s', hl.border, hl.corner) end local function get_buf() if vim.api.nvim_buf_is_valid(state.buffer) then return state.buffer end state.buffer = vim.api.nvim_create_buf(false, true) vim.bo[state.buffer].buflisted = false vim.bo[state.buffer].filetype = 'bugabinga_cmdline' return state.buffer end local function get_win(buf, width, fmt) if vim.api.nvim_win_is_valid(state.window) then return state.window end local screen_pos = position.at_cursor(width or vim.o.columns) state.window = vim.api.nvim_open_win(buf, false, { relative = 'editor', width = width or vim.o.columns, height = 1, row = screen_pos.row, col = 0, style = 'minimal', border = vim.g.border_style, focusable = false, zindex = 50, }) vim.wo[state.window].cursorline = false vim.wo[state.window].winbar = '' vim.wo[state.window].foldenable = false vim.wo[state.window].winhighlight = get_winhighlight(fmt.type) return state.window end local M = {} function M.show(cmdline_state) local text = cmdline_state:text() state.text = text local fmt = format.detect(cmdline_state.firstc, text, cmdline_state.prompt) local fmt_changed = not state.current_fmt or fmt.type ~= state.current_fmt.type state.current_fmt = fmt local display_text = ' ' .. text .. ' ' local width = vim.o.columns local buf = get_buf() local win = get_win(buf, width, fmt) local is_new = not position.get_cmdline_anchor() if is_new then local screen_pos = position.at_cursor(width) vim.api.nvim_win_set_config(win, { relative = 'editor', width = width, height = 1, row = screen_pos.row, col = 0, }) local anchor = { screenpos = { row = screen_pos.row, col = 0 }, cursor = 0, buf = buf, win = win, bufpos = { row = 0, col = 0 }, } position.set_cmdline_anchor(anchor) vim.g.ui_cmdline_pos = { anchor.screenpos.row, anchor.screenpos.col } end if fmt_changed then vim.wo[win].winhighlight = get_winhighlight(fmt.type) end vim.api.nvim_buf_set_lines(buf, 0, -1, false, { display_text }) vim.api.nvim_buf_clear_namespace(buf, ns, 0, -1) local icon_hl = ICON_HIGHLIGHT_MAP[fmt.type] or 'CmdlineIconCmdline' vim.api.nvim_buf_set_extmark(buf, ns, 0, 0, { virt_text = { { fmt.icon, icon_hl } }, virt_text_pos = 'overlay', }) highlight.apply(buf, hl_ns, text, fmt.lang, 0, 2) local cursor_byte = cmdline_state:cursor_byte() local cursor_col = 2 + cursor_byte local anchor = position.get_cmdline_anchor() if anchor then anchor.cursor = cursor_col anchor.bufpos.col = cursor_col position.set_cmdline_anchor(anchor) end cursor.fix(win, cursor_col) end function M.hide() if vim.api.nvim_win_is_valid(state.window) then vim.api.nvim_win_hide(state.window) end state.window = -1 state.current_fmt = nil state.text = '' position.set_cmdline_anchor(nil) vim.g.ui_cmdline_pos = nil end function M.update_cursor(cmdline_state) if not vim.api.nvim_win_is_valid(state.window) then return end local info = vim.api.nvim_get_cmdline_info() local raw = info.cmdline and #info.cmdline > 0 and table.concat(info.cmdline, '') or '' local firstc = info.firstc and info.firstc ~= '' and info.firstc or '' local content = raw if firstc ~= '' and raw:find('^' .. firstc) then content = raw:sub(#firstc + 1) end if content ~= state.text then state.text = content local fmt = format.detect(firstc, content, cmdline_state.prompt) if fmt.type ~= state.current_fmt.type then state.current_fmt = fmt vim.wo[state.window].winhighlight = get_winhighlight(fmt.type) vim.api.nvim_buf_clear_namespace(state.buffer, ns, 0, -1) local icon_hl = ICON_HIGHLIGHT_MAP[fmt.type] or 'CmdlineIconCmdline' vim.api.nvim_buf_set_extmark(state.buffer, ns, 0, 0, { virt_text = { { fmt.icon, icon_hl } }, virt_text_pos = 'overlay', }) end end local cursor_byte = cmdline_state:cursor_byte() local cursor_col = 2 + cursor_byte local anchor = position.get_cmdline_anchor() if anchor then anchor.cursor = cursor_col position.set_cmdline_anchor(anchor) end cursor.fix(state.window, cursor_col) end function M.get_format_type() return state.current_fmt and state.current_fmt.type or 'cmdline' end return M