xml-hint.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. var inner = CodeMirror.innerMode(cm.getMode(), token.state);
  19. if (inner.mode.name != "xml") return;
  20. var result = [], replaceToken = false, prefix;
  21. var tag = /\btag\b/.test(token.type), tagName = tag && /^\w/.test(token.string), tagStart;
  22. if (tagName) {
  23. var before = cm.getLine(cur.line).slice(Math.max(0, token.start - 2), token.start);
  24. var tagType = /<\/$/.test(before) ? "close" : /<$/.test(before) ? "open" : null;
  25. if (tagType) tagStart = token.start - (tagType == "close" ? 2 : 1);
  26. } else if (tag && token.string == "<") {
  27. tagType = "open";
  28. } else if (tag && token.string == "</") {
  29. tagType = "close";
  30. }
  31. if (!tag && !inner.state.tagName || tagType) {
  32. if (tagName)
  33. prefix = token.string;
  34. replaceToken = tagType;
  35. var cx = inner.state.context, curTag = cx && tags[cx.tagName];
  36. var childList = cx ? curTag && curTag.children : tags["!top"];
  37. if (childList && tagType != "close") {
  38. for (var i = 0; i < childList.length; ++i) if (!prefix || childList[i].lastIndexOf(prefix, 0) == 0)
  39. result.push("<" + childList[i]);
  40. } else if (tagType != "close") {
  41. for (var name in tags)
  42. if (tags.hasOwnProperty(name) && name != "!top" && name != "!attrs" && (!prefix || name.lastIndexOf(prefix, 0) == 0))
  43. result.push("<" + name);
  44. }
  45. if (cx && (!prefix || tagType == "close" && cx.tagName.lastIndexOf(prefix, 0) == 0))
  46. result.push("</" + cx.tagName + ">");
  47. } else {
  48. // Attribute completion
  49. var curTag = tags[inner.state.tagName], attrs = curTag && curTag.attrs;
  50. var globalAttrs = tags["!attrs"];
  51. if (!attrs && !globalAttrs) return;
  52. if (!attrs) {
  53. attrs = globalAttrs;
  54. } else if (globalAttrs) { // Combine tag-local and global attributes
  55. var set = {};
  56. for (var nm in globalAttrs) if (globalAttrs.hasOwnProperty(nm)) set[nm] = globalAttrs[nm];
  57. for (var nm in attrs) if (attrs.hasOwnProperty(nm)) set[nm] = attrs[nm];
  58. attrs = set;
  59. }
  60. if (token.type == "string" || token.string == "=") { // A value
  61. var before = cm.getRange(Pos(cur.line, Math.max(0, cur.ch - 60)),
  62. Pos(cur.line, token.type == "string" ? token.start : token.end));
  63. var atName = before.match(/([^\s\u00a0=<>\"\']+)=$/), atValues;
  64. if (!atName || !attrs.hasOwnProperty(atName[1]) || !(atValues = attrs[atName[1]])) return;
  65. if (typeof atValues == 'function') atValues = atValues.call(this, cm); // Functions can be used to supply values for autocomplete widget
  66. if (token.type == "string") {
  67. prefix = token.string;
  68. var n = 0;
  69. if (/['"]/.test(token.string.charAt(0))) {
  70. quote = token.string.charAt(0);
  71. prefix = token.string.slice(1);
  72. n++;
  73. }
  74. var len = token.string.length;
  75. if (/['"]/.test(token.string.charAt(len - 1))) {
  76. quote = token.string.charAt(len - 1);
  77. prefix = token.string.substr(n, len - 2);
  78. }
  79. replaceToken = true;
  80. }
  81. for (var i = 0; i < atValues.length; ++i) if (!prefix || atValues[i].lastIndexOf(prefix, 0) == 0)
  82. result.push(quote + atValues[i] + quote);
  83. } else { // An attribute name
  84. if (token.type == "attribute") {
  85. prefix = token.string;
  86. replaceToken = true;
  87. }
  88. for (var attr in attrs) if (attrs.hasOwnProperty(attr) && (!prefix || attr.lastIndexOf(prefix, 0) == 0))
  89. result.push(attr);
  90. }
  91. }
  92. return {
  93. list: result,
  94. from: replaceToken ? Pos(cur.line, tagStart == null ? token.start : tagStart) : cur,
  95. to: replaceToken ? Pos(cur.line, token.end) : cur
  96. };
  97. }
  98. CodeMirror.registerHelper("hint", "xml", getHints);
  99. });