Snippets outside of Autocomplete

The snippetCompletion in @codemirror/autocomplete works really well and has been super useful for a project I’m working on. Is there an example of inserting a snippet programmatically without it being a part of of autocomplete?

I’d like my users to be able to use the snippets through autocomplete and also by, say, clicking a toolbar button or selecting them from help documentation.

The docs for snippets should make it pretty obvious how to do this.

On closer look, this patch makes it more straightforward to call snippet functions outside of a completion context.

1 Like

Thank you, @marijn! This got me (I think) on the right track. I’m now doing something along these lines:

import { Completion, snippet } from "@codemirror/autocomplete";
import { EditorView } from "@codemirror/view";

function insertSnippet(text: string, view: EditorView) {
  const { dispatch, state } = view;
  const { to, from } = state.selection.ranges[0];
	{ dispatch, state },
	null as unknown as Completion, // remove as after the patch is published