5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
var Pos = CodeMirror.Pos;
16
function matches(hint, typed, matchInMiddle) {
17
if (matchInMiddle) return hint.indexOf(typed) >= 0;
18
else return hint.lastIndexOf(typed, 0) == 0;
21
function getHints(cm, options) {
22
var tags = options && options.schemaInfo;
23
var quote = (options && options.quoteChar) || '"';
24
var matchInMiddle = options && options.matchInMiddle;
26
var cur = cm.getCursor(), token = cm.getTokenAt(cur);
27
if (token.end > cur.ch) {
29
token.string = token.string.slice(0, cur.ch - token.start);
31
var inner = CodeMirror.innerMode(cm.getMode(), token.state);
32
if (!inner.mode.xmlCurrentTag) return
33
var result = [], replaceToken = false, prefix;
34
var tag = /\btag\b/.test(token.type) && !/>$/.test(token.string);
35
var tagName = tag && /^\w/.test(token.string), tagStart;
38
var before = cm.getLine(cur.line).slice(Math.max(0, token.start - 2), token.start);
39
var tagType = /<\/$/.test(before) ? "close" : /<$/.test(before) ? "open" : null;
40
if (tagType) tagStart = token.start - (tagType == "close" ? 2 : 1);
41
} else if (tag && token.string == "<") {
43
} else if (tag && token.string == "</") {
47
var tagInfo = inner.mode.xmlCurrentTag(inner.state)
48
if (!tag && !tagInfo || tagType) {
50
prefix = token.string;
51
replaceToken = tagType;
52
var context = inner.mode.xmlCurrentContext ? inner.mode.xmlCurrentContext(inner.state) : []
53
var inner = context.length && context[context.length - 1]
54
var curTag = inner && tags[inner]
55
var childList = inner ? curTag && curTag.children : tags["!top"];
56
if (childList && tagType != "close") {
57
for (var i = 0; i < childList.length; ++i) if (!prefix || matches(childList[i], prefix, matchInMiddle))
58
result.push("<" + childList[i]);
59
} else if (tagType != "close") {
60
for (var name in tags)
61
if (tags.hasOwnProperty(name) && name != "!top" && name != "!attrs" && (!prefix || matches(name, prefix, matchInMiddle)))
62
result.push("<" + name);
64
if (inner && (!prefix || tagType == "close" && matches(inner, prefix, matchInMiddle)))
65
result.push("</" + inner + ">");
68
var curTag = tagInfo && tags[tagInfo.name], attrs = curTag && curTag.attrs;
69
var globalAttrs = tags["!attrs"];
70
if (!attrs && !globalAttrs) return;
73
} else if (globalAttrs) {
75
for (var nm in globalAttrs) if (globalAttrs.hasOwnProperty(nm)) set[nm] = globalAttrs[nm];
76
for (var nm in attrs) if (attrs.hasOwnProperty(nm)) set[nm] = attrs[nm];
79
if (token.type == "string" || token.string == "=") {
80
var before = cm.getRange(Pos(cur.line, Math.max(0, cur.ch - 60)),
81
Pos(cur.line, token.type == "string" ? token.start : token.end));
82
var atName = before.match(/([^\s\u00a0=<>\"\']+)=$/), atValues;
83
if (!atName || !attrs.hasOwnProperty(atName[1]) || !(atValues = attrs[atName[1]])) return;
84
if (typeof atValues == 'function') atValues = atValues.call(this, cm);
85
if (token.type == "string") {
86
prefix = token.string;
88
if (/['"]/.test(token.string.charAt(0))) {
89
quote = token.string.charAt(0);
90
prefix = token.string.slice(1);
93
var len = token.string.length;
94
if (/['"]/.test(token.string.charAt(len - 1))) {
95
quote = token.string.charAt(len - 1);
96
prefix = token.string.substr(n, len - 2);
99
var line = cm.getLine(cur.line);
100
if (line.length > token.end && line.charAt(token.end) == quote) token.end++;
104
var returnHintsFromAtValues = function(atValues) {
106
for (var i = 0; i < atValues.length; ++i) if (!prefix || matches(atValues[i], prefix, matchInMiddle))
107
result.push(quote + atValues[i] + quote);
108
return returnHints();
110
if (atValues && atValues.then) return atValues.then(returnHintsFromAtValues);
111
return returnHintsFromAtValues(atValues);
113
if (token.type == "attribute") {
114
prefix = token.string;
117
for (var attr in attrs) if (attrs.hasOwnProperty(attr) && (!prefix || matches(attr, prefix, matchInMiddle)))
121
function returnHints() {
124
from: replaceToken ? Pos(cur.line, tagStart == null ? token.start : tagStart) : cur,
125
to: replaceToken ? Pos(cur.line, token.end) : cur
128
return returnHints();
131
CodeMirror.registerHelper("hint", "xml", getHints);