matchbrackets.js 6.4 KB

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