Skip to content

code/editor/CodeCompletion

import { … } from 'zidane-ui/code/editor'ui/src/code/editor/CodeCompletion.tsx

Functions#

matchWordFunction#

How text matches typed: every typed character in order, the first one at a word start. Class 3: a case-exact prefix, 2: a prefix, 1: all hits on word starts (camelCase, snake_case), 0: anywhere. null: no match.

TypeScript
function matchWord(
  text: string,
  typed: string,
): { positions: number[]; rank: number } | null
Parameters
NameTypeDescription
textstring

The label (or filter text) to match.

typedstring

The word typed so far.

{ positions: number[]; rank: number } | null

ui/src/code/editor/CodeCompletion.tsx:135

rankCompletionsFunction#

The items matching typed, best first: match class, then the server's own order.

TypeScript
function rankCompletions(items: readonly CompletionItem[], typed: string): RankedItem[]
Parameters
NameTypeDescription
itemsreadonly CompletionItem[]

The answer's items, in the server's order.

typedstring

The word typed so far.

ui/src/code/editor/CodeCompletion.tsx:183

useCompletionFunction#

Drives completion for one editor: call onChange with the editor's changes and render node.

TypeScript
function useCompletion(
  source: CompletionSource | null,
  handle: RefObject<CodeEditorHandle | null>,
): { close: () => void; node: Element | null; onChange: (next: string, changes: readonly EditChange[]) => void; open: boolean }
Parameters
NameTypeDescription
sourceCompletionSource | null

Where suggestions come from; null turns completion off.

handleRefObject<CodeEditorHandle | null>

The editor's handle (its host takes the keys, its editor the edits).

{ close: () => void; node: Element | null; onChange: (next: string, changes: readonly EditChange[]) => void; open: boolean }

ui/src/code/editor/CodeCompletion.tsx:246

Interfaces#

CompletionAnswerInterface#

What a completion request answers.

TypeScript
interface CompletionAnswer
CompletionAnswer properties
NameTypeDescription
busy?string

The server is still loading.

idnumber

Answer id, passed back to CompletionSource.resolve.

incompleteboolean

Typing on must ask again instead of filtering.

itemsCompletionItem[]

The suggestions, in the server's order.

ui/src/code/editor/CodeCompletion.tsx:55

CompletionDetailInterface#

The rest of an item, fetched for the highlighted item and on accept.

TypeScript
interface CompletionDetail
CompletionDetail properties
NameTypeDescription
additionalEdits?{ newText: string; range: { end: Pos; start: Pos } }[]

More edits to apply on accept (an auto-import), in 0-based positions; only those ahead of the inserted text apply.

detail?string

Full type or signature, shown in the documentation panel when it says more than the item's own.

documentation?string

Documentation, shown as plain text under the menu.

ui/src/code/editor/CodeCompletion.tsx:67

CompletionItemInterface#

One suggestion (the LSP item, flattened by the source).

TypeScript
interface CompletionItem
CompletionItem properties
NameTypeDescription
cursor?{ end: number; start: number }

Caret (or selected placeholder) inside insertText, as offsets; absent: its end.

detail?string

Type or signature, shown dimmed after the label.

filterText?string

Text the typed word is matched against instead of label.

insertTextstring

Text that replaces the typed word on accept.

kind?string

LSP kind as a word (function, field, …): picks the icon.

labelstring

Text shown in the menu; the typed word is matched against it unless filterText is set.

preselect?boolean

Selected when the menu opens on an empty word.

range?{ end: Pos; start: Pos }

The span the item replaces at request time; absent: the word before the caret.

sortText?string

Sort key within a match class; absent: label.

ui/src/code/editor/CodeCompletion.tsx:33

CompletionSourceInterface#

Where the menu's suggestions come from (the Files panel's language server).

TypeScript
interface CompletionSource
CompletionSource properties
NameTypeDescription
complete(text: string, line: number, character: number, trigger?: string) => Promise<CompletionAnswer>

Suggestions at a 0-based caret in text; trigger is the character just typed when it is not a word character.

resolve?(id: number, index: number) => Promise<CompletionDetail | null>

The rest of item index of answer id.

ui/src/code/editor/CodeCompletion.tsx:77

RankedItemInterface#

An item that matches what was typed, with the label characters that matched.

TypeScript
interface RankedItem
RankedItem properties
NameTypeDescription
indexnumber

Index in the answer (for resolve).

itemCompletionItem

The suggestion.

matchesnumber[]

Label indices of the matched characters, for highlighting; empty when it matched on filterText.

ui/src/code/editor/CodeCompletion.tsx:85