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 PyQt5.QtCore import (
pyqtSignal, pyqtSlot, QObject, QPoint, QRegularExpression, QRunnable, Qt,
QTimer
QObject, QPoint, QRegularExpression, QRunnable, Qt, QTimer, pyqtSignal,
pyqtSlot
)
from PyQt5.QtGui import (
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.types import (
QtAlignCenterTop, QtAlignJustify, QtAlignLeft, QtAlignLeftTop,
QtAlignRight, QtKeepAnchor, QtModCtrl, QtMouseLeft, QtModeNone, QtModShift,
QtAlignRight, QtKeepAnchor, QtModCtrl, QtModeNone, QtModShift, QtMouseLeft,
QtMoveAnchor, QtMoveLeft, QtMoveRight
)
@@ -840,14 +840,15 @@ class GuiDocEditor(QPlainTextEdit):
)
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."""
if self._docHandle is None:
logger.error("No document open")
return False
return
newBlock = False
goAfter = False
text = ""
block = False
after = False
if isinstance(insert, str):
text = insert
@@ -862,46 +863,41 @@ class GuiDocEditor(QPlainTextEdit):
text = self._typDQuoteC
elif insert == nwDocInsert.SYNOPSIS:
text = "%Synopsis: "
newBlock = True
goAfter = True
block = True
after = True
elif insert == nwDocInsert.SHORT:
text = "%Short: "
newBlock = True
goAfter = True
block = True
after = True
elif insert == nwDocInsert.NEW_PAGE:
text = "[newpage]"
newBlock = True
goAfter = False
block = True
after = False
elif insert == nwDocInsert.VSPACE_S:
text = "[vspace]"
newBlock = True
goAfter = False
block = True
after = False
elif insert == nwDocInsert.VSPACE_M:
text = "[vspace:2]"
newBlock = True
goAfter = False
block = True
after = False
elif insert == nwDocInsert.LIPSUM:
text = GuiLipsum.getLipsum(self)
newBlock = True
goAfter = False
block = True
after = False
elif insert == nwDocInsert.FOOTNOTE:
text = ""
self._insertCommentStructure(nwComment.FOOTNOTE)
else:
return False
else:
return False
if text:
if newBlock:
self.insertNewBlock(text, defaultAfter=goAfter)
if block:
self.insertNewBlock(text, defaultAfter=after)
else:
cursor = self.textCursor()
cursor.beginEditBlock()
cursor.insertText(text)
cursor.endEditBlock()
return True
return
def insertNewBlock(self, text: str, defaultAfter: bool = True) -> bool:
"""Insert a piece of text on a blank line."""
@@ -1167,7 +1163,8 @@ class GuiDocEditor(QPlainTextEdit):
lambda _, option=option: self._correctWord(sCursor, option)
)
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()
action = ctxMenu.addAction(self.tr("Add Word to Dictionary"))
+6 -5
View File
@@ -358,12 +358,12 @@ class GuiDocHighlighter(QSyntaxHighlighter):
elif text.startswith("%"): # Comments
self.setCurrentBlockState(BLOCK_TEXT)
hRules = self._cmnRules
cStyle, cMod, _, cDot, cPos = processComment(text)
cLen = len(text) - cPos
xOff = cPos
if cMod == "ERR":
self.setFormat(0, cPos, self._hStyles["invalid"])
elif cStyle == nwComment.PLAIN:
if cStyle == nwComment.PLAIN:
self.setFormat(0, cLen, self._hStyles["hidden"])
elif cStyle == nwComment.IGNORE:
self.setFormat(0, cLen, self._hStyles["strike"])
@@ -376,9 +376,10 @@ class GuiDocHighlighter(QSyntaxHighlighter):
self.setFormat(0, cPos, self._hStyles["modifier"])
self.setFormat(cPos, cLen, self._hStyles["hidden"])
hRules = self._cmnRules
elif text.startswith("["): # Special Command
self.setCurrentBlockState(BLOCK_TEXT)
hRules = self._txtRules
sText = text.rstrip().lower()
if sText in ("[newpage]", "[new page]", "[vspace]"):
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
from tools import C, writeFile, buildTestProject
from PyQt5.QtGui import QTextCursor, QTextBlock
from PyQt5.QtGui import QTextBlock, QTextCursor
from PyQt5.QtWidgets import QAction, QFileDialog, QMessageBox
from tools import C, buildTestProject, writeFile
from novelwriter import CONFIG, SHARED
from novelwriter.constants import nwKeyWords, nwUnicode
@@ -146,7 +145,7 @@ def testGuiMenu_EditFormat(qtbot, monkeypatch, nwGUI, prjLipsum):
# Check comment with no space before text
nwGUI.docEditor.setCursorPosition(54)
assert nwGUI.docEditor.insertText("%")
nwGUI.docEditor.insertText("%")
fmtStr = "%Pellentesque nec erat ut nulla posuere commodo."
assert nwGUI.docEditor.getText()[54:102] == fmtStr
@@ -435,14 +434,14 @@ def testGuiMenu_Insert(qtbot, monkeypatch, nwGUI, fncPath, projPath, mockRnd):
nwGUI.docEditor.clear()
# Test Faulty Inserts
assert nwGUI.docEditor.insertText("hello world")
nwGUI.docEditor.insertText("hello world")
assert nwGUI.docEditor.getText() == "hello world"
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.insertText(None) is False
nwGUI.docEditor.insertText(None)
assert nwGUI.docEditor.isEmpty
# qtbot.stop()