##// END OF EJS Templates
clean code, remove duplicate unused lines
Matthias BUSSONNIER -
Show More
@@ -50,9 +50,7 var IPython = (function (IPython) {
50 this.collapse();
50 this.collapse();
51
51
52 // construct a completer
52 // construct a completer
53 // And give it the function to call to get the completion list
53 this.completer = new IPython.Completer(this);
54 var that = this;
55 this.completer = new IPython.Completer(that);
56 };
54 };
57
55
58 //TODO, try to diminish the number of parameters.
56 //TODO, try to diminish the number of parameters.
@@ -255,22 +253,6 var IPython = (function (IPython) {
255 // setTimeout(that.remove_and_cancel_tooltip, 5000);
253 // setTimeout(that.remove_and_cancel_tooltip, 5000);
256 };
254 };
257
255
258 // As you type completer
259 // this should be called by the completer, that in return will
260 // be reclled by finish_completing
261 CodeCell.prototype.requestCompletion= function(callback)
262 {
263 console.log('requestiong through cell');
264 this._compcallback = callback;
265 var cur = this.code_mirror.getCursor();
266 var pre_cursor = this.code_mirror.getRange({line:cur.line,ch:0},cur);
267 pre_cursor.trim();
268 // Autocomplete the current line.
269 var line = this.code_mirror.getLine(cur.line);
270 // one could fork here and directly call finish completing
271 // if kernel is busy
272 IPython.notebook.complete_cell(this, line, cur.ch);
273 }
274
256
275 // called when completion came back from the kernel. this will inspect the
257 // called when completion came back from the kernel. this will inspect the
276 // curent cell for (more) completion merge the resuults with the ones
258 // curent cell for (more) completion merge the resuults with the ones
@@ -1,35 +1,22
1 // function completer.
1 // function completer.
2 //
2 //
3 // completer should be a class that take an editor instance, and a list of
3 // completer should be a class that take an cell instance
4 // function to call to get the list of completion.
5 //
6 // the function that send back the list of completion should received the
7 // editor handle as sole argument, and should return a json object with the
8 // following structure
9
10 // {list: clist, # list of n string containing the completions
11 // type : rp, # list of n string containingtype/ origin of the completion
12 // # (will be set as the class of the <option> to be able to style
13 // # them according to the origin of the completion)
14 // from: {line: cur.line, ch: token.start},
15 // to: {line: cur.line, ch: token.end}
16 // };
17 //
4 //
18
5
19 var IPython = (function(IPython ) {
6 var IPython = (function(IPython ) {
20 // that will prevent us froom missspelling
7 // that will prevent us from misspelling
21 "use strict";
8 "use strict";
22
9
23 // easyier key mapping
10 // easyier key mapping
24 var key = IPython.utils.keycodes;
11 var key = IPython.utils.keycodes;
25
12
26 // what is the common start of all completions
13 // what is the common start of all completions
27 function sharedStart(B){
14 function sharedStart(B){
28 if(B.length == 1){return B[0]}
15 if(B.length == 1){return B[0]}
29 var A = new Array()
16 var A = new Array()
30 for(var i=0; i< B.length; i++)
17 for(var i=0; i< B.length; i++)
31 {
18 {
32 A.push(B[i].str);
19 A.push(B[i].str);
33 }
20 }
34 if(A.length > 1 ){
21 if(A.length > 1 ){
35 var tem1, tem2, s, A = A.slice(0).sort();
22 var tem1, tem2, s, A = A.slice(0).sort();
@@ -53,15 +40,12 var IPython = (function(IPython ) {
53 var Completer = function(cell) {
40 var Completer = function(cell) {
54 this.cell = cell;
41 this.cell = cell;
55 this.editor = cell.code_mirror;
42 this.editor = cell.code_mirror;
56 console.log(this.editor);
57 // if last caractere before cursor is not in this, we stop completing
43 // if last caractere before cursor is not in this, we stop completing
58 this.reg = /[A-Za-z.]/;
44 this.reg = /[A-Za-z.]/;
59 }
45 }
60
46
61 Completer.prototype.kernelCompletionRequest = function(){
47 Completer.prototype.kernelCompletionRequest = function(){
62 var cur = this.editor.getCursor();
48 var cur = this.editor.getCursor();
63 var pre_cursor = this.editor.getRange({line:cur.line,ch:0},cur);
64 pre_cursor.trim();
65 // Autocomplete the current line.
49 // Autocomplete the current line.
66 var line = this.editor.getLine(cur.line);
50 var line = this.editor.getLine(cur.line);
67 // one could fork here and directly call finish completing
51 // one could fork here and directly call finish completing
@@ -69,33 +53,10 var IPython = (function(IPython ) {
69 IPython.notebook.complete_cell(this.cell, line, cur.ch);
53 IPython.notebook.complete_cell(this.cell, line, cur.ch);
70 }
54 }
71
55
72 Completer.prototype.finish_completing =function (matched_text, matches) {
73 // let's build a function that wrap all that stuff into what is needed for the
74 // new completer:
75 //
76 var cur = this.editor.getCursor();
77 var res = CodeMirror.contextHint(this.editor);
78
79 // append the introspection result, in order, at
80 // at the beginning of the table and compute the replacement rance
81 // from current cursor positon and matched_text length.
82 for(var i= matches.length-1; i>=0 ;--i)
83 {
84 res.unshift(
85 {
86 str : matches[i],
87 type : "introspection",
88 from : {line: cur.line, ch: cur.ch-matched_text.length},
89 to : {line: cur.line, ch: cur.ch}
90 }
91 )
92 }
93 this._resume_completion(res);
94 };
95
56
96 Completer.prototype.startCompletion = function()
57 Completer.prototype.startCompletion = function()
97 {
58 {
98 // call for a 'first' completion, that will set the editor and do some
59 // call for a 'first' completion, that will set the editor and do some
99 // special behaviour like autopicking if only one completion availlable
60 // special behaviour like autopicking if only one completion availlable
100 //
61 //
101 if (this.editor.somethingSelected()) return;
62 if (this.editor.somethingSelected()) return;
@@ -106,17 +67,17 var IPython = (function(IPython ) {
106
67
107 Completer.prototype.carryOnCompletion = function(ff)
68 Completer.prototype.carryOnCompletion = function(ff)
108 {
69 {
109 // pass true as parameter if you want the commpleter to autopick
70 // pass true as parameter if you want the commpleter to autopick
110 // when only one completion
71 // when only one completion
111 // as this function is auto;atically reinvoked at each keystroke with
72 // as this function is automatically reinvoked at each keystroke with
112 // ff = false
73 // ff = false
113 var cur = this.editor.getCursor();
74 var cur = this.editor.getCursor();
114 var pre_cursor = this.editor.getRange({line:cur.line,ch:cur.ch-1},cur);
75 var pre_cursor = this.editor.getRange({line:cur.line,ch:cur.ch-1},cur);
115
76
116 // we nned to check that we are still on a word boundary
77 // we need to check that we are still on a word boundary
117 // because while typing the completer is still reinvoking itself
78 // because while typing the completer is still reinvoking itself
118 if(!this.reg.test(pre_cursor)){ this.close(); return;}
79 if(!this.reg.test(pre_cursor)){ this.close(); return;}
119
80
120 this.autopick = false;
81 this.autopick = false;
121 if( ff != 'undefined' && ff==true)
82 if( ff != 'undefined' && ff==true)
122 {
83 {
@@ -125,21 +86,38 var IPython = (function(IPython ) {
125 // We want a single cursor position.
86 // We want a single cursor position.
126 if (this.editor.somethingSelected()) return;
87 if (this.editor.somethingSelected()) return;
127
88
128 // there we will need to gather the results for all the function (and merge them ?)
89 //one kernel completion came back, finish_completing will be called with the results
129 // lets assume for now only one source
130 //
131 var that = this;
132 this.kernelCompletionRequest();
90 this.kernelCompletionRequest();
133 }
91 }
134 Completer.prototype._resume_completion = function(results)
92
135 {
93 Completer.prototype.finish_completing =function (matched_text, matches) {
94 // let's build a function that wrap all that stuff into what is needed for the
95 // new completer:
96 //
97 var cur = this.editor.getCursor();
98 var results = CodeMirror.contextHint(this.editor);
99
100 // append the introspection result, in order, at
101 // at the beginning of the table and compute the replacement rance
102 // from current cursor positon and matched_text length.
103 for(var i= matches.length-1; i>=0 ;--i)
104 {
105 results.unshift(
106 {
107 str : matches[i],
108 type : "introspection",
109 from : {line: cur.line, ch: cur.ch-matched_text.length},
110 to : {line: cur.line, ch: cur.ch}
111 }
112 )
113 }
114
115 // one the 2 sources results have been merge, deal with it
136 this.raw_result = results;
116 this.raw_result = results;
137
117
138 // if empty result return
118 // if empty result return
139 if (!this.raw_result || !this.raw_result.length) return;
119 if (!this.raw_result || !this.raw_result.length) return;
140
120
141
142
143 // When there is only one completion, use it directly.
121 // When there is only one completion, use it directly.
144 if (this.autopick == true && this.raw_result.length == 1)
122 if (this.autopick == true && this.raw_result.length == 1)
145 {
123 {
@@ -149,7 +127,7 var IPython = (function(IPython ) {
149
127
150 if (this.raw_result.length == 1)
128 if (this.raw_result.length == 1)
151 {
129 {
152 // test if first and only completion totally matches
130 // test if first and only completion totally matches
153 // what is typed, in this case dismiss
131 // what is typed, in this case dismiss
154 var str = this.raw_result[0].str
132 var str = this.raw_result[0].str
155 var cur = this.editor.getCursor();
133 var cur = this.editor.getCursor();
@@ -182,7 +160,6 var IPython = (function(IPython ) {
182 this.sel.keydown(function(event){that.keydown(event)});
160 this.sel.keydown(function(event){that.keydown(event)});
183
161
184 this.build_gui_list(this.raw_result);
162 this.build_gui_list(this.raw_result);
185 var that=this;
186 //CodeMirror.connect(that.sel, "dblclick", function(){that.pick()});
163 //CodeMirror.connect(that.sel, "dblclick", function(){that.pick()});
187
164
188 this.sel.focus();
165 this.sel.focus();
@@ -205,7 +182,7 var IPython = (function(IPython ) {
205 this.sel.append(opt);
182 this.sel.append(opt);
206 }
183 }
207 this.sel.children().first().attr('selected','true');
184 this.sel.children().first().attr('selected','true');
208
185
209 //sel.size = Math.min(10, completions.length);
186 //sel.size = Math.min(10, completions.length);
210 // Hack to hide the scrollbar.
187 // Hack to hide the scrollbar.
211 //if (completions.length <= 10)
188 //if (completions.length <= 10)
@@ -225,7 +202,7 var IPython = (function(IPython ) {
225 setTimeout(function(){that.editor.focus();}, 50);
202 setTimeout(function(){that.editor.focus();}, 50);
226 }
203 }
227
204
228
205
229 Completer.prototype.keydown = function(event) {
206 Completer.prototype.keydown = function(event) {
230 var code = event.keyCode;
207 var code = event.keyCode;
231 // Enter
208 // Enter
@@ -234,7 +211,7 var IPython = (function(IPython ) {
234 else if (code == key.esc ) {CodeMirror.e_stop(event); this.close(); this.editor.focus();}
211 else if (code == key.esc ) {CodeMirror.e_stop(event); this.close(); this.editor.focus();}
235 else if (code == key.space || code == key.backspace) {this.close(); this.editor.focus();}
212 else if (code == key.space || code == key.backspace) {this.close(); this.editor.focus();}
236 else if (code == key.tab){
213 else if (code == key.tab){
237 //all the fastforwarding operation,
214 //all the fastforwarding operation,
238 //Check that shared start is not null which can append with prefixed completion
215 //Check that shared start is not null which can append with prefixed completion
239 // like %pylab , pylab have no shred start, and ff will result in py<tab><tab>
216 // like %pylab , pylab have no shred start, and ff will result in py<tab><tab>
240 // to erase py
217 // to erase py
@@ -242,7 +219,7 var IPython = (function(IPython ) {
242 if(sh){
219 if(sh){
243 this.insert(sh);
220 this.insert(sh);
244 }
221 }
245 this.close();
222 this.close();
246 CodeMirror.e_stop(event);
223 CodeMirror.e_stop(event);
247 this.editor.focus();
224 this.editor.focus();
248 //reinvoke self
225 //reinvoke self
@@ -250,7 +227,7 var IPython = (function(IPython ) {
250 setTimeout(function(){that.carryOnCompletion();}, 50);
227 setTimeout(function(){that.carryOnCompletion();}, 50);
251 }
228 }
252 else if (code == key.upArrow || code == key.downArrow) {
229 else if (code == key.upArrow || code == key.downArrow) {
253 // need to do that to be able to move the arrow
230 // need to do that to be able to move the arrow
254 // when on the first or last line ofo a code cell
231 // when on the first or last line ofo a code cell
255 event.stopPropagation();
232 event.stopPropagation();
256 }
233 }
General Comments 0
You need to be logged in to leave comments. Login now