csharp.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. define(function(require, exports, module) {
  31. "use strict";
  32. var oop = require("../../lib/oop");
  33. var Range = require("../../range").Range;
  34. var CFoldMode = require("./cstyle").FoldMode;
  35. var FoldMode = exports.FoldMode = function(commentRegex) {
  36. if (commentRegex) {
  37. this.foldingStartMarker = new RegExp(
  38. this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
  39. );
  40. this.foldingStopMarker = new RegExp(
  41. this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
  42. );
  43. }
  44. };
  45. oop.inherits(FoldMode, CFoldMode);
  46. (function() {
  47. this.usingRe = /^\s*using \S/;
  48. this.getFoldWidgetRangeBase = this.getFoldWidgetRange;
  49. this.getFoldWidgetBase = this.getFoldWidget;
  50. this.getFoldWidget = function(session, foldStyle, row) {
  51. var fw = this.getFoldWidgetBase(session, foldStyle, row);
  52. if (!fw) {
  53. var line = session.getLine(row);
  54. if (/^\s*#region\b/.test(line))
  55. return "start";
  56. var usingRe = this.usingRe;
  57. if (usingRe.test(line)) {
  58. var prev = session.getLine(row - 1);
  59. var next = session.getLine(row + 1);
  60. if (!usingRe.test(prev) && usingRe.test(next))
  61. return "start"
  62. }
  63. }
  64. return fw;
  65. };
  66. this.getFoldWidgetRange = function(session, foldStyle, row) {
  67. var range = this.getFoldWidgetRangeBase(session, foldStyle, row);
  68. if (range)
  69. return range;
  70. var line = session.getLine(row);
  71. if (this.usingRe.test(line))
  72. return this.getUsingStatementBlock(session, line, row);
  73. if (/^\s*#region\b/.test(line))
  74. return this.getRegionBlock(session, line, row);
  75. };
  76. this.getUsingStatementBlock = function(session, line, row) {
  77. var startColumn = line.match(this.usingRe)[0].length - 1;
  78. var maxRow = session.getLength();
  79. var startRow = row;
  80. var endRow = row;
  81. while (++row < maxRow) {
  82. line = session.getLine(row);
  83. if (/^\s*$/.test(line))
  84. continue;
  85. if (!this.usingRe.test(line))
  86. break;
  87. endRow = row;
  88. }
  89. if (endRow > startRow) {
  90. var endColumn = session.getLine(endRow).length;
  91. return new Range(startRow, startColumn, endRow, endColumn);
  92. }
  93. };
  94. this.getRegionBlock = function(session, line, row) {
  95. var startColumn = line.search(/\s*$/);
  96. var maxRow = session.getLength();
  97. var startRow = row;
  98. var re = /^\s*#(end)?region\b/;
  99. var depth = 1;
  100. while (++row < maxRow) {
  101. line = session.getLine(row);
  102. var m = re.exec(line);
  103. if (!m)
  104. continue;
  105. if (m[1])
  106. depth--;
  107. else
  108. depth++;
  109. if (!depth)
  110. break;
  111. }
  112. var endRow = row;
  113. if (endRow > startRow) {
  114. return new Range(startRow, startColumn, endRow, line.length);
  115. }
  116. };
  117. }).call(FoldMode.prototype);
  118. });