Skip to content

Commit

Permalink
Fix completion starting logic, avoid preventDefault
Browse files Browse the repository at this point in the history
Fixes #245
  • Loading branch information
ileasile committed Jul 18, 2021
1 parent e5987c6 commit 8c0ec16
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions resources/notebook-extension/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,33 @@ define(function(){
return /^[A-Z0-9.:"]$/i.test(key);
}

function _isCompletionChangeBundle(cm, changes, completer) {
var close_completion = false
for (var i = 0; i < changes.length; ++i) {
var change = changes[i];
if (change.origin === "+input") {
for (var j = 0; j < change.text.length; ++j) {
var t = change.text[j];
for (var k = 0; k < t.length; ++k) {
if (_isCompletionKey(t[k])) return true;
}
}
} else {
var line = change.from.line;
var ch = change.from.ch;
if (ch === 0) continue;
var prevChar = cm.getRange({line: line, ch: ch - 1}, change.from);
var removed = change.removed;
if (removed.length > 1 || removed[0].length > 0) {
if (_isCompletionKey(prevChar)) return true;
else close_completion = true;
}
}
}
if (close_completion) completer.close();
return false;
}

Completer.prototype.keypress = function (event) {
/**
* FIXME: This is a band-aid.
Expand Down Expand Up @@ -719,6 +746,10 @@ define(function(){
}

CodeCell.prototype._handle_change = function(cm, changes) {
if (_isCompletionChangeBundle(cm, changes, this.completer)) {
this.completer.startCompletion(false);
}

clearAllErrors(this.notebook);
this.kernel.listErrors(cm.getValue(), (msg) => {
var content = msg.content;
Expand Down Expand Up @@ -851,22 +882,11 @@ define(function(){
// is empty. In this case, let CodeMirror handle indentation.
return false;
} else {
event.preventDefault();
event.codemirrorIgnore = true;

var doAutoPrint = event.keyCode === keycodes.tab;

if (!doAutoPrint && event.key.length === 1) {
editor.replaceRange(event.key, cur, cur);
} else if (event.keyCode === keycodes.backspace) {
var fromInd = this.code_mirror.indexFromPos(cur) - 1;

if (fromInd >= 0) {
editor.replaceRange("", this.code_mirror.posFromIndex(fromInd), cur);
}
if(event.keyCode === keycodes.tab) {
event.preventDefault();
event.codemirrorIgnore = true;
this.completer.startCompletion(true);
}

this.completer.startCompletion(doAutoPrint);
return true;
}
}
Expand Down

0 comments on commit 8c0ec16

Please sign in to comment.