matchbrackets.js 6.1 KB

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