##// END OF EJS Templates
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
Major refactoring of the Notebook, Kernel and CodeCell JavaScript. * Kernel doesn't depend on Notebook or CodeCell. * CodeCell doesn't depend on Notebook, only Kernel. * All of the kernel management logic has been moved out of the Notebook into the Kernel. * Public methods of the Kernel (execute, complete, etc) take a callbacks object that registers the callbacks for that msg. (rebased, cherrypicked, by Bussonnier Matthias <bussonniermatthias@gmail.com>) (and tabs removed) Conflicts: IPython/frontend/html/notebook/static/js/codecell.js IPython/frontend/html/notebook/static/js/completer.js IPython/frontend/html/notebook/static/js/tooltip.js

File last commit:

r7168:cf831228
r7168:cf831228
Show More
completer.js
232 lines | 8.2 KiB | application/javascript | JavascriptLexer
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 // function completer.
//
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142 // completer should be a class that take an cell instance
Matthias BUSSONNIER
implement the completer in a separate class...
r7131
var IPython = (function(IPython ) {
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142 // that will prevent us from misspelling
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 "use strict";
// easyier key mapping
Matthias BUSSONNIER
import Utils keycodes into the completer
r7137 var key = IPython.utils.keycodes;
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 // what is the common start of all completions
function sharedStart(B){
if(B.length == 1){return B[0]}
var A = new Array()
Matthias BUSSONNIER
use strict and clean a little....
r7132 for(var i=0; i< B.length; i++)
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 {
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142 A.push(B[i].str);
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 }
if(A.length > 1 ){
var tem1, tem2, s, A = A.slice(0).sort();
tem1 = A[0];
s = tem1.length;
tem2 = A.pop();
while(s && tem2.indexOf(tem1) == -1){
tem1 = tem1.substring(0, --s);
}
Matthias BUSSONNIER
fix replace range bug...
r7135 if (tem1 == "" || tem2.indexOf(tem1) != 0){return null;}
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 return { str : tem1,
type : "computed",
from : B[0].from,
to : B[0].to
};
}
return null;
}
Matthias BUSSONNIER
move more code into the completer itself
r7141
var Completer = function(cell) {
this.editor = cell.code_mirror;
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 // if last caractere before cursor is not in this, we stop completing
Matthias BUSSONNIER
allow number in tokens
r7167 this.reg = /[0-9a-z.]/i; // casse insensitive
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 }
Matthias BUSSONNIER
move more code into the completer itself
r7141 Completer.prototype.kernelCompletionRequest = function(){
Matthias BUSSONNIER
more cleaning
r7143 var cur = this.editor.getCursor();
Matthias BUSSONNIER
move more code into the completer itself
r7141 var line = this.editor.getLine(cur.line);
Matthias BUSSONNIER
more cleaning
r7143 // one could fork here and directly call finish completing if kernel is busy
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 var callbacks = {'complete_reply': $.proxy(this.finish_completing,this)};
IPython.notebook.kernel.complete(line, cur.ch, callbacks);
Matthias BUSSONNIER
move more code into the completer itself
r7141 }
Matthias BUSSONNIER
use strict and clean a little....
r7132 Completer.prototype.startCompletion = function()
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 {
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142 // call for a 'first' completion, that will set the editor and do some
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 // special behaviour like autopicking if only one completion availlable
//
if (this.editor.somethingSelected()) return;
this.done = false;
// use to get focus back on opera
this.carryOnCompletion(true);
}
Matthias BUSSONNIER
more cleaning
r7143 Completer.prototype.carryOnCompletion = function(ff) {
// Pass true as parameter if you want the commpleter to autopick when
// only one completion. This function is automatically reinvoked at
// each keystroke with ff = false
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 var cur = this.editor.getCursor();
var pre_cursor = this.editor.getRange({line:cur.line,ch:cur.ch-1},cur);
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142 // we need to check that we are still on a word boundary
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 // because while typing the completer is still reinvoking itself
if(!this.reg.test(pre_cursor)){ this.close(); return;}
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 this.autopick = false;
if( ff != 'undefined' && ff==true)
Matthias BUSSONNIER
more cleaning
r7143 { this.autopick=true; }
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 // We want a single cursor position.
if (this.editor.somethingSelected()) return;
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142 //one kernel completion came back, finish_completing will be called with the results
Matthias BUSSONNIER
move more code into the completer itself
r7141 this.kernelCompletionRequest();
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 }
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 Completer.prototype.finish_completing =function (content) {
Matthias BUSSONNIER
more cleaning
r7143 // let's build a function that wrap all that stuff into what is needed
// for the new completer:
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 var matched_text = content.matched_text;
var matches = content.matches;
Matthias BUSSONNIER
more cleaning
r7143
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142 var cur = this.editor.getCursor();
var results = CodeMirror.contextHint(this.editor);
Matthias BUSSONNIER
more cleaning
r7143 // append the introspection result, in order, at at the beginning of
// the table and compute the replacement range from current cursor
// positon and matched_text length.
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142 for(var i= matches.length-1; i>=0 ;--i)
{
results.unshift(
Matthias BUSSONNIER
more cleaning
r7143 { str : matches[i],
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142 type : "introspection",
from : {line: cur.line, ch: cur.ch-matched_text.length},
to : {line: cur.line, ch: cur.ch}
Matthias BUSSONNIER
more cleaning
r7143 })
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142 }
// one the 2 sources results have been merge, deal with it
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 this.raw_result = results;
// if empty result return
if (!this.raw_result || !this.raw_result.length) return;
// When there is only one completion, use it directly.
if (this.autopick == true && this.raw_result.length == 1)
{
this.insert(this.raw_result[0]);
return true;
}
if (this.raw_result.length == 1)
{
Matthias BUSSONNIER
more cleaning
r7143 // test if first and only completion totally matches
// what is typed, in this case dismiss
var str = this.raw_result[0].str
var cur = this.editor.getCursor();
var pre_cursor = this.editor.getRange({line:cur.line,ch:cur.ch-str.length},cur);
if(pre_cursor == str)
{ this.close(); return ; }
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 }
this.complete = $('<div/>').addClass('completions');
this.complete.attr('id','complete');
Matthias BUSSONNIER
use strict and clean a little....
r7132 this.sel = $('<select/>')
.attr('multiple','true')
.attr('size',Math.min(10,this.raw_result.length));
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 var pos = this.editor.cursorCoords();
// TODO: I propose to remove enough horizontal pixel
// to align the text later
this.complete.css('left',pos.x+'px');
this.complete.css('top',pos.yBot+'px');
this.complete.append(this.sel);
$('body').append(this.complete);
//build the container
var that = this;
this.sel.dblclick(function(){that.pick()});
this.sel.blur(this.close);
this.sel.keydown(function(event){that.keydown(event)});
this.build_gui_list(this.raw_result);
this.sel.focus();
// Opera sometimes ignores focusing a freshly created node
if (window.opera) setTimeout(function(){if (!this.done) this.sel.focus();}, 100);
return true;
}
Completer.prototype.insert = function(completion) {
this.editor.replaceRange(completion.str, completion.from, completion.to);
}
Completer.prototype.build_gui_list = function(completions){
// Need to clear the all list
for (var i = 0; i < completions.length; ++i) {
var opt = $('<option/>')
.text(completions[i].str)
.addClass(completions[i].type);
this.sel.append(opt);
}
this.sel.children().first().attr('selected','true');
}
Completer.prototype.close = function() {
if (this.done) return;
this.done = true;
$('.completions').remove();
}
Completer.prototype.pick = function(){
this.insert(this.raw_result[this.sel[0].selectedIndex]);
this.close();
var that = this;
setTimeout(function(){that.editor.focus();}, 50);
}
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 Completer.prototype.keydown = function(event) {
var code = event.keyCode;
// Enter
if (code == key.enter) {CodeMirror.e_stop(event); this.pick();}
// Escape or backspace
else if (code == key.esc ) {CodeMirror.e_stop(event); this.close(); this.editor.focus();}
else if (code == key.space || code == key.backspace) {this.close(); this.editor.focus();}
else if (code == key.tab){
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142 //all the fastforwarding operation,
Matthias BUSSONNIER
use strict and clean a little....
r7132 //Check that shared start is not null which can append with prefixed completion
// like %pylab , pylab have no shred start, and ff will result in py<tab><tab>
// to erase py
Matthias BUSSONNIER
fix replace range bug...
r7135 var sh = sharedStart(this.raw_result);
Matthias BUSSONNIER
use strict and clean a little....
r7132 if(sh){
this.insert(sh);
}
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142 this.close();
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 CodeMirror.e_stop(event);
this.editor.focus();
//reinvoke self
var that = this;
setTimeout(function(){that.carryOnCompletion();}, 50);
}
else if (code == key.upArrow || code == key.downArrow) {
Matthias BUSSONNIER
clean code, remove duplicate unused lines
r7142 // need to do that to be able to move the arrow
Matthias BUSSONNIER
implement the completer in a separate class...
r7131 // when on the first or last line ofo a code cell
event.stopPropagation();
}
else if (code != key.upArrow && code != key.downArrow) {
this.close(); this.editor.focus();
//we give focus to the editor immediately and call sell in 50 ms
var that = this;
setTimeout(function(){that.carryOnCompletion();}, 50);
}
}
IPython.Completer = Completer;
return IPython;
}(IPython));