Repository navigation
Expand file tree
/
Copy pathhandlebarsFold.js
More file actions
1 lines (1 loc) · 2.62 KB
/
Copy pathhandlebarsFold.js
File metadata and controls
1 lines (1 loc) · 2.62 KB
1
define(function(require,exports,module){var CodeMirror=brackets.getModule("thirdparty/CodeMirror/lib/codemirror"),_=brackets.getModule("thirdparty/lodash"),StringUtils=brackets.getModule("utils/StringUtils");function scanTextUntil(cm,startCh,startLine,condition){for(var line=cm.getLine(startLine),seen="",characterIndex=startCh,currentLine=startLine,range;currentLine<=cm.lastLine();)if(0===line.length)characterIndex=0,line=cm.getLine(++currentLine);else{if(condition(seen=seen.concat(line[characterIndex]||"")))return range={from:{ch:startCh,line:startLine},to:{ch:characterIndex,line:currentLine},string:seen};if(characterIndex>=line.length){if(condition(seen=seen.concat(cm.lineSeparator())))return range={from:{ch:startCh,line:startLine},to:{ch:characterIndex,line:currentLine},string:seen};characterIndex=0,line=cm.getLine(++currentLine)}else++characterIndex}}function endHelperName(seen){return/\s$/.test(seen)||StringUtils.endsWith(seen,"}")}function readUntil(character){return function(seen){return seen[seen.length-1]===character}}function getRange(cm,start){var currentLine=start.line,text=cm.getLine(currentLine)||"",i=0,tagStack=[],braceStack=[],found,openTag,openPos,currentCharacter,openTagIndex=text.indexOf("{{"),range;if(!(openTagIndex<0||"/"===text[openTagIndex+2])&&(found=scanTextUntil(cm,openTagIndex+2,currentLine,endHelperName)))for(openPos={from:{line:currentLine,ch:openTagIndex},to:found.to},"#"===(openTag=found.string.substring(0,found.string.length-1))[0]||"~"===openTag[0]||"^"===openTag[0]?((found=scanTextUntil(cm,openPos.to.ch,openPos.to.line,function(seen){return seen.length>1&&"}}"===seen.substr(-2)}))&&(openPos.to={line:found.to.line,ch:found.to.ch+1}),tagStack.push(openTag.substr(1))):braceStack.push("{{"),i=found.to.ch,currentLine=found.to.line;currentLine<=cm.lastLine();){switch(currentCharacter=(text=cm.getLine(currentLine))&&text[i]||""){case"{":if("{"===text[i+1]&&(found=scanTextUntil(cm,i+2,currentLine,endHelperName))){var tag=found.string.substring(0,found.string.length-1);if("#"===tag[0]||"~"===tag[0]||"^"===tag[0])tagStack.push(tag.substr(1));else if("/"!==tag[0]||_.last(tagStack)!==tag.substr(1)&&_.last(tagStack)!=="*"+tag.substr(1))braceStack.push("{{");else if(tagStack.pop(),0===tagStack.length&&0===braceStack.length)return range={from:openPos.to,to:{ch:i,line:currentLine}}}break;case"}":if("}"===text[i+1]&&(braceStack.pop(),0===braceStack.length&&0===tagStack.length))return range={from:openPos.to,to:{ch:i,line:currentLine}};break;case'"':case"'":(found=scanTextUntil(cm,i+1,currentLine,readUntil(text[i])))&&(i=found.to.ch,currentLine=found.to.line)}++i>=text.length&&(++currentLine,i=0)}}module.exports=getRange});