Show More
@@ -1,90 +1,72 | |||
|
1 | 1 | // highly adapted for codemiror jshint |
|
2 | 2 | |
|
3 | 3 | (function () { |
|
4 | "use strict"; | |
|
5 | function forEach(arr, f) { | |
|
6 | for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]); | |
|
7 | } | |
|
8 | ||
|
9 | function arrayContains(arr, item) { | |
|
10 | if (!Array.prototype.indexOf) { | |
|
11 | var i = arr.length; | |
|
12 | while (i--) { | |
|
13 | if (arr[i] === item) { | |
|
14 | return true; | |
|
15 | } | |
|
16 | } | |
|
17 | return false; | |
|
4 | "use strict"; | |
|
5 | function forEach(arr, f) { | |
|
6 | for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]); | |
|
18 | 7 | } |
|
19 | return arr.indexOf(item) != -1; | |
|
20 | } | |
|
21 | ||
|
22 | CodeMirror.contextHint = function(editor) { | |
|
23 | // Find the token at the cursor | |
|
24 | var cur = editor.getCursor(), token = editor.getTokenAt(cur), tprop = token; | |
|
25 | // If it's not a 'word-style' token, ignore the token. | |
|
26 | // If it is a property, find out what it is a property of. | |
|
27 | ||
|
28 | var list = new Array(); | |
|
29 | var clist = getCompletions(token,editor) ; | |
|
30 | for( var i = 0 ; i < clist.length ; i++) | |
|
31 | { | |
|
32 | list.push( | |
|
33 | { | |
|
34 | str : clist[i], | |
|
35 | type : "context", | |
|
36 | from : {line: cur.line, ch: token.start}, | |
|
37 | to : {line: cur.line, ch: token.end} | |
|
38 | } | |
|
39 | ) | |
|
40 | ||
|
8 | ||
|
9 | function arrayContains(arr, item) { | |
|
10 | if (!Array.prototype.indexOf) { | |
|
11 | var i = arr.length; | |
|
12 | while (i--) { | |
|
13 | if (arr[i] === item) {return true;} | |
|
14 | } | |
|
15 | return false; | |
|
16 | } | |
|
17 | return arr.indexOf(item) != -1; | |
|
41 | 18 | } |
|
42 | return list; | |
|
43 | } | |
|
44 | ||
|
45 | // find all 'words' of current cell | |
|
46 | function getAllTokens(editor) | |
|
47 | { | |
|
48 | var found = []; | |
|
49 | // get all text remove and split it before dot and at space | |
|
50 | // keep the dot for completing token that also start with dot | |
|
51 | var candidates = editor.getValue() | |
|
52 | .replace(/[. ]/g,"\n") | |
|
53 | .split('\n'); | |
|
54 | // append to arry if not already (the function) | |
|
55 | function maybeAdd(str) { | |
|
56 | if (!arrayContains(found, str)) found.push(str); | |
|
19 | ||
|
20 | CodeMirror.contextHint = function(editor) { | |
|
21 | // Find the token at the cursor | |
|
22 | var cur = editor.getCursor(), token = editor.getTokenAt(cur), tprop = token; | |
|
23 | // If it's not a 'word-style' token, ignore the token. | |
|
24 | // If it is a property, find out what it is a property of. | |
|
25 | var list = new Array(); | |
|
26 | var clist = getCompletions(token,editor); | |
|
27 | for(var i = 0 ; i < clist.length ; i++) | |
|
28 | { | |
|
29 | list.push( | |
|
30 | { str : clist[i], | |
|
31 | type : "context", | |
|
32 | from : {line: cur.line, ch: token.start}, | |
|
33 | to : {line: cur.line, ch: token.end} }) | |
|
34 | } | |
|
35 | return list; | |
|
57 | 36 | } |
|
58 | 37 | |
|
59 | // append to arry if not already | |
|
60 | // (here we do it ) | |
|
61 | for( var c in candidates ) | |
|
38 | // find all 'words' of current cell | |
|
39 | function getAllTokens(editor) | |
|
62 | 40 | { |
|
63 | if(candidates[c].length >= 1){ | |
|
64 | maybeAdd(candidates[c]);} | |
|
41 | var found = []; | |
|
42 | // get all text remove and split it before dot and at space | |
|
43 | // keep the dot for completing token that also start with dot | |
|
44 | var candidates = editor.getValue() | |
|
45 | .replace(/[.]/g," .") | |
|
46 | .replace(/[ ]/g,"\n") | |
|
47 | .split('\n'); | |
|
48 | // append to arry if not already (the function) | |
|
49 | function maybeAdd(str) { | |
|
50 | if (!arrayContains(found, str)) found.push(str); | |
|
51 | } | |
|
52 | // append to arry if not already | |
|
53 | // (here we do it ) | |
|
54 | for( var c in candidates ) | |
|
55 | { | |
|
56 | if(candidates[c].length >= 1){ | |
|
57 | maybeAdd(candidates[c]);} | |
|
58 | } | |
|
59 | return found; | |
|
65 | 60 | } |
|
66 | return found; | |
|
67 | 61 | |
|
68 | } | |
|
69 | ||
|
70 | function getCompletions(token,editor) | |
|
71 | { | |
|
72 | var candidates = getAllTokens(editor); | |
|
73 | // filter all token that have a common start (but nox exactly) the lenght of the current token | |
|
74 | var prependchar =''; | |
|
75 | if(token.string.indexOf('.') == 0) | |
|
76 | { | |
|
77 | prependchar = '.' | |
|
78 | } | |
|
79 | var lambda = function(x){ | |
|
80 | x = prependchar+x; | |
|
81 | return (x.indexOf(token.string)==0 && x != token.string)}; | |
|
82 | var filterd = candidates.filter(lambda); | |
|
83 | for( var i in filterd) | |
|
62 | function getCompletions(token,editor) | |
|
84 | 63 | { |
|
85 | // take care of reappending '.' at the beginning | |
|
86 | filterd[i] = prependchar+filterd[i]; | |
|
64 | var candidates = getAllTokens(editor); | |
|
65 | // filter all token that have a common start (but nox exactly) the lenght of the current token | |
|
66 | var lambda = function(x){ | |
|
67 | return (x.indexOf(token.string)==0 && x != token.string) | |
|
68 | }; | |
|
69 | var filterd = candidates.filter(lambda); | |
|
70 | return filterd; | |
|
87 | 71 | } |
|
88 | return filterd; | |
|
89 | } | |
|
90 | 72 | })(); |
General Comments 0
You need to be logged in to leave comments.
Login now