From 6b2d9ccee443e570bb0a999011dadb0aafa381e1 2011-11-29 16:54:27 From: Matthias BUSSONNIER Date: 2011-11-29 16:54:27 Subject: [PATCH] Base of an as you type conpleter. when invoking the completer, instead of having to chose/dismiss, you can continue typing, it will filter the result "as you type" and dismiss itself if ther is no match left. As it is now, it's only works with lowercase letters, I need to find a workaroud for this. for example if you type : * P-y--S-o-m-e-t-h-i-n-g * it will propose PySide, but will dismiss when 'o' is pressed and pasting Pyso with a lower case 's' --- diff --git a/IPython/frontend/html/notebook/static/css/notebook.css b/IPython/frontend/html/notebook/static/css/notebook.css index d2a37af..01d376c 100644 --- a/IPython/frontend/html/notebook/static/css/notebook.css +++ b/IPython/frontend/html/notebook/static/css/notebook.css @@ -406,6 +406,15 @@ div.text_cell_render { min-height:50px; } +.completions p{ + background: #DDF; + /*outline: none; + padding: 0px;*/ + border-bottom: black solid 1px; + padding: 1px; + font-family: monospace; +} + @media print { body { overflow: visible !important; } .ui-widget-content { border: 0px; } diff --git a/IPython/frontend/html/notebook/static/js/codecell.js b/IPython/frontend/html/notebook/static/js/codecell.js index 8aa7441..6d79011 100644 --- a/IPython/frontend/html/notebook/static/js/codecell.js +++ b/IPython/frontend/html/notebook/static/js/codecell.js @@ -229,9 +229,10 @@ var IPython = (function (IPython) { // setTimeout(CodeCell.prototype.remove_and_cancell_tooltip, 5000); }; - + // As you type completer CodeCell.prototype.finish_completing = function (matched_text, matches) { - // console.log("Got matches", matched_text, matches); + + // smart completion, sort kwarg ending with '=' var newm = new Array(); if(this.notebook.smart_completer) { @@ -245,6 +246,8 @@ var IPython = (function (IPython) { newm = kwargs.concat(other); matches=newm; } + // end sort kwargs + if (!this.is_completing || matches.length === 0) {return;} //try to check if the user is typing tab at least twice after a word @@ -268,54 +271,90 @@ var IPython = (function (IPython) { this.prevmatch=""; this.npressed=0; } + // end fallback on tooltip + // Real completion logic start here var that = this; var cur = this.completion_cursor; + var done = false; + + // call to dismmiss the completer + var close = function () { + if (done) return; + done = true; + if (complete!=undefined) + {complete.remove();} + that.is_completing = false; + that.completion_cursor = null; + }; + // insert the given text and exit the completer var insert = function (selected_text) { that.code_mirror.replaceRange( selected_text, {line: cur.line, ch: (cur.ch-matched_text.length)}, {line: cur.line, ch: cur.ch} ); + event.stopPropagation(); + event.preventDefault(); + close(); + setTimeout(function(){that.code_mirror.focus();}, 50); + }; + + // insert the curent highlited selection and exit + var pick = function () { + insert(select.val()[0]); }; + // if only one match, complete to it, don't ask user if (matches.length === 1) { insert(matches[0]); - setTimeout(function(){that.code_mirror.focus();}, 50); return; }; + + // Define function to clear the completer, refill it with the new + // matches, update the pseuso typing field. Note that this is case + // insensitive for now + var complete_with = function(matches,typed_text) + { + //clear the previous completion if any + if (matches.length < 1) { + insert(typed_text); + } + complete.children().children().remove(); + $('#asyoutype').text(typed_text); + select=$('#asyoutypeselect'); + for (var i=0; i').html(matches[i])); + } + select.children().first().attr('selected','true'); + } + + // create html for completer var complete = $('
').addClass('completions'); + complete.attr('id','complete'); + complete.append($('

').attr('id', 'asyoutype').html(matched_text));//pseudo input field + var select = $('