Monday, May 20, 2024
117
rated 0 times [  124] [ 7]  / answers: 1 / hits: 15397  / 10 Years ago, sun, september 28, 2014, 12:00:00

TL;DR



I am trying to manually trigger ace editor snippets through a function call, rather than the conventional approach (keyboard keys).



Explanation



I need a function that takes in the editor and a snippet string as the parameters, and adds that snippet to the editor. function addSnippet(editor, snippet).



Ace editor supports TextMate-ish snippets.



if (${1:condition_name}) {
${2:body}
}


So when we call this function, it should add the snippet, highlight the snippet markers and select the first one. After filling the first one and hitting tab, the editor should move to the next snippet marker. Just like in the Kitchen Sink example (but I want to add/trigger snippets via a function call instead).



I tried hacking my way through and made this function. But it's messy and incomplete (doesn't support markers and tab presses). Is there any native method for this? I've seen a few examples using snippetManager, but they use keyboard triggers, not manual functions.



Any help regarding this issue would be appreciated.
Thanks.


More From » code-snippets

 Answers
59

After hours of hacks and research, I finally came across the insertSnippet function of snippetManager in ext-language_tools.js, it works this way:



var snippetManager = ace.require(ace/snippets).snippetManager;
snippetManager.insertSnippet(editor, snippet);


Pretty easy actually, couldn't find it earlier due to lack of documentation.


[#69307] Thursday, September 25, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isaacvalentinn

Total Points: 325
Total Questions: 120
Total Answers: 131

Location: North Korea
Member since Tue, Jun 16, 2020
4 Years ago
isaacvalentinn questions
Mon, Jan 18, 21, 00:00, 3 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Wed, Sep 23, 20, 00:00, 4 Years ago
;