completer.js
276 lines
| 8.8 KiB
| application/javascript
|
JavascriptLexer
|
r7131 | // function completer. | ||
// | ||||
|
r7142 | // completer should be a class that take an cell instance | ||
|
r7191 | var IPython = (function (IPython) { | ||
|
r7142 | // that will prevent us from misspelling | ||
|
r7131 | "use strict"; | ||
// easyier key mapping | ||||
|
r7137 | var key = IPython.utils.keycodes; | ||
|
r7142 | |||
|
r7131 | // what is the common start of all completions | ||
|
r7190 | |||
|
r7192 | function shared_start(B) { | ||
|
r7190 | if (B.length == 1) { | ||
return B[0]; | ||||
} | ||||
var A = new Array(); | ||||
for (var i = 0; i < B.length; i++) { | ||||
A.push(B[i].str); | ||||
} | ||||
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); | ||||
|
r7131 | } | ||
|
r7190 | if (tem1 == "" || tem2.indexOf(tem1) != 0) { | ||
return null; | ||||
|
r7131 | } | ||
|
r7190 | return { | ||
str: tem1, | ||||
type: "computed", | ||||
from: B[0].from, | ||||
to: B[0].to | ||||
}; | ||||
|
r7131 | } | ||
|
r7190 | return null; | ||
} | ||||
|
r7131 | |||
|
r7141 | |||
|
r7191 | var Completer = function (cell) { | ||
|
r7190 | this.editor = cell.code_mirror; | ||
var that = this; | ||||
|
r7191 | $([IPython.events]).on('status_busy.Kernel', function () { | ||
|
r7190 | that.skip_kernel_completion = true; | ||
}); | ||||
|
r7191 | $([IPython.events]).on('status_idle.Kernel', function () { | ||
|
r7190 | that.skip_kernel_completion = false; | ||
}); | ||||
|
r7174 | |||
|
r7131 | |||
|
r7190 | }; | ||
|
r7141 | |||
|
r7174 | |||
|
r7191 | Completer.prototype.startCompletion = function () { | ||
|
r7142 | // call for a 'first' completion, that will set the editor and do some | ||
|
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 | ||||
|
r7192 | this.carry_on_completion(true); | ||
|
r7170 | }; | ||
|
r7131 | |||
|
r7192 | Completer.prototype.carry_on_completion = function (ff) { | ||
|
r7143 | // 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 | ||||
|
r7131 | var cur = this.editor.getCursor(); | ||
|
r7174 | var line = this.editor.getLine(cur.line); | ||
|
r7190 | var pre_cursor = this.editor.getRange({ | ||
line: cur.line, | ||||
ch: cur.ch - 1 | ||||
}, cur); | ||||
|
r7131 | |||
|
r7142 | // we need to check that we are still on a word boundary | ||
|
r7131 | // because while typing the completer is still reinvoking itself | ||
|
r7190 | if (!/[0-9a-z._]/i.test(pre_cursor)) { | ||
this.close(); | ||||
return; | ||||
} | ||||
|
r7142 | |||
|
r7131 | this.autopick = false; | ||
|
r7190 | if (ff != 'undefined' && ff == true) { | ||
this.autopick = true; | ||||
} | ||||
|
r7143 | |||
|
r7131 | // We want a single cursor position. | ||
if (this.editor.somethingSelected()) return; | ||||
|
r7174 | // 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 | ||||
|
r7190 | if (this.skip_kernel_completion == true) { | ||
this.finish_completing({ | ||||
'matches': [], | ||||
matched_text: "" | ||||
}) | ||||
|
r7174 | } else { | ||
|
r7190 | var callbacks = { | ||
'complete_reply': $.proxy(this.finish_completing, this) | ||||
}; | ||||
|
r7174 | IPython.notebook.kernel.complete(line, cur.ch, callbacks); | ||
} | ||||
|
r7170 | }; | ||
|
r7142 | |||
|
r7191 | Completer.prototype.finish_completing = function (content) { | ||
|
r7143 | // let's build a function that wrap all that stuff into what is needed | ||
// for the new completer: | ||||
|
r7168 | var matched_text = content.matched_text; | ||
var matches = content.matches; | ||||
|
r7143 | |||
|
r7142 | var cur = this.editor.getCursor(); | ||
var results = CodeMirror.contextHint(this.editor); | ||||
|
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. | ||||
|
r7190 | for (var i = matches.length - 1; i >= 0; --i) { | ||
results.unshift({ | ||||
str: matches[i], | ||||
type: "introspection", | ||||
from: { | ||||
line: cur.line, | ||||
ch: cur.ch - matched_text.length | ||||
}, | ||||
to: { | ||||
line: cur.line, | ||||
ch: cur.ch | ||||
} | ||||
}); | ||||
|
r7142 | } | ||
// one the 2 sources results have been merge, deal with it | ||||
|
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. | ||||
|
r7190 | if (this.autopick == true && this.raw_result.length == 1) { | ||
|
r7170 | this.insert(this.raw_result[0]); | ||
return; | ||||
} | ||||
|
r7131 | |||
|
r7190 | if (this.raw_result.length == 1) { | ||
|
r7143 | // test if first and only completion totally matches | ||
// what is typed, in this case dismiss | ||||
|
r7170 | var str = this.raw_result[0].str; | ||
|
r7190 | var pre_cursor = this.editor.getRange({ | ||
line: cur.line, | ||||
ch: cur.ch - str.length | ||||
}, cur); | ||||
if (pre_cursor == str) { | ||||
this.close(); | ||||
return; | ||||
} | ||||
|
r7131 | } | ||
this.complete = $('<div/>').addClass('completions'); | ||||
|
r7190 | this.complete.attr('id', 'complete'); | ||
|
r7131 | |||
|
r7190 | this.sel = $('<select/>').attr('multiple', 'true').attr('size', Math.min(10, this.raw_result.length)); | ||
|
r7131 | var pos = this.editor.cursorCoords(); | ||
// TODO: I propose to remove enough horizontal pixel | ||||
// to align the text later | ||||
|
r7190 | this.complete.css('left', pos.x + 'px'); | ||
this.complete.css('top', pos.yBot + 'px'); | ||||
|
r7131 | this.complete.append(this.sel); | ||
$('body').append(this.complete); | ||||
//build the container | ||||
var that = this; | ||||
|
r7191 | this.sel.dblclick(function () { | ||
|
r7190 | that.pick(); | ||
}); | ||||
|
r7131 | this.sel.blur(this.close); | ||
|
r7191 | this.sel.keydown(function (event) { | ||
|
r7190 | that.keydown(event); | ||
}); | ||||
|
r7131 | |||
this.build_gui_list(this.raw_result); | ||||
this.sel.focus(); | ||||
// Opera sometimes ignores focusing a freshly created node | ||||
|
r7191 | if (window.opera) setTimeout(function () { | ||
|
r7190 | if (!this.done) this.sel.focus(); | ||
}, 100); | ||||
|
r7131 | return true; | ||
} | ||||
|
r7191 | Completer.prototype.insert = function (completion) { | ||
|
r7131 | this.editor.replaceRange(completion.str, completion.from, completion.to); | ||
|
r7190 | } | ||
|
r7131 | |||
|
r7191 | Completer.prototype.build_gui_list = function (completions) { | ||
|
r7131 | // Need to clear the all list | ||
for (var i = 0; i < completions.length; ++i) { | ||||
|
r7190 | var opt = $('<option/>').text(completions[i].str).addClass(completions[i].type); | ||
|
r7131 | this.sel.append(opt); | ||
} | ||||
|
r7190 | this.sel.children().first().attr('selected', 'true'); | ||
|
r7131 | } | ||
|
r7191 | Completer.prototype.close = function () { | ||
|
r7190 | if (this.done) return; | ||
this.done = true; | ||||
$('.completions').remove(); | ||||
} | ||||
|
r7131 | |||
|
r7191 | Completer.prototype.pick = function () { | ||
|
r7131 | this.insert(this.raw_result[this.sel[0].selectedIndex]); | ||
this.close(); | ||||
var that = this; | ||||
|
r7191 | setTimeout(function () { | ||
|
r7190 | that.editor.focus(); | ||
}, 50); | ||||
} | ||||
|
r7131 | |||
|
r7142 | |||
|
r7191 | Completer.prototype.keydown = function (event) { | ||
|
r7190 | var code = event.keyCode; | ||
var that = this; | ||||
// Enter | ||||
|
r7193 | if (code == key.ENTER) { | ||
|
r7190 | CodeMirror.e_stop(event); | ||
this.pick(); | ||||
} | ||||
// Escape or backspace | ||||
|
r7193 | else if (code == key.ESC) { | ||
|
r7190 | CodeMirror.e_stop(event); | ||
this.close(); | ||||
this.editor.focus(); | ||||
|
r7193 | } else if (code == key.SPACE || code == key.BACKSPACE) { | ||
|
r7190 | this.close(); | ||
this.editor.focus(); | ||||
|
r7193 | } else if (code == key.TAB) { | ||
|
r7142 | //all the fastforwarding operation, | ||
|
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 | ||||
|
r7192 | var sh = shared_start(this.raw_result); | ||
|
r7190 | if (sh) { | ||
|
r7132 | this.insert(sh); | ||
} | ||||
|
r7142 | this.close(); | ||
|
r7131 | CodeMirror.e_stop(event); | ||
this.editor.focus(); | ||||
//reinvoke self | ||||
|
r7191 | setTimeout(function () { | ||
|
r7192 | that.carry_on_completion(); | ||
|
r7190 | }, 50); | ||
|
r7193 | } else if (code == key.UPARROW || code == key.DOWNARROW) { | ||
|
r7190 | // need to do that to be able to move the arrow | ||
// when on the first or last line ofo a code cell | ||||
event.stopPropagation(); | ||||
|
r7193 | } else if (code != key.UPARROW && code != key.DOWNARROW) { | ||
|
r7190 | this.close(); | ||
this.editor.focus(); | ||||
//we give focus to the editor immediately and call sell in 50 ms | ||||
|
r7191 | setTimeout(function () { | ||
|
r7192 | that.carry_on_completion(); | ||
|
r7190 | }, 50); | ||
} | ||||
|
r7131 | } | ||
IPython.Completer = Completer; | ||||
return IPython; | ||||
}(IPython)); | ||||