diff --git a/novelwriter/gui/doceditor.py b/novelwriter/gui/doceditor.py index 7b1b7867..ceb59dfd 100644 --- a/novelwriter/gui/doceditor.py +++ b/novelwriter/gui/doceditor.py @@ -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)): diff --git a/novelwriter/gui/projtree.py b/novelwriter/gui/projtree.py index ef7c97d2..787f5671 100644 --- a/novelwriter/gui/projtree.py +++ b/novelwriter/gui/projtree.py @@ -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 diff --git a/tests/test_gui/test_gui_doceditor.py b/tests/test_gui/test_gui_doceditor.py index 7bcfc92c..9a28e5c2 100644 --- a/tests/test_gui/test_gui_doceditor.py +++ b/tests/test_gui/test_gui_doceditor.py @@ -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