-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
contexthint.js
98 lines (87 loc) · 3.04 KB
/
contexthint.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
// highly adapted for codemiror jshint
define(['codemirror/lib/codemirror'], function(CodeMirror) {
"use strict";
var forEach = function(arr, f) {
for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
};
var arrayContains = function(arr, item) {
if (!Array.prototype.indexOf) {
var i = arr.length;
while (i--) {
if (arr[i] === item) {
return true;
}
}
return false;
}
return arr.indexOf(item) != -1;
};
CodeMirror.contextHint = function (editor) {
// Find the token at the cursor
var cur = editor.getCursor(),
token = editor.getTokenAt(cur),
tprop = token;
// If it's not a 'word-style' token, ignore the token.
// If it is a property, find out what it is a property of.
var list = [];
var clist = getCompletions(token, editor);
for (var i = 0; i < clist.length; i++) {
list.push({
str: clist[i],
type: "context",
from: {
line: cur.line,
ch: token.start
},
to: {
line: cur.line,
ch: token.end
}
});
}
return list;
};
// find all 'words' of current cell
var getAllTokens = function (editor) {
var found = [];
// add to found if not already in it
function maybeAdd(str) {
if (!arrayContains(found, str)) found.push(str);
}
// loop through all token on all lines
var lineCount = editor.lineCount();
// loop on line
for (var l = 0; l < lineCount; l++) {
var line = editor.getLine(l);
//loop on char
for (var c = 1; c < line.length; c++) {
var tk = editor.getTokenAt({
line: l,
ch: c
});
// if token has a class, it has geat chances of beeing
// of interest. Add it to the list of possible completions.
// we could skip token of ClassName 'comment'
// or 'number' and 'operator'
if (tk.className !== null) {
maybeAdd(tk.string);
}
// jump to char after end of current token
c = tk.end;
}
}
return found;
};
var getCompletions = function(token, editor) {
var candidates = getAllTokens(editor);
// filter all token that have a common start (but nox exactly) the length of the current token
var lambda = function (x) {
return (x.indexOf(token.string) === 0 && x != token.string);
};
var filterd = candidates.filter(lambda);
return filterd;
};
return {'contextHint': CodeMirror.contextHint};
});