From 8c0ec16428baf2e9aeb177d6c4ffaea9c4263e48 Mon Sep 17 00:00:00 2001 From: Ilya Muradyan Date: Sun, 18 Jul 2021 07:23:30 +0300 Subject: [PATCH] Fix completion starting logic, avoid preventDefault Fixes #245 --- resources/notebook-extension/kernel.js | 50 ++++++++++++++++++-------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/resources/notebook-extension/kernel.js b/resources/notebook-extension/kernel.js index b87e701a5..7718d022c 100644 --- a/resources/notebook-extension/kernel.js +++ b/resources/notebook-extension/kernel.js @@ -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. @@ -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; @@ -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; } }