From 9677bf8a994dc939ecca8ddd071cd35a5b3b582a Mon Sep 17 00:00:00 2001 From: Julien Cochuyt Date: Sun, 13 Dec 2020 09:44:31 +0100 Subject: [PATCH 1/2] Python Console: Fix handling of the tab key in the input pane (#11532) * Support indenting with tabs when editing a non-empty input line * Support tab-completion in the middle of an input line --- source/pythonConsole.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/source/pythonConsole.py b/source/pythonConsole.py index 3612dfdd8b3..3b575a47e33 100755 --- a/source/pythonConsole.py +++ b/source/pythonConsole.py @@ -306,9 +306,11 @@ def historyMove(self, movement): return True RE_COMPLETE_UNIT = re.compile(r"[\w.]*$") + def complete(self): + textBeforeCursor = self.inputCtrl.GetRange(0, self.inputCtrl.GetInsertionPoint()) try: - original = self.RE_COMPLETE_UNIT.search(self.inputCtrl.GetValue()).group(0) + original = self.RE_COMPLETE_UNIT.search(textBeforeCursor).group(0) except AttributeError: return False @@ -373,16 +375,20 @@ def _insertCompletion(self, original, completed): insert = completed[len(original):] if not insert: return - self.inputCtrl.SetValue(self.inputCtrl.GetValue() + insert) + inputCtrl = self.inputCtrl + curPos = inputCtrl.GetInsertionPoint() + prefix = inputCtrl.GetRange(0, curPos) + suffix = inputCtrl.GetRange(curPos, inputCtrl.GetLastPosition()) + inputCtrl.SetValue(prefix + insert + suffix) queueHandler.queueFunction(queueHandler.eventQueue, speech.speakText, insert) - self.inputCtrl.SetInsertionPointEnd() + inputCtrl.SetInsertionPoint(curPos + len(insert)) def onInputChar(self, evt): key = evt.GetKeyCode() if key == wx.WXK_TAB: - line = self.inputCtrl.GetValue() - if line and not line.isspace(): + textBeforeCursor = self.inputCtrl.GetRange(0, self.inputCtrl.GetInsertionPoint()) + if textBeforeCursor and not textBeforeCursor.isspace(): if not self.complete(): wx.Bell() return From fd6f15859768db2be0e5b5b01c55eab3e2462c4a Mon Sep 17 00:00:00 2001 From: Julien Cochuyt Date: Sun, 13 Dec 2020 14:53:00 +0100 Subject: [PATCH 2/2] Python Console: Tab-completion: Handle selection * Consider selection start rather than cursor position (different if selection is anchored at start) * Replace selection upon successful completion --- source/pythonConsole.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/source/pythonConsole.py b/source/pythonConsole.py index 3b575a47e33..33cc9384d93 100755 --- a/source/pythonConsole.py +++ b/source/pythonConsole.py @@ -308,7 +308,7 @@ def historyMove(self, movement): RE_COMPLETE_UNIT = re.compile(r"[\w.]*$") def complete(self): - textBeforeCursor = self.inputCtrl.GetRange(0, self.inputCtrl.GetInsertionPoint()) + textBeforeCursor = self.inputCtrl.GetRange(0, self.inputCtrl.GetSelection()[0]) try: original = self.RE_COMPLETE_UNIT.search(textBeforeCursor).group(0) except AttributeError: @@ -376,18 +376,18 @@ def _insertCompletion(self, original, completed): if not insert: return inputCtrl = self.inputCtrl - curPos = inputCtrl.GetInsertionPoint() - prefix = inputCtrl.GetRange(0, curPos) - suffix = inputCtrl.GetRange(curPos, inputCtrl.GetLastPosition()) + selStartPos, selEndPos = inputCtrl.GetSelection() + prefix = inputCtrl.GetRange(0, selStartPos) + suffix = inputCtrl.GetRange(selEndPos, inputCtrl.GetLastPosition()) inputCtrl.SetValue(prefix + insert + suffix) queueHandler.queueFunction(queueHandler.eventQueue, speech.speakText, insert) - inputCtrl.SetInsertionPoint(curPos + len(insert)) + inputCtrl.SetInsertionPoint(selStartPos + len(insert)) def onInputChar(self, evt): key = evt.GetKeyCode() if key == wx.WXK_TAB: - textBeforeCursor = self.inputCtrl.GetRange(0, self.inputCtrl.GetInsertionPoint()) + textBeforeCursor = self.inputCtrl.GetRange(0, self.inputCtrl.GetSelection()[0]) if textBeforeCursor and not textBeforeCursor.isspace(): if not self.complete(): wx.Bell()