You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
6.1 KiB

2 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/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") // 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((/\w/.test(query.charAt(0)) ? "\\b" : "") +
  85. query.replace(/[\\\[.+*?(){|^$]/g, "\\$&") +
  86. (/\w/.test(query.charAt(query.length - 1)) ? "\\b" : "")) : query;
  87. state.matchesonscroll = cm.showMatchesOnScrollbar(searchFor, false,
  88. {className: "CodeMirror-selection-highlight-scrollbar"});
  89. }
  90. }
  91. function removeOverlay(cm) {
  92. var state = cm.state.matchHighlighter;
  93. if (state.overlay) {
  94. cm.removeOverlay(state.overlay);
  95. state.overlay = null;
  96. if (state.matchesonscroll) {
  97. state.matchesonscroll.clear();
  98. state.matchesonscroll = null;
  99. }
  100. }
  101. }
  102. function highlightMatches(cm) {
  103. cm.operation(function() {
  104. var state = cm.state.matchHighlighter;
  105. removeOverlay(cm);
  106. if (!cm.somethingSelected() && state.options.showToken) {
  107. var re = state.options.showToken === true ? /[\w$]/ : state.options.showToken;
  108. var cur = cm.getCursor(), line = cm.getLine(cur.line), start = cur.ch, end = start;
  109. while (start && re.test(line.charAt(start - 1))) --start;
  110. while (end < line.length && re.test(line.charAt(end))) ++end;
  111. if (start < end)
  112. addOverlay(cm, line.slice(start, end), re, state.options.style);
  113. return;
  114. }
  115. var from = cm.getCursor("from"), to = cm.getCursor("to");
  116. if (from.line != to.line) return;
  117. if (state.options.wordsOnly && !isWord(cm, from, to)) return;
  118. var selection = cm.getRange(from, to)
  119. if (state.options.trim) selection = selection.replace(/^\s+|\s+$/g, "")
  120. if (selection.length >= state.options.minChars)
  121. addOverlay(cm, selection, false, state.options.style);
  122. });
  123. }
  124. function isWord(cm, from, to) {
  125. var str = cm.getRange(from, to);
  126. if (str.match(/^\w+$/) !== null) {
  127. if (from.ch > 0) {
  128. var pos = {line: from.line, ch: from.ch - 1};
  129. var chr = cm.getRange(pos, from);
  130. if (chr.match(/\W/) === null) return false;
  131. }
  132. if (to.ch < cm.getLine(from.line).length) {
  133. var pos = {line: to.line, ch: to.ch + 1};
  134. var chr = cm.getRange(to, pos);
  135. if (chr.match(/\W/) === null) return false;
  136. }
  137. return true;
  138. } else return false;
  139. }
  140. function boundariesAround(stream, re) {
  141. return (!stream.start || !re.test(stream.string.charAt(stream.start - 1))) &&
  142. (stream.pos == stream.string.length || !re.test(stream.string.charAt(stream.pos)));
  143. }
  144. function makeOverlay(query, hasBoundary, style) {
  145. return {token: function(stream) {
  146. if (stream.match(query) &&
  147. (!hasBoundary || boundariesAround(stream, hasBoundary)))
  148. return style;
  149. stream.next();
  150. stream.skipTo(query.charAt(0)) || stream.skipToEnd();
  151. }};
  152. }
  153. });