Skip to content

Commit d1194a3

Browse files
committed
Add completer processing of left, right and tab keys (#2742)
1 parent d4cfd6e commit d1194a3

2 files changed

Lines changed: 34 additions & 15 deletions

File tree

novelwriter/gui/doceditor.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2637,14 +2637,22 @@ def updateCommentText(self, text: str, pos: int) -> bool:
26372637

26382638
def keyPressEvent(self, event: QKeyEvent) -> None:
26392639
"""Capture keypresses and forward most of them to the editor."""
2640-
if event.key() in (
2641-
Qt.Key.Key_Up, Qt.Key.Key_Down, Qt.Key.Key_Return,
2642-
Qt.Key.Key_Enter, Qt.Key.Key_Escape
2643-
):
2644-
super().keyPressEvent(event)
2645-
else:
2646-
self.close() # Close to release the event lock before forwarding the key press (#2510)
2647-
self._parent.keyPressEvent(event)
2640+
match event.key():
2641+
case Qt.Key.Key_Up | Qt.Key.Key_Down:
2642+
super().keyPressEvent(event)
2643+
case Qt.Key.Key_Right | Qt.Key.Key_Return | Qt.Key.Key_Enter | Qt.Key.Key_Tab:
2644+
if action := self.activeAction():
2645+
action.trigger()
2646+
else:
2647+
self.clear()
2648+
self.close()
2649+
case Qt.Key.Key_Left | Qt.Key.Key_Escape:
2650+
self.clear()
2651+
self.close()
2652+
case _:
2653+
self.clear()
2654+
self.close() # Close to release the event lock before forwarding key press (#2510)
2655+
self._parent.keyPressEvent(event)
26482656

26492657
##
26502658
# Internal Functions

tests/test_gui/test_gui_doceditor.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from novelwriter.core.item import NWItem
3939
from novelwriter.dialogs.editlabel import GuiEditLabel
4040
from novelwriter.enum import nwDocAction, nwDocInsert, nwItemClass, nwItemLayout, nwState
41-
from novelwriter.gui.doceditor import GuiDocEditor, TextAutoReplace, _TagAction
41+
from novelwriter.gui.doceditor import CommandCompleter, GuiDocEditor, TextAutoReplace, _TagAction
4242
from novelwriter.gui.dochighlight import TextBlockData
4343
from novelwriter.text.counting import standardCounter
4444
from novelwriter.types import (
@@ -1964,7 +1964,7 @@ def testGuiEditor_Completer(qtbot, nwGUI, projPath, mockRnd):
19641964
nwGUI.saveDocument()
19651965

19661966
docEditor.replaceText("")
1967-
completer = docEditor._completer
1967+
completer: CommandCompleter = docEditor._completer
19681968

19691969
# Create Scene
19701970
nwGUI.docEditor.setFocus()
@@ -2009,13 +2009,24 @@ def testGuiEditor_Completer(qtbot, nwGUI, projPath, mockRnd):
20092009

20102010
# Selecting "Jane" should insert it
20112011
completer.actions()[0].trigger()
2012-
qtbot.keyClick(docEditor, Qt.Key.Key_Return, delay=KEY_DELAY)
20132012
assert docEditor.getText() == (
20142013
"### Scene One\n\n"
2015-
"@char: Jane\n"
2014+
"@char: Jane"
20162015
)
20172016

2017+
# Adding a comma should reopen it
2018+
qtbot.keyClick(docEditor, ",", delay=KEY_DELAY)
2019+
qtbot.keyClick(docEditor, " ", delay=KEY_DELAY)
2020+
assert [a.text() for a in completer.actions()] == ["Jane", "John"]
2021+
2022+
# Pressing return without selecting anything should just close it
2023+
qtbot.keyClick(completer, Qt.Key.Key_Return, delay=KEY_DELAY)
2024+
assert [a.text() for a in completer.actions()] == []
2025+
qtbot.keyClick(docEditor, Qt.Key.Key_Backspace, delay=KEY_DELAY)
2026+
qtbot.keyClick(docEditor, Qt.Key.Key_Backspace, delay=KEY_DELAY)
2027+
20182028
# Start a new line with a nonsense keyword, which should be handled
2029+
qtbot.keyClick(docEditor, Qt.Key.Key_Return, delay=KEY_DELAY)
20192030
for c in "@: ":
20202031
qtbot.keyClick(docEditor, c, delay=KEY_DELAY)
20212032
qtbot.keyClick(docEditor, Qt.Key.Key_Backspace, delay=KEY_DELAY)
@@ -2074,19 +2085,19 @@ def testGuiEditor_Completer(qtbot, nwGUI, projPath, mockRnd):
20742085
"%Story.Resolution: \n"
20752086
)
20762087

2077-
# Auto-complete note comment
2088+
# Auto-complete note comment, but select with Tab
20782089
SHARED.project.index._itemIndex._cache.note.add("Consistency")
20792090
qtbot.keyClick(docEditor, "%", delay=KEY_DELAY)
20802091
assert len(completer.actions()) == 4
20812092
qtbot.keyClick(completer, Qt.Key.Key_Down, delay=KEY_DELAY)
20822093
qtbot.keyClick(completer, Qt.Key.Key_Down, delay=KEY_DELAY)
20832094
qtbot.keyClick(completer, Qt.Key.Key_Down, delay=KEY_DELAY)
20842095
qtbot.keyClick(completer, Qt.Key.Key_Down, delay=KEY_DELAY)
2085-
qtbot.keyClick(completer, Qt.Key.Key_Return, delay=KEY_DELAY)
2096+
qtbot.keyClick(completer, Qt.Key.Key_Tab, delay=KEY_DELAY)
20862097
qtbot.keyClick(completer, ".", delay=KEY_DELAY)
20872098
assert len(completer.actions()) == 1
20882099
qtbot.keyClick(completer, Qt.Key.Key_Down, delay=KEY_DELAY)
2089-
qtbot.keyClick(completer, Qt.Key.Key_Return, delay=KEY_DELAY)
2100+
qtbot.keyClick(completer, Qt.Key.Key_Tab, delay=KEY_DELAY)
20902101
qtbot.keyClick(docEditor, Qt.Key.Key_Return, delay=KEY_DELAY)
20912102
assert docEditor.getText() == (
20922103
"### Scene One\n\n"

0 commit comments

Comments
 (0)