not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
5.2 KiB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. var ie_lt8 = /MSIE \d/.test(navigator.userAgent) &&
  12. (document.documentMode == null || document.documentMode < 8);
  13. var Pos = CodeMirror.Pos;
  14. var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"};
  15. function findMatchingBracket(cm, where, strict, config) {
  16. var line = cm.getLineHandle(where.line), pos = where.ch - 1;
  17. var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)];
  18. if (!match) return null;
  19. var dir = match.charAt(1) == ">" ? 1 : -1;
  20. if (strict && (dir > 0) != (pos == where.ch)) return null;
  21. var style = cm.getTokenTypeAt(Pos(where.line, pos + 1));
  22. var found = scanForBracket(cm, Pos(where.line, pos + (dir > 0 ? 1 : 0)), dir, style || null, config);
  23. if (found == null) return null;
  24. return {from: Pos(where.line, pos), to: found && found.pos,
  25. match: found && found.ch == match.charAt(0), forward: dir > 0};
  26. }
  27. // bracketRegex is used to specify which type of bracket to scan
  28. // should be a regexp, e.g. /[[\]]/
  29. //
  30. // Note: If "where" is on an open bracket, then this bracket is ignored.
  31. //
  32. // Returns false when no bracket was found, null when it reached
  33. // maxScanLines and gave up
  34. function scanForBracket(cm, where, dir, style, config) {
  35. var maxScanLen = (config && config.maxScanLineLength) || 10000;
  36. var maxScanLines = (config && config.maxScanLines) || 1000;
  37. var stack = [];
  38. var re = config && config.bracketRegex ? config.bracketRegex : /[(){}[\]]/;
  39. var lineEnd = dir > 0 ? Math.min(where.line + maxScanLines, cm.lastLine() + 1)
  40. : Math.max(cm.firstLine() - 1, where.line - maxScanLines);
  41. for (var lineNo = where.line; lineNo != lineEnd; lineNo += dir) {
  42. var line = cm.getLine(lineNo);
  43. if (!line) continue;
  44. var pos = dir > 0 ? 0 : line.length - 1, end = dir > 0 ? line.length : -1;
  45. if (line.length > maxScanLen) continue;
  46. if (lineNo == where.line) pos = where.ch - (dir < 0 ? 1 : 0);
  47. for (; pos != end; pos += dir) {
  48. var ch = line.charAt(pos);
  49. if (re.test(ch) && (style === undefined || cm.getTokenTypeAt(Pos(lineNo, pos + 1)) == style)) {
  50. var match = matching[ch];
  51. if ((match.charAt(1) == ">") == (dir > 0)) stack.push(ch);
  52. else if (!stack.length) return {pos: Pos(lineNo, pos), ch: ch};
  53. else stack.pop();
  54. }
  55. }
  56. }
  57. return lineNo - dir == (dir > 0 ? cm.lastLine() : cm.firstLine()) ? false : null;
  58. }
  59. function matchBrackets(cm, autoclear, config) {
  60. // Disable brace matching in long lines, since it'll cause hugely slow updates
  61. var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000;
  62. var marks = [], ranges = cm.listSelections();
  63. for (var i = 0; i < ranges.length; i++) {
  64. var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, false, config);
  65. if (match && cm.getLine(match.from.line).length <= maxHighlightLen) {
  66. var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";
  67. marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch + 1), {className: style}));
  68. if (match.to && cm.getLine(match.to.line).length <= maxHighlightLen)
  69. marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1), {className: style}));
  70. }
  71. }
  72. if (marks.length) {
  73. // Kludge to work around the IE bug from issue #1193, where text
  74. // input stops going to the textare whever this fires.
  75. if (ie_lt8 && cm.state.focused) cm.focus();
  76. var clear = function() {
  77. cm.operation(function() {
  78. for (var i = 0; i < marks.length; i++) marks[i].clear();
  79. });
  80. };
  81. if (autoclear) setTimeout(clear, 800);
  82. else return clear;
  83. }
  84. }
  85. var currentlyHighlighted = null;
  86. function doMatchBrackets(cm) {
  87. cm.operation(function() {
  88. if (currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted = null;}
  89. currentlyHighlighted = matchBrackets(cm, false, cm.state.matchBrackets);
  90. });
  91. }
  92. CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) {
  93. if (old && old != CodeMirror.Init) {
  94. cm.off("cursorActivity", doMatchBrackets);
  95. if (currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted = null;}
  96. }
  97. if (val) {
  98. cm.state.matchBrackets = typeof val == "object" ? val : {};
  99. cm.on("cursorActivity", doMatchBrackets);
  100. }
  101. });
  102. CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});
  103. CodeMirror.defineExtension("findMatchingBracket", function(pos, strict, config){
  104. return findMatchingBracket(this, pos, strict, config);
  105. });
  106. CodeMirror.defineExtension("scanForBracket", function(pos, dir, style, config){
  107. return scanForBracket(this, pos, dir, style, config);
  108. });
  109. });