Skip to content

Latest commit

 

History

History
114 lines (90 loc) · 3.29 KB

codeactions.md

File metadata and controls

114 lines (90 loc) · 3.29 KB

codeactions

Code actions module for cosmic-ui. This module validates setup/module enable state and runs code action requests directly. Lua API is primary; :CosmicCodeActions is an optional wrapper.

Setup

require("cosmic-ui").setup({
  codeactions = {
    enabled = true,
  },
})

⚙️ Config

codeactions = {
  enabled = true,
  min_width = nil,
  border = {
    bottom_hl = "FloatBorder",
    highlight = "FloatBorder",
    style = nil, -- falls back to vim.o.winborder
    title = "Code Actions",
    title_align = "center",
    title_hl = "FloatBorder",
  },
}

Types

opts

Optional request options passed to the code action core module.

Field Type Required Default Description
params table|nil No nil Complete LSP CodeActionParams replacement.
range table|nil No nil Optional range { start, end }.
range.start {integer, integer}|nil No nil Optional start position {line, col}.
range.end {integer, integer}|nil No nil Optional end position {line, col}.

params replaces automatic position construction, so it must include textDocument and range. Its optional context is preserved, and missing diagnostics are populated by cosmic-ui. Build cursor positions with the target client's offset encoding.

Module

require("cosmic-ui").codeactions.open(opts?)

Requests and opens code actions at the current cursor context.

Behavior:

  • warns and no-ops if setup() has not run
  • warns and no-ops if codeactions is disabled
  • executes code action requests via the internal cosmic-ui.codeactions implementation
  • opens a native floating menu (no NUI dependency)
  • menu groups are ordered deterministically by client name (tie-break: client id)
  • actions within each client group keep server response order
  • group headers are rendered as plain section rows
  • selection index/count is shown in the float border footer (right-aligned)
  • key hints (Enter:apply q:close) are rendered as an in-buffer footer row
  • the menu opens below the cursor and flips above it when there is not enough screen space below
  • keymaps: j/k, <Down>/<Up>, <Tab>/<S-Tab>, <CR>/<Space>, <Esc>/<C-c>/q
require("cosmic-ui").codeactions.open()
local bufnr = vim.api.nvim_get_current_buf()
local target_client = assert(vim.lsp.get_clients({
  bufnr = bufnr,
  method = "textDocument/codeAction",
})[1])
local params = vim.lsp.util.make_range_params(0, target_client.offset_encoding)
params.context = { only = { "quickfix" } }

require("cosmic-ui").codeactions.open({
  params = params,
})

require("cosmic-ui").codeactions.range(opts?)

Requests code actions for the active visual selection.

Behavior:

  • uses opts.range when provided
  • else uses opts.params when provided
  • else builds range from the active visual selection, or from the '< and '> marks outside visual mode
  • warns and no-ops if setup() has not run or module is disabled
vim.keymap.set("v", "<leader>ga", function()
  require("cosmic-ui").codeactions.range()
end)
require("cosmic-ui").codeactions.range({
  range = { start = { 10, 0 }, ["end"] = { 12, 0 } },
})

Optional command:

  • :CosmicCodeActions opens the Cosmic code action panel.