repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

neovim/lua/bugabinga/cmdline/popupmenu.lua

Raw
File outline
local icon = require 'std.icon'
local position = require 'bugabinga.cmdline.position'

local ns = vim.api.nvim_create_namespace 'bugabinga.cmdline.popupmenu'

local state = {
  buffer = -1,
  window = -1,
  items = {},
  selected = -1,
  grid = -1,
  above = false,
}

local function kind_icon(kind)
  if not kind or kind == '' then
    return ''
  end
  return icon[kind] or icon.variable or ''
end

local function format_item(item)
  local abbr = item[1] or ''
  local kind = item[2] or ''
  local menu = item[3] or ''
  local info = item[4] or ''
  local kind_str = kind_icon(kind)
  local parts = {}
  if kind_str ~= '' then
    table.insert(parts, kind_str .. ' ')
  end
  table.insert(parts, abbr)
  if menu ~= '' then
    table.insert(parts, '  ')
    table.insert(parts, menu)
  end
  return table.concat(parts)
end

local function max_width(items)
  local max = 0
  for _, item in ipairs(items) do
    local text = format_item(item)
    max = math.max(max, #text)
  end
  return max
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_popupmenu'
  return state.buffer
end

local function get_anchor(row, col, grid, popup_height)
  if grid == -1 then
    local cmdline_anchor = position.get_cmdline_anchor()
    if cmdline_anchor then
      local cmdline_row = cmdline_anchor.screenpos.row
      local screen_mid = vim.o.lines / 2
      local above = cmdline_row > screen_mid
      state.above = above
      if above then
        return {
          row = cmdline_row - popup_height - 2,
          col = cmdline_anchor.screenpos.col + col,
        }
      else
        return {
          row = cmdline_row + 3,
          col = cmdline_anchor.screenpos.col + col,
        }
      end
    end
  end
  state.above = false
  return {
    row = row + 1,
    col = col,
  }
end

local function get_win(buf, anchor, width, height)
  if vim.api.nvim_win_is_valid(state.window) then
    return state.window
  end
  state.window = vim.api.nvim_open_win(buf, false, {
    relative = 'editor',
    width = width,
    height = height,
    row = anchor.row,
    col = anchor.col,
    style = 'minimal',
    border = vim.g.border_style,
    focusable = false,
    zindex = 201,
  })
  vim.wo[state.window].cursorline = true
  vim.wo[state.window].winbar = ''
  vim.wo[state.window].foldenable = false
  vim.wo[state.window].winhighlight =
    'Normal:Pmenu,CursorLine:PmenuSel,FloatBorder:FloatBorder'
  return state.window
end

local M = {}

function M.on_show(items, selected, row, col, grid)
  if not items or #items == 0 then
    M.on_hide()
    return
  end
  state.items = items
  state.selected = selected
  state.grid = grid
  local width = math.min(max_width(items) + 2, vim.o.columns - 4)
  local height = math.min(#items, vim.o.pumheight)
  local anchor = get_anchor(row, col, grid, height)
  local buf = get_buf()
  local win = get_win(buf, anchor, width, height)
  vim.api.nvim_win_set_config(win, {
    relative = 'editor',
    width = width,
    height = height,
    row = anchor.row,
    col = anchor.col,
  })
  local lines = vim.iter(items):map(function(item)
    return format_item(item)
  end):totable()
  if state.above then
    lines = vim.iter(lines):rev():totable()
  end
  vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
  if selected >= 0 and selected < #items then
    if state.above then
      local visible_start = #items - height
      if selected >= visible_start then
        local reversed_idx = #items - 1 - selected
        vim.api.nvim_win_set_cursor(win, { reversed_idx + 1, 0 })
      end
    else
      vim.api.nvim_win_set_cursor(win, { selected + 1, 0 })
    end
  end
end

function M.on_select(selected)
  if not vim.api.nvim_win_is_valid(state.window) then
    return
  end
  state.selected = selected
  if selected >= 0 and selected < #state.items then
    if state.above then
      local reversed_idx = #state.items - 1 - selected
      vim.api.nvim_win_set_cursor(state.window, { reversed_idx + 1, 0 })
    else
      vim.api.nvim_win_set_cursor(state.window, { selected + 1, 0 })
    end
  end
end

function M.on_hide()
  if vim.api.nvim_win_is_valid(state.window) then
    vim.api.nvim_win_hide(state.window)
  end
  if vim.api.nvim_buf_is_valid(state.buffer) then
    vim.api.nvim_buf_delete(state.buffer, { force = true })
  end
  state.buffer = -1
  state.items = {}
  state.selected = -1
  state.grid = -1
  state.above = false
end

return M