##// END OF EJS Templates
Base of an as you type conpleter....
Matthias BUSSONNIER -
Show More
@@ -406,6 +406,15 b' div.text_cell_render {'
406 406 min-height:50px;
407 407 }
408 408
409 .completions p{
410 background: #DDF;
411 /*outline: none;
412 padding: 0px;*/
413 border-bottom: black solid 1px;
414 padding: 1px;
415 font-family: monospace;
416 }
417
409 418 @media print {
410 419 body { overflow: visible !important; }
411 420 .ui-widget-content { border: 0px; }
@@ -229,9 +229,10 b' var IPython = (function (IPython) {'
229 229 // setTimeout(CodeCell.prototype.remove_and_cancell_tooltip, 5000);
230 230 };
231 231
232
232 // As you type completer
233 233 CodeCell.prototype.finish_completing = function (matched_text, matches) {
234 // console.log("Got matches", matched_text, matches);
234
235 // smart completion, sort kwarg ending with '='
235 236 var newm = new Array();
236 237 if(this.notebook.smart_completer)
237 238 {
@@ -245,6 +246,8 b' var IPython = (function (IPython) {'
245 246 newm = kwargs.concat(other);
246 247 matches=newm;
247 248 }
249 // end sort kwargs
250
248 251 if (!this.is_completing || matches.length === 0) {return;}
249 252
250 253 //try to check if the user is typing tab at least twice after a word
@@ -268,54 +271,90 b' var IPython = (function (IPython) {'
268 271 this.prevmatch="";
269 272 this.npressed=0;
270 273 }
274 // end fallback on tooltip
271 275
276 // Real completion logic start here
272 277 var that = this;
273 278 var cur = this.completion_cursor;
279 var done = false;
280
281 // call to dismmiss the completer
282 var close = function () {
283 if (done) return;
284 done = true;
285 if (complete!=undefined)
286 {complete.remove();}
287 that.is_completing = false;
288 that.completion_cursor = null;
289 };
274 290
291 // insert the given text and exit the completer
275 292 var insert = function (selected_text) {
276 293 that.code_mirror.replaceRange(
277 294 selected_text,
278 295 {line: cur.line, ch: (cur.ch-matched_text.length)},
279 296 {line: cur.line, ch: cur.ch}
280 297 );
298 event.stopPropagation();
299 event.preventDefault();
300 close();
301 setTimeout(function(){that.code_mirror.focus();}, 50);
281 302 };
282 303
304 // insert the curent highlited selection and exit
305 var pick = function () {
306 insert(select.val()[0]);
307 };
308
309 // if only one match, complete to it, don't ask user
283 310 if (matches.length === 1) {
284 311 insert(matches[0]);
285 setTimeout(function(){that.code_mirror.focus();}, 50);
286 312 return;
287 313 };
288 314
289 var complete = $('<div/>').addClass('completions');
290 var select = $('<select/>').attr('multiple','true');
315
316 // Define function to clear the completer, refill it with the new
317 // matches, update the pseuso typing field. Note that this is case
318 // insensitive for now
319 var complete_with = function(matches,typed_text)
320 {
321 //clear the previous completion if any
322 if (matches.length < 1) {
323 insert(typed_text);
324 }
325 complete.children().children().remove();
326 $('#asyoutype').text(typed_text);
327 select=$('#asyoutypeselect');
291 328 for (var i=0; i<matches.length; ++i) {
292 select.append($('<option/>').text(matches[i]));
329 select.append($('<option/>').html(matches[i]));
293 330 }
294 331 select.children().first().attr('selected','true');
332 }
333
334 // create html for completer
335 var complete = $('<div/>').addClass('completions');
336 complete.attr('id','complete');
337 complete.append($('<p/>').attr('id', 'asyoutype').html(matched_text));//pseudo input field
338
339 var select = $('<select/>').attr('multiple','true');
340 select.attr('id', 'asyoutypeselect')
295 341 select.attr('size',Math.min(10,matches.length));
296 342 var pos = this.code_mirror.cursorCoords();
343
344 // TODO: I propose to remove enough horizontal pixel
345 // to align the text later
297 346 complete.css('left',pos.x+'px');
298 347 complete.css('top',pos.yBot+'px');
299 348 complete.append(select);
300 349
301 350 $('body').append(complete);
302 var done = false;
303 351
304 var close = function () {
305 if (done) return;
306 done = true;
307 complete.remove();
308 that.is_completing = false;
309 that.completion_cursor = null;
310 };
352 //do a first actual completion
353 complete_with(matches,matched_text);
311 354
312 var pick = function () {
313 insert(select.val()[0]);
314 close();
315 setTimeout(function(){that.code_mirror.focus();}, 50);
316 };
317
318 select.blur(close);
355 // Give focus to select, and make it filter the match as the user type
356 // by filtering the previous matches
357 typed_characters = "";
319 358 select.keydown(function (event) {
320 359 var code = event.which;
321 360 if (code === 13 || code === 32) {
@@ -327,16 +366,35 b' var IPython = (function (IPython) {'
327 366 // We don't want the document keydown handler to handle UP/DOWN,
328 367 // but we want the default action.
329 368 event.stopPropagation();
369 } else if (code>64 && code <90 || code==8){
370 // issues with _-.. on chrome at least
371 if(code != 8)
372 {
373 var newchar = String.fromCharCode(code).toLowerCase();
374 typed_characters=typed_characters+newchar;
330 375 } else {
331 // All other key presses exit completion.
332 event.stopPropagation();
333 event.preventDefault();
334 close();
335 that.code_mirror.focus();
376 // 8 is backspace remove 1 char cancel if
377 // user have erase everything, otherwise
378 // decrease what we filter with
379 if (typed_characters.length <= 0)
380 {
381 insert(matched_text)
382 }
383 typed_characters=typed_characters.substr(0,typed_characters.length-1);
384 }
385 re = new RegExp("^"+"\%?"+matched_text+typed_characters,"i");
386 filterd= matches.filter(function(x){return re.test(x)});
387 complete_with(filterd,matched_text+typed_characters);
388 } else {
389 // abort with what the user have pressed until now
390 console.log('aborting with keycode : '+code);
391 insert(matched_text+typed_characters);
336 392 }
337 393 });
338 394 // Double click also causes a pick.
395 // and bind the last actions.
339 396 select.dblclick(pick);
397 select.blur(close);
340 398 select.focus();
341 399 };
342 400
General Comments 0
You need to be logged in to leave comments. Login now