|
| 1 | +/* ***** BEGIN LICENSE BLOCK ***** |
| 2 | + * Distributed under the BSD license: |
| 3 | + * |
| 4 | + * Copyright (c) 2010, Ajax.org B.V. |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * * Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * * Neither the name of Ajax.org B.V. nor the |
| 15 | + * names of its contributors may be used to endorse or promote products |
| 16 | + * derived from this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | + * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY |
| 22 | + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + * |
| 29 | + * ***** END LICENSE BLOCK ***** */ |
| 30 | + |
| 31 | +define(function(require, exports, module) { |
| 32 | +"use strict"; |
| 33 | + |
| 34 | +var oop = require("../../lib/oop"); |
| 35 | +var Range = require("../../range").Range; |
| 36 | +var BaseFoldMode = require("./fold_mode").FoldMode; |
| 37 | + |
| 38 | +var FoldMode = exports.FoldMode = function(commentRegex) { |
| 39 | + if (commentRegex) { |
| 40 | + this.foldingStartMarker = new RegExp( |
| 41 | + this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start) |
| 42 | + ); |
| 43 | + this.foldingStopMarker = new RegExp( |
| 44 | + this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end) |
| 45 | + ); |
| 46 | + } |
| 47 | +}; |
| 48 | +oop.inherits(FoldMode, BaseFoldMode); |
| 49 | + |
| 50 | +(function() { |
| 51 | + |
| 52 | + this.foldingStartMarker = /(\()[^\)]*$/; |
| 53 | + this.foldingStopMarker = /^[^\(]*(\))/; |
| 54 | + |
| 55 | + this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/; |
| 56 | + this.tripleStarBlockCommentRe = /^\s*(\/\*\*\*).*\*\/\s*$/; |
| 57 | + this.startRegionRe = /^\s*(\/\*|\/\/|#)#?region\b/; |
| 58 | + |
| 59 | + //prevent naming conflict with any modes that inherit from cstyle and override this (like csharp) |
| 60 | + this._getFoldWidgetBase = this.getFoldWidget; |
| 61 | + |
| 62 | + /** |
| 63 | + * Gets fold widget with some non-standard extras: |
| 64 | + * |
| 65 | + * @example lineCommentRegionStart |
| 66 | + * //#region [optional description] |
| 67 | + * |
| 68 | + * @example blockCommentRegionStart |
| 69 | + * /*#region [optional description] *[/] |
| 70 | + * |
| 71 | + * @example tripleStarFoldingSection |
| 72 | + * /*** this folds even though 1 line because it has 3 stars ***[/] |
| 73 | + * |
| 74 | + * @note the pound symbol for region tags is optional |
| 75 | + */ |
| 76 | + this.getFoldWidget = function(session, foldStyle, row) { |
| 77 | + var line = session.getLine(row); |
| 78 | + |
| 79 | + if (this.singleLineBlockCommentRe.test(line)) { |
| 80 | + // No widget for single line block comment unless region or triple star |
| 81 | + if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line)) |
| 82 | + return ""; |
| 83 | + } |
| 84 | + |
| 85 | + var fw = this._getFoldWidgetBase(session, foldStyle, row); |
| 86 | + |
| 87 | + if (!fw && this.startRegionRe.test(line)) |
| 88 | + return "start"; // lineCommentRegionStart |
| 89 | + |
| 90 | + return fw; |
| 91 | + }; |
| 92 | + |
| 93 | + this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) { |
| 94 | + var line = session.getLine(row); |
| 95 | + |
| 96 | + if (this.startRegionRe.test(line)) |
| 97 | + return this.getCommentRegionBlock(session, line, row); |
| 98 | + |
| 99 | + var match = line.match(this.foldingStartMarker); |
| 100 | + if (match) { |
| 101 | + var i = match.index; |
| 102 | + |
| 103 | + if (match[1]) |
| 104 | + return this.openingBracketBlock(session, match[1], row, i); |
| 105 | + |
| 106 | + var range = session.getCommentFoldRange(row, i + match[0].length, 1); |
| 107 | + |
| 108 | + if (range && !range.isMultiLine()) { |
| 109 | + if (forceMultiline) { |
| 110 | + range = this.getSectionRange(session, row); |
| 111 | + } else if (foldStyle != "all") |
| 112 | + range = null; |
| 113 | + } |
| 114 | + |
| 115 | + return range; |
| 116 | + } |
| 117 | + |
| 118 | + if (foldStyle === "markbegin") |
| 119 | + return; |
| 120 | + |
| 121 | + var match = line.match(this.foldingStopMarker); |
| 122 | + if (match) { |
| 123 | + var i = match.index + match[0].length; |
| 124 | + |
| 125 | + if (match[1]) |
| 126 | + return this.closingBracketBlock(session, match[1], row, i); |
| 127 | + |
| 128 | + return session.getCommentFoldRange(row, i, -1); |
| 129 | + } |
| 130 | + }; |
| 131 | + |
| 132 | + this.getSectionRange = function(session, row) { |
| 133 | + var line = session.getLine(row); |
| 134 | + var startIndent = line.search(/\S/); |
| 135 | + var startRow = row; |
| 136 | + var startColumn = line.length; |
| 137 | + row = row + 1; |
| 138 | + var endRow = row; |
| 139 | + var maxRow = session.getLength(); |
| 140 | + while (++row < maxRow) { |
| 141 | + line = session.getLine(row); |
| 142 | + var indent = line.search(/\S/); |
| 143 | + if (indent === -1) |
| 144 | + continue; |
| 145 | + if (startIndent > indent) |
| 146 | + break; |
| 147 | + var subRange = this.getFoldWidgetRange(session, "all", row); |
| 148 | + |
| 149 | + if (subRange) { |
| 150 | + if (subRange.start.row <= startRow) { |
| 151 | + break; |
| 152 | + } else if (subRange.isMultiLine()) { |
| 153 | + row = subRange.end.row; |
| 154 | + } else if (startIndent == indent) { |
| 155 | + break; |
| 156 | + } |
| 157 | + } |
| 158 | + endRow = row; |
| 159 | + } |
| 160 | + |
| 161 | + return new Range(startRow, startColumn, endRow, session.getLine(endRow).length); |
| 162 | + }; |
| 163 | + |
| 164 | + /** |
| 165 | + * gets comment region block with end region assumed to be start of comment in any cstyle mode or SQL mode (--) which inherits from this. |
| 166 | + * There may optionally be a pound symbol before the region/endregion statement |
| 167 | + */ |
| 168 | + this.getCommentRegionBlock = function(session, line, row) { |
| 169 | + var startColumn = line.search(/\s*$/); |
| 170 | + var maxRow = session.getLength(); |
| 171 | + var startRow = row; |
| 172 | + |
| 173 | + var re = /^\s*(?:\/\*|\/\/|--)#?(end)?region\b/; |
| 174 | + var depth = 1; |
| 175 | + while (++row < maxRow) { |
| 176 | + line = session.getLine(row); |
| 177 | + var m = re.exec(line); |
| 178 | + if (!m) continue; |
| 179 | + if (m[1]) depth--; |
| 180 | + else depth++; |
| 181 | + |
| 182 | + if (!depth) break; |
| 183 | + } |
| 184 | + |
| 185 | + var endRow = row; |
| 186 | + if (endRow > startRow) { |
| 187 | + return new Range(startRow, startColumn, endRow, line.length); |
| 188 | + } |
| 189 | + }; |
| 190 | + |
| 191 | +}).call(FoldMode.prototype); |
| 192 | + |
| 193 | +}); |
0 commit comments