Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function addSnippet (editor, snippet) {
- editor.focus();
- var cursor = editor.selection.getCursor();
- var pattern = '(\\$\\{[0-9]+\\:(.*)\\})';
- var regex = new RegExp(pattern, "ig");
- var lines = snippet.split("\n");
- var placeholderStarts = 0;
- var placeholderEnds = 0;
- var Range = ace.require("ace/range").Range;
- var firstSelection;
- for(var i in lines) {
- var line = lines[i];
- var match = regex.exec(line);
- if(match) {
- plaseholderStarts = match.index;
- placeholderEnds = match.index + match[2].length;
- line = line.replace(regex, match[2]);
- }
- editor.insert("\n");
- var thisCursor = editor.selection.getCursor();
- var lineInfo = editor.insert(line);
- if(match) {
- // add marker
- var thisRange = new Range(thisCursor.row, thisCursor.column + plaseholderStarts, thisCursor.row, thisCursor.column + placeholderEnds);
- if(!firstSelection) {
- firstSelection = thisRange;
- }
- }
- }
- if(firstSelection) {
- var sel = editor.getSelection();
- sel.setSelectionRange( firstSelection );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement