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