cf49dd77f126c1faf469ef40141442016a36faa920c23f1091f33c503e8048b568021e9abd055eb6258110ccb1d5a61f66d855219578fbcec8bc98a95d36cc 2.3 KB

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