matchbrackets.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. function scanForBracket(cm, where, dir, style, config) {
  25. var maxScanLen = (config && config.maxScanLineLength) || 10000;
  26. var maxScanLines = (config && config.maxScanLines) || 500;
  27. var stack = [], re = /[(){}[\]]/;
  28. var lineEnd = dir > 0 ? Math.min(where.line + maxScanLines, cm.lastLine() + 1)
  29. : Math.max(cm.firstLine() - 1, where.line - maxScanLines);
  30. for (var lineNo = where.line; lineNo != lineEnd; lineNo += dir) {
  31. var line = cm.getLine(lineNo);
  32. if (!line) continue;
  33. var pos = dir > 0 ? 0 : line.length - 1, end = dir > 0 ? line.length : -1;
  34. if (line.length > maxScanLen) continue;
  35. if (lineNo == where.line) pos = where.ch - (dir < 0 ? 1 : 0);
  36. for (; pos != end; pos += dir) {
  37. var ch = line.charAt(pos);
  38. if (re.test(ch) && (style === undefined || cm.getTokenTypeAt(Pos(lineNo, pos + 1)) == style)) {
  39. var match = matching[ch];
  40. if ((match.charAt(1) == ">") == (dir > 0)) stack.push(ch);
  41. else if (!stack.length) return {pos: Pos(lineNo, pos), ch: ch};
  42. else stack.pop();
  43. }
  44. }
  45. }
  46. }
  47. function matchBrackets(cm, autoclear, config) {
  48. // Disable brace matching in long lines, since it'll cause hugely slow updates
  49. var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000;
  50. var marks = [], ranges = cm.listSelections();
  51. for (var i = 0; i < ranges.length; i++) {
  52. var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, false, config);
  53. if (match && cm.getLine(match.from.line).length <= maxHighlightLen &&
  54. match.to && cm.getLine(match.to.line).length <= maxHighlightLen) {
  55. var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";
  56. marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch + 1), {className: style}));
  57. if (match.to)
  58. marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1), {className: style}));
  59. }
  60. }
  61. if (marks.length) {
  62. // Kludge to work around the IE bug from issue #1193, where text
  63. // input stops going to the textare whever this fires.
  64. if (ie_lt8 && cm.state.focused) cm.display.input.focus();
  65. var clear = function() {
  66. cm.operation(function() {
  67. for (var i = 0; i < marks.length; i++) marks[i].clear();
  68. });
  69. };
  70. if (autoclear) setTimeout(clear, 800);
  71. else return clear;
  72. }
  73. }
  74. var currentlyHighlighted = null;
  75. function doMatchBrackets(cm) {
  76. cm.operation(function() {
  77. if (currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted = null;}
  78. currentlyHighlighted = matchBrackets(cm, false, cm.state.matchBrackets);
  79. });
  80. }
  81. CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) {
  82. if (old && old != CodeMirror.Init)
  83. cm.off("cursorActivity", doMatchBrackets);
  84. if (val) {
  85. cm.state.matchBrackets = typeof val == "object" ? val : {};
  86. cm.on("cursorActivity", doMatchBrackets);
  87. }
  88. });
  89. CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});
  90. CodeMirror.defineExtension("findMatchingBracket", function(pos, strict){
  91. return findMatchingBracket(this, pos, strict);
  92. });
  93. CodeMirror.defineExtension("scanForBracket", function(pos, dir, style){
  94. return scanForBracket(this, pos, dir, style);
  95. });
  96. });