-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdatalog-hint.js
39 lines (37 loc) · 1.29 KB
/
datalog-hint.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
(function () {
CodeMirror.datalogHint = function(editor) {
var cur = editor.getCursor();
var token = editor.getTokenAt(editor.getCursor());
var arr = editor.getValue().split("\n")
.reduce(function(prev,curr,idx,a){ // zip lines with their numbers
return prev.concat([[idx, curr]])
},[]).filter(function(v) { // remove current line
return v[0] != cur.line;
}).map(function(w) { // remove line numbers
return w[1];
}).map(function(w) { // remove comments
return w.replace(/%.*/,"");
}).map(function(w) {
return w.replace(/\([^\)]+\)/,"");
}).join("\n").split(/[.\?~]/) // split on datalog literal splitters (TODO: only outside parens)
.map(function(w) {
return w.trim();
}).filter(function(w) {
return w !== "";
}).map(function(w) { // get heads
return w.split(":-")[0];
}).map(function(w) {
return w.trim();
}).reduce(function(prev,curr,idx,a) {
return (prev.indexOf(curr) === -1) ? [curr].concat(prev) : prev;
},[]).filter(function(w) {
return w.substr(0,token.string.length) === token.string;
});
//console.log(arr);
return {
list: arr,
from: {line: cur.line, ch: token.start},
to: {line: cur.line, ch: token.end}
};
};
})();