Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / 3rdParty / codemirror / addon / edit / matchtags.js
1 (function(mod) {
2 if (typeof exports == "object" && typeof module == "object") // CommonJS
3 mod(require("../../lib/codemirror"), require("../fold/xml-fold"));
4 else if (typeof define == "function" && define.amd) // AMD
5 define(["../../lib/codemirror", "../fold/xml-fold"], mod);
6 else // Plain browser env
7 mod(CodeMirror);
8 })(function(CodeMirror) {
9 "use strict";
10
11 CodeMirror.defineOption("matchTags", false, function(cm, val, old) {
12 if (old && old != CodeMirror.Init) {
13 cm.off("cursorActivity", doMatchTags);
14 cm.off("viewportChange", maybeUpdateMatch);
15 clear(cm);
16 }
17 if (val) {
18 cm.state.matchBothTags = typeof val == "object" && val.bothTags;
19 cm.on("cursorActivity", doMatchTags);
20 cm.on("viewportChange", maybeUpdateMatch);
21 doMatchTags(cm);
22 }
23 });
24
25 function clear(cm) {
26 if (cm.state.tagHit) cm.state.tagHit.clear();
27 if (cm.state.tagOther) cm.state.tagOther.clear();
28 cm.state.tagHit = cm.state.tagOther = null;
29 }
30
31 function doMatchTags(cm) {
32 cm.state.failedTagMatch = false;
33 cm.operation(function() {
34 clear(cm);
35 if (cm.somethingSelected()) return;
36 var cur = cm.getCursor(), range = cm.getViewport();
37 range.from = Math.min(range.from, cur.line); range.to = Math.max(cur.line + 1, range.to);
38 var match = CodeMirror.findMatchingTag(cm, cur, range);
39 if (!match) return;
40 if (cm.state.matchBothTags) {
41 var hit = match.at == "open" ? match.open : match.close;
42 if (hit) cm.state.tagHit = cm.markText(hit.from, hit.to, {className: "CodeMirror-matchingtag"});
43 }
44 var other = match.at == "close" ? match.open : match.close;
45 if (other)
46 cm.state.tagOther = cm.markText(other.from, other.to, {className: "CodeMirror-matchingtag"});
47 else
48 cm.state.failedTagMatch = true;
49 });
50 }
51
52 function maybeUpdateMatch(cm) {
53 if (cm.state.failedTagMatch) doMatchTags(cm);
54 }
55
56 CodeMirror.commands.toMatchingTag = function(cm) {
57 var found = CodeMirror.findMatchingTag(cm, cm.getCursor());
58 if (found) {
59 var other = found.at == "close" ? found.open : found.close;
60 if (other) cm.extendSelection(other.to, other.from);
61 }
62 };
63 });