Advertisement
Guest User

Untitled

a guest
Sep 28th, 2014
1,428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function addSnippet (editor, snippet) {
  2.  
  3.     editor.focus();
  4.     var cursor = editor.selection.getCursor();
  5.  
  6.     var pattern = '(\\$\\{[0-9]+\\:(.*)\\})';
  7.     var regex = new RegExp(pattern, "ig");
  8.  
  9.     var lines = snippet.split("\n");
  10.  
  11.     var placeholderStarts = 0;
  12.     var placeholderEnds = 0;
  13.  
  14.     var Range = ace.require("ace/range").Range;
  15.     var firstSelection;
  16.  
  17.     for(var i in lines) {
  18.         var line = lines[i];
  19.  
  20.         var match = regex.exec(line);
  21.  
  22.         if(match) {
  23.             plaseholderStarts = match.index;
  24.             placeholderEnds = match.index + match[2].length;
  25.  
  26.             line = line.replace(regex, match[2]);
  27.         }
  28.  
  29.         editor.insert("\n");
  30.         var thisCursor = editor.selection.getCursor();
  31.         var lineInfo = editor.insert(line);
  32.  
  33.         if(match) {
  34.             // add marker
  35.  
  36.             var thisRange = new Range(thisCursor.row, thisCursor.column + plaseholderStarts, thisCursor.row, thisCursor.column + placeholderEnds);
  37.  
  38.             if(!firstSelection) {
  39.                 firstSelection = thisRange;
  40.             }
  41.         }
  42.  
  43.     }
  44.  
  45.     if(firstSelection) {
  46.         var sel = editor.getSelection();
  47.         sel.setSelectionRange( firstSelection );
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement