Merge pull request #4292 from Carreau/more-cpl

improve js-completer logic.
This commit is contained in:
Matthias Bussonnier 2013-10-17 09:59:23 -07:00
commit 40eb635824

View File

@ -9,8 +9,9 @@ var IPython = (function (IPython) {
var key = IPython.utils.keycodes; var key = IPython.utils.keycodes;
function prepend_n_prc(str, n) { function prepend_n_prc(str, n) {
for( var i =0 ; i< n ; i++) for( var i =0 ; i< n ; i++){
{ str = '%'+str } str = '%'+str ;
}
return str; return str;
} }
@ -33,7 +34,7 @@ var IPython = (function (IPython) {
for (var i = 0; i < B.length; i++) { for (var i = 0; i < B.length; i++) {
var str = B[i].str; var str = B[i].str;
var localmin = 0; var localmin = 0;
if(drop_prct == true){ if(drop_prct === true){
while ( str.substr(0, 1) == '%') { while ( str.substr(0, 1) == '%') {
localmin = localmin+1; localmin = localmin+1;
str = str.substring(1); str = str.substring(1);
@ -52,13 +53,13 @@ var IPython = (function (IPython) {
while (s && tem2.indexOf(tem1) == -1) { while (s && tem2.indexOf(tem1) == -1) {
tem1 = tem1.substring(0, --s); tem1 = tem1.substring(0, --s);
} }
if (tem1 == "" || tem2.indexOf(tem1) != 0) { if (tem1 === "" || tem2.indexOf(tem1) !== 0) {
return { return {
str:prepend_n_prc('', min_lead_prct), str:prepend_n_prc('', min_lead_prct),
type: "computed", type: "computed",
from: B[0].from, from: B[0].from,
to: B[0].to to: B[0].to
} };
} }
return { return {
str: prepend_n_prc(tem1, min_lead_prct), str: prepend_n_prc(tem1, min_lead_prct),
@ -94,10 +95,28 @@ var IPython = (function (IPython) {
this.carry_on_completion(true); this.carry_on_completion(true);
}; };
Completer.prototype.carry_on_completion = function (ff) {
// easy access for julia to monkeypatch
//
Completer.reinvoke_re = /[%0-9a-z._/\\:~-]/i;
Completer.prototype.reinvoke= function(pre_cursor, block, cursor){
return Completer.reinvoke_re.test(pre_cursor);
}
/**
*
* pass true as parameter if this is the first invocation of the completer
* this will prevent the completer to dissmiss itself if it is not on a
* word boundary like pressing tab after a space, and make it autopick the
* only choice if there is only one which prevent from popping the UI. as
* well as fast-forwarding the typing if all completion have a common
* shared start
**/
Completer.prototype.carry_on_completion = function (first_invocation) {
// Pass true as parameter if you want the commpleter to autopick when // Pass true as parameter if you want the commpleter to autopick when
// only one completion. This function is automatically reinvoked at // only one completion. This function is automatically reinvoked at
// each keystroke with ff = false // each keystroke with first_invocation = false
var cur = this.editor.getCursor(); var cur = this.editor.getCursor();
var line = this.editor.getLine(cur.line); var line = this.editor.getLine(cur.line);
var pre_cursor = this.editor.getRange({ var pre_cursor = this.editor.getRange({
@ -107,18 +126,21 @@ var IPython = (function (IPython) {
// we need to check that we are still on a word boundary // we need to check that we are still on a word boundary
// because while typing the completer is still reinvoking itself // because while typing the completer is still reinvoking itself
if (!/[%0-9a-z._/\\:~-]/i.test(pre_cursor)) { // so dismiss if we are on a "bad" caracter
if (!this.reinvoke(pre_cursor) && !first_invocation) {
this.close(); this.close();
return; return;
} }
this.autopick = false; this.autopick = false;
if (ff != 'undefined' && ff == true) { if (first_invocation) {
this.autopick = true; this.autopick = true;
} }
// We want a single cursor position. // We want a single cursor position.
if (this.editor.somethingSelected()) return; if (this.editor.somethingSelected()) {
return;
};
// one kernel completion came back, finish_completing will be called with the results // one kernel completion came back, finish_completing will be called with the results
// we fork here and directly call finish completing if kernel is busy // we fork here and directly call finish completing if kernel is busy
@ -198,7 +220,9 @@ var IPython = (function (IPython) {
this.complete = $('<div/>').addClass('completions'); this.complete = $('<div/>').addClass('completions');
this.complete.attr('id', 'complete'); this.complete.attr('id', 'complete');
this.sel = $('<select style="width: auto"/>').attr('multiple', 'true').attr('size', Math.min(10, this.raw_result.length)); this.sel = $('<select style="width: auto"/>')
.attr('multiple', 'true')
.attr('size', Math.min(10, this.raw_result.length));
this.complete.append(this.sel); this.complete.append(this.sel);
$('body').append(this.complete); $('body').append(this.complete);