move a few things away in to-be-integrated/
[myslice.git] / to-be-integrated / third-party / codemirror-3.15 / addon / edit / matchtags.js
1 (function() {
2   "use strict";
3
4   CodeMirror.defineOption("matchTags", false, function(cm, val, old) {
5     if (old && old != CodeMirror.Init) {
6       cm.off("cursorActivity", doMatchTags);
7       cm.off("viewportChange", maybeUpdateMatch);
8       clear(cm);
9     }
10     if (val) {
11       cm.on("cursorActivity", doMatchTags);
12       cm.on("viewportChange", maybeUpdateMatch);
13       doMatchTags(cm);
14     }
15   });
16
17   function clear(cm) {
18     if (cm.state.matchedTag) {
19       cm.state.matchedTag.clear();
20       cm.state.matchedTag = null;
21     }
22   }
23
24   function doMatchTags(cm) {
25     cm.state.failedTagMatch = false;
26     cm.operation(function() {
27       clear(cm);
28       var cur = cm.getCursor(), range = cm.getViewport();
29       range.from = Math.min(range.from, cur.line); range.to = Math.max(cur.line + 1, range.to);
30       var match = CodeMirror.findMatchingTag(cm, cur, range);
31       if (!match) return;
32       var other = match.at == "close" ? match.open : match.close;
33       if (other)
34         cm.state.matchedTag = cm.markText(other.from, other.to, {className: "CodeMirror-matchingtag"});
35       else
36         cm.state.failedTagMatch = true;
37     });
38   }
39
40   function maybeUpdateMatch(cm) {
41     if (cm.state.failedTagMatch) doMatchTags(cm);
42   }
43
44   CodeMirror.commands.toMatchingTag = function(cm) {
45     var found = CodeMirror.findMatchingTag(cm, cm.getCursor());
46     if (found) {
47       var other = found.at == "close" ? found.open : found.close;
48       if (other) cm.setSelection(other.to, other.from);
49     }
50   };
51 })();