Revert parts of previous implementation

This commit is contained in:
Veronica Berglyd Olsen
2024-01-30 21:00:04 +01:00
parent e5c6dae080
commit f15563cf6e
4 changed files with 38 additions and 52 deletions
+10 -18
View File
@@ -32,7 +32,7 @@ import json
import logging import logging
from time import time from time import time
from typing import TYPE_CHECKING, ItemsView, Iterable, Iterator, Literal from typing import TYPE_CHECKING, ItemsView, Iterable, Iterator
from pathlib import Path from pathlib import Path
from novelwriter import SHARED from novelwriter import SHARED
@@ -472,41 +472,33 @@ class NWIndex:
return True, tBits, tPos return True, tBits, tPos
def checkThese(self, tBits: list[str], nwItem: NWItem) -> list[Literal[0, 1, 2, 3]]: def checkThese(self, tBits: list[str], nwItem: NWItem) -> list[bool]:
"""Check the tags against the index to see if they are valid """Check the tags against the index to see if they are valid
tags. This is needed for syntax highlighting. The return values tags. This is needed for syntax highlighting.
for each item are:
0: Invalid
1: Valid and a keyword
2: Valid and a value
3: Valid and an optional value
""" """
nBits = len(tBits) nBits = len(tBits)
isGood = [False]*nBits
if nBits == 0: if nBits == 0:
return [] return []
# Check that the key is valid # Check that the key is valid
isGood: list[Literal[0, 1, 2, 3]] = [0]*nBits isGood[0] = tBits[0] in nwKeyWords.VALID_KEYS
isGood[0] = 1 if tBits[0] in nwKeyWords.VALID_KEYS else 0 if not isGood[0] or nBits == 1:
if isGood[0] == 0 or nBits == 1:
return isGood return isGood
# For a tag, the first value is the tag, and the second is # For a tag, only the first value is accepted, the rest are ignored
# optional and is the display name
if tBits[0] == nwKeyWords.TAG_KEY and nBits > 1: if tBits[0] == nwKeyWords.TAG_KEY and nBits > 1:
if tBits[1] in self._tagsIndex: if tBits[1] in self._tagsIndex:
isGood[1] = 2 if self._tagsIndex.tagHandle(tBits[1]) == nwItem.itemHandle else 0 isGood[1] = self._tagsIndex.tagHandle(tBits[1]) == nwItem.itemHandle
else: else:
isGood[1] = 2 isGood[1] = True
if nBits > 2:
isGood[2] = 3
return isGood return isGood
# If we're still here, we check that the references exist # If we're still here, we check that the references exist
refKey = nwKeyWords.KEY_CLASS[tBits[0]].name refKey = nwKeyWords.KEY_CLASS[tBits[0]].name
for n in range(1, nBits): for n in range(1, nBits):
if tBits[n] in self._tagsIndex: if tBits[n] in self._tagsIndex:
isGood[n] = 2 if self._tagsIndex.tagClass(tBits[n]) == refKey else 0 isGood[n] = self._tagsIndex.tagClass(tBits[n]) == refKey
return isGood return isGood
+6 -6
View File
@@ -1842,7 +1842,7 @@ class GuiDocEditor(QPlainTextEdit):
return nwTrinary.NEUTRAL return nwTrinary.NEUTRAL
tag = "" tag = ""
exist = 0 exist = False
cPos = cursor.selectionStart() - block.position() cPos = cursor.selectionStart() - block.position()
tExist = SHARED.project.index.checkThese(tBits, self._nwItem) tExist = SHARED.project.index.checkThese(tBits, self._nwItem)
for sTag, sPos, sExist in zip(reversed(tBits), reversed(tPos), reversed(tExist)): for sTag, sPos, sExist in zip(reversed(tBits), reversed(tPos), reversed(tExist)):
@@ -1854,14 +1854,14 @@ class GuiDocEditor(QPlainTextEdit):
exist = sExist exist = sExist
break break
if exist in (1, 3) or not tag: if not tag or tag.startswith("@"):
# Ignore keywords, optionals and empty tags # The keyword cannot be looked up, so we ignore that
return nwTrinary.NEUTRAL return nwTrinary.NEUTRAL
if follow and exist == 2: if follow and exist:
logger.debug("Attempting to follow tag '%s'", tag) logger.debug("Attempting to follow tag '%s'", tag)
self.loadDocumentTagRequest.emit(tag, nwDocMode.VIEW) self.loadDocumentTagRequest.emit(tag, nwDocMode.VIEW)
elif create and exist == 0: elif create and not exist:
if SHARED.question(self.tr( if SHARED.question(self.tr(
"Do you want to create a new project note for the tag '{0}'?" "Do you want to create a new project note for the tag '{0}'?"
).format(tag)): ).format(tag)):
@@ -1874,7 +1874,7 @@ class GuiDocEditor(QPlainTextEdit):
"If one doesn't exist, you must create one first." "If one doesn't exist, you must create one first."
).format(trConst(nwLabels.CLASS_NAME[itemClass]))) ).format(trConst(nwLabels.CLASS_NAME[itemClass])))
return nwTrinary.POSITIVE if exist == 2 else nwTrinary.NEGATIVE return nwTrinary.POSITIVE if exist else nwTrinary.NEGATIVE
return nwTrinary.NEUTRAL return nwTrinary.NEUTRAL
+8 -7
View File
@@ -287,14 +287,15 @@ class GuiDocHighlighter(QSyntaxHighlighter):
for n, bit in enumerate(bits): for n, bit in enumerate(bits):
xPos = pos[n] xPos = pos[n]
xLen = len(bit) xLen = len(bit)
if isGood[n] == 1: if isGood[n]:
self.setFormat(xPos, xLen, self._hStyles["keyword"]) if n == 0:
elif isGood[n] == 2: self.setFormat(xPos, xLen, self._hStyles["keyword"])
self.setFormat(xPos, xLen, self._hStyles["value"]) else:
elif isGood[n] == 3: self.setFormat(xPos, xLen, self._hStyles["value"])
self.setFormat(xPos, xLen, self._hStyles["optional"])
else: else:
self.setFormat(xPos, xLen, self._hStyles["codeinval"]) kwFmt = self.format(xPos)
kwFmt.merge(self._hStyles["codeinval"])
self.setFormat(xPos, xLen, kwFmt)
# We never want to run the spell checker on keyword/values, # We never want to run the spell checker on keyword/values,
# so we force a return here # so we force a return here
+14 -21
View File
@@ -269,33 +269,26 @@ def testCoreIndex_CheckThese(mockGUI, fncPath, mockRnd):
assert nItem.mainHeading == "H1" assert nItem.mainHeading == "H1"
# Zero Items # Zero Items
assert index.checkThese([], cItem) == [] assert index.checkThese([], cHandle) == []
# One Item # One Item
assert index.checkThese(["@tag"], cItem) == [1] assert index.checkThese(["@tag"], cItem) == [True]
assert index.checkThese(["@who"], cItem) == [0] assert index.checkThese(["@who"], cItem) == [False]
# Two Items # Two Items
assert index.checkThese(["@tag", "Jane"], cItem) == [1, 2] assert index.checkThese(["@tag", "Jane"], cItem) == [True, True]
assert index.checkThese(["@tag", "John"], cItem) == [1, 2] assert index.checkThese(["@tag", "John"], cItem) == [True, True]
assert index.checkThese(["@tag", "Jane"], nItem) == [1, 0] assert index.checkThese(["@tag", "Jane"], nItem) == [True, False]
assert index.checkThese(["@tag", "John"], nItem) == [1, 2] assert index.checkThese(["@tag", "John"], nItem) == [True, True]
assert index.checkThese(["@pov", "John"], nItem) == [1, 0] assert index.checkThese(["@pov", "John"], nItem) == [True, False]
assert index.checkThese(["@pov", "Jane"], nItem) == [1, 2] assert index.checkThese(["@pov", "Jane"], nItem) == [True, True]
assert index.checkThese(["@ pov", "Jane"], nItem) == [0, 0] assert index.checkThese(["@ pov", "Jane"], nItem) == [False, False]
assert index.checkThese(["@what", "Jane"], nItem) == [0, 0] assert index.checkThese(["@what", "Jane"], nItem) == [False, False]
# Three Items # Three Items
assert index.checkThese(["@tag", "Jane", "Jany"], cItem) == [1, 2, 3] assert index.checkThese(["@tag", "Jane", "John"], cItem) == [True, True, False]
assert index.checkThese(["@who", "Jane", "John"], cItem) == [0, 0, 0] assert index.checkThese(["@who", "Jane", "John"], cItem) == [False, False, False]
assert index.checkThese(["@pov", "Jane", "John"], nItem) == [1, 2, 0] assert index.checkThese(["@pov", "Jane", "John"], nItem) == [True, True, False]
assert index.checkThese(["@pov", "Jane", "Jane"], nItem) == [1, 2, 2]
# Four Items
assert index.checkThese(["@tag", "Jane", "Jany", "John"], cItem) == [1, 2, 3, 0]
assert index.checkThese(["@who", "Jane", "Jane", "Jane"], cItem) == [0, 0, 0, 0]
assert index.checkThese(["@pov", "Jane", "John", "Jane"], nItem) == [1, 2, 0, 2]
assert index.checkThese(["@pov", "Jane", "Jane", "Jane"], nItem) == [1, 2, 2, 2]
project.closeProject() project.closeProject()