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