1
0

matchbrackets.js 4.8 KB

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