match-highlighter.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. // Highlighting text that matches the selection
  4. //
  5. // Defines an option highlightSelectionMatches, which, when enabled,
  6. // will style strings that match the selection throughout the
  7. // document.
  8. //
  9. // The option can be set to true to simply enable it, or to a
  10. // {minChars, style, wordsOnly, showToken, delay} object to explicitly
  11. // configure it. minChars is the minimum amount of characters that should be
  12. // selected for the behavior to occur, and style is the token style to
  13. // apply to the matches. This will be prefixed by "cm-" to create an
  14. // actual CSS class name. If wordsOnly is enabled, the matches will be
  15. // highlighted only if the selected text is a word. showToken, when enabled,
  16. // will cause the current token to be highlighted when nothing is selected.
  17. // delay is used to specify how much time to wait, in milliseconds, before
  18. // highlighting the matches. If annotateScrollbar is enabled, the occurrences
  19. // will be highlighted on the scrollbar via the matchesonscrollbar addon.
  20. ;(function (mod) {
  21. if (typeof exports == 'object' && typeof module == 'object')
  22. // CommonJS
  23. mod(require('../../lib/codemirror'), require('./matchesonscrollbar'))
  24. else if (typeof define == 'function' && define.amd)
  25. // AMD
  26. define(['../../lib/codemirror', './matchesonscrollbar'], mod)
  27. // Plain browser env
  28. else mod(CodeMirror)
  29. })(function (CodeMirror) {
  30. 'use strict'
  31. var defaults = {
  32. style: 'matchhighlight',
  33. minChars: 2,
  34. delay: 100,
  35. wordsOnly: false,
  36. annotateScrollbar: false,
  37. showToken: false,
  38. trim: true,
  39. }
  40. function State(options) {
  41. this.options = {}
  42. for (var name in defaults) this.options[name] = (options && options.hasOwnProperty(name) ? options : defaults)[name]
  43. this.overlay = this.timeout = null
  44. this.matchesonscroll = null
  45. this.active = false
  46. }
  47. CodeMirror.defineOption('highlightSelectionMatches', false, function (cm, val, old) {
  48. if (old && old != CodeMirror.Init) {
  49. removeOverlay(cm)
  50. clearTimeout(cm.state.matchHighlighter.timeout)
  51. cm.state.matchHighlighter = null
  52. cm.off('cursorActivity', cursorActivity)
  53. cm.off('focus', onFocus)
  54. }
  55. if (val) {
  56. var state = (cm.state.matchHighlighter = new State(val))
  57. if (cm.hasFocus()) {
  58. state.active = true
  59. highlightMatches(cm)
  60. } else {
  61. cm.on('focus', onFocus)
  62. }
  63. cm.on('cursorActivity', cursorActivity)
  64. }
  65. })
  66. function cursorActivity(cm) {
  67. var state = cm.state.matchHighlighter
  68. if (state.active || cm.hasFocus()) scheduleHighlight(cm, state)
  69. }
  70. function onFocus(cm) {
  71. var state = cm.state.matchHighlighter
  72. if (!state.active) {
  73. state.active = true
  74. scheduleHighlight(cm, state)
  75. }
  76. }
  77. function scheduleHighlight(cm, state) {
  78. clearTimeout(state.timeout)
  79. state.timeout = setTimeout(function () {
  80. highlightMatches(cm)
  81. }, state.options.delay)
  82. }
  83. function addOverlay(cm, query, hasBoundary, style) {
  84. var state = cm.state.matchHighlighter
  85. cm.addOverlay((state.overlay = makeOverlay(query, hasBoundary, style)))
  86. if (state.options.annotateScrollbar && cm.showMatchesOnScrollbar) {
  87. var searchFor = hasBoundary
  88. ? new RegExp((/\w/.test(query.charAt(0)) ? '\\b' : '') + query.replace(/[\\\[.+*?(){|^$]/g, '\\$&') + (/\w/.test(query.charAt(query.length - 1)) ? '\\b' : ''))
  89. : query
  90. state.matchesonscroll = cm.showMatchesOnScrollbar(searchFor, false, { className: 'CodeMirror-selection-highlight-scrollbar' })
  91. }
  92. }
  93. function removeOverlay(cm) {
  94. var state = cm.state.matchHighlighter
  95. if (state.overlay) {
  96. cm.removeOverlay(state.overlay)
  97. state.overlay = null
  98. if (state.matchesonscroll) {
  99. state.matchesonscroll.clear()
  100. state.matchesonscroll = null
  101. }
  102. }
  103. }
  104. function highlightMatches(cm) {
  105. cm.operation(function () {
  106. var state = cm.state.matchHighlighter
  107. removeOverlay(cm)
  108. if (!cm.somethingSelected() && state.options.showToken) {
  109. var re = state.options.showToken === true ? /[\w$]/ : state.options.showToken
  110. var cur = cm.getCursor(),
  111. line = cm.getLine(cur.line),
  112. start = cur.ch,
  113. end = start
  114. while (start && re.test(line.charAt(start - 1))) --start
  115. while (end < line.length && re.test(line.charAt(end))) ++end
  116. if (start < end) addOverlay(cm, line.slice(start, end), re, state.options.style)
  117. return
  118. }
  119. var from = cm.getCursor('from'),
  120. to = cm.getCursor('to')
  121. if (from.line != to.line) return
  122. if (state.options.wordsOnly && !isWord(cm, from, to)) return
  123. var selection = cm.getRange(from, to)
  124. if (state.options.trim) selection = selection.replace(/^\s+|\s+$/g, '')
  125. if (selection.length >= state.options.minChars) addOverlay(cm, selection, false, state.options.style)
  126. })
  127. }
  128. function isWord(cm, from, to) {
  129. var str = cm.getRange(from, to)
  130. if (str.match(/^\w+$/) !== null) {
  131. if (from.ch > 0) {
  132. var pos = { line: from.line, ch: from.ch - 1 }
  133. var chr = cm.getRange(pos, from)
  134. if (chr.match(/\W/) === null) return false
  135. }
  136. if (to.ch < cm.getLine(from.line).length) {
  137. var pos = { line: to.line, ch: to.ch + 1 }
  138. var chr = cm.getRange(to, pos)
  139. if (chr.match(/\W/) === null) return false
  140. }
  141. return true
  142. } else return false
  143. }
  144. function boundariesAround(stream, re) {
  145. return (!stream.start || !re.test(stream.string.charAt(stream.start - 1))) && (stream.pos == stream.string.length || !re.test(stream.string.charAt(stream.pos)))
  146. }
  147. function makeOverlay(query, hasBoundary, style) {
  148. return {
  149. token: function (stream) {
  150. if (stream.match(query) && (!hasBoundary || boundariesAround(stream, hasBoundary))) return style
  151. stream.next()
  152. stream.skipTo(query.charAt(0)) || stream.skipToEnd()
  153. },
  154. }
  155. }
  156. })