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