xml-hint.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. var Pos = CodeMirror.Pos;
  13. function getHints(cm, options) {
  14. var tags = options && options.schemaInfo;
  15. var quote = (options && options.quoteChar) || '"';
  16. if (!tags) return;
  17. var cur = cm.getCursor(), token = cm.getTokenAt(cur);
  18. if (token.end > cur.ch) {
  19. token.end = cur.ch;
  20. token.string = token.string.slice(0, cur.ch - token.start);
  21. }
  22. var inner = CodeMirror.innerMode(cm.getMode(), token.state);
  23. if (inner.mode.name != "xml") return;
  24. var result = [], replaceToken = false, prefix;
  25. var tag = /\btag\b/.test(token.type) && !/>$/.test(token.string);
  26. var tagName = tag && /^\w/.test(token.string), tagStart;
  27. if (tagName) {
  28. var before = cm.getLine(cur.line).slice(Math.max(0, token.start - 2), token.start);
  29. var tagType = /<\/$/.test(before) ? "close" : /<$/.test(before) ? "open" : null;
  30. if (tagType) tagStart = token.start - (tagType == "close" ? 2 : 1);
  31. } else if (tag && token.string == "<") {
  32. tagType = "open";
  33. } else if (tag && token.string == "</") {
  34. tagType = "close";
  35. }
  36. if (!tag && !inner.state.tagName || tagType) {
  37. if (tagName)
  38. prefix = token.string;
  39. replaceToken = tagType;
  40. var cx = inner.state.context, curTag = cx && tags[cx.tagName];
  41. var childList = cx ? curTag && curTag.children : tags["!top"];
  42. if (childList && tagType != "close") {
  43. for (var i = 0; i < childList.length; ++i) if (!prefix || childList[i].lastIndexOf(prefix, 0) == 0)
  44. result.push("<" + childList[i]);
  45. } else if (tagType != "close") {
  46. for (var name in tags)
  47. if (tags.hasOwnProperty(name) && name != "!top" && name != "!attrs" && (!prefix || name.lastIndexOf(prefix, 0) == 0))
  48. result.push("<" + name);
  49. }
  50. if (cx && (!prefix || tagType == "close" && cx.tagName.lastIndexOf(prefix, 0) == 0))
  51. result.push("</" + cx.tagName + ">");
  52. } else {
  53. // Attribute completion
  54. var curTag = tags[inner.state.tagName], attrs = curTag && curTag.attrs;
  55. var globalAttrs = tags["!attrs"];
  56. if (!attrs && !globalAttrs) return;
  57. if (!attrs) {
  58. attrs = globalAttrs;
  59. } else if (globalAttrs) { // Combine tag-local and global attributes
  60. var set = {};
  61. for (var nm in globalAttrs) if (globalAttrs.hasOwnProperty(nm)) set[nm] = globalAttrs[nm];
  62. for (var nm in attrs) if (attrs.hasOwnProperty(nm)) set[nm] = attrs[nm];
  63. attrs = set;
  64. }
  65. if (token.type == "string" || token.string == "=") { // A value
  66. var before = cm.getRange(Pos(cur.line, Math.max(0, cur.ch - 60)),
  67. Pos(cur.line, token.type == "string" ? token.start : token.end));
  68. var atName = before.match(/([^\s\u00a0=<>\"\']+)=$/), atValues;
  69. if (!atName || !attrs.hasOwnProperty(atName[1]) || !(atValues = attrs[atName[1]])) return;
  70. if (typeof atValues == 'function') atValues = atValues.call(this, cm); // Functions can be used to supply values for autocomplete widget
  71. if (token.type == "string") {
  72. prefix = token.string;
  73. var n = 0;
  74. if (/['"]/.test(token.string.charAt(0))) {
  75. quote = token.string.charAt(0);
  76. prefix = token.string.slice(1);
  77. n++;
  78. }
  79. var len = token.string.length;
  80. if (/['"]/.test(token.string.charAt(len - 1))) {
  81. quote = token.string.charAt(len - 1);
  82. prefix = token.string.substr(n, len - 2);
  83. }
  84. replaceToken = true;
  85. }
  86. for (var i = 0; i < atValues.length; ++i) if (!prefix || atValues[i].lastIndexOf(prefix, 0) == 0)
  87. result.push(quote + atValues[i] + quote);
  88. } else { // An attribute name
  89. if (token.type == "attribute") {
  90. prefix = token.string;
  91. replaceToken = true;
  92. }
  93. for (var attr in attrs) if (attrs.hasOwnProperty(attr) && (!prefix || attr.lastIndexOf(prefix, 0) == 0))
  94. result.push(attr);
  95. }
  96. }
  97. return {
  98. list: result,
  99. from: replaceToken ? Pos(cur.line, tagStart == null ? token.start : tagStart) : cur,
  100. to: replaceToken ? Pos(cur.line, token.end) : cur
  101. };
  102. }
  103. CodeMirror.registerHelper("hint", "xml", getHints);
  104. });