##// END OF EJS Templates
band-aid for completion...
MinRK -
Show More
@@ -28,7 +28,7 var IPython = (function (IPython) {
28 if (B.length == 1) {
28 if (B.length == 1) {
29 return B[0];
29 return B[0];
30 }
30 }
31 var A = new Array();
31 var A = [];
32 var common;
32 var common;
33 var min_lead_prct = 10;
33 var min_lead_prct = 10;
34 for (var i = 0; i < B.length; i++) {
34 for (var i = 0; i < B.length; i++) {
@@ -102,7 +102,7 var IPython = (function (IPython) {
102
102
103 Completer.prototype.reinvoke= function(pre_cursor, block, cursor){
103 Completer.prototype.reinvoke= function(pre_cursor, block, cursor){
104 return Completer.reinvoke_re.test(pre_cursor);
104 return Completer.reinvoke_re.test(pre_cursor);
105 }
105 };
106
106
107 /**
107 /**
108 *
108 *
@@ -140,15 +140,15 var IPython = (function (IPython) {
140 // We want a single cursor position.
140 // We want a single cursor position.
141 if (this.editor.somethingSelected()) {
141 if (this.editor.somethingSelected()) {
142 return;
142 return;
143 };
143 }
144
144
145 // one kernel completion came back, finish_completing will be called with the results
145 // one kernel completion came back, finish_completing will be called with the results
146 // we fork here and directly call finish completing if kernel is busy
146 // we fork here and directly call finish completing if kernel is busy
147 if (this.skip_kernel_completion == true) {
147 if (this.skip_kernel_completion) {
148 this.finish_completing({
148 this.finish_completing({
149 'matches': [],
149 'matches': [],
150 matched_text: ""
150 matched_text: ""
151 })
151 });
152 } else {
152 } else {
153 this.cell.kernel.complete(line, cur.ch, $.proxy(this.finish_completing, this));
153 this.cell.kernel.complete(line, cur.ch, $.proxy(this.finish_completing, this));
154 }
154 }
@@ -163,19 +163,20 var IPython = (function (IPython) {
163
163
164 var cur = this.editor.getCursor();
164 var cur = this.editor.getCursor();
165 var results = CodeMirror.contextHint(this.editor);
165 var results = CodeMirror.contextHint(this.editor);
166 var filterd_results = Array();
166 var filtered_results = [];
167 //remove results from context completion
167 //remove results from context completion
168 //that are already in kernel completion
168 //that are already in kernel completion
169 for(var elm in results) {
169 for (var elm in results) {
170 if(_existing_completion(results[elm]['str'], matches) == false)
170 if (!_existing_completion(results[elm].str, matches)) {
171 { filterd_results.push(results[elm]); }
171 filtered_results.push(results[elm]);
172 }
172 }
173 }
173
174
174 // append the introspection result, in order, at at the beginning of
175 // append the introspection result, in order, at at the beginning of
175 // the table and compute the replacement range from current cursor
176 // the table and compute the replacement range from current cursor
176 // positon and matched_text length.
177 // positon and matched_text length.
177 for (var i = matches.length - 1; i >= 0; --i) {
178 for (var i = matches.length - 1; i >= 0; --i) {
178 filterd_results.unshift({
179 filtered_results.unshift({
179 str: matches[i],
180 str: matches[i],
180 type: "introspection",
181 type: "introspection",
181 from: {
182 from: {
@@ -190,13 +191,13 var IPython = (function (IPython) {
190 }
191 }
191
192
192 // one the 2 sources results have been merge, deal with it
193 // one the 2 sources results have been merge, deal with it
193 this.raw_result = filterd_results;
194 this.raw_result = filtered_results;
194
195
195 // if empty result return
196 // if empty result return
196 if (!this.raw_result || !this.raw_result.length) return;
197 if (!this.raw_result || !this.raw_result.length) return;
197
198
198 // When there is only one completion, use it directly.
199 // When there is only one completion, use it directly.
199 if (this.autopick == true && this.raw_result.length == 1) {
200 if (this.autopick && this.raw_result.length == 1) {
200 this.insert(this.raw_result[0]);
201 this.insert(this.raw_result[0]);
201 return;
202 return;
202 }
203 }
@@ -228,7 +229,6 var IPython = (function (IPython) {
228
229
229 // After everything is on the page, compute the postion.
230 // After everything is on the page, compute the postion.
230 // We put it above the code if it is too close to the bottom of the page.
231 // We put it above the code if it is too close to the bottom of the page.
231 var cur = this.editor.getCursor();
232 cur.ch = cur.ch-matched_text.length;
232 cur.ch = cur.ch-matched_text.length;
233 var pos = this.editor.cursorCoords(cur);
233 var pos = this.editor.cursorCoords(cur);
234 var left = pos.left-3;
234 var left = pos.left-3;
@@ -253,6 +253,9 var IPython = (function (IPython) {
253 this.sel.keydown(function (event) {
253 this.sel.keydown(function (event) {
254 that.keydown(event);
254 that.keydown(event);
255 });
255 });
256 this.sel.keypress(function (event) {
257 that.keypress(event);
258 });
256
259
257 this.build_gui_list(this.raw_result);
260 this.build_gui_list(this.raw_result);
258
261
@@ -263,11 +266,11 var IPython = (function (IPython) {
263 if (!this.done) this.sel.focus();
266 if (!this.done) this.sel.focus();
264 }, 100);
267 }, 100);
265 return true;
268 return true;
266 }
269 };
267
270
268 Completer.prototype.insert = function (completion) {
271 Completer.prototype.insert = function (completion) {
269 this.editor.replaceRange(completion.str, completion.from, completion.to);
272 this.editor.replaceRange(completion.str, completion.from, completion.to);
270 }
273 };
271
274
272 Completer.prototype.build_gui_list = function (completions) {
275 Completer.prototype.build_gui_list = function (completions) {
273 for (var i = 0; i < completions.length; ++i) {
276 for (var i = 0; i < completions.length; ++i) {
@@ -276,14 +279,14 var IPython = (function (IPython) {
276 }
279 }
277 this.sel.children().first().attr('selected', 'true');
280 this.sel.children().first().attr('selected', 'true');
278 this.sel.scrollTop(0);
281 this.sel.scrollTop(0);
279 }
282 };
280
283
281 Completer.prototype.close = function () {
284 Completer.prototype.close = function () {
282 if (this.done) return;
285 if (this.done) return;
283 this.done = true;
286 this.done = true;
284 $('.completions').remove();
287 $('.completions').remove();
285 IPython.keyboard_manager.enable();
288 IPython.keyboard_manager.enable();
286 }
289 };
287
290
288 Completer.prototype.pick = function () {
291 Completer.prototype.pick = function () {
289 this.insert(this.raw_result[this.sel[0].selectedIndex]);
292 this.insert(this.raw_result[this.sel[0].selectedIndex]);
@@ -292,8 +295,7 var IPython = (function (IPython) {
292 setTimeout(function () {
295 setTimeout(function () {
293 that.editor.focus();
296 that.editor.focus();
294 }, 50);
297 }, 50);
295 }
298 };
296
297
299
298 Completer.prototype.keydown = function (event) {
300 Completer.prototype.keydown = function (event) {
299 var code = event.keyCode;
301 var code = event.keyCode;
@@ -305,7 +307,7 var IPython = (function (IPython) {
305 if (code == key[_key] ) {
307 if (code == key[_key] ) {
306 special_key = true;
308 special_key = true;
307 }
309 }
308 };
310 }
309
311
310 // Enter
312 // Enter
311 if (code == key.ENTER) {
313 if (code == key.ENTER) {
@@ -317,7 +319,7 var IPython = (function (IPython) {
317 CodeMirror.e_stop(event);
319 CodeMirror.e_stop(event);
318 this.close();
320 this.close();
319 this.editor.focus();
321 this.editor.focus();
320 } else if (code == key.SPACE || code == key.BACKSPACE) {
322 } else if (code == key.BACKSPACE) {
321 this.close();
323 this.close();
322 this.editor.focus();
324 this.editor.focus();
323 } else if (code == key.TAB) {
325 } else if (code == key.TAB) {
@@ -340,16 +342,41 var IPython = (function (IPython) {
340 // need to do that to be able to move the arrow
342 // need to do that to be able to move the arrow
341 // when on the first or last line ofo a code cell
343 // when on the first or last line ofo a code cell
342 event.stopPropagation();
344 event.stopPropagation();
343 } else if (special_key != true) {
344 this.close();
345 this.editor.focus();
346 //we give focus to the editor immediately and call sell in 50 ms
347 setTimeout(function () {
348 that.carry_on_completion();
349 }, 50);
350 }
345 }
351 }
346 };
352
347
348 Completer.prototype.keypress = function (event) {
349 // FIXME: This is a band-aid.
350 // on keypress, trigger insertion of a single character.
351 // This simulates the old behavior of completion as you type,
352 // before events were disconnected and CodeMirror stopped
353 // receiving events while the completer is focused.
354
355 var that = this;
356 var code = event.keyCode;
357
358 // don't handle keypress if it's not a character (arrows on FF)
359 // or ENTER/TAB
360 if (event.charCode === 0 ||
361 code == key.ENTER ||
362 code == key.TAB
363 ) return;
364
365 var cur = this.editor.getCursor();
366 var completion = {
367 str: String.fromCharCode(event.which),
368 type: "introspection",
369 from: cur,
370 to: cur,
371 };
372 this.insert(completion);
373
374 this.close();
375 this.editor.focus();
376 setTimeout(function () {
377 that.carry_on_completion();
378 }, 50);
379 };
353
380
354 IPython.Completer = Completer;
381 IPython.Completer = Completer;
355
382
General Comments 0
You need to be logged in to leave comments. Login now