Update editor and highlight, and update tests

This commit is contained in:
Veronica Berglyd Olsen
2024-04-28 18:05:53 +02:00
parent e9596e1bbc
commit e01e14075c
4 changed files with 829 additions and 706 deletions
+25 -28
View File
@@ -39,8 +39,8 @@ from time import time
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from PyQt5.QtCore import ( from PyQt5.QtCore import (
pyqtSignal, pyqtSlot, QObject, QPoint, QRegularExpression, QRunnable, Qt, QObject, QPoint, QRegularExpression, QRunnable, Qt, QTimer, pyqtSignal,
QTimer pyqtSlot
) )
from PyQt5.QtGui import ( from PyQt5.QtGui import (
QColor, QCursor, QFont, QKeyEvent, QKeySequence, QMouseEvent, QPalette, QColor, QCursor, QFont, QKeyEvent, QKeySequence, QMouseEvent, QPalette,
@@ -65,7 +65,7 @@ from novelwriter.text.counting import standardCounter
from novelwriter.tools.lipsum import GuiLipsum from novelwriter.tools.lipsum import GuiLipsum
from novelwriter.types import ( from novelwriter.types import (
QtAlignCenterTop, QtAlignJustify, QtAlignLeft, QtAlignLeftTop, QtAlignCenterTop, QtAlignJustify, QtAlignLeft, QtAlignLeftTop,
QtAlignRight, QtKeepAnchor, QtModCtrl, QtMouseLeft, QtModeNone, QtModShift, QtAlignRight, QtKeepAnchor, QtModCtrl, QtModeNone, QtModShift, QtMouseLeft,
QtMoveAnchor, QtMoveLeft, QtMoveRight QtMoveAnchor, QtMoveLeft, QtMoveRight
) )
@@ -840,14 +840,15 @@ class GuiDocEditor(QPlainTextEdit):
) )
return return
def insertText(self, insert: str | nwDocInsert) -> bool: def insertText(self, insert: str | nwDocInsert) -> None:
"""Insert a specific type of text at the cursor position.""" """Insert a specific type of text at the cursor position."""
if self._docHandle is None: if self._docHandle is None:
logger.error("No document open") logger.error("No document open")
return False return
newBlock = False text = ""
goAfter = False block = False
after = False
if isinstance(insert, str): if isinstance(insert, str):
text = insert text = insert
@@ -862,46 +863,41 @@ class GuiDocEditor(QPlainTextEdit):
text = self._typDQuoteC text = self._typDQuoteC
elif insert == nwDocInsert.SYNOPSIS: elif insert == nwDocInsert.SYNOPSIS:
text = "%Synopsis: " text = "%Synopsis: "
newBlock = True block = True
goAfter = True after = True
elif insert == nwDocInsert.SHORT: elif insert == nwDocInsert.SHORT:
text = "%Short: " text = "%Short: "
newBlock = True block = True
goAfter = True after = True
elif insert == nwDocInsert.NEW_PAGE: elif insert == nwDocInsert.NEW_PAGE:
text = "[newpage]" text = "[newpage]"
newBlock = True block = True
goAfter = False after = False
elif insert == nwDocInsert.VSPACE_S: elif insert == nwDocInsert.VSPACE_S:
text = "[vspace]" text = "[vspace]"
newBlock = True block = True
goAfter = False after = False
elif insert == nwDocInsert.VSPACE_M: elif insert == nwDocInsert.VSPACE_M:
text = "[vspace:2]" text = "[vspace:2]"
newBlock = True block = True
goAfter = False after = False
elif insert == nwDocInsert.LIPSUM: elif insert == nwDocInsert.LIPSUM:
text = GuiLipsum.getLipsum(self) text = GuiLipsum.getLipsum(self)
newBlock = True block = True
goAfter = False after = False
elif insert == nwDocInsert.FOOTNOTE: elif insert == nwDocInsert.FOOTNOTE:
text = ""
self._insertCommentStructure(nwComment.FOOTNOTE) self._insertCommentStructure(nwComment.FOOTNOTE)
else:
return False
else:
return False
if text: if text:
if newBlock: if block:
self.insertNewBlock(text, defaultAfter=goAfter) self.insertNewBlock(text, defaultAfter=after)
else: else:
cursor = self.textCursor() cursor = self.textCursor()
cursor.beginEditBlock() cursor.beginEditBlock()
cursor.insertText(text) cursor.insertText(text)
cursor.endEditBlock() cursor.endEditBlock()
return True return
def insertNewBlock(self, text: str, defaultAfter: bool = True) -> bool: def insertNewBlock(self, text: str, defaultAfter: bool = True) -> bool:
"""Insert a piece of text on a blank line.""" """Insert a piece of text on a blank line."""
@@ -1167,7 +1163,8 @@ class GuiDocEditor(QPlainTextEdit):
lambda _, option=option: self._correctWord(sCursor, option) lambda _, option=option: self._correctWord(sCursor, option)
) )
else: else:
ctxMenu.addAction("%s %s" % (nwUnicode.U_ENDASH, self.tr("No Suggestions"))) trNone = self.tr("No Suggestions")
ctxMenu.addAction(f"{nwUnicode.U_ENDASH} {trNone}")
ctxMenu.addSeparator() ctxMenu.addSeparator()
action = ctxMenu.addAction(self.tr("Add Word to Dictionary")) action = ctxMenu.addAction(self.tr("Add Word to Dictionary"))
+6 -5
View File
@@ -358,12 +358,12 @@ class GuiDocHighlighter(QSyntaxHighlighter):
elif text.startswith("%"): # Comments elif text.startswith("%"): # Comments
self.setCurrentBlockState(BLOCK_TEXT) self.setCurrentBlockState(BLOCK_TEXT)
hRules = self._cmnRules
cStyle, cMod, _, cDot, cPos = processComment(text) cStyle, cMod, _, cDot, cPos = processComment(text)
cLen = len(text) - cPos cLen = len(text) - cPos
xOff = cPos xOff = cPos
if cMod == "ERR": if cStyle == nwComment.PLAIN:
self.setFormat(0, cPos, self._hStyles["invalid"])
elif cStyle == nwComment.PLAIN:
self.setFormat(0, cLen, self._hStyles["hidden"]) self.setFormat(0, cLen, self._hStyles["hidden"])
elif cStyle == nwComment.IGNORE: elif cStyle == nwComment.IGNORE:
self.setFormat(0, cLen, self._hStyles["strike"]) self.setFormat(0, cLen, self._hStyles["strike"])
@@ -376,9 +376,10 @@ class GuiDocHighlighter(QSyntaxHighlighter):
self.setFormat(0, cPos, self._hStyles["modifier"]) self.setFormat(0, cPos, self._hStyles["modifier"])
self.setFormat(cPos, cLen, self._hStyles["hidden"]) self.setFormat(cPos, cLen, self._hStyles["hidden"])
hRules = self._cmnRules
elif text.startswith("["): # Special Command elif text.startswith("["): # Special Command
self.setCurrentBlockState(BLOCK_TEXT)
hRules = self._txtRules
sText = text.rstrip().lower() sText = text.rstrip().lower()
if sText in ("[newpage]", "[new page]", "[vspace]"): if sText in ("[newpage]", "[new page]", "[vspace]"):
self.setFormat(0, len(text), self._hStyles["code"]) self.setFormat(0, len(text), self._hStyles["code"])
File diff suppressed because it is too large Load Diff
+6 -7
View File
@@ -22,10 +22,9 @@ from __future__ import annotations
import pytest import pytest
from tools import C, writeFile, buildTestProject from PyQt5.QtGui import QTextBlock, QTextCursor
from PyQt5.QtGui import QTextCursor, QTextBlock
from PyQt5.QtWidgets import QAction, QFileDialog, QMessageBox from PyQt5.QtWidgets import QAction, QFileDialog, QMessageBox
from tools import C, buildTestProject, writeFile
from novelwriter import CONFIG, SHARED from novelwriter import CONFIG, SHARED
from novelwriter.constants import nwKeyWords, nwUnicode from novelwriter.constants import nwKeyWords, nwUnicode
@@ -146,7 +145,7 @@ def testGuiMenu_EditFormat(qtbot, monkeypatch, nwGUI, prjLipsum):
# Check comment with no space before text # Check comment with no space before text
nwGUI.docEditor.setCursorPosition(54) nwGUI.docEditor.setCursorPosition(54)
assert nwGUI.docEditor.insertText("%") nwGUI.docEditor.insertText("%")
fmtStr = "%Pellentesque nec erat ut nulla posuere commodo." fmtStr = "%Pellentesque nec erat ut nulla posuere commodo."
assert nwGUI.docEditor.getText()[54:102] == fmtStr assert nwGUI.docEditor.getText()[54:102] == fmtStr
@@ -435,14 +434,14 @@ def testGuiMenu_Insert(qtbot, monkeypatch, nwGUI, fncPath, projPath, mockRnd):
nwGUI.docEditor.clear() nwGUI.docEditor.clear()
# Test Faulty Inserts # Test Faulty Inserts
assert nwGUI.docEditor.insertText("hello world") nwGUI.docEditor.insertText("hello world")
assert nwGUI.docEditor.getText() == "hello world" assert nwGUI.docEditor.getText() == "hello world"
nwGUI.docEditor.clear() nwGUI.docEditor.clear()
assert nwGUI.docEditor.insertText(nwDocInsert.NO_INSERT) is False nwGUI.docEditor.insertText(nwDocInsert.NO_INSERT)
assert nwGUI.docEditor.isEmpty assert nwGUI.docEditor.isEmpty
assert nwGUI.docEditor.insertText(None) is False nwGUI.docEditor.insertText(None)
assert nwGUI.docEditor.isEmpty assert nwGUI.docEditor.isEmpty
# qtbot.stop() # qtbot.stop()