Make some improvements and update tests

This commit is contained in:
Veronica Berglyd Olsen
2023-11-06 22:42:02 +01:00
parent f17e1f38de
commit ccd3122633
3 changed files with 33 additions and 23 deletions
+10 -9
View File
@@ -155,12 +155,12 @@ class GuiDocEditor(QPlainTextEdit):
self.followTag1 = QShortcut(self)
self.followTag1.setKey(Qt.Key_Return | Qt.ControlModifier)
self.followTag1.setContext(Qt.WidgetShortcut)
self.followTag1.activated.connect(self._followTag)
self.followTag1.activated.connect(self._processTag)
self.followTag2 = QShortcut(self)
self.followTag2.setKey(Qt.Key_Enter | Qt.ControlModifier)
self.followTag2.setContext(Qt.WidgetShortcut)
self.followTag2.activated.connect(self._followTag)
self.followTag2.activated.connect(self._processTag)
# Set Up Document Word Counter
self.wcTimerDoc = QTimer()
@@ -919,7 +919,7 @@ class GuiDocEditor(QPlainTextEdit):
follow tag function.
"""
if qApp.keyboardModifiers() == Qt.ControlModifier:
self._followTag(self.cursorForPosition(event.pos()))
self._processTag(self.cursorForPosition(event.pos()))
super().mouseReleaseEvent(event)
self.docFooter.updateLineCount()
return
@@ -1008,14 +1008,14 @@ class GuiDocEditor(QPlainTextEdit):
ctxMenu = QMenu(self)
# Follow
status = self._followTag(cursor=pCursor, process=False)
status = self._processTag(cursor=pCursor, follow=False)
if status == nwTrinary.POSITIVE:
aTag = ctxMenu.addAction(self.tr("Follow Tag"))
aTag.triggered.connect(lambda: self._followTag(cursor=pCursor, process=True))
aTag.triggered.connect(lambda: self._processTag(cursor=pCursor, follow=True))
ctxMenu.addSeparator()
elif status == nwTrinary.NEGATIVE:
aTag = ctxMenu.addAction(self.tr("Create Note for Tag"))
aTag.triggered.connect(lambda: self._followTag(cursor=pCursor, process=True))
aTag.triggered.connect(lambda: self._processTag(cursor=pCursor, create=True))
ctxMenu.addSeparator()
# Cut, Copy and Paste
@@ -1696,7 +1696,8 @@ class GuiDocEditor(QPlainTextEdit):
# Internal Functions
##
def _followTag(self, cursor: QTextCursor | None = None, process: bool = True) -> nwTrinary:
def _processTag(self, cursor: QTextCursor | None = None,
follow: bool = True, create: bool = False) -> nwTrinary:
"""Activated by Ctrl+Enter. Checks that we're in a block
starting with '@'. We then find the tag under the cursor and
check that it is not the tag itself. If all this is fine, we
@@ -1734,10 +1735,10 @@ class GuiDocEditor(QPlainTextEdit):
# The keyword cannot be looked up, so we ignore that
return nwTrinary.UNKNOWN
if process and exist:
if follow and exist:
logger.debug("Attempting to follow tag '%s'", tag)
self.loadDocumentTagRequest.emit(tag, nwDocMode.VIEW)
elif process and not exist:
elif create and not exist:
if SHARED.question(self.tr(
"Do you want to create a new project note for the tag '{0}'?"
).format(tag)):
+1 -2
View File
@@ -579,8 +579,7 @@ class GuiProjectTree(QTreeWidget):
if rHandle:
tHandle = SHARED.project.newFile(tag, rHandle)
if tHandle:
text = f"# {tag}\n\n@tag: {tag}\n\n"
SHARED.project.writeNewFile(tHandle, 1, False, text)
SHARED.project.writeNewFile(tHandle, 1, False, f"@tag: {tag}\n\n")
self.revealNewTreeItem(tHandle, wordCount=True)
return True
return False
+22 -12
View File
@@ -29,7 +29,7 @@ from PyQt5.QtGui import QTextBlock, QTextCursor, QTextOption
from PyQt5.QtWidgets import QAction, qApp
from novelwriter import CONFIG, SHARED
from novelwriter.enum import nwDocAction, nwDocInsert, nwItemLayout, nwWidget
from novelwriter.enum import nwDocAction, nwDocInsert, nwItemLayout, nwTrinary, nwWidget
from novelwriter.constants import nwKeyWords, nwUnicode
from novelwriter.core.index import countWords
from novelwriter.gui.doceditor import GuiDocEditor
@@ -1041,7 +1041,7 @@ def testGuiEditor_Tags(qtbot, nwGUI, projPath, ipsumText, mockRnd):
assert nwGUI.openDocument(C.hSceneDoc) is True
# Create Scene
text = "### A Scene\n\n@char: Jane, John\n\n" + ipsumText[0] + "\n\n"
text = "### A Scene\n\n@char: Jane, John\n\n@object: Gun\n\n@:\n\n" + ipsumText[0] + "\n\n"
nwGUI.docEditor.replaceText(text)
# Create Character
@@ -1059,34 +1059,44 @@ def testGuiEditor_Tags(qtbot, nwGUI, projPath, ipsumText, mockRnd):
# Empty Block
nwGUI.docEditor.setCursorLine(2)
assert nwGUI.docEditor._followTag() is False
assert nwGUI.docEditor._processTag() is nwTrinary.UNKNOWN
# Not On Tag
nwGUI.docEditor.setCursorLine(1)
assert nwGUI.docEditor._followTag() is False
assert nwGUI.docEditor._processTag() is nwTrinary.UNKNOWN
# On Tag Keyword
nwGUI.docEditor.setCursorPosition(15)
assert nwGUI.docEditor._followTag() is False
# On Unknown Tag
nwGUI.docEditor.setCursorPosition(28)
assert nwGUI.docEditor._followTag() is True
assert nwGUI.docViewer._docHandle is None
assert nwGUI.docEditor._processTag() is nwTrinary.UNKNOWN
# On Known Tag, No Follow
nwGUI.docEditor.setCursorPosition(22)
assert nwGUI.docEditor._followTag(loadTag=False) is True
assert nwGUI.docEditor._processTag(follow=False) is nwTrinary.POSITIVE
assert nwGUI.docViewer._docHandle is None
# On Known Tag, Follow
nwGUI.docEditor.setCursorPosition(22)
assert nwGUI.docViewer._docHandle is None
assert nwGUI.docEditor._followTag(loadTag=True) is True
assert nwGUI.docEditor._processTag(follow=True) is nwTrinary.POSITIVE
assert nwGUI.docViewer._docHandle == cHandle
assert nwGUI.closeDocViewer() is True
assert nwGUI.docViewer._docHandle is None
# On Unknown Tag, Create It
assert "0000000000011" not in SHARED.project.tree
nwGUI.docEditor.setCursorPosition(28)
assert nwGUI.docEditor._processTag(create=True) is nwTrinary.NEGATIVE
assert "0000000000011" in SHARED.project.tree
# On Unknown Tag, Missing Root
assert "0000000000012" not in SHARED.project.tree
nwGUI.docEditor.setCursorPosition(42)
assert nwGUI.docEditor._processTag(create=True) is nwTrinary.NEGATIVE
assert "0000000000012" not in SHARED.project.tree
nwGUI.docEditor.setCursorPosition(47)
assert nwGUI.docEditor._processTag() is nwTrinary.UNKNOWN
# qtbot.stop()
# END Test testGuiEditor_Tags