-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontexts-hinter.js
78 lines (76 loc) · 2.14 KB
/
contexts-hinter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(
require("codemirror/lib/codemirror"),
require("codemirror/addon/hint/show-hint")
);
else if (typeof define == "function" && define.amd) // AMD
define([
"codemirror/lib/codemirror",
"codemirror/addon/hint/show-hint"
], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
/**
* Requires editor config to contain:
* hintOptions.contexts = {
* schema: {
* property: ["name", ...],
* string: ["Person", ...]
* }
* }
**/
function hint(cm, options) {
var cur = cm.getCursor(),
token = cm.getTokenAt(cur);
var start = token.start - 1,
end = token.end;
var colon_at = token.string.indexOf(':');
if (token.type !== 'property' && token.type !== 'string') {
return;
} else {
// set prefix to the default @vocab
var prefix = '@vocab';
var suffix = '';
// remove any `"` form the lookup (typically start/end)
var search = token.string.replace(/"/gi, '').split(':');
if (search.length > 1) {
prefix = search[0];
suffix = search[1];
} else {
suffix = search[0];
}
if (prefix in options.contexts
&& token.type in options.contexts[prefix]) {
var terms = options.contexts[prefix][token.type];
var list = terms.map(function(item) {
if (item.indexOf(suffix) > -1) {
return {
text: (prefix !== '@vocab'
? prefix + ':' + item + '"'
: item + '"'),
displayText: item
};
}
}).filter(function(item) {
return item
});
return {
list: list,
from: CodeMirror.Pos(cur.line, start+2),
to: CodeMirror.Pos(cur.line, end)
};
}
}
}
CodeMirror.registerHelper("hint", "jsonldContexts", function(cm, options) {
setTimeout(function() {
cm.showHint({
completeSingle: false,
hint: hint,
});
}, 100);
return CodeMirror.Pass;
});
});