completer.js
348 lines
| 11.6 KiB
| application/javascript
|
JavascriptLexer
|
r7131 | // function completer. | ||
// | ||||
|
r13822 | // completer should be a class that takes an cell instance | ||
|
r7191 | var IPython = (function (IPython) { | ||
|
r7142 | // that will prevent us from misspelling | ||
|
r7131 | "use strict"; | ||
|
r13822 | // easier key mapping | ||
|
r15619 | var keycodes = IPython.keyboard.keycodes; | ||
|
r7142 | |||
|
r7349 | function prepend_n_prc(str, n) { | ||
|
r12912 | for( var i =0 ; i< n ; i++){ | ||
str = '%'+str ; | ||||
} | ||||
|
r7348 | return str; | ||
} | ||||
|
r7293 | function _existing_completion(item, completion_array){ | ||
|
r11282 | for( var c in completion_array ) { | ||
if(completion_array[c].trim().substr(-item.length) == item) | ||||
{ return true; } | ||||
} | ||||
|
r7303 | return false; | ||
|
r7288 | } | ||
|
r7190 | |||
|
r7303 | // what is the common start of all completions | ||
|
r7349 | function shared_start(B, drop_prct) { | ||
|
r7190 | if (B.length == 1) { | ||
return B[0]; | ||||
} | ||||
|
r15322 | var A = []; | ||
|
r7348 | var common; | ||
var min_lead_prct = 10; | ||||
|
r7190 | for (var i = 0; i < B.length; i++) { | ||
|
r7349 | var str = B[i].str; | ||
var localmin = 0; | ||||
|
r12912 | if(drop_prct === true){ | ||
|
r7349 | while ( str.substr(0, 1) == '%') { | ||
|
r7348 | localmin = localmin+1; | ||
str = str.substring(1); | ||||
} | ||||
} | ||||
|
r7349 | min_lead_prct = Math.min(min_lead_prct, localmin); | ||
|
r7348 | A.push(str); | ||
|
r7190 | } | ||
|
r7348 | |||
|
r7190 | 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 | } | ||
|
r12912 | if (tem1 === "" || tem2.indexOf(tem1) !== 0) { | ||
|
r12068 | return { | ||
str:prepend_n_prc('', min_lead_prct), | ||||
type: "computed", | ||||
from: B[0].from, | ||||
to: B[0].to | ||||
|
r12912 | }; | ||
|
r7131 | } | ||
|
r7190 | return { | ||
|
r7349 | str: prepend_n_prc(tem1, min_lead_prct), | ||
|
r7190 | type: "computed", | ||
from: B[0].from, | ||||
to: B[0].to | ||||
}; | ||||
|
r7131 | } | ||
|
r7190 | return null; | ||
} | ||||
|
r7131 | |||
|
r7141 | |||
|
r7191 | var Completer = function (cell) { | ||
|
r9221 | this.cell = cell; | ||
this.editor = cell.code_mirror; | ||||
var that = this; | ||||
$([IPython.events]).on('status_busy.Kernel', function () { | ||||
that.skip_kernel_completion = true; | ||||
}); | ||||
$([IPython.events]).on('status_idle.Kernel', function () { | ||||
that.skip_kernel_completion = false; | ||||
}); | ||||
}; | ||||
|
r7141 | |||
|
r7191 | Completer.prototype.startCompletion = function () { | ||
|
r7142 | // call for a 'first' completion, that will set the editor and do some | ||
|
r15952 | // special behavior like autopicking if only one completion available. | ||
|
r7131 | if (this.editor.somethingSelected()) return; | ||
this.done = false; | ||||
// use to get focus back on opera | ||||
|
r7192 | this.carry_on_completion(true); | ||
|
r7170 | }; | ||
|
r7131 | |||
|
r12912 | |||
// 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); | ||||
|
r15322 | }; | ||
|
r12912 | |||
/** | ||||
* | ||||
* 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) { | ||||
|
r13822 | // Pass true as parameter if you want the completer to autopick when | ||
|
r7143 | // only one completion. This function is automatically reinvoked at | ||
|
r12912 | // each keystroke with first_invocation = 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 | ||
|
r12912 | // so dismiss if we are on a "bad" caracter | ||
if (!this.reinvoke(pre_cursor) && !first_invocation) { | ||||
|
r7190 | this.close(); | ||
return; | ||||
} | ||||
|
r7142 | |||
|
r7131 | this.autopick = false; | ||
|
r12912 | if (first_invocation) { | ||
|
r7190 | this.autopick = true; | ||
} | ||||
|
r7143 | |||
|
r7131 | // We want a single cursor position. | ||
|
r12912 | if (this.editor.somethingSelected()) { | ||
return; | ||||
|
r15322 | } | ||
|
r7131 | |||
|
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 | ||||
|
r15322 | if (this.skip_kernel_completion) { | ||
|
r7190 | this.finish_completing({ | ||
'matches': [], | ||||
matched_text: "" | ||||
|
r15322 | }); | ||
|
r7174 | } else { | ||
|
r13208 | this.cell.kernel.complete(line, cur.ch, $.proxy(this.finish_completing, this)); | ||
|
r7174 | } | ||
|
r7170 | }; | ||
|
r7142 | |||
|
r13207 | Completer.prototype.finish_completing = function (msg) { | ||
|
r7143 | // let's build a function that wrap all that stuff into what is needed | ||
// for the new completer: | ||||
|
r13207 | var content = msg.content; | ||
|
r7168 | var matched_text = content.matched_text; | ||
var matches = content.matches; | ||||
|
r7143 | |||
|
r7142 | var cur = this.editor.getCursor(); | ||
var results = CodeMirror.contextHint(this.editor); | ||||
|
r15322 | var filtered_results = []; | ||
|
r7287 | //remove results from context completion | ||
//that are already in kernel completion | ||||
|
r15322 | for (var elm in results) { | ||
if (!_existing_completion(results[elm].str, matches)) { | ||||
filtered_results.push(results[elm]); | ||||
} | ||||
|
r7287 | } | ||
|
r7142 | |||
|
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) { | ||
|
r15322 | filtered_results.unshift({ | ||
|
r7190 | 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 | ||||
|
r15322 | this.raw_result = filtered_results; | ||
|
r7131 | |||
// if empty result return | ||||
if (!this.raw_result || !this.raw_result.length) return; | ||||
// When there is only one completion, use it directly. | ||||
|
r15322 | if (this.autopick && 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 | } | ||
|
r15952 | if (!this.visible) { | ||
console.log('add div'); | ||||
this.complete = $('<div/>').addClass('completions'); | ||||
this.complete.attr('id', 'complete'); | ||||
// Currently webkit doesn't use the size attr correctly. See: | ||||
// https://code.google.com/p/chromium/issues/detail?id=4579 | ||||
this.sel = $('<select style="width: auto"/>') | ||||
.attr('tabindex', -1) | ||||
.attr('multiple', 'true'); | ||||
this.complete.append(this.sel); | ||||
this.visible = true; | ||||
$('body').append(this.complete); | ||||
//build the container | ||||
var that = this; | ||||
this.sel.dblclick(function () { | ||||
that.pick(); | ||||
}); | ||||
|
r15953 | this._handle_keydown = function (cm, event) { | ||
|
r15952 | that.keydown(event); | ||
|
r15953 | }; | ||
this.editor.on('keydown', this._handle_keydown); | ||||
|
r15952 | } | ||
this.sel.attr('size', Math.min(10, this.raw_result.length)); | ||||
|
r12895 | |||
|
r12897 | // After everything is on the page, compute the postion. | ||
// We put it above the code if it is too close to the bottom of the page. | ||||
|
r12895 | cur.ch = cur.ch-matched_text.length; | ||
var pos = this.editor.cursorCoords(cur); | ||||
var left = pos.left-3; | ||||
var top; | ||||
var cheight = this.complete.height(); | ||||
var wheight = $(window).height(); | ||||
if (pos.bottom+cheight+5 > wheight) { | ||||
top = pos.top-cheight-4; | ||||
} else { | ||||
top = pos.bottom+1; | ||||
} | ||||
this.complete.css('left', left + 'px'); | ||||
this.complete.css('top', top + 'px'); | ||||
|
r15952 | // Clear and fill the list. | ||
this.sel.text(''); | ||||
|
r12896 | this.build_gui_list(this.raw_result); | ||
|
r7131 | return true; | ||
|
r15322 | }; | ||
|
r7131 | |||
|
r7191 | Completer.prototype.insert = function (completion) { | ||
|
r7131 | this.editor.replaceRange(completion.str, completion.from, completion.to); | ||
|
r15322 | }; | ||
|
r7131 | |||
|
r12896 | Completer.prototype.build_gui_list = function (completions) { | ||
for (var i = 0; i < completions.length; ++i) { | ||||
var opt = $('<option/>').text(completions[i].str).addClass(completions[i].type); | ||||
this.sel.append(opt); | ||||
|
r7131 | } | ||
|
r12896 | this.sel.children().first().attr('selected', 'true'); | ||
this.sel.scrollTop(0); | ||||
|
r15322 | }; | ||
|
r7131 | |||
|
r7191 | Completer.prototype.close = function () { | ||
|
r7190 | this.done = true; | ||
|
r15779 | $('#complete').remove(); | ||
|
r15953 | if (this._handle_keydown) { | ||
this.editor.off('keydown', this._handle_keydown); | ||||
this._handle_keydown = undefined; | ||||
} | ||||
|
r15952 | this.visible = false; | ||
|
r15322 | }; | ||
|
r7131 | |||
|
r7191 | Completer.prototype.pick = function () { | ||
|
r7131 | this.insert(this.raw_result[this.sel[0].selectedIndex]); | ||
this.close(); | ||||
|
r15322 | }; | ||
|
r7142 | |||
|
r7191 | Completer.prototype.keydown = function (event) { | ||
|
r15953 | console.log(event); | ||
|
r7190 | var code = event.keyCode; | ||
var that = this; | ||||
|
r15953 | if (!this.visible) return; | ||
|
r10239 | |||
|
r7190 | // Enter | ||
|
r15619 | if (code == keycodes.enter) { | ||
|
r15953 | event.stopPropagation(); | ||
event.codemirrorIgnore = true; | ||||
|
r7190 | this.pick(); | ||
// Escape or backspace | ||||
|
r15953 | } else if (code == keycodes.esc || code == keycodes.backspace) { | ||
event.stopPropagation(); | ||||
event.codemirrorIgnore = true; | ||||
|
r7190 | this.close(); | ||
|
r15619 | } else if (code == keycodes.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 | ||||
|
r7349 | var sh = shared_start(this.raw_result, true); | ||
|
r7190 | if (sh) { | ||
|
r7132 | this.insert(sh); | ||
} | ||||
|
r7142 | this.close(); | ||
|
r15953 | event.codemirrorIgnore = true; | ||
event.stopPropagation(); | ||||
|
r7131 | //reinvoke self | ||
|
r7191 | setTimeout(function () { | ||
|
r7192 | that.carry_on_completion(); | ||
|
r7190 | }, 50); | ||
|
r15619 | } else if (code == keycodes.up || code == keycodes.down) { | ||
|
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(); | ||||
|
r15953 | event.codemirrorIgnore = true; | ||
|
r15952 | |||
|
r15953 | var options = this.sel.find('option'); | ||
var index = this.sel[0].selectedIndex; | ||||
if (code == keycodes.up) { | ||||
index--; | ||||
} | ||||
if (code == keycodes.down) { | ||||
index++; | ||||
} | ||||
index = Math.min(Math.max(index, 0), options.length-1); | ||||
console.log('compl set index', index); | ||||
this.sel[0].selectedIndex = index; | ||||
} | ||||
|
r15322 | }; | ||
|
r7131 | |||
IPython.Completer = Completer; | ||||
return IPython; | ||||
}(IPython)); | ||||