match-highlighter.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Highlighting text that matches the selection
  2. //
  3. // Defines an option highlightSelectionMatches, which, when enabled,
  4. // will style strings that match the selection throughout the
  5. // document.
  6. //
  7. // The option can be set to true to simply enable it, or to a
  8. // {minChars, style, showToken} object to explicitly configure it.
  9. // minChars is the minimum amount of characters that should be
  10. // selected for the behavior to occur, and style is the token style to
  11. // apply to the matches. This will be prefixed by "cm-" to create an
  12. // actual CSS class name. showToken, when enabled, will cause the
  13. // current token to be highlighted when nothing is selected.
  14. (function() {
  15. var DEFAULT_MIN_CHARS = 2;
  16. var DEFAULT_TOKEN_STYLE = "matchhighlight";
  17. var DEFAULT_DELAY = 100;
  18. function State(options) {
  19. if (typeof options == "object") {
  20. this.minChars = options.minChars;
  21. this.style = options.style;
  22. this.showToken = options.showToken;
  23. this.delay = options.delay;
  24. }
  25. if (this.style == null) this.style = DEFAULT_TOKEN_STYLE;
  26. if (this.minChars == null) this.minChars = DEFAULT_MIN_CHARS;
  27. if (this.delay == null) this.delay = DEFAULT_DELAY;
  28. this.overlay = this.timeout = null;
  29. }
  30. CodeMirror.defineOption("highlightSelectionMatches", false, function(cm, val, old) {
  31. if (old && old != CodeMirror.Init) {
  32. var over = cm.state.matchHighlighter.overlay;
  33. if (over) cm.removeOverlay(over);
  34. clearTimeout(cm.state.matchHighlighter.timeout);
  35. cm.state.matchHighlighter = null;
  36. cm.off("cursorActivity", cursorActivity);
  37. }
  38. if (val) {
  39. cm.state.matchHighlighter = new State(val);
  40. highlightMatches(cm);
  41. cm.on("cursorActivity", cursorActivity);
  42. }
  43. });
  44. function cursorActivity(cm) {
  45. var state = cm.state.matchHighlighter;
  46. clearTimeout(state.timeout);
  47. state.timeout = setTimeout(function() {highlightMatches(cm);}, state.delay);
  48. }
  49. function highlightMatches(cm) {
  50. cm.operation(function() {
  51. var state = cm.state.matchHighlighter;
  52. if (state.overlay) {
  53. cm.removeOverlay(state.overlay);
  54. state.overlay = null;
  55. }
  56. if (!cm.somethingSelected() && state.showToken) {
  57. var re = state.showToken === true ? /[\w$]/ : state.showToken;
  58. var cur = cm.getCursor(), line = cm.getLine(cur.line), start = cur.ch, end = start;
  59. while (start && re.test(line.charAt(start - 1))) --start;
  60. while (end < line.length && re.test(line.charAt(end))) ++end;
  61. if (start < end)
  62. cm.addOverlay(state.overlay = makeOverlay(line.slice(start, end), re, state.style));
  63. return;
  64. }
  65. if (cm.getCursor("head").line != cm.getCursor("anchor").line) return;
  66. var selection = cm.getSelection().replace(/^\s+|\s+$/g, "");
  67. if (selection.length >= state.minChars)
  68. cm.addOverlay(state.overlay = makeOverlay(selection, false, state.style));
  69. });
  70. }
  71. function boundariesAround(stream, re) {
  72. return (!stream.start || !re.test(stream.string.charAt(stream.start - 1))) &&
  73. (stream.pos == stream.string.length || !re.test(stream.string.charAt(stream.pos)));
  74. }
  75. function makeOverlay(query, hasBoundary, style) {
  76. return {token: function(stream) {
  77. if (stream.match(query) &&
  78. (!hasBoundary || boundariesAround(stream, hasBoundary)))
  79. return style;
  80. stream.next();
  81. stream.skipTo(query.charAt(0)) || stream.skipToEnd();
  82. }};
  83. }
  84. })();