25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

133 satır
5.6KB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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 matches(hint, typed, matchInMiddle) {
  14. if (matchInMiddle) return hint.indexOf(typed) >= 0;
  15. else return hint.lastIndexOf(typed, 0) == 0;
  16. }
  17. function getHints(cm, options) {
  18. var tags = options && options.schemaInfo;
  19. var quote = (options && options.quoteChar) || '"';
  20. var matchInMiddle = options && options.matchInMiddle;
  21. if (!tags) return;
  22. var cur = cm.getCursor(), token = cm.getTokenAt(cur);
  23. if (token.end > cur.ch) {
  24. token.end = cur.ch;
  25. token.string = token.string.slice(0, cur.ch - token.start);
  26. }
  27. var inner = CodeMirror.innerMode(cm.getMode(), token.state);
  28. if (!inner.mode.xmlCurrentTag) return
  29. var result = [], replaceToken = false, prefix;
  30. var tag = /\btag\b/.test(token.type) && !/>$/.test(token.string);
  31. var tagName = tag && /^\w/.test(token.string), tagStart;
  32. if (tagName) {
  33. var before = cm.getLine(cur.line).slice(Math.max(0, token.start - 2), token.start);
  34. var tagType = /<\/$/.test(before) ? "close" : /<$/.test(before) ? "open" : null;
  35. if (tagType) tagStart = token.start - (tagType == "close" ? 2 : 1);
  36. } else if (tag && token.string == "<") {
  37. tagType = "open";
  38. } else if (tag && token.string == "</") {
  39. tagType = "close";
  40. }
  41. var tagInfo = inner.mode.xmlCurrentTag(inner.state)
  42. if (!tag && !tagInfo || tagType) {
  43. if (tagName)
  44. prefix = token.string;
  45. replaceToken = tagType;
  46. var context = inner.mode.xmlCurrentContext ? inner.mode.xmlCurrentContext(inner.state) : []
  47. var inner = context.length && context[context.length - 1]
  48. var curTag = inner && tags[inner]
  49. var childList = inner ? curTag && curTag.children : tags["!top"];
  50. if (childList && tagType != "close") {
  51. for (var i = 0; i < childList.length; ++i) if (!prefix || matches(childList[i], prefix, matchInMiddle))
  52. result.push("<" + childList[i]);
  53. } else if (tagType != "close") {
  54. for (var name in tags)
  55. if (tags.hasOwnProperty(name) && name != "!top" && name != "!attrs" && (!prefix || matches(name, prefix, matchInMiddle)))
  56. result.push("<" + name);
  57. }
  58. if (inner && (!prefix || tagType == "close" && matches(inner, prefix, matchInMiddle)))
  59. result.push("</" + inner + ">");
  60. } else {
  61. // Attribute completion
  62. var curTag = tagInfo && tags[tagInfo.name], attrs = curTag && curTag.attrs;
  63. var globalAttrs = tags["!attrs"];
  64. if (!attrs && !globalAttrs) return;
  65. if (!attrs) {
  66. attrs = globalAttrs;
  67. } else if (globalAttrs) { // Combine tag-local and global attributes
  68. var set = {};
  69. for (var nm in globalAttrs) if (globalAttrs.hasOwnProperty(nm)) set[nm] = globalAttrs[nm];
  70. for (var nm in attrs) if (attrs.hasOwnProperty(nm)) set[nm] = attrs[nm];
  71. attrs = set;
  72. }
  73. if (token.type == "string" || token.string == "=") { // A value
  74. var before = cm.getRange(Pos(cur.line, Math.max(0, cur.ch - 60)),
  75. Pos(cur.line, token.type == "string" ? token.start : token.end));
  76. var atName = before.match(/([^\s\u00a0=<>\"\']+)=$/), atValues;
  77. if (!atName || !attrs.hasOwnProperty(atName[1]) || !(atValues = attrs[atName[1]])) return;
  78. if (typeof atValues == 'function') atValues = atValues.call(this, cm); // Functions can be used to supply values for autocomplete widget
  79. if (token.type == "string") {
  80. prefix = token.string;
  81. var n = 0;
  82. if (/['"]/.test(token.string.charAt(0))) {
  83. quote = token.string.charAt(0);
  84. prefix = token.string.slice(1);
  85. n++;
  86. }
  87. var len = token.string.length;
  88. if (/['"]/.test(token.string.charAt(len - 1))) {
  89. quote = token.string.charAt(len - 1);
  90. prefix = token.string.substr(n, len - 2);
  91. }
  92. if (n) { // an opening quote
  93. var line = cm.getLine(cur.line);
  94. if (line.length > token.end && line.charAt(token.end) == quote) token.end++; // include a closing quote
  95. }
  96. replaceToken = true;
  97. }
  98. var returnHintsFromAtValues = function(atValues) {
  99. if (atValues)
  100. for (var i = 0; i < atValues.length; ++i) if (!prefix || matches(atValues[i], prefix, matchInMiddle))
  101. result.push(quote + atValues[i] + quote);
  102. return returnHints();
  103. };
  104. if (atValues && atValues.then) return atValues.then(returnHintsFromAtValues);
  105. return returnHintsFromAtValues(atValues);
  106. } else { // An attribute name
  107. if (token.type == "attribute") {
  108. prefix = token.string;
  109. replaceToken = true;
  110. }
  111. for (var attr in attrs) if (attrs.hasOwnProperty(attr) && (!prefix || matches(attr, prefix, matchInMiddle)))
  112. result.push(attr);
  113. }
  114. }
  115. function returnHints() {
  116. return {
  117. list: result,
  118. from: replaceToken ? Pos(cur.line, tagStart == null ? token.start : tagStart) : cur,
  119. to: replaceToken ? Pos(cur.line, token.end) : cur
  120. };
  121. }
  122. return returnHints();
  123. }
  124. CodeMirror.registerHelper("hint", "xml", getHints);
  125. });