##// END OF EJS Templates
beautify context-hint.js
Matthias BUSSONNIER -
Show More
@@ -1,81 +1,92
1 // highly adapted for codemiror jshint
1 // highly adapted for codemiror jshint
2
2 (function() {
3 (function () {
4 "use strict";
3 "use strict";
4
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) {return true;}
13 if (arr[i] === item) {
14 return true;
15 }
14 }
16 }
15 return false;
17 return false;
16 }
18 }
17 return arr.indexOf(item) != -1;
19 return arr.indexOf(item) != -1;
18 }
20 }
19
21
20 CodeMirror.contextHint = function(editor) {
22 CodeMirror.contextHint = function(editor) {
21 // Find the token at the cursor
23 // Find the token at the cursor
22 var cur = editor.getCursor(), token = editor.getTokenAt(cur), tprop = token;
24 var cur = editor.getCursor(),
25 token = editor.getTokenAt(cur),
26 tprop = token;
23 // If it's not a 'word-style' token, ignore the token.
27 // 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.
28 // If it is a property, find out what it is a property of.
25 var list = new Array();
29 var list = new Array();
26 var clist = getCompletions(token,editor);
30 var clist = getCompletions(token, editor);
27 for(var i = 0 ; i < clist.length ; i++)
31 for (var i = 0; i < clist.length; i++) {
28 {
32 list.push({
29 list.push(
33 str: clist[i],
30 { str : clist[i],
34 type: "context",
31 type : "context",
35 from: {
32 from : {line: cur.line, ch: token.start},
36 line: cur.line,
33 to : {line: cur.line, ch: token.end} })
37 ch: token.start
38 },
39 to: {
40 line: cur.line,
41 ch: token.end
42 }
43 })
34 }
44 }
35 return list;
45 return list;
36 }
46 }
37
47
38 // find all 'words' of current cell
48 // find all 'words' of current cell
39 var getAllTokens = function(editor)
49 var getAllTokens = function(editor) {
40 {
50 var found = [];
41 var found = [];
51
52 // add to found if not already in it
42
53
43 // add to found if not already in it
44 function maybeAdd(str) {
45 if (!arrayContains(found, str)) found.push(str);
46 }
47
54
48 // loop through all token on all lines
55 function maybeAdd(str) {
49 var lineCount = editor.lineCount();
56 if (!arrayContains(found, str)) found.push(str);
50 // loop on line
57 }
51 for( var l=0; l< lineCount ; l++)
58
52 {
59 // loop through all token on all lines
53 var line = editor.getLine(l);
60 var lineCount = editor.lineCount();
54 //loop on char
61 // loop on line
55 for( var c = 1 ; c < line.length ; c++)
62 for (var l = 0; l < lineCount; l++) {
56 {
63 var line = editor.getLine(l);
57 var tk = editor.getTokenAt({line:l,ch:c});
64 //loop on char
58 // if token has a class, it has geat chances of beeing
65 for (var c = 1; c < line.length; c++) {
59 // of interest. Add it to the list of possible completions.
66 var tk = editor.getTokenAt({
60 // we could skip token of ClassName 'comment'
67 line: l,
61 // or 'number' and 'operator'
68 ch: c
62 if(tk.className != null){
69 });
63 maybeAdd(tk.string);
70 // if token has a class, it has geat chances of beeing
71 // of interest. Add it to the list of possible completions.
72 // we could skip token of ClassName 'comment'
73 // or 'number' and 'operator'
74 if (tk.className != null) {
75 maybeAdd(tk.string);
76 }
77 // jump to char after end of current token
78 c = tk.end;
64 }
79 }
65 // jump to char after end of current token
66 c = tk.end;
67 }
80 }
81 return found;
68 }
82 }
69 return found;
70 }
71
83
72
84
73 function getCompletions(token,editor)
85 function getCompletions(token, editor) {
74 {
75 var candidates = getAllTokens(editor);
86 var candidates = getAllTokens(editor);
76 // filter all token that have a common start (but nox exactly) the lenght of the current token
87 // filter all token that have a common start (but nox exactly) the lenght of the current token
77 var lambda = function(x){
88 var lambda = function(x) {
78 return (x.indexOf(token.string)==0 && x != token.string)
89 return (x.indexOf(token.string) == 0 && x != token.string)
79 };
90 };
80 var filterd = candidates.filter(lambda);
91 var filterd = candidates.filter(lambda);
81 return filterd;
92 return filterd;
General Comments 0
You need to be logged in to leave comments. Login now