diff --git a/novelwriter/core/index.py b/novelwriter/core/index.py index d8e9c8e4..07933690 100644 --- a/novelwriter/core/index.py +++ b/novelwriter/core/index.py @@ -708,17 +708,19 @@ class Index: return 0, 0, 0 def getReferences(self, tHandle: str, sTitle: str | None = None) -> dict[str, list[str]]: - """Extract all references made in a file, and optionally title - section. + """Extract all tags and references made in a file, and + optionally title section. """ - tRefs = {x: [] for x in nwKeyWords.VALID_KEYS} + refs = {x: [] for x in nwKeyWords.VALID_KEYS} for rTitle, hItem in self._itemIndex.iterItemHeaders(tHandle): if sTitle is None or sTitle == rTitle: for aTag, refTypes in hItem.references.items(): for refType in refTypes: - if refType in tRefs: - tRefs[refType].append(self._tagsIndex.tagName(aTag)) - return tRefs + if refType in refs: + refs[refType].append(self._tagsIndex.tagName(aTag)) + if tag := hItem.tag: + refs[nwKeyWords.TAG_KEY] = [self._tagsIndex.tagName(tag)] + return refs def getReferenceForHeader(self, tHandle: str, nHead: int, keyClass: str) -> list[str]: """Get the display names for a tags class for insertion into a diff --git a/novelwriter/core/indexdata.py b/novelwriter/core/indexdata.py index 070558ce..b74ce4c4 100644 --- a/novelwriter/core/indexdata.py +++ b/novelwriter/core/indexdata.py @@ -342,20 +342,26 @@ class IndexHeading: ## def getReferences(self) -> dict[str, list[str]]: - """Extract all references for this heading.""" + """Extract all tags and references for this heading.""" refs = {x: [] for x in nwKeyWords.VALID_KEYS} for tag, types in self._refs.items(): for keyword in types: if keyword in refs and (name := self._cache.tags.tagName(tag)): refs[keyword].append(name) + if name := self._cache.tags.tagName(self._tag): + refs[nwKeyWords.TAG_KEY] = [name] return refs def getReferencesByKeyword(self, keyword: str) -> list[str]: """Extract all references for this heading.""" refs = [] - for tag, types in self._refs.items(): - if keyword in types and (name := self._cache.tags.tagName(tag)): + if keyword == nwKeyWords.TAG_KEY: + if name := self._cache.tags.tagName(self._tag): refs.append(name) + else: + for tag, types in self._refs.items(): + if keyword in types and (name := self._cache.tags.tagName(tag)): + refs.append(name) return refs ## diff --git a/novelwriter/gui/noveltree.py b/novelwriter/gui/noveltree.py index 752c6d39..86913b61 100644 --- a/novelwriter/gui/noveltree.py +++ b/novelwriter/gui/noveltree.py @@ -593,6 +593,7 @@ class GuiNovelTree(NTreeView): lines = [] if head := SHARED.project.index.getItemHeading(tHandle, sTitle): tags = head.getReferences() + appendTags(tags, nwKeyWords.TAG_KEY, lines) appendTags(tags, nwKeyWords.POV_KEY, lines) appendTags(tags, nwKeyWords.FOCUS_KEY, lines) appendTags(tags, nwKeyWords.CHAR_KEY, lines) diff --git a/sample/content/636b6aa9b697b.nwd b/sample/content/636b6aa9b697b.nwd index e3d4bfde..416efc4e 100644 --- a/sample/content/636b6aa9b697b.nwd +++ b/sample/content/636b6aa9b697b.nwd @@ -1,10 +1,11 @@ %%~name: Making a Scene %%~path: 6a2d6d5f4f401/636b6aa9b697b %%~kind: NOVEL/DOCUMENT -%%~hash: 1f3d98a6a27b4f9a7f2239fcd78f9e2263cd3b97 -%%~date: Unknown/2025-05-18 22:36:59 +%%~hash: b39201b02e3db63493d61d09e5fec5765a937255 +%%~date: Unknown/2025-06-24 09:09:56 ### Making a Scene +@tag: Scene @pov: Jane @char: John, Jane @location: Earth diff --git a/sample/nwProject.nwx b/sample/nwProject.nwx index ae0fee4d..c53ff1c9 100644 --- a/sample/nwProject.nwx +++ b/sample/nwProject.nwx @@ -1,6 +1,6 @@ - - + + Sample Project Jane Smith @@ -58,7 +58,7 @@ Chapter One - + Making a Scene diff --git a/tests/test_core/test_core_index.py b/tests/test_core/test_core_index.py index dca4575a..e86b8dc0 100644 --- a/tests/test_core/test_core_index.py +++ b/tests/test_core/test_core_index.py @@ -695,6 +695,7 @@ def testCoreIndex_ExtractData(nwGUI, fncPath, mockRnd): )) assert index.scanText(nHandle, ( "# Hello World!\n" + "@tag: Scene\n" "@pov: Jane\n" "@char: Jane, John\n\n" "% this is a comment\n\n" @@ -749,11 +750,13 @@ def testCoreIndex_ExtractData(nwGUI, fncPath, mockRnd): # Look up an invalid handle refs = index.getReferences("Not a handle") + assert refs["@tag"] == [] assert refs["@pov"] == [] assert refs["@char"] == [] # The novel file should now refer to Jane as @pov and @char refs = index.getReferences(nHandle) + assert refs["@tag"] == ["Scene"] assert refs["@pov"] == ["Jane"] assert refs["@char"] == ["Jane", "John"] @@ -791,7 +794,7 @@ def testCoreIndex_ExtractData(nwGUI, fncPath, mockRnd): # getKeyWordTags # ============== - assert index.getKeyWordTags("@mention") == ["Jane", "John"] + assert index.getKeyWordTags("@mention") == ["Jane", "John", "Scene"] assert index.getKeyWordTags("@char") == ["Jane", "John"] assert index.getKeyWordTags("@plot") == [] assert index.getKeyWordTags("@tag") == [] diff --git a/tests/test_core/test_core_indexdata.py b/tests/test_core/test_core_indexdata.py index 5bf45c8a..d4797aee 100644 --- a/tests/test_core/test_core_indexdata.py +++ b/tests/test_core/test_core_indexdata.py @@ -267,6 +267,7 @@ def testCoreIndexData_IndexHeadingReferences(): head = IndexHeading(cache, "T0001") # Add some references + head.setTag("Scene") head.addReference("Jane", "@pov") head.addReference("Jane", "@char") head.addReference("John", "@char") @@ -290,6 +291,7 @@ def testCoreIndexData_IndexHeadingReferences(): } # Set names + cache.tags.add("Scene", "Scene", "0000000000000", "T00001", "NOVEL") cache.tags.add("Jane", "Jane", "0000000000000", "T00001", "CHARACTER") cache.tags.add("John", "John", "0000000000000", "T00001", "CHARACTER") cache.tags.add("Main", "Main", "0000000000000", "T00001", "PLOT") @@ -301,7 +303,7 @@ def testCoreIndexData_IndexHeadingReferences(): "@plot": ["Main"], "@object": ["Gun"], "@story": [], - "@tag": [], + "@tag": ["Scene"], "@focus": [], "@custom": [], "@time": [], @@ -316,7 +318,7 @@ def testCoreIndexData_IndexHeadingReferences(): assert head.getReferencesByKeyword("@plot") == ["Main"] assert head.getReferencesByKeyword("@object") == ["Gun"] assert head.getReferencesByKeyword("@story") == [] - assert head.getReferencesByKeyword("@tag") == [] + assert head.getReferencesByKeyword("@tag") == ["Scene"] assert head.getReferencesByKeyword("@focus") == [] assert head.getReferencesByKeyword("@custom") == [] assert head.getReferencesByKeyword("@time") == []