move more code into the completer itself

This commit is contained in:
Matthias BUSSONNIER 2012-02-29 11:39:36 +01:00 committed by Brian Granger
parent e4784b984b
commit 6a658a6fa8
2 changed files with 45 additions and 28 deletions

View File

@ -52,7 +52,7 @@ var IPython = (function (IPython) {
// construct a completer
// And give it the function to call to get the completion list
var that = this;
this.completer = new IPython.Completer(this.code_mirror,function(callback){that.requestCompletion(callback)});
this.completer = new IPython.Completer(that);
};
//TODO, try to diminish the number of parameters.
@ -260,6 +260,7 @@ var IPython = (function (IPython) {
// be reclled by finish_completing
CodeCell.prototype.requestCompletion= function(callback)
{
console.log('requestiong through cell');
this._compcallback = callback;
var cur = this.code_mirror.getCursor();
var pre_cursor = this.code_mirror.getRange({line:cur.line,ch:0},cur);
@ -275,28 +276,8 @@ var IPython = (function (IPython) {
// curent cell for (more) completion merge the resuults with the ones
// comming from the kernel and forward it to the completer
CodeCell.prototype.finish_completing = function (matched_text, matches) {
// let's build a function that wrap all that stuff into what is needed for the
// new completer:
//
var cur = this.code_mirror.getCursor();
var res = CodeMirror.contextHint(this.code_mirror);
// append the introspection result, in order, at
// at the beginning of the table and compute the replacement rance
// from current cursor positon and matched_text length.
for(var i= matches.length-1; i>=0 ;--i)
{
res.unshift(
{
str : matches[i],
type : "introspection",
from : {line: cur.line, ch: cur.ch-matched_text.length},
to : {line: cur.line, ch: cur.ch}
}
)
}
this._compcallback(res);
};
this.completer.finish_completing(matched_text,matches);
}
CodeCell.prototype.select = function () {

View File

@ -49,14 +49,50 @@ var IPython = (function(IPython ) {
return null;
}
// user to nsert the given completion
var Completer = function(editor,getHints) {
this.editor = editor;
this.hintfunc = getHints;
var Completer = function(cell) {
this.cell = cell;
this.editor = cell.code_mirror;
console.log(this.editor);
// if last caractere before cursor is not in this, we stop completing
this.reg = /[A-Za-z.]/;
}
Completer.prototype.kernelCompletionRequest = function(){
var cur = this.editor.getCursor();
var pre_cursor = this.editor.getRange({line:cur.line,ch:0},cur);
pre_cursor.trim();
// Autocomplete the current line.
var line = this.editor.getLine(cur.line);
// one could fork here and directly call finish completing
// if kernel is busy
IPython.notebook.complete_cell(this.cell, line, cur.ch);
}
Completer.prototype.finish_completing =function (matched_text, matches) {
// let's build a function that wrap all that stuff into what is needed for the
// new completer:
//
var cur = this.editor.getCursor();
var res = CodeMirror.contextHint(this.editor);
// append the introspection result, in order, at
// at the beginning of the table and compute the replacement rance
// from current cursor positon and matched_text length.
for(var i= matches.length-1; i>=0 ;--i)
{
res.unshift(
{
str : matches[i],
type : "introspection",
from : {line: cur.line, ch: cur.ch-matched_text.length},
to : {line: cur.line, ch: cur.ch}
}
)
}
this._resume_completion(res);
};
Completer.prototype.startCompletion = function()
{
// call for a 'first' completion, that will set the editor and do some
@ -93,7 +129,7 @@ var IPython = (function(IPython ) {
// lets assume for now only one source
//
var that = this;
this.hintfunc(function(result){that._resume_completion(result)});
this.kernelCompletionRequest();
}
Completer.prototype._resume_completion = function(results)
{