Skip to content

Commit 590c66d

Browse files
authored
Fix missing tag display in outline (#2429)
2 parents 4b5f3c8 + b4db960 commit 590c66d

File tree

7 files changed

+32
-17
lines changed

7 files changed

+32
-17
lines changed

novelwriter/core/index.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -708,17 +708,19 @@ def getCounts(self, tHandle: str, sTitle: str | None = None) -> tuple[int, int,
708708
return 0, 0, 0
709709

710710
def getReferences(self, tHandle: str, sTitle: str | None = None) -> dict[str, list[str]]:
711-
"""Extract all references made in a file, and optionally title
712-
section.
711+
"""Extract all tags and references made in a file, and
712+
optionally title section.
713713
"""
714-
tRefs = {x: [] for x in nwKeyWords.VALID_KEYS}
714+
refs = {x: [] for x in nwKeyWords.VALID_KEYS}
715715
for rTitle, hItem in self._itemIndex.iterItemHeaders(tHandle):
716716
if sTitle is None or sTitle == rTitle:
717717
for aTag, refTypes in hItem.references.items():
718718
for refType in refTypes:
719-
if refType in tRefs:
720-
tRefs[refType].append(self._tagsIndex.tagName(aTag))
721-
return tRefs
719+
if refType in refs:
720+
refs[refType].append(self._tagsIndex.tagName(aTag))
721+
if tag := hItem.tag:
722+
refs[nwKeyWords.TAG_KEY] = [self._tagsIndex.tagName(tag)]
723+
return refs
722724

723725
def getReferenceForHeader(self, tHandle: str, nHead: int, keyClass: str) -> list[str]:
724726
"""Get the display names for a tags class for insertion into a

novelwriter/core/indexdata.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,20 +342,26 @@ def addReference(self, tag: str, keyword: str) -> None:
342342
##
343343

344344
def getReferences(self) -> dict[str, list[str]]:
345-
"""Extract all references for this heading."""
345+
"""Extract all tags and references for this heading."""
346346
refs = {x: [] for x in nwKeyWords.VALID_KEYS}
347347
for tag, types in self._refs.items():
348348
for keyword in types:
349349
if keyword in refs and (name := self._cache.tags.tagName(tag)):
350350
refs[keyword].append(name)
351+
if name := self._cache.tags.tagName(self._tag):
352+
refs[nwKeyWords.TAG_KEY] = [name]
351353
return refs
352354

353355
def getReferencesByKeyword(self, keyword: str) -> list[str]:
354356
"""Extract all references for this heading."""
355357
refs = []
356-
for tag, types in self._refs.items():
357-
if keyword in types and (name := self._cache.tags.tagName(tag)):
358+
if keyword == nwKeyWords.TAG_KEY:
359+
if name := self._cache.tags.tagName(self._tag):
358360
refs.append(name)
361+
else:
362+
for tag, types in self._refs.items():
363+
if keyword in types and (name := self._cache.tags.tagName(tag)):
364+
refs.append(name)
359365
return refs
360366

361367
##

novelwriter/gui/noveltree.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ def appendTags(refs: dict, key: str, lines: list[str]) -> None:
593593
lines = []
594594
if head := SHARED.project.index.getItemHeading(tHandle, sTitle):
595595
tags = head.getReferences()
596+
appendTags(tags, nwKeyWords.TAG_KEY, lines)
596597
appendTags(tags, nwKeyWords.POV_KEY, lines)
597598
appendTags(tags, nwKeyWords.FOCUS_KEY, lines)
598599
appendTags(tags, nwKeyWords.CHAR_KEY, lines)

sample/content/636b6aa9b697b.nwd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
%%~name: Making a Scene
22
%%~path: 6a2d6d5f4f401/636b6aa9b697b
33
%%~kind: NOVEL/DOCUMENT
4-
%%~hash: 1f3d98a6a27b4f9a7f2239fcd78f9e2263cd3b97
5-
%%~date: Unknown/2025-05-18 22:36:59
4+
%%~hash: b39201b02e3db63493d61d09e5fec5765a937255
5+
%%~date: Unknown/2025-06-24 09:09:56
66
### Making a Scene
77

8+
@tag: Scene
89
@pov: Jane
910
@char: John, Jane
1011
@location: Earth

sample/nwProject.nwx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version='1.0' encoding='utf-8'?>
2-
<novelWriterXML appVersion="2.7.1" hexVersion="0x020701f0" fileVersion="1.5" fileRevision="5" timeStamp="2025-06-09 22:54:50">
3-
<project id="e2be99af-f9bf-4403-857a-c3d1ac25abea" saveCount="2194" autoCount="286" editTime="96892">
2+
<novelWriterXML appVersion="2.7.1" hexVersion="0x020701f0" fileVersion="1.5" fileRevision="5" timeStamp="2025-06-24 16:14:48">
3+
<project id="e2be99af-f9bf-4403-857a-c3d1ac25abea" saveCount="2208" autoCount="287" editTime="97150">
44
<name>Sample Project</name>
55
<author>Jane Smith</author>
66
</project>
@@ -58,7 +58,7 @@
5858
<name status="sf24ce6" import="ia857f0" active="yes">Chapter One</name>
5959
</item>
6060
<item handle="636b6aa9b697b" parent="6a2d6d5f4f401" root="7031beac91f75" order="0" type="FILE" class="NOVEL" layout="DOCUMENT">
61-
<meta expanded="no" heading="H3" charCount="2999" wordCount="530" paraCount="16" cursorPos="718" />
61+
<meta expanded="no" heading="H3" charCount="2999" wordCount="530" paraCount="16" cursorPos="93" />
6262
<name status="s90e6c9" import="ia857f0" active="yes">Making a Scene</name>
6363
</item>
6464
<item handle="bc0cbd2a407f3" parent="6a2d6d5f4f401" root="7031beac91f75" order="1" type="FILE" class="NOVEL" layout="DOCUMENT">

tests/test_core/test_core_index.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ def testCoreIndex_ExtractData(nwGUI, fncPath, mockRnd):
695695
))
696696
assert index.scanText(nHandle, (
697697
"# Hello World!\n"
698+
"@tag: Scene\n"
698699
"@pov: Jane\n"
699700
"@char: Jane, John\n\n"
700701
"% this is a comment\n\n"
@@ -749,11 +750,13 @@ def testCoreIndex_ExtractData(nwGUI, fncPath, mockRnd):
749750

750751
# Look up an invalid handle
751752
refs = index.getReferences("Not a handle")
753+
assert refs["@tag"] == []
752754
assert refs["@pov"] == []
753755
assert refs["@char"] == []
754756

755757
# The novel file should now refer to Jane as @pov and @char
756758
refs = index.getReferences(nHandle)
759+
assert refs["@tag"] == ["Scene"]
757760
assert refs["@pov"] == ["Jane"]
758761
assert refs["@char"] == ["Jane", "John"]
759762

@@ -791,7 +794,7 @@ def testCoreIndex_ExtractData(nwGUI, fncPath, mockRnd):
791794

792795
# getKeyWordTags
793796
# ==============
794-
assert index.getKeyWordTags("@mention") == ["Jane", "John"]
797+
assert index.getKeyWordTags("@mention") == ["Jane", "John", "Scene"]
795798
assert index.getKeyWordTags("@char") == ["Jane", "John"]
796799
assert index.getKeyWordTags("@plot") == []
797800
assert index.getKeyWordTags("@tag") == []

tests/test_core/test_core_indexdata.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ def testCoreIndexData_IndexHeadingReferences():
267267
head = IndexHeading(cache, "T0001")
268268

269269
# Add some references
270+
head.setTag("Scene")
270271
head.addReference("Jane", "@pov")
271272
head.addReference("Jane", "@char")
272273
head.addReference("John", "@char")
@@ -290,6 +291,7 @@ def testCoreIndexData_IndexHeadingReferences():
290291
}
291292

292293
# Set names
294+
cache.tags.add("Scene", "Scene", "0000000000000", "T00001", "NOVEL")
293295
cache.tags.add("Jane", "Jane", "0000000000000", "T00001", "CHARACTER")
294296
cache.tags.add("John", "John", "0000000000000", "T00001", "CHARACTER")
295297
cache.tags.add("Main", "Main", "0000000000000", "T00001", "PLOT")
@@ -301,7 +303,7 @@ def testCoreIndexData_IndexHeadingReferences():
301303
"@plot": ["Main"],
302304
"@object": ["Gun"],
303305
"@story": [],
304-
"@tag": [],
306+
"@tag": ["Scene"],
305307
"@focus": [],
306308
"@custom": [],
307309
"@time": [],
@@ -316,7 +318,7 @@ def testCoreIndexData_IndexHeadingReferences():
316318
assert head.getReferencesByKeyword("@plot") == ["Main"]
317319
assert head.getReferencesByKeyword("@object") == ["Gun"]
318320
assert head.getReferencesByKeyword("@story") == []
319-
assert head.getReferencesByKeyword("@tag") == []
321+
assert head.getReferencesByKeyword("@tag") == ["Scene"]
320322
assert head.getReferencesByKeyword("@focus") == []
321323
assert head.getReferencesByKeyword("@custom") == []
322324
assert head.getReferencesByKeyword("@time") == []

0 commit comments

Comments
 (0)