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