xml-hint.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. (function(mod) {
  2. if (typeof exports == "object" && typeof module == "object") // CommonJS
  3. mod(require("../../lib/codemirror"));
  4. else if (typeof define == "function" && define.amd) // AMD
  5. define(["../../lib/codemirror"], mod);
  6. else // Plain browser env
  7. mod(CodeMirror);
  8. })(function(CodeMirror) {
  9. "use strict";
  10. var Pos = CodeMirror.Pos;
  11. function getHints(cm, options) {
  12. var tags = options && options.schemaInfo;
  13. var quote = (options && options.quoteChar) || '"';
  14. if (!tags) return;
  15. var cur = cm.getCursor(), token = cm.getTokenAt(cur);
  16. var inner = CodeMirror.innerMode(cm.getMode(), token.state);
  17. if (inner.mode.name != "xml") return;
  18. var result = [], replaceToken = false, prefix;
  19. var isTag = token.string.charAt(0) == "<";
  20. if (!inner.state.tagName || isTag) { // Tag completion
  21. if (isTag) {
  22. prefix = token.string.slice(1);
  23. replaceToken = true;
  24. }
  25. var cx = inner.state.context, curTag = cx && tags[cx.tagName];
  26. var childList = cx ? curTag && curTag.children : tags["!top"];
  27. if (childList) {
  28. for (var i = 0; i < childList.length; ++i) if (!prefix || childList[i].lastIndexOf(prefix, 0) == 0)
  29. result.push("<" + childList[i]);
  30. } else {
  31. for (var name in tags) if (tags.hasOwnProperty(name) && name != "!top" && (!prefix || name.lastIndexOf(prefix, 0) == 0))
  32. result.push("<" + name);
  33. }
  34. if (cx && (!prefix || ("/" + cx.tagName).lastIndexOf(prefix, 0) == 0))
  35. result.push("</" + cx.tagName + ">");
  36. } else {
  37. // Attribute completion
  38. var curTag = tags[inner.state.tagName], attrs = curTag && curTag.attrs;
  39. if (!attrs) return;
  40. if (token.type == "string" || token.string == "=") { // A value
  41. var before = cm.getRange(Pos(cur.line, Math.max(0, cur.ch - 60)),
  42. Pos(cur.line, token.type == "string" ? token.start : token.end));
  43. var atName = before.match(/([^\s\u00a0=<>\"\']+)=$/), atValues;
  44. if (!atName || !attrs.hasOwnProperty(atName[1]) || !(atValues = attrs[atName[1]])) return;
  45. if (typeof atValues == 'function') atValues = atValues.call(this, cm); // Functions can be used to supply values for autocomplete widget
  46. if (token.type == "string") {
  47. prefix = token.string;
  48. if (/['"]/.test(token.string.charAt(0))) {
  49. quote = token.string.charAt(0);
  50. prefix = token.string.slice(1);
  51. }
  52. replaceToken = true;
  53. }
  54. for (var i = 0; i < atValues.length; ++i) if (!prefix || atValues[i].lastIndexOf(prefix, 0) == 0)
  55. result.push(quote + atValues[i] + quote);
  56. } else { // An attribute name
  57. if (token.type == "attribute") {
  58. prefix = token.string;
  59. replaceToken = true;
  60. }
  61. for (var attr in attrs) if (attrs.hasOwnProperty(attr) && (!prefix || attr.lastIndexOf(prefix, 0) == 0))
  62. result.push(attr);
  63. }
  64. }
  65. return {
  66. list: result,
  67. from: replaceToken ? Pos(cur.line, token.start) : cur,
  68. to: replaceToken ? Pos(cur.line, token.end) : cur
  69. };
  70. }
  71. CodeMirror.registerHelper("hint", "xml", getHints);
  72. });