Add parsing for terms in comments

This commit is contained in:
Veronica Berglyd Olsen
2024-04-07 18:06:21 +02:00
parent a7cf236ba5
commit 08b17daec9
5 changed files with 62 additions and 34 deletions
+11 -5
View File
@@ -336,7 +336,7 @@ class NWIndex:
elif line.startswith("%"): elif line.startswith("%"):
if cTitle != TT_NONE: if cTitle != TT_NONE:
cStyle, cText, _ = processComment(line) cStyle, cMod, cText, _, _ = processComment(line)
if cStyle in (nwComment.SYNOPSIS, nwComment.SHORT): if cStyle in (nwComment.SYNOPSIS, nwComment.SHORT):
self._itemIndex.setHeadingSynopsis(tHandle, cTitle, cText) self._itemIndex.setHeadingSynopsis(tHandle, cTitle, cText)
@@ -1303,17 +1303,23 @@ class IndexHeading:
# =============================================================================================== # # =============================================================================================== #
CLASSIFIERS = { CLASSIFIERS = {
"short": nwComment.SHORT,
"synopsis": nwComment.SYNOPSIS, "synopsis": nwComment.SYNOPSIS,
"summary": nwComment.SUMMARY,
"short": nwComment.SHORT,
"note": nwComment.NOTE,
} }
TERMS = ["note"]
def processComment(text: str) -> tuple[nwComment, str, int]:
def processComment(text: str) -> tuple[nwComment, str, str, int, int]:
"""Extract comment style and text. Should only be called on text """Extract comment style and text. Should only be called on text
starting with a %. starting with a %.
""" """
check = text[1:].lstrip() check = text[1:].lstrip()
classifier, _, content = check.partition(":") classifier, _, content = check.partition(":")
classifier, _, term = classifier.partition(".")
if content and (clean := classifier.strip().lower()) in CLASSIFIERS: if content and (clean := classifier.strip().lower()) in CLASSIFIERS:
return CLASSIFIERS[clean], content.strip(), text.find(":") + 1 term = "ERR" if term and clean not in TERMS else term.strip().lower()
return nwComment.PLAIN, check, 0 return CLASSIFIERS[clean], term, content.strip(), text.find(".") + 1, text.find(":") + 1
return nwComment.PLAIN, "", check, 0, 0
+1 -1
View File
@@ -533,7 +533,7 @@ class Tokenizer(ABC):
if aLine.startswith("%~"): if aLine.startswith("%~"):
continue continue
cStyle, cText, _ = processComment(aLine) cStyle, cMod, cText, _, _ = processComment(aLine)
if cStyle == nwComment.SYNOPSIS: if cStyle == nwComment.SYNOPSIS:
self._tokens.append(( self._tokens.append((
self.T_SYNOPSIS, nHead, cText, [], sAlign self.T_SYNOPSIS, nHead, cText, [], sAlign
+3 -1
View File
@@ -66,7 +66,9 @@ class nwComment(Enum):
PLAIN = 0 PLAIN = 0
SYNOPSIS = 1 SYNOPSIS = 1
SHORT = 2 SUMMARY = 2
SHORT = 3
NOTE = 4
# END Enum nwComment # END Enum nwComment
+19 -8
View File
@@ -276,6 +276,7 @@ class GuiDocHighlighter(QSyntaxHighlighter):
if self._tHandle is None or not text: if self._tHandle is None or not text:
return return
xOff = 0
if text.startswith("@"): # Keywords and commands if text.startswith("@"): # Keywords and commands
self.setCurrentBlockState(BLOCK_META) self.setCurrentBlockState(BLOCK_META)
index = SHARED.project.index index = SHARED.project.index
@@ -333,12 +334,20 @@ class GuiDocHighlighter(QSyntaxHighlighter):
elif text.startswith("%"): # Comments elif text.startswith("%"): # Comments
self.setCurrentBlockState(BLOCK_TEXT) self.setCurrentBlockState(BLOCK_TEXT)
cStyle, _, cPos = processComment(text) cStyle, cMod, _, cDot, cPos = processComment(text)
if cStyle == nwComment.PLAIN: cLen = len(text) - cPos
self.setFormat(0, len(text), self._hStyles["hidden"]) xOff = cPos
if cMod == "ERR":
self.setFormat(0, cPos, self._hStyles["codeinval"])
elif cStyle == nwComment.PLAIN:
self.setFormat(0, cLen, self._hStyles["hidden"])
elif cMod:
self.setFormat(0, cDot, self._hStyles["modifier"])
self.setFormat(cDot, cPos - cDot, self._hStyles["optional"])
self.setFormat(cPos, cLen, self._hStyles["hidden"])
else: else:
self.setFormat(0, cPos, self._hStyles["modifier"]) self.setFormat(0, cPos, self._hStyles["modifier"])
self.setFormat(cPos, len(text), self._hStyles["hidden"]) self.setFormat(cPos, cLen, self._hStyles["hidden"])
else: # Text Paragraph else: # Text Paragraph
@@ -377,7 +386,7 @@ class GuiDocHighlighter(QSyntaxHighlighter):
self.setCurrentBlockUserData(data) self.setCurrentBlockUserData(data)
if self._spellCheck: if self._spellCheck:
for xPos, xLen in data.spellCheck(text): for xPos, xLen in data.spellCheck(text, xOff):
for x in range(xPos, xPos+xLen): for x in range(xPos, xPos+xLen):
spFmt = self.format(x) spFmt = self.format(x)
spFmt.merge(self._spellErr) spFmt.merge(self._spellErr)
@@ -435,17 +444,19 @@ class TextBlockData(QTextBlockUserData):
"""Return spell error data from last check.""" """Return spell error data from last check."""
return self._spellErrors return self._spellErrors
def spellCheck(self, text: str) -> list[tuple[int, int]]: def spellCheck(self, text: str, offset: int) -> list[tuple[int, int]]:
"""Run the spell checker and cache the result, and return the """Run the spell checker and cache the result, and return the
list of spell check errors. list of spell check errors.
""" """
self._spellErrors = [] self._spellErrors = []
rxSpell = SPELLRX.globalMatch(text.replace("_", " "), 0) rxSpell = SPELLRX.globalMatch(text[offset:].replace("_", " "), 0)
while rxSpell.hasNext(): while rxSpell.hasNext():
rxMatch = rxSpell.next() rxMatch = rxSpell.next()
if not SHARED.spelling.checkWord(rxMatch.captured(0)): if not SHARED.spelling.checkWord(rxMatch.captured(0)):
if not rxMatch.captured(0).isnumeric() and not rxMatch.captured(0).isupper(): if not rxMatch.captured(0).isnumeric() and not rxMatch.captured(0).isupper():
self._spellErrors.append((rxMatch.capturedStart(0), rxMatch.capturedLength(0))) self._spellErrors.append(
(rxMatch.capturedStart(0) + offset, rxMatch.capturedLength(0))
)
return self._spellErrors return self._spellErrors
# END Class TextBlockData # END Class TextBlockData
+28 -19
View File
@@ -1322,26 +1322,35 @@ def testCoreIndex_ItemIndex(mockGUI, fncPath, mockRnd):
def testCoreIndex_processComment(): def testCoreIndex_processComment():
"""Test the comment processing function.""" """Test the comment processing function."""
# Regular comment # Regular comment
assert processComment("%Hi") == (nwComment.PLAIN, "Hi", 0) assert processComment("%Hi") == (nwComment.PLAIN, "", "Hi", 0, 0)
assert processComment("% Hi") == (nwComment.PLAIN, "Hi", 0) assert processComment("% Hi") == (nwComment.PLAIN, "", "Hi", 0, 0)
assert processComment("% Hi:You") == (nwComment.PLAIN, "Hi:You", 0) assert processComment("% Hi:You") == (nwComment.PLAIN, "", "Hi:You", 0, 0)
assert processComment("% Hi.You:There") == (nwComment.PLAIN, "", "Hi.You:There", 0, 0)
# Synopsis # Check Non-Term
assert processComment("%synopsis:") == (nwComment.PLAIN, "synopsis:", 0) assert processComment("%summary: Hi") == (nwComment.SUMMARY, "", "Hi", 0, 9)
assert processComment("%synopsis: Hi") == (nwComment.SYNOPSIS, "Hi", 10) assert processComment("%summary.term: Hi") == (nwComment.SUMMARY, "ERR", "Hi", 9, 14)
assert processComment("% synopsis: Hi") == (nwComment.SYNOPSIS, "Hi", 11)
assert processComment("% synopsis : Hi") == (nwComment.SYNOPSIS, "Hi", 13)
assert processComment("% Synopsis : Hi") == (nwComment.SYNOPSIS, "Hi", 15)
assert processComment("% \t SYNOPSIS : Hi") == (nwComment.SYNOPSIS, "Hi", 16)
assert processComment("% \t SYNOPSIS : Hi:You") == (nwComment.SYNOPSIS, "Hi:You", 16)
# Short Description # Check Term
assert processComment("%short:") == (nwComment.PLAIN, "short:", 0) assert processComment("%note: Hi") == (nwComment.NOTE, "", "Hi", 0, 6)
assert processComment("%short: Hi") == (nwComment.SHORT, "Hi", 7) assert processComment("%note.term: Hi") == (nwComment.NOTE, "term", "Hi", 6, 11)
assert processComment("% short: Hi") == (nwComment.SHORT, "Hi", 8)
assert processComment("% short : Hi") == (nwComment.SHORT, "Hi", 10) # Check Padding
assert processComment("% Short : Hi") == (nwComment.SHORT, "Hi", 12) assert processComment("%short: Hi") == (nwComment.SHORT, "", "Hi", 0, 7)
assert processComment("% \t SHORT : Hi") == (nwComment.SHORT, "Hi", 13) assert processComment("% short: Hi") == (nwComment.SHORT, "", "Hi", 0, 8)
assert processComment("% \t SHORT : Hi:You") == (nwComment.SHORT, "Hi:You", 13) assert processComment("% short : Hi") == (nwComment.SHORT, "", "Hi", 0, 10)
assert processComment("% short : Hi") == (nwComment.SHORT, "", "Hi", 0, 12)
assert processComment("% \t short : Hi") == (nwComment.SHORT, "", "Hi", 0, 13)
assert processComment("%note.term: Hi") == (nwComment.NOTE, "term", "Hi", 6, 11)
assert processComment("% note.term: Hi") == (nwComment.NOTE, "term", "Hi", 7, 12)
assert processComment("% note . term : Hi") == (nwComment.NOTE, "term", "Hi", 9, 16)
assert processComment("% note . term : Hi") == (nwComment.NOTE, "term", "Hi", 11, 20)
# Check Classifiers
assert processComment("%short: Hi") == (nwComment.SHORT, "", "Hi", 0, 7)
assert processComment("%synopsis: Hi") == (nwComment.SYNOPSIS, "", "Hi", 0, 10)
assert processComment("%summary: Hi") == (nwComment.SUMMARY, "", "Hi", 0, 9)
assert processComment("%note.term: Hi") == (nwComment.NOTE, "term", "Hi", 6, 11)
# END Test testCoreIndex_processComment # END Test testCoreIndex_processComment