diff options
author | Eric Guzman | 2012-03-12 15:33:04 -0700 |
---|---|---|
committer | Eric Guzman | 2012-03-12 15:33:04 -0700 |
commit | 7e3161153b87b891875ac65368a19aed12909fa3 (patch) | |
tree | b80f48d711a9729fc39dbbdff28c4f0620e7302d /imports/codemirror/lib/util/match-highlighter.js | |
parent | 7a28932ba8a7517bbaaabe1f5edf678416aafc9c (diff) | |
parent | 69d90467865a1384725b2301901be2180c5a841f (diff) | |
download | ninja-7e3161153b87b891875ac65368a19aed12909fa3.tar.gz |
Merge branch 'refs/heads/master' into CSSPanelUpdates
Conflicts:
js/panels/PanelContainer/PanelContainer.reel/PanelContainer.js
Diffstat (limited to 'imports/codemirror/lib/util/match-highlighter.js')
-rw-r--r-- | imports/codemirror/lib/util/match-highlighter.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/imports/codemirror/lib/util/match-highlighter.js b/imports/codemirror/lib/util/match-highlighter.js new file mode 100644 index 00000000..b70cc4cf --- /dev/null +++ b/imports/codemirror/lib/util/match-highlighter.js | |||
@@ -0,0 +1,44 @@ | |||
1 | // Define match-highlighter commands. Depends on searchcursor.js | ||
2 | // Use by attaching the following function call to the onCursorActivity event: | ||
3 | //myCodeMirror.matchHighlight(minChars); | ||
4 | // And including a special span.CodeMirror-matchhighlight css class (also optionally a separate one for .CodeMirror-focused -- see demo matchhighlighter.html) | ||
5 | |||
6 | (function() { | ||
7 | var DEFAULT_MIN_CHARS = 2; | ||
8 | |||
9 | function MatchHighlightState() { | ||
10 | this.marked = []; | ||
11 | } | ||
12 | function getMatchHighlightState(cm) { | ||
13 | return cm._matchHighlightState || (cm._matchHighlightState = new MatchHighlightState()); | ||
14 | } | ||
15 | |||
16 | function clearMarks(cm) { | ||
17 | var state = getMatchHighlightState(cm); | ||
18 | for (var i = 0; i < state.marked.length; ++i) | ||
19 | state.marked[i].clear(); | ||
20 | state.marked = []; | ||
21 | } | ||
22 | |||
23 | function markDocument(cm, className, minChars) { | ||
24 | clearMarks(cm); | ||
25 | minChars = (typeof minChars !== 'undefined' ? minChars : DEFAULT_MIN_CHARS); | ||
26 | if (cm.somethingSelected() && cm.getSelection().length >= minChars) { | ||
27 | var state = getMatchHighlightState(cm); | ||
28 | var query = cm.getSelection(); | ||
29 | cm.operation(function() { | ||
30 | if (cm.lineCount() < 2000) { // This is too expensive on big documents. | ||
31 | for (var cursor = cm.getSearchCursor(query); cursor.findNext();) { | ||
32 | //Only apply matchhighlight to the matches other than the one actually selected | ||
33 | if (!(cursor.from().line === cm.getCursor(true).line && cursor.from().ch === cm.getCursor(true).ch)) | ||
34 | state.marked.push(cm.markText(cursor.from(), cursor.to(), className)); | ||
35 | } | ||
36 | } | ||
37 | }); | ||
38 | } | ||
39 | } | ||
40 | |||
41 | CodeMirror.defineExtension("matchHighlight", function(className, minChars) { | ||
42 | markDocument(this, className, minChars); | ||
43 | }); | ||
44 | })(); | ||