From 1aeb66d9af08691a57e3f1437e6e499fdb081740 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 1 Oct 2022 22:35:00 +0200 Subject: [PATCH 1/4] Make sure existing space is stripped before inserting non-breaking space (#1061) --- novelwriter/gui/doceditor.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/novelwriter/gui/doceditor.py b/novelwriter/gui/doceditor.py index 134b2e1c..e10f3fb4 100644 --- a/novelwriter/gui/doceditor.py +++ b/novelwriter/gui/doceditor.py @@ -111,6 +111,7 @@ class GuiDocEditor(QTextEdit): self._typSQOpen = "'" self._typSQClose = "'" self._typPadChar = " " + self._doSpacePad = False # Core Elements and Signals qDoc = self.document() @@ -221,6 +222,7 @@ class GuiDocEditor(QTextEdit): self._nonWord += "".join(self.mainConf.fmtSingleQuotes) # Typography + self._doSpacePad = bool(self.mainConf.fmtPadBefore) or bool(self.mainConf.fmtPadAfter) if self.mainConf.fmtPadThin: self._typPadChar = nwUnicode.U_THNBSP else: @@ -1992,16 +1994,21 @@ class GuiDocEditor(QTextEdit): nDelete = 3 tInsert = nwUnicode.U_HELLIP - tCheck = tInsert - if tCheck in self.mainConf.fmtPadBefore: - if self._allowSpaceBeforeColon(theText, tCheck): - nDelete = max(nDelete, 1) - tInsert = self._typPadChar + tInsert + if self._doSpacePad: + tCheck = tInsert + if tCheck in self.mainConf.fmtPadBefore: + if self._allowSpaceBeforeColon(theText, tCheck): + nDelete = max(nDelete, 1) + chkPos = thePos - nDelete - 1 + if chkPos >= 0 and theText[chkPos].isspace(): + # Strip existing space before inserting a new (#1061) + nDelete += 1 + tInsert = self._typPadChar + tInsert - if tCheck in self.mainConf.fmtPadAfter: - if self._allowSpaceBeforeColon(theText, tCheck): - nDelete = max(nDelete, 1) - tInsert = tInsert + self._typPadChar + if tCheck in self.mainConf.fmtPadAfter: + if self._allowSpaceBeforeColon(theText, tCheck): + nDelete = max(nDelete, 1) + tInsert = tInsert + self._typPadChar if nDelete > 0: theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, nDelete) From 03db815723d0992b167cec63f1988cd667a53572 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 1 Oct 2022 22:35:26 +0200 Subject: [PATCH 2/4] Update test coverage --- tests/reference/guiEditor_Main_Final_000000000000f.nwd | 2 ++ tests/reference/guiEditor_Main_Final_nwProject.nwx | 8 ++++---- tests/test_gui/test_gui_guimain.py | 9 +++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/tests/reference/guiEditor_Main_Final_000000000000f.nwd b/tests/reference/guiEditor_Main_Final_000000000000f.nwd index 1ac700fb..ab603231 100644 --- a/tests/reference/guiEditor_Main_Final_000000000000f.nwd +++ b/tests/reference/guiEditor_Main_Final_000000000000f.nwd @@ -35,6 +35,8 @@ Some “ double quoted text with spaces padded ”. Add space before this colon : See? +But don’t add a double space : See? + “Tab-indented text” >“Paragraph-indented text” diff --git a/tests/reference/guiEditor_Main_Final_nwProject.nwx b/tests/reference/guiEditor_Main_Final_nwProject.nwx index 5496b5b3..f61d3493 100644 --- a/tests/reference/guiEditor_Main_Final_nwProject.nwx +++ b/tests/reference/guiEditor_Main_Final_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project New Novel @@ -17,8 +17,8 @@ None 0000000000008 0000000000008 - 145 - 118 + 153 + 126 27 @@ -59,7 +59,7 @@ New Chapter - + New Scene diff --git a/tests/test_gui/test_gui_guimain.py b/tests/test_gui/test_gui_guimain.py index b2e446bc..1dd69314 100644 --- a/tests/test_gui/test_gui_guimain.py +++ b/tests/test_gui/test_gui_guimain.py @@ -412,6 +412,7 @@ def testGuiMain_Editing(qtbot, monkeypatch, nwGUI, fncProj, refDir, outDir, mock # Insert spaces before and after quotes nwGUI.mainConf.fmtPadBefore = "\u201d" nwGUI.mainConf.fmtPadAfter = "\u201c" + nwGUI.docEditor.initEditor() for c in "Some \"double quoted text with spaces padded\".": qtbot.keyClick(nwGUI.docEditor, c, delay=typeDelay) @@ -420,9 +421,11 @@ def testGuiMain_Editing(qtbot, monkeypatch, nwGUI, fncProj, refDir, outDir, mock nwGUI.mainConf.fmtPadBefore = "" nwGUI.mainConf.fmtPadAfter = "" + nwGUI.docEditor.initEditor() # Insert spaces before colon, but ignore tags and synopsis nwGUI.mainConf.fmtPadBefore = ":" + nwGUI.docEditor.initEditor() for c in "@object: NoSpaceAdded": qtbot.keyClick(nwGUI.docEditor, c, delay=typeDelay) @@ -439,7 +442,13 @@ def testGuiMain_Editing(qtbot, monkeypatch, nwGUI, fncProj, refDir, outDir, mock qtbot.keyClick(nwGUI.docEditor, Qt.Key_Return, delay=keyDelay) qtbot.keyClick(nwGUI.docEditor, Qt.Key_Return, delay=keyDelay) + for c in "But don't add a double space : See?": + qtbot.keyClick(nwGUI.docEditor, c, delay=typeDelay) + qtbot.keyClick(nwGUI.docEditor, Qt.Key_Return, delay=keyDelay) + qtbot.keyClick(nwGUI.docEditor, Qt.Key_Return, delay=keyDelay) + nwGUI.mainConf.fmtPadBefore = "" + nwGUI.docEditor.initEditor() # Indent and Align # ================ From a24d37ffa4998a4b8c473d36040f5dd64e74070d Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 1 Oct 2022 22:41:18 +0200 Subject: [PATCH 3/4] Simplify the auto-insert space check code as this is efficient enough --- novelwriter/gui/doceditor.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/novelwriter/gui/doceditor.py b/novelwriter/gui/doceditor.py index e10f3fb4..cfcb8409 100644 --- a/novelwriter/gui/doceditor.py +++ b/novelwriter/gui/doceditor.py @@ -111,7 +111,6 @@ class GuiDocEditor(QTextEdit): self._typSQOpen = "'" self._typSQClose = "'" self._typPadChar = " " - self._doSpacePad = False # Core Elements and Signals qDoc = self.document() @@ -222,7 +221,6 @@ class GuiDocEditor(QTextEdit): self._nonWord += "".join(self.mainConf.fmtSingleQuotes) # Typography - self._doSpacePad = bool(self.mainConf.fmtPadBefore) or bool(self.mainConf.fmtPadAfter) if self.mainConf.fmtPadThin: self._typPadChar = nwUnicode.U_THNBSP else: @@ -1994,21 +1992,20 @@ class GuiDocEditor(QTextEdit): nDelete = 3 tInsert = nwUnicode.U_HELLIP - if self._doSpacePad: - tCheck = tInsert - if tCheck in self.mainConf.fmtPadBefore: - if self._allowSpaceBeforeColon(theText, tCheck): - nDelete = max(nDelete, 1) - chkPos = thePos - nDelete - 1 - if chkPos >= 0 and theText[chkPos].isspace(): - # Strip existing space before inserting a new (#1061) - nDelete += 1 - tInsert = self._typPadChar + tInsert + tCheck = tInsert + if self.mainConf.fmtPadBefore and tCheck in self.mainConf.fmtPadBefore: + if self._allowSpaceBeforeColon(theText, tCheck): + nDelete = max(nDelete, 1) + chkPos = thePos - nDelete - 1 + if chkPos >= 0 and theText[chkPos].isspace(): + # Strip existing space before inserting a new (#1061) + nDelete += 1 + tInsert = self._typPadChar + tInsert - if tCheck in self.mainConf.fmtPadAfter: - if self._allowSpaceBeforeColon(theText, tCheck): - nDelete = max(nDelete, 1) - tInsert = tInsert + self._typPadChar + if self.mainConf.fmtPadAfter and tCheck in self.mainConf.fmtPadAfter: + if self._allowSpaceBeforeColon(theText, tCheck): + nDelete = max(nDelete, 1) + tInsert = tInsert + self._typPadChar if nDelete > 0: theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, nDelete) From 47c39e68b7786e79bf1f9304380ef295548a52a6 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 1 Oct 2022 22:44:11 +0200 Subject: [PATCH 4/4] Remove the no longer needed changes to the test --- tests/test_gui/test_gui_guimain.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/test_gui/test_gui_guimain.py b/tests/test_gui/test_gui_guimain.py index 1dd69314..fd1218a0 100644 --- a/tests/test_gui/test_gui_guimain.py +++ b/tests/test_gui/test_gui_guimain.py @@ -412,7 +412,6 @@ def testGuiMain_Editing(qtbot, monkeypatch, nwGUI, fncProj, refDir, outDir, mock # Insert spaces before and after quotes nwGUI.mainConf.fmtPadBefore = "\u201d" nwGUI.mainConf.fmtPadAfter = "\u201c" - nwGUI.docEditor.initEditor() for c in "Some \"double quoted text with spaces padded\".": qtbot.keyClick(nwGUI.docEditor, c, delay=typeDelay) @@ -421,11 +420,9 @@ def testGuiMain_Editing(qtbot, monkeypatch, nwGUI, fncProj, refDir, outDir, mock nwGUI.mainConf.fmtPadBefore = "" nwGUI.mainConf.fmtPadAfter = "" - nwGUI.docEditor.initEditor() # Insert spaces before colon, but ignore tags and synopsis nwGUI.mainConf.fmtPadBefore = ":" - nwGUI.docEditor.initEditor() for c in "@object: NoSpaceAdded": qtbot.keyClick(nwGUI.docEditor, c, delay=typeDelay) @@ -448,7 +445,6 @@ def testGuiMain_Editing(qtbot, monkeypatch, nwGUI, fncProj, refDir, outDir, mock qtbot.keyClick(nwGUI.docEditor, Qt.Key_Return, delay=keyDelay) nwGUI.mainConf.fmtPadBefore = "" - nwGUI.docEditor.initEditor() # Indent and Align # ================