Monaco-editor: Trigger insert snippet programmatically

Created on 28 Jan 2017  路  6Comments  路  Source: microsoft/monaco-editor

Is it possible to trigger 'insert snippet' programmatically?

There is a class named InsertSnippetAction but it's never used in the code base.

What we are trying to call is something like this one:
editor.trigger('bla', 'editor.action.insertSnippet', { text: "function {{<function_name>}} ()" });

'editor.action.insertSnippet' is not available in editor.getActions().

api feature-request

Most helpful comment

This worked for me, using monaco-editor-core:
editor.getContribution("snippetController2") returns the SnippetController of the editor.
First set the cursor position, and then call the "insert" method of the snippet controller, first parameter being the template for your snippet in string form.

Still experimenting with this myself, hope it helps.

I would still like a more "official" way to do it though.

All 6 comments

@erenbaydemir did you find a way to achieve this?

Did you make it

Did anyone found a way to accomplish this? Inserting by reference/id would be enough for my purposes.

https://microsoft.github.io/monaco-editor/playground.html#extending-language-services-completion-provider-example

@Myqilixiang That's the way to trigger snippets in the editor, I'm looking for a way to do so programmatically. For instance running a snippet with a keyboard shortcut. Or am I missing something?

This worked for me, using monaco-editor-core:
editor.getContribution("snippetController2") returns the SnippetController of the editor.
First set the cursor position, and then call the "insert" method of the snippet controller, first parameter being the template for your snippet in string form.

Still experimenting with this myself, hope it helps.

I would still like a more "official" way to do it though.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aarinsmith picture aarinsmith  路  3Comments

akosyakov picture akosyakov  路  3Comments

Spongman picture Spongman  路  3Comments

inf9144 picture inf9144  路  3Comments

Kang-Jun-sik picture Kang-Jun-sik  路  3Comments