|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 - present Adobe Systems Incorporated. All rights reserved. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | + * copy of this software and associated documentation files (the "Software"), |
| 6 | + * to deal in the Software without restriction, including without limitation |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | + * Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | + * DEALINGS IN THE SOFTWARE. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * Fold range finder for handlebars/mustache template type files. |
| 26 | + * @author Patrick Oladimeji |
| 27 | + * @date 14/08/2016 22:04:21 |
| 28 | + */ |
| 29 | + |
| 30 | +define(function (require, exports, module) { |
| 31 | + "use strict"; |
| 32 | + var CodeMirror = brackets.getModule("thirdparty/CodeMirror/lib/codemirror"), |
| 33 | + _ = brackets.getModule("thirdparty/lodash"), |
| 34 | + StringUtils = brackets.getModule("utils/StringUtils"); |
| 35 | + |
| 36 | + /** |
| 37 | + * Utility function for scanning the text in a document until a certain condition is met |
| 38 | + * @param {object} cm The code mirror object representing the document |
| 39 | + * @param {string} startCh The start character position for the scan operation |
| 40 | + * @param {number} startLine The start line position for the scan operation |
| 41 | + * @param {function (string): boolean} condition A predicate function that takes in the text seen so far and returns true if the scanning process should be halted |
| 42 | + * @returns {{from:CodeMirror.Pos, to: CodeMirror.Pos, string: string}} An object representing the range of text scanned. |
| 43 | + */ |
| 44 | + function scanTextUntil(cm, startCh, startLine, condition) { |
| 45 | + var line = cm.getLine(startLine), |
| 46 | + seen = "", |
| 47 | + characterIndex = startCh, |
| 48 | + currentLine = startLine, |
| 49 | + range; |
| 50 | + while (currentLine <= cm.lastLine()) { |
| 51 | + if (line.length === 0) { |
| 52 | + characterIndex = 0; |
| 53 | + line = cm.getLine(++currentLine); |
| 54 | + } else { |
| 55 | + seen = seen.concat(line[characterIndex] || ""); |
| 56 | + if (condition(seen)) { |
| 57 | + range = { |
| 58 | + from: {ch: startCh, line: startLine}, |
| 59 | + to: {ch: characterIndex, line: currentLine}, |
| 60 | + string: seen |
| 61 | + }; |
| 62 | + return range; |
| 63 | + } else if (characterIndex >= line.length) { |
| 64 | + seen = seen.concat(cm.lineSeparator()); |
| 65 | + if (condition(seen)) { |
| 66 | + range = { |
| 67 | + from: {ch: startCh, line: startLine}, |
| 68 | + to: {ch: characterIndex, line: currentLine}, |
| 69 | + string: seen |
| 70 | + }; |
| 71 | + return range; |
| 72 | + } |
| 73 | + characterIndex = 0; |
| 74 | + line = cm.getLine(++currentLine); |
| 75 | + } else { |
| 76 | + ++characterIndex; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Utility function used to detect the end of a helper name when scanning a series of text. |
| 84 | + * The end of a helper name is signalled by a space character or the `}` |
| 85 | + * @param {string} seen The string seen so far |
| 86 | + * @returns {boolean} True when the end of a helper name has been detected. |
| 87 | + */ |
| 88 | + function endHelperName(seen) { |
| 89 | + return (/\s$/).test(seen) || StringUtils.endsWith(seen, "}"); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns a predicate function that returns true when a specific character is found |
| 94 | + * @param {string} character the character to use in the match function |
| 95 | + * @returns {function} A function that checks if the last character of the parameter string matches the parameter character |
| 96 | + */ |
| 97 | + function readUntil(character) { |
| 98 | + return function (seen) { |
| 99 | + return seen[seen.length - 1] === character; |
| 100 | + }; |
| 101 | + } |
| 102 | + |
| 103 | + function getRange(cm, start) { |
| 104 | + var currentLine = start.line, |
| 105 | + text = cm.getLine(currentLine) || "", |
| 106 | + i = 0, |
| 107 | + tagStack = [], |
| 108 | + braceStack = [], |
| 109 | + found, |
| 110 | + openTag, |
| 111 | + openPos, |
| 112 | + currentCharacter, |
| 113 | + openTagIndex = text.indexOf("{{"), |
| 114 | + range; |
| 115 | + |
| 116 | + if (openTagIndex < 0 || text[openTagIndex + 2] === "/") { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + found = scanTextUntil(cm, openTagIndex + 2, currentLine, endHelperName); |
| 121 | + if (!found) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + openPos = { |
| 126 | + from: {line: currentLine, ch: openTagIndex}, |
| 127 | + to: found.to |
| 128 | + }; |
| 129 | + openTag = found.string.substring(0, found.string.length - 1); |
| 130 | + if (openTag[0] === "#" || openTag[0] === "~" || openTag[0] === "^") { |
| 131 | + found = scanTextUntil(cm, openPos.to.ch, openPos.to.line, function (seen) { |
| 132 | + return seen.length > 1 && seen.substr(-2) === "}}"; |
| 133 | + }); |
| 134 | + if (found) { |
| 135 | + openPos.to = {line: found.to.line, ch: found.to.ch + 1}; |
| 136 | + } |
| 137 | + tagStack.push(openTag.substr(1)); |
| 138 | + } else { |
| 139 | + braceStack.push("{{"); |
| 140 | + } |
| 141 | + |
| 142 | + i = found.to.ch; |
| 143 | + currentLine = found.to.line; |
| 144 | + |
| 145 | + while (currentLine <= cm.lastLine()) { |
| 146 | + text = cm.getLine(currentLine); |
| 147 | + currentCharacter = (text && text[i]) || ""; |
| 148 | + switch (currentCharacter) { |
| 149 | + case "{": |
| 150 | + if (text[i + 1] === "{") { |
| 151 | + found = scanTextUntil(cm, i + 2, currentLine, endHelperName); |
| 152 | + if (found) { |
| 153 | + var tag = found.string.substring(0, found.string.length - 1); |
| 154 | + if (tag[0] === "#" || tag[0] === "~" || tag[0] === "^") { |
| 155 | + tagStack.push(tag.substr(1)); |
| 156 | + } else if (tag[0] === "/" && |
| 157 | + (_.last(tagStack) === tag.substr(1) || _.last(tagStack) === "*" + tag.substr(1))) { |
| 158 | + tagStack.pop(); |
| 159 | + if (tagStack.length === 0 && braceStack.length === 0) { |
| 160 | + range = { |
| 161 | + from: openPos.to, |
| 162 | + to: {ch: i, line: currentLine} |
| 163 | + }; |
| 164 | + return range; |
| 165 | + } |
| 166 | + } else { |
| 167 | + braceStack.push("{{"); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + break; |
| 172 | + case "}": |
| 173 | + if (text[i + 1] === "}") { |
| 174 | + braceStack.pop(); |
| 175 | + if (braceStack.length === 0 && tagStack.length === 0) { |
| 176 | + range = { |
| 177 | + from: openPos.to, |
| 178 | + to: {ch: i, line: currentLine} |
| 179 | + }; |
| 180 | + return range; |
| 181 | + } |
| 182 | + } |
| 183 | + break; |
| 184 | + case "\"": |
| 185 | + case "'": |
| 186 | + found = scanTextUntil(cm, i + 1, currentLine, readUntil(text[i])); |
| 187 | + if (found) { |
| 188 | + i = found.to.ch; |
| 189 | + currentLine = found.to.line; |
| 190 | + } |
| 191 | + break; |
| 192 | + default: |
| 193 | + break; |
| 194 | + } |
| 195 | + |
| 196 | + ++i; |
| 197 | + if (i >= text.length) { |
| 198 | + ++currentLine; |
| 199 | + i = 0; |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + module.exports = getRange; |
| 205 | +}); |
0 commit comments