Add handlers for document label change

This commit is contained in:
Veronica Berglyd Olsen
2023-11-15 20:13:23 +01:00
parent 8508248f12
commit 6b9d92e75a
5 changed files with 72 additions and 48 deletions
+18 -18
View File
@@ -36,7 +36,7 @@ from typing import TYPE_CHECKING, ItemsView, Iterable, Iterator
from pathlib import Path
from novelwriter import SHARED
from novelwriter.enum import nwItemClass, nwItemType, nwItemLayout, nwTrinary
from novelwriter.enum import nwItemClass, nwItemType, nwItemLayout
from novelwriter.error import logException
from novelwriter.common import checkInt, isHandle, isItemClass, isTitleTag, jsonEncode
from novelwriter.constants import nwFiles, nwKeyWords, nwRegEx, nwUnicode, nwHeaders
@@ -255,7 +255,7 @@ class NWIndex:
return False
# Keep a record of existing tags, and create a new item entry
itemTags = dict.fromkeys(self._itemIndex.allItemTags(tHandle), nwTrinary.NEGATIVE)
itemTags = dict.fromkeys(self._itemIndex.allItemTags(tHandle), False)
self._itemIndex.add(tHandle, tItem)
# Run word counter for the whole text
@@ -290,8 +290,7 @@ class NWIndex:
# Internal Indexer Helpers
##
def _scanActive(self, tHandle: str, nwItem: NWItem, text: str,
tags: dict[str, nwTrinary]) -> None:
def _scanActive(self, tHandle: str, nwItem: NWItem, text: str, tags: dict[str, bool]) -> None:
"""Scan an active document for meta data."""
nTitle = 0 # Line Number of the previous title
cTitle = TT_NONE # Tag of the current title
@@ -347,20 +346,18 @@ class NWIndex:
self._indexWordCounts(tHandle, text, cTitle)
# Prune no longer used tags
for tTag, tStatus in tags.items():
added = []
for tTag, isActive in tags.items():
updated = []
deleted = []
if tStatus == nwTrinary.NEGATIVE:
if isActive:
logger.debug("Added/updated tag '%s'", tTag)
updated.append(tTag)
else:
logger.debug("Removed tag '%s'", tTag)
del self._tagsIndex[tTag]
deleted.append(tTag)
elif tStatus == nwTrinary.POSITIVE:
logger.debug("Added new tag '%s'", tTag)
added.append(tTag)
else:
logger.debug("Unchanged tag '%s'", tTag)
if added or deleted:
SHARED.indexUpdatedTags(added, deleted)
if updated or deleted:
SHARED.indexUpdatedTags(updated, deleted)
return
@@ -397,7 +394,7 @@ class NWIndex:
return
def _indexKeyword(self, tHandle: str, line: str, sTitle: str,
itemClass: nwItemClass, tags: dict[str, nwTrinary]) -> None:
itemClass: nwItemClass, tags: dict[str, bool]) -> None:
"""Validate and save the information about a reference to a tag
in another file, or the setting of a tag in the file. A record
of active tags is updated so that no longer used tags can be
@@ -414,10 +411,9 @@ class NWIndex:
if tBits[0] == nwKeyWords.TAG_KEY:
tagName = tBits[1]
tagKey = tagName.lower()
self._tagsIndex.add(tagName, tHandle, sTitle, itemClass)
self._itemIndex.setHeadingTag(tHandle, sTitle, tagName)
tags[tagKey] = nwTrinary.NEUTRAL if tagKey in tags else nwTrinary.POSITIVE
tags[tagName.lower()] = True
else:
self._itemIndex.addHeadingRef(tHandle, sTitle, tBits[1:], tBits[0])
@@ -634,7 +630,11 @@ class NWIndex:
sTitle = self._tagsIndex.tagHeading(tagKey)
return tHandle, sTitle
def getTags(self, itemClass: nwItemClass) -> list[str]:
def getDocumentTags(self, tHandle: str | None) -> list[str]:
"""Return all tags used by a specific document."""
return self._itemIndex.allItemTags(tHandle) if tHandle else []
def getClassTags(self, itemClass: nwItemClass) -> list[str]:
"""Return all tags based on itemClass."""
return self._tagsIndex.filterTagNames(itemClass.name)