Search code examples
javascriptace-editor

ACE Editor adding Snippet with new line character


I am trying to add snippets for ACE Editor, I was able to achieve that by adding the snippets manually as follows.

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

# std::vector\n\
snippet vector\n\
std::vector<${1:T}> ${2};${3}\n\
# std::deque\n\
snippet deque\n\
std::deque<${1:T}> ${2};${3}\n\
...

Everything works well until I add a new line in a snippets, it then doesn't work since the editor is using the new line as a token to separate snippets.

#if\n\
snippet if\n\
if(${1:a}>${2:b}){ \n  } else { }\n\

It then only shows this and stops after the newline. I couldn't find a proper way to use the newline in the snippet.

if(${1:a}>${2:b}){

I also tried \n and got this in the editor instead of a newline.

\n


Solution

  • you need to indent every line inside the snippet with a tab character, like this:

    exports.snippetText =  "\
    snippet cl\n\
    \tclass ${1:$FILE_NAME} {\n\
    \t\t${2:contents}\n\
    \t}\n\
    "