Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
// Copyright (c) IPython Development Team.
2
// Distributed under the terms of the Modified BSD License.
3
4
// highly adapted for codemiror jshint
5
define(['codemirror/lib/codemirror'], function(CodeMirror) {
6
"use strict";
7
8
var forEach = function(arr, f) {
9
for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
10
};
11
12
var arrayContains = function(arr, item) {
13
if (!Array.prototype.indexOf) {
14
var i = arr.length;
15
while (i--) {
16
if (arr[i] === item) {
17
return true;
18
}
19
}
20
return false;
21
}
22
return arr.indexOf(item) != -1;
23
};
24
25
CodeMirror.contextHint = function (editor) {
26
// Find the token at the cursor
27
var cur = editor.getCursor(),
28
token = editor.getTokenAt(cur),
29
tprop = token;
30
// If it's not a 'word-style' token, ignore the token.
31
// If it is a property, find out what it is a property of.
32
var list = [];
33
var clist = getCompletions(token, editor);
34
for (var i = 0; i < clist.length; i++) {
35
list.push({
36
str: clist[i],
37
type: "context",
38
from: {
39
line: cur.line,
40
ch: token.start
41
},
42
to: {
43
line: cur.line,
44
ch: token.end
45
}
46
});
47
}
48
return list;
49
};
50
51
// find all 'words' of current cell
52
var getAllTokens = function (editor) {
53
var found = [];
54
55
// add to found if not already in it
56
57
58
function maybeAdd(str) {
59
if (!arrayContains(found, str)) found.push(str);
60
}
61
62
// loop through all token on all lines
63
var lineCount = editor.lineCount();
64
// loop on line
65
for (var l = 0; l < lineCount; l++) {
66
var line = editor.getLine(l);
67
//loop on char
68
for (var c = 1; c < line.length; c++) {
69
var tk = editor.getTokenAt({
70
line: l,
71
ch: c
72
});
73
// if token has a class, it has geat chances of beeing
74
// of interest. Add it to the list of possible completions.
75
// we could skip token of ClassName 'comment'
76
// or 'number' and 'operator'
77
if (tk.className !== null) {
78
maybeAdd(tk.string);
79
}
80
// jump to char after end of current token
81
c = tk.end;
82
}
83
}
84
return found;
85
};
86
87
var getCompletions = function(token, editor) {
88
var candidates = getAllTokens(editor);
89
// filter all token that have a common start (but nox exactly) the lenght of the current token
90
var lambda = function (x) {
91
return (x.indexOf(token.string) === 0 && x != token.string);
92
};
93
var filterd = candidates.filter(lambda);
94
return filterd;
95
};
96
97
return {'contextHint': CodeMirror.contextHint};
98
});
99
100