javascriptace-editor

How do you unregister snippets in Ace Editor?


I can register the snippets...

var editor = ace.edit("editor");
var snippetManager = ace.require("ace/snippets").snippetManager;
var snippetText = "snippet vfx\n\t<visalforce>\n\t\t$1\n\t</visalforce>";
var snippets = snippetManager.parseSnippetFile(snippetText);
snippetManager.register(snippets);

editor.setOptions({
    enableBasicAutocompletion: true,
    enableSnippets: false
});

But, the unregister just does not work and there is almost zero documentation or samples on the inter-webs...but I keep trying variations of this...

snippetText = "snippet vfx\n\t<visalforce>\n\t\t$1\n\t</visalforce>";
snippets = snippetManager.parseSnippetFile(snippetText);
snippetManager.unregister(snippets);

And it does not work. The snippets are still available in the editor.


Solution

  • It works if you pass the same object for insert and remove, see this example:

    <script src="https://cdn.jsdelivr.net/npm/ace-builds/src-noconflict/ace.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/ace-builds/src-noconflict/ext-language_tools.js"></script>
    <script>
      var editor = ace.edit();
      editor.container.style.height = "100px";
      document.body.append(editor.container);
      var snippetManager = ace.require("ace/snippets").snippetManager;
      var snippetText = "snippet vfx\n\t<visalforce>\n\t\t$1\n\t</visalforce>";
      var snippets = snippetManager.parseSnippetFile(snippetText);
      snippetManager.register(snippets);
    
      editor.setOptions({
        enableBasicAutocompletion: true,
        enableLiveAutocompletion: true,
        enableSnippets: true
      });
      
      function removeSnippets() {
        snippetManager.unregister(snippets);
      }
      function addSnippets() {
        snippetManager.register(snippets);
      }
    </script>
    
    <button onclick=removeSnippets()>remove snippets</button>
    <button onclick=addSnippets()>add snippets</button>