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
8 })(function(CodeMirror
) {
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
);
18 cm
.state
.matchBothTags
= typeof val
== "object" && val
.bothTags
;
19 cm
.on("cursorActivity", doMatchTags
);
20 cm
.on("viewportChange", maybeUpdateMatch
);
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;
31 function doMatchTags(cm
) {
32 cm
.state
.failedTagMatch
= false;
33 cm
.operation(function() {
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
);
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"});
44 var other
= match
.at
== "close" ? match
.open
: match
.close
;
46 cm
.state
.tagOther
= cm
.markText(other
.from, other
.to
, {className
: "CodeMirror-matchingtag"});
48 cm
.state
.failedTagMatch
= true;
52 function maybeUpdateMatch(cm
) {
53 if (cm
.state
.failedTagMatch
) doMatchTags(cm
);
56 CodeMirror
.commands
.toMatchingTag = function(cm
) {
57 var found
= CodeMirror
.findMatchingTag(cm
, cm
.getCursor());
59 var other
= found
.at
== "close" ? found
.open
: found
.close
;
60 if (other
) cm
.extendSelection(other
.to
, other
.from);