local state = require 'bugabinga.cmdline.state' local render = require 'bugabinga.cmdline.render' local popupmenu = require 'bugabinga.cmdline.popupmenu' local ns = vim.api.nvim_create_namespace 'bugabinga.cmdline' local attached = false local handlers = { cmdline_show = function(content, pos, firstc, prompt, indent, level) local cmdline_state = state.new(content, pos, firstc, prompt, indent, level) render.show(cmdline_state) end, cmdline_pos = function(pos, level) local cmdline_state = state.new({}, pos, '', '', 0, level) render.update_cursor(cmdline_state) end, cmdline_hide = function(abort) render.hide() end, popupmenu_show = function(items, selected, row, col, grid) local text = render.get_text() or '' local prefix = (col >= 0 and col < #text) and text:sub(col + 1) or '' popupmenu.on_show(items, selected, row, col, grid, prefix) end, popupmenu_select = function(selected) popupmenu.on_select(selected) end, popupmenu_hide = function() popupmenu.on_hide() end, } local function dispatch(event, ...) local handler = handlers[event] if handler then handler(...) return true end return false end local function disable() if not attached then return end vim.ui_detach(ns) attached = false render.hide() popupmenu.on_hide() end if not attached then vim.ui_attach(ns, { ext_cmdline = true, ext_popupmenu = true }, function(event, ...) return dispatch(event, ...) end) attached = true end _G.BugabingaCmdline = { disable = disable, } return BugabingaCmdline