Skip to content

Autocomplete

The autoComplete() extension can be used to show a list of completable options to users while they’re typing.

autoComplete() takes a AutoCompleteConfig object as the first parameter. The only required property is the filter function to use. The optional properties configure when to open the tooltip, when to close it, and where to place it.

The job of a CompletionFilter is to filter and score the options based on the word before the cursor. They should also return which characters in the option’s label were matched so they can be highlighted. This library exports two filter functions you can use; fuzzyFilter() and strictFilter(). If you’re dissatisfied with these, you can define your own filter instead.

The extension doesn’t define any completion sources. You have to register them with registerCompletions().

Try it:

completeScope() is another completion source that can be used to add completion for the window object for example, but using it as the same time as completeIdentifiers() does lead to a lot of duplicated options.

markupCompletion() adds completion for the specified namespaces of tags.

The example below shows how markupCompletion() can be used to add autocompletion to HTML, SVG, MathML, and one custom element. Here, globalHtmlAttributes are used for HTML elements, the custom element, and unrecognized elements.

Autocompletion works for nested languages too. Try writing in the script/style tags:

vueCompletion() adds completion for HTML and SVG tags to Vue. When configured, it can also provide completions for specific Vue components.

Try it:

svelteCompletion() is very similar to vueCompletion(), but the first parameter is instead used to configure snippets for blocks like {#each}, {#if}, etc.

Try it:

CompletionDefinition objects passed to registerCompletions() have two properties: context and sources. The optional context property is a function that’s supposed to return an object with extra properties that are added to the CompletionContext passed to the sources. This is useful to avoid repeating logic between multiple completion sources.

The sources are called with the completion context along with the editor and can return a CompletionResult containing the completion items and where the completions should start from. These completion items don’t need to be filtered or sorted. That’s the job done by the filter function passed to the extension. It should be noted that completion sources must be synchronous and can be called on every keystroke, so they shouldn’t do any expensive computations.

The completion items returned have multiple properties to customize their insertion and appearance. Read the Completion API reference for more info.

To create snippets, use the insert property along with tabStops. The indentation will be preserved if there are line feeds in insert. Tabs are replaced with spaces according to the tab size when options.insertSpaces isn’t set to false.

findWords() can be used to find words in the editor’s tokens that can be returned as completions.

getClosestToken() is extremely useful for determining if the completion is happening inside a specific token. It’s important that you pass context.pos as the fifth parameter to ensure the search always happens in the correct position.

The example below shows how completion of a listOfKeywords can be done.

For the example above, completeFromList() could be used instead if we added a completion context that defined the path property. See JSContext.path for what the path property is supposed to represent.

prism-code-editor/autocomplete-icons.css adds 14 icons from VS Code. Below is a list showing all icons.

  • class
  • constant
  • enum
  • event
  • function
  • interface
  • keyword
  • namespace
  • parameter
  • property
  • snippet
  • text
  • unit
  • variable

The class pce-ac-icon- followed by the icon’s name is added to the icon element. For icons named variable you can use the selector .pce-ac-icon-variable to style them. You can also use the CSS variable --pce-ac-icon- followed by the icon’s name to set its color. The selector .pce-ac-icon can be used to style all icons. prism-code-editor/autocomplete-icons.css uses mask images to style the icons.