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