From fcfe7507d69af4e8b4f758c6e274025f4c16221d Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Wed, 17 Aug 2022 23:01:03 +0200 Subject: [PATCH] Block adding spaces before colon (French language feature) in certain meta data cases --- novelwriter/gui/doceditor.py | 24 +++++++++++++++++++---- tests/test_gui/test_gui_doceditor.py | 29 +++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/novelwriter/gui/doceditor.py b/novelwriter/gui/doceditor.py index 59275295..f93fcab4 100644 --- a/novelwriter/gui/doceditor.py +++ b/novelwriter/gui/doceditor.py @@ -1982,12 +1982,14 @@ class GuiDocEditor(QTextEdit): tCheck = tInsert if tCheck in self.mainConf.fmtPadBefore: - nDelete = max(nDelete, 1) - tInsert = self._typPadChar + tInsert + if self.allowSpaceBeforeColon(theText, tCheck): + nDelete = max(nDelete, 1) + tInsert = self._typPadChar + tInsert if tCheck in self.mainConf.fmtPadAfter: - nDelete = max(nDelete, 1) - tInsert = tInsert + self._typPadChar + if self.allowSpaceBeforeColon(theText, tCheck): + nDelete = max(nDelete, 1) + tInsert = tInsert + self._typPadChar if nDelete > 0: theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, nDelete) @@ -1995,6 +1997,20 @@ class GuiDocEditor(QTextEdit): return + @staticmethod + def _allowSpaceBeforeColon(text, char): + """Special checker function only used by the insert space + feature for French, Spanish, etc, so it doesn't insert a + sapce before colons in meta data lines. + """ + if char == ":" and len(text) > 1: + if text[0] == "@": + return False + if text[0] == "%": + if text[1:].lstrip()[:9].lower() == "synopsis:": + return False + return True + def _updateHeaders(self, checkPos=False, checkLevel=False): """Update the headers record and return True if anything changed, if a check flag was provided. diff --git a/tests/test_gui/test_gui_doceditor.py b/tests/test_gui/test_gui_doceditor.py index 8057ca41..81fb2249 100644 --- a/tests/test_gui/test_gui_doceditor.py +++ b/tests/test_gui/test_gui_doceditor.py @@ -1189,7 +1189,7 @@ def testGuiEditor_Tags(qtbot, monkeypatch, nwGUI, nwMinimal, ipsumText): @pytest.mark.gui -def testGuiEditor_WordCounters(qtbot, monkeypatch, caplog, nwGUI, nwMinimal, ipsumText): +def testGuiEditor_WordCounters(qtbot, monkeypatch, nwGUI, nwMinimal, ipsumText): """Test saving text from the editor. """ # Block message box @@ -1488,3 +1488,30 @@ def testGuiEditor_Search(qtbot, monkeypatch, nwGUI, nwLipsum): # qtbot.stopForInteraction() # END Test testGuiEditor_Search + + +@pytest.mark.gui +def testGuiEditor_StaticMethods(): + """Test the document editor's static methods. + """ + # Check the method that decides if it is allowed to insert a space + # before a colon using the French, Spanish, etc language feature + assert GuiDocEditor._allowSpaceBeforeColon("", "") is True + assert GuiDocEditor._allowSpaceBeforeColon("", ":") is True + assert GuiDocEditor._allowSpaceBeforeColon("some text", ":") is True + + assert GuiDocEditor._allowSpaceBeforeColon("@:", ":") is False + assert GuiDocEditor._allowSpaceBeforeColon("@>", ">") is True + + assert GuiDocEditor._allowSpaceBeforeColon("%", ":") is True + assert GuiDocEditor._allowSpaceBeforeColon("%:", ":") is True + assert GuiDocEditor._allowSpaceBeforeColon("%synopsis:", ":") is False + assert GuiDocEditor._allowSpaceBeforeColon("%Synopsis:", ":") is False + assert GuiDocEditor._allowSpaceBeforeColon("% synopsis:", ":") is False + assert GuiDocEditor._allowSpaceBeforeColon("% Synopsis:", ":") is False + assert GuiDocEditor._allowSpaceBeforeColon("% synopsis:", ":") is False + assert GuiDocEditor._allowSpaceBeforeColon("% Synopsis:", ":") is False + assert GuiDocEditor._allowSpaceBeforeColon("%synopsis :", ":") is True + assert GuiDocEditor._allowSpaceBeforeColon("%Synopsis :", ":") is True + +# END Test testGuiEditor_MinorMethods