initializing snippets dynamically

1,120 views
Skip to first unread message

costa

unread,
Jan 2, 2015, 7:15:16 PM1/2/15
to ace-d...@googlegroups.com
Hello:

Currently, from what I've seen, the js file containing the snippets for a mode, let's call it X, has to be placed under under the snippets subfolder, i.e. you'd have the snippets/X.js file and X.js contains something like this:

ace.define("ace/snippets/X",["require","exports","module"], function(require, exports, module) {
  "use strict";

exports.snippetText = "snippet cs1\n\
\tcase\n\
\t\twhen ${1:when} then ${2:then}\n\
\t\tdefault ${3:default}\n\
\tend\n\
snippet iif\n\
\tif (${1:cond}, ${2:then}, ${3:else})\n\
";


  exports.scope = "X";

});


In my case I need the code snippets to be specific to a user, i.e. each user can define his own snippets, and each user's snippets are saved to a database.

What are the API calls that I have to make to dynamically assign the code snippets?

I checked the kitchen-sink/demo.js file but it's doing some stuff that I don't fully understand. This is the code that I looked at:

env.editSnippets = function() {
    var sp = env.split;
    if (sp.getSplits() == 2) {
        sp.setSplits(1);
        return;
    }
    sp.setSplits(1);
    sp.setSplits(2);
    sp.setOrientation(sp.BESIDE);
    var editor = sp.$editors[1];
    var id = sp.$editors[0].session.$mode.$id || "";
    var m = snippetManager.files[id];
    if (!doclist["snippets/" + id]) {
        var text = m.snippetText;
        var s = doclist.initDoc(text, "", {});
        s.setMode("ace/mode/snippets");
        doclist["snippets/" + id] = s;
    }
    editor.on("blur", function() {
        m.snippetText = editor.getValue();
        snippetManager.unregister(m.snippets);
        m.snippets = snippetManager.parseSnippetFile(m.snippetText, m.scope);
        snippetManager.register(m.snippets);
    });
    sp.$editors[0].once("changeMode", function() {
        sp.setSplits(1);
    });
    editor.setSession(doclist["snippets/" + id], 1);
    editor.focus();

What I don't understand is the m object... I assume doclist keeps track of all the open documents. In my case I have only one ace editor object . The key calls seem to be register/unregister/parseSnippetFile...

So, let's say I can get the snippets string from the database and put it in a variable xsnippets, How do I assign this variable to the editor dynamically, assuming the editor is stored in the editor variable? I also want to do something similar to what's being done in the kitchen-sink demo and save the snippets to the database.

Thanks!

costa

unread,
Jan 2, 2015, 9:27:25 PM1/2/15
to ace-d...@googlegroups.com
This function works for me:

  assignEditorSnippets: function (editor, snippets)
  {
    var snippetManager = ace.require("ace/snippets").snippetManager;
    var m = snippetManager.files[editor.session.$mode.$id];
    m.snippetText = snippets;
    if (m.snippets)

      snippetManager.unregister(m.snippets);
    m.snippets = snippetManager.parseSnippetFile(m.snippetText, m.scope);
    snippetManager.register(m.snippets);
  },

sandhya pillai

unread,
Feb 22, 2020, 9:11:12 AM2/22/20
to Ajax.org Cloud9 Editor (Ace)
even i want to do something similar to this.
In the function that you have created -assign editor snippets what are you passing as parameters. It would be great if you can send the codes
Reply all
Reply to author
Forward
0 new messages