Minga.Editing.Completion (Minga v0.1.0)

Copy Markdown View Source

Pure data structure for managing LSP completion state.

Holds the list of completion items returned by a language server, tracks the selected index, and filters items as the user continues typing. All functions are pure transformations with no side effects.

Lifecycle

  1. new/2 — create from parsed LSP completion items and a trigger position
  2. filter/2 — narrow the visible items as the user types more characters
  3. move_up/1 / move_down/1 — navigate the selection
  4. selected_item/1 — get the currently highlighted item
  5. accept/1 — returns the text/edit to insert for the selected item

Summary

Types

A parsed completion item.

LSP CompletionItemKind as an atom.

t()

A text edit to apply when accepting a completion.

Functions

Returns the insert text and optional text edit for the selected item.

Returns true if there are any filtered items to show.

Returns the count of currently filtered items.

Filters completion items by the text typed since the trigger position.

Returns a single-character kind indicator for rendering.

Returns the number of semantic candidates matching the current filter before truncation.

Moves the selection down one item, wrapping at the bottom.

Moves the selection up one item, wrapping at the top.

Creates a new completion state from a list of parsed items and the cursor position where completion was triggered.

Creates completion state while retaining one matching stable selection in the bounded snapshot.

Parses a single LSP CompletionItem map into an item() struct.

Parses one completion item with its provider identity.

Parses an LSP completion response into a list of item() maps.

Parses a completion response and qualifies every item with its provider identity.

Selects an item by stable identity when it remains visible.

Selects an item by its offset in the current visible completion window.

Selects an item by its stable wire identifier. Unknown or stale identifiers are ignored.

Returns the currently selected item, or nil if no items.

Returns true when the selected item's raw identity equals the expected raw item.

Updates documentation for one stable item identity.

Updates documentation only when the currently selected item matches the expected raw identity.

Returns {visible_items, selected_offset} for rendering.

Types

item()

A parsed completion item.

item_kind()

@type item_kind() :: Minga.Editing.Completion.Item.kind()

LSP CompletionItemKind as an atom.

t()

@type t() :: %Minga.Editing.Completion{
  filter_text: String.t(),
  filtered: [item()],
  incomplete?: boolean(),
  index: Minga.Editing.Completion.Index.t() | nil,
  items: [item()],
  last_resolved_identity: map() | nil,
  matched_count: non_neg_integer(),
  max_visible: pos_integer(),
  resolve_timer: reference() | nil,
  selected: non_neg_integer(),
  selected_item_id: Minga.Editing.Completion.Item.id() | nil,
  total_count: non_neg_integer(),
  trigger_position: {non_neg_integer(), non_neg_integer()}
}

text_edit()

A text edit to apply when accepting a completion.

Functions

accept(completion)

@spec accept(t()) :: {:insert_text, String.t()} | {:text_edit, text_edit()} | nil

Returns the insert text and optional text edit for the selected item.

If the item has a text_edit, returns {:text_edit, edit}. Otherwise returns {:insert_text, text}.

active?(completion)

@spec active?(t()) :: boolean()

Returns true if there are any filtered items to show.

count(completion)

@spec count(t()) :: non_neg_integer()

Returns the count of currently filtered items.

filter(completion, prefix)

@spec filter(t(), String.t()) :: t()

Filters completion items by the text typed since the trigger position.

Items whose filter_text starts with prefix (case-insensitive) are kept. Resets selection to the top.

kind_label(atom)

@spec kind_label(item_kind()) :: String.t()

Returns a single-character kind indicator for rendering.

matched_count(completion)

@spec matched_count(t()) :: non_neg_integer()

Returns the number of semantic candidates matching the current filter before truncation.

move_down(c)

@spec move_down(t()) :: t()

Moves the selection down one item, wrapping at the bottom.

move_up(c)

@spec move_up(t()) :: t()

Moves the selection up one item, wrapping at the top.

new(items, trigger_position)

Creates a new completion state from a list of parsed items and the cursor position where completion was triggered.

new(index, trigger_position, selected_item_id)

Creates completion state while retaining one matching stable selection in the bounded snapshot.

parse_item(raw)

@spec parse_item(map()) :: item()

Parses a single LSP CompletionItem map into an item() struct.

parse_item(raw, provider_id)

@spec parse_item(map(), Minga.Editing.Completion.Item.provider_id()) :: item()

Parses one completion item with its provider identity.

parse_response(response)

@spec parse_response(map() | [map()] | nil) :: [item()]

Parses an LSP completion response into a list of item() maps.

Handles both CompletionList (%{"items" => [...]}) and bare CompletionItem[] response formats.

parse_response(items, provider_id)

@spec parse_response(
  map() | [map()] | nil,
  Minga.Editing.Completion.Item.provider_id()
) :: [item()]

Parses a completion response and qualifies every item with its provider identity.

select_item(completion, item_id)

@spec select_item(t(), Minga.Editing.Completion.Item.id() | nil) :: t()

Selects an item by stable identity when it remains visible.

select_visible(completion, offset)

@spec select_visible(t(), non_neg_integer()) :: t()

Selects an item by its offset in the current visible completion window.

select_wire_id(completion, wire_id)

@spec select_wire_id(t(), String.t()) :: {:ok, t()} | :stale

Selects an item by its stable wire identifier. Unknown or stale identifiers are ignored.

selected_item(completion)

@spec selected_item(t()) :: item() | nil

Returns the currently selected item, or nil if no items.

selected_raw?(completion, raw_item)

@spec selected_raw?(t(), map()) :: boolean()

Returns true when the selected item's raw identity equals the expected raw item.

update_item_documentation(completion, item_id, doc_text)

@spec update_item_documentation(t(), Minga.Editing.Completion.Item.id(), String.t()) ::
  t()

Updates documentation for one stable item identity.

update_selected_documentation(completion, raw_item, doc_text)

@spec update_selected_documentation(t(), map(), String.t()) :: t()

Updates documentation only when the currently selected item matches the expected raw identity.

Called when a completionItem/resolve response arrives with the full documentation text.

visible_items(c)

@spec visible_items(t()) :: {[item()], non_neg_integer()}

Returns {visible_items, selected_offset} for rendering.

visible_items is a window of at most max_visible items centered around the selection. selected_offset is the index of the selected item within that window.