-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjsonld-hinter.js
67 lines (66 loc) · 1.75 KB
/
jsonld-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
(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) {
var terms = {
property: [
'@base',
'@container', '@context',
'@graph',
'@id',
'@language', '@list',
'@reverse',
'@set',
'@type',
'@vocab'
],
// TODO: these can be values of @container...any others like this?
string: ["@list", "@index", "@set"]
};
function hint(cm, options) {
var cur = cm.getCursor(),
token = cm.getTokenAt(cur);
var start = token.start - 1,
end = token.end;
if (token.type !== 'property' && token.type !== 'string') {
// early return if it's not what we're looking for
return;
} else {
// token.type === 'property' || token.type === 'string'
var list = terms[token.type];
var search = token.string.match(/[@]?\w+/);
if (search !== null) {
list = list.filter(function(t) {
return (t.indexOf(search) > -1);
});
}
return {
list: list.map(function(item) {
return {
text: item + '"',
displayText: item
};
}),
from: CodeMirror.Pos(cur.line, start+2),
to: CodeMirror.Pos(cur.line, end)
};
}
}
CodeMirror.registerHelper("hint", "jsonld", function(cm, options) {
cm.showHint({
completeSingle: false,
hint: hint,
});
return CodeMirror.Pass;
});
});