Make sure project items in inactive classes are removed from the index (#2387)

This commit is contained in:
Veronica Berglyd Olsen
2025-06-06 19:07:32 +02:00
parent 53784b310e
commit bf1067a346
3 changed files with 37 additions and 10 deletions
+10 -6
View File
@@ -131,12 +131,6 @@ class Index:
self._novelExtra = extra self._novelExtra = extra
return return
def setItemClass(self, tHandle: str, itemClass: nwItemClass) -> None:
"""Update the class for all tags of a handle."""
logger.info("Updating class for '%s'", tHandle)
self._tagsIndex.updateClass(tHandle, itemClass.name)
return
## ##
# Public Methods # Public Methods
## ##
@@ -183,6 +177,16 @@ class Index:
self.scanText(tHandle, self._project.storage.getDocumentText(tHandle)) self.scanText(tHandle, self._project.storage.getDocumentText(tHandle))
return return
def refreshHandle(self, tHandle: str) -> None:
"""Update the class for all tags of a handle."""
if item := self._project.tree[tHandle]:
logger.info("Updating class for '%s'", tHandle)
if item.isInactiveClass():
self.deleteHandle(tHandle)
else:
self._tagsIndex.updateClass(tHandle, item.itemClass.name)
return
def indexChangedSince(self, checkTime: int | float) -> bool: def indexChangedSince(self, checkTime: int | float) -> bool:
"""Check if the index has changed since a given time.""" """Check if the index has changed since a given time."""
return self._indexChange > float(checkTime) return self._indexChange > float(checkTime)
+1 -1
View File
@@ -439,7 +439,7 @@ class NWItem:
self.setClass(itemClass) self.setClass(itemClass)
if self._type == nwItemType.FILE: if self._type == nwItemType.FILE:
# Notify the index of the class change # Notify the index of the class change
self._project.index.setItemClass(self._handle, itemClass) self._project.index.refreshHandle(self._handle)
if self._layout == nwItemLayout.NO_LAYOUT: if self._layout == nwItemLayout.NO_LAYOUT:
# If no layout is set, pick one # If no layout is set, pick one
+26 -3
View File
@@ -95,12 +95,35 @@ def testCoreIndex_LoadSave(qtbot, monkeypatch, prjLipsum, nwGUI, tstPaths):
tagIndex = str(index._tagsIndex.packData()) tagIndex = str(index._tagsIndex.packData())
itemsIndex = str(index._itemIndex.packData()) itemsIndex = str(index._itemIndex.packData())
# Update item class
bHandle = "4c4f28287af27"
bItem = project.tree[bHandle]
assert bItem is not None
tagsBod = index._tagsIndex["Bod"]
assert tagsBod is not None
assert tagsBod["handle"] == bHandle
assert tagsBod["class"] == "CHARACTER"
bItem.setClass(nwItemClass.CUSTOM)
index.refreshHandle(bHandle)
assert tagsBod is not None
assert tagsBod["handle"] == bHandle
assert tagsBod["class"] == "CUSTOM"
# Update item class to inactive
bItem.setClass(nwItemClass.TRASH)
index.refreshHandle(bHandle)
assert "Bod" not in index._tagsIndex
bItem.setClass(nwItemClass.CHARACTER)
index.reIndexHandle(bHandle)
# Delete a handle # Delete a handle
assert index._tagsIndex["Bod"] is not None assert index._tagsIndex["Bod"] is not None
assert index._itemIndex["4c4f28287af27"] is not None assert index._itemIndex[bHandle] is not None
index.deleteHandle("4c4f28287af27") index.deleteHandle(bHandle)
assert index._tagsIndex["Bod"] is None assert index._tagsIndex["Bod"] is None
assert index._itemIndex["4c4f28287af27"] is None assert index._itemIndex[bHandle] is None
# Clear the index # Clear the index
index.clear() index.clear()