matchbrackets.js 7.2 KB

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