match-highlighter.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 occurences
  19. // will be highlighted on the scrollbar via the matchesonscrollbar addon.
  20. (function(mod) {
  21. if (typeof exports == "object" && typeof module == "object") // CommonJS
  22. mod(require("../../lib/codemirror"), require("./matchesonscrollbar"));
  23. else if (typeof define == "function" && define.amd) // AMD
  24. define(["../../lib/codemirror", "./matchesonscrollbar"], mod);
  25. else // Plain browser env
  26. mod(CodeMirror);
  27. })(function(CodeMirror) {
  28. "use strict";
  29. var defaults = {
  30. style: "matchhighlight",
  31. minChars: 2,
  32. delay: 100,
  33. wordsOnly: false,
  34. annotateScrollbar: false,
  35. showToken: false,
  36. trim: true
  37. }
  38. function State(options) {
  39. this.options = {}
  40. for (var name in defaults)
  41. this.options[name] = (options && options.hasOwnProperty(name) ? options : defaults)[name]
  42. this.overlay = this.timeout = null;
  43. this.matchesonscroll = null;
  44. this.active = false;
  45. }
  46. CodeMirror.defineOption("highlightSelectionMatches", false, function(cm, val, old) {
  47. if (old && old != CodeMirror.Init) {
  48. removeOverlay(cm);
  49. clearTimeout(cm.state.matchHighlighter.timeout);
  50. cm.state.matchHighlighter = null;
  51. cm.off("cursorActivity", cursorActivity);
  52. cm.off("focus", onFocus)
  53. }
  54. if (val) {
  55. var state = cm.state.matchHighlighter = new State(val);
  56. if (cm.hasFocus()) {
  57. state.active = true
  58. highlightMatches(cm)
  59. } else {
  60. cm.on("focus", onFocus)
  61. }
  62. cm.on("cursorActivity", cursorActivity);
  63. }
  64. });
  65. function cursorActivity(cm) {
  66. var state = cm.state.matchHighlighter;
  67. if (state.active || cm.hasFocus()) scheduleHighlight(cm, state)
  68. }
  69. function onFocus(cm) {
  70. var state = cm.state.matchHighlighter
  71. if (!state.active) {
  72. state.active = true
  73. scheduleHighlight(cm, state)
  74. }
  75. }
  76. function scheduleHighlight(cm, state) {
  77. clearTimeout(state.timeout);
  78. state.timeout = setTimeout(function() {highlightMatches(cm);}, state.options.delay);
  79. }
  80. function addOverlay(cm, query, hasBoundary, style) {
  81. var state = cm.state.matchHighlighter;
  82. cm.addOverlay(state.overlay = makeOverlay(query, hasBoundary, style));
  83. if (state.options.annotateScrollbar && cm.showMatchesOnScrollbar) {
  84. var searchFor = hasBoundary ? new RegExp("\\b" + query.replace(/[\\\[.+*?(){|^$]/g, "\\$&") + "\\b") : query;
  85. state.matchesonscroll = cm.showMatchesOnScrollbar(searchFor, false,
  86. {className: "CodeMirror-selection-highlight-scrollbar"});
  87. }
  88. }
  89. function removeOverlay(cm) {
  90. var state = cm.state.matchHighlighter;
  91. if (state.overlay) {
  92. cm.removeOverlay(state.overlay);
  93. state.overlay = null;
  94. if (state.matchesonscroll) {
  95. state.matchesonscroll.clear();
  96. state.matchesonscroll = null;
  97. }
  98. }
  99. }
  100. function highlightMatches(cm) {
  101. cm.operation(function() {
  102. var state = cm.state.matchHighlighter;
  103. removeOverlay(cm);
  104. if (!cm.somethingSelected() && state.options.showToken) {
  105. var re = state.options.showToken === true ? /[\w$]/ : state.options.showToken;
  106. var cur = cm.getCursor(), line = cm.getLine(cur.line), start = cur.ch, end = start;
  107. while (start && re.test(line.charAt(start - 1))) --start;
  108. while (end < line.length && re.test(line.charAt(end))) ++end;
  109. if (start < end)
  110. addOverlay(cm, line.slice(start, end), re, state.options.style);
  111. return;
  112. }
  113. var from = cm.getCursor("from"), to = cm.getCursor("to");
  114. if (from.line != to.line) return;
  115. if (state.options.wordsOnly && !isWord(cm, from, to)) return;
  116. var selection = cm.getRange(from, to)
  117. if (state.options.trim) selection = selection.replace(/^\s+|\s+$/g, "")
  118. if (selection.length >= state.options.minChars)
  119. addOverlay(cm, selection, false, state.options.style);
  120. });
  121. }
  122. function isWord(cm, from, to) {
  123. var str = cm.getRange(from, to);
  124. if (str.match(/^\w+$/) !== null) {
  125. if (from.ch > 0) {
  126. var pos = {line: from.line, ch: from.ch - 1};
  127. var chr = cm.getRange(pos, from);
  128. if (chr.match(/\W/) === null) return false;
  129. }
  130. if (to.ch < cm.getLine(from.line).length) {
  131. var pos = {line: to.line, ch: to.ch + 1};
  132. var chr = cm.getRange(to, pos);
  133. if (chr.match(/\W/) === null) return false;
  134. }
  135. return true;
  136. } else return false;
  137. }
  138. function boundariesAround(stream, re) {
  139. return (!stream.start || !re.test(stream.string.charAt(stream.start - 1))) &&
  140. (stream.pos == stream.string.length || !re.test(stream.string.charAt(stream.pos)));
  141. }
  142. function makeOverlay(query, hasBoundary, style) {
  143. return {token: function(stream) {
  144. if (stream.match(query) &&
  145. (!hasBoundary || boundariesAround(stream, hasBoundary)))
  146. return style;
  147. stream.next();
  148. stream.skipTo(query.charAt(0)) || stream.skipToEnd();
  149. }};
  150. }
  151. });