|
29 | 29 | })(function(CodeMirror) { |
30 | 30 | "use strict"; |
31 | 31 |
|
32 | | - var DEFAULT_MIN_CHARS = 2; |
33 | | - var DEFAULT_TOKEN_STYLE = "matchhighlight"; |
34 | | - var DEFAULT_DELAY = 100; |
35 | | - var DEFAULT_WORDS_ONLY = false; |
| 32 | + var defaults = { |
| 33 | + style: "matchhighlight", |
| 34 | + minChars: 2, |
| 35 | + delay: 100, |
| 36 | + wordsOnly: false, |
| 37 | + annotateScrollbar: false, |
| 38 | + showToken: false, |
| 39 | + trim: true |
| 40 | + } |
36 | 41 |
|
37 | 42 | function State(options) { |
38 | | - if (typeof options == "object") { |
39 | | - this.minChars = options.minChars; |
40 | | - this.style = options.style; |
41 | | - this.showToken = options.showToken; |
42 | | - this.delay = options.delay; |
43 | | - this.wordsOnly = options.wordsOnly; |
44 | | - this.annotateScrollbar = options.annotateScrollbar; |
45 | | - } |
46 | | - if (this.style == null) this.style = DEFAULT_TOKEN_STYLE; |
47 | | - if (this.minChars == null) this.minChars = DEFAULT_MIN_CHARS; |
48 | | - if (this.delay == null) this.delay = DEFAULT_DELAY; |
49 | | - if (this.wordsOnly == null) this.wordsOnly = DEFAULT_WORDS_ONLY; |
| 43 | + this.options = {} |
| 44 | + for (var name in defaults) |
| 45 | + this.options[name] = (options && options.hasOwnProperty(name) ? options : defaults)[name] |
50 | 46 | this.overlay = this.timeout = null; |
51 | 47 | this.matchesonscroll = null; |
52 | 48 | } |
|
68 | 64 | function cursorActivity(cm) { |
69 | 65 | var state = cm.state.matchHighlighter; |
70 | 66 | clearTimeout(state.timeout); |
71 | | - state.timeout = setTimeout(function() {highlightMatches(cm);}, state.delay); |
| 67 | + state.timeout = setTimeout(function() {highlightMatches(cm);}, state.options.delay); |
72 | 68 | } |
73 | 69 |
|
74 | 70 | function addOverlay(cm, query, hasBoundary, style) { |
75 | 71 | var state = cm.state.matchHighlighter; |
76 | 72 | cm.addOverlay(state.overlay = makeOverlay(query, hasBoundary, style)); |
77 | | - if (state.annotateScrollbar) { |
| 73 | + if (state.options.annotateScrollbar && cm.showMatchesOnScrollbar) { |
78 | 74 | var searchFor = hasBoundary ? new RegExp("\\b" + query + "\\b") : query; |
79 | 75 | state.matchesonscroll = cm.showMatchesOnScrollbar(searchFor, true, |
80 | 76 | {className: "CodeMirror-selection-highlight-scrollbar"}); |
|
86 | 82 | if (state.overlay) { |
87 | 83 | cm.removeOverlay(state.overlay); |
88 | 84 | state.overlay = null; |
89 | | - if (state.annotateScrollbar) { |
| 85 | + if (state.matchesonscroll) { |
90 | 86 | state.matchesonscroll.clear(); |
91 | 87 | state.matchesonscroll = null; |
92 | 88 | } |
|
97 | 93 | cm.operation(function() { |
98 | 94 | var state = cm.state.matchHighlighter; |
99 | 95 | removeOverlay(cm); |
100 | | - if (!cm.somethingSelected() && state.showToken) { |
101 | | - var re = state.showToken === true ? /[\w$]/ : state.showToken; |
| 96 | + if (!cm.somethingSelected() && state.options.showToken) { |
| 97 | + var re = state.options.showToken === true ? /[\w$]/ : state.options.showToken; |
102 | 98 | var cur = cm.getCursor(), line = cm.getLine(cur.line), start = cur.ch, end = start; |
103 | 99 | while (start && re.test(line.charAt(start - 1))) --start; |
104 | 100 | while (end < line.length && re.test(line.charAt(end))) ++end; |
105 | 101 | if (start < end) |
106 | | - addOverlay(cm, line.slice(start, end), re, state.style); |
| 102 | + addOverlay(cm, line.slice(start, end), re, state.options.style); |
107 | 103 | return; |
108 | 104 | } |
109 | 105 | var from = cm.getCursor("from"), to = cm.getCursor("to"); |
110 | 106 | if (from.line != to.line) return; |
111 | | - if (state.wordsOnly && !isWord(cm, from, to)) return; |
112 | | - var selection = cm.getRange(from, to).replace(/^\s+|\s+$/g, ""); |
113 | | - if (selection.length >= state.minChars) |
114 | | - addOverlay(cm, selection, false, state.style); |
| 107 | + if (state.options.wordsOnly && !isWord(cm, from, to)) return; |
| 108 | + var selection = cm.getRange(from, to) |
| 109 | + if (state.options.trim) selection = selection.replace(/^\s+|\s+$/g, "") |
| 110 | + if (selection.length >= state.options.minChars) |
| 111 | + addOverlay(cm, selection, false, state.options.style); |
115 | 112 | }); |
116 | 113 | } |
117 | 114 |
|
|
0 commit comments