Skip to content

Commit c960ba1

Browse files
authored
Merge pull request #81 from shm613/bit-addressing-reference
Add support for including tag submembers and bit addressed tags in reference searches.
2 parents 2f44f64 + c51d0f9 commit c960ba1

4 files changed

Lines changed: 61 additions & 7 deletions

File tree

‎src/L5Sharp.Core/Components/Tag.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,19 @@ public override IEnumerable<Reference> References()
449449
return document.References(TagName.LocalPath).Where(r => r.Scope.IsVisibleTo(Scope));
450450
}
451451

452+
/// <summary>
453+
/// Retrieves a collection of <see cref="Reference"/> objects associated with the current tag.
454+
/// </summary>
455+
/// <param name="includeSubmembers">Specifies whether to include references for submembers of the tag.</param>
456+
/// <returns>A collection of <see cref="Reference"/> that represent the references of the tag in the project.</returns>
457+
public IEnumerable<Reference> References(bool includeSubmembers)
458+
{
459+
if (!TryGetDocument(out var document))
460+
return [];
461+
462+
return document.References(TagName.LocalPath, includeSubmembers).Where(r => r.Scope.IsVisibleTo(Scope));
463+
}
464+
452465
/// <inheritdoc />
453466
public override IEnumerable<ILogixEntity> Dependencies()
454467
{

‎src/L5Sharp.Core/L5X.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,16 +420,17 @@ public bool TryGet<TCode>(uint number, string program, string routine, out TCode
420420
/// provided string name.
421421
/// </summary>
422422
/// <param name="name">The name ot find references for in the project.</param>
423+
/// <param name="includeSubmembers">Whether to include tag submembers of the specified name in the search</param>
423424
/// <returns>A collection of <see cref="Reference"/> objects that represent references to the name.</returns>
424425
/// <remarks>
425426
/// The name typically represents a logix component such as a <c>Tag</c>, <c>DataType</c>, <c>AOI</c>, etc.
426427
/// This method relies on internal indexing of the L5X project file to function. While this method is optimized for
427428
/// repeated lookups in read-only scenarios, frequent modifications to the underlying XML will trigger reindexing,
428429
/// which may impact performance.
429430
/// </remarks>
430-
public IEnumerable<Reference> References(string name)
431+
public IEnumerable<Reference> References(string name, bool includeSubmembers = false)
431432
{
432-
return _index.FindReferences(name);
433+
return _index.FindReferences(name, includeSubmembers);
433434
}
434435

435436
/// <summary>

‎src/L5Sharp.Core/LogixIndex.cs‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,29 @@ public bool TryGetElement<TElement>(Reference reference, out TElement result) wh
122122
/// Retrieves all references associated with the specified name.
123123
/// </summary>
124124
/// <param name="name">The name for which references are to be found. Must not be null or empty.</param>
125+
/// <param name="includeSubmembers">Whether to include tag submembers of the specified name in the search</param>
125126
/// <returns>
126127
/// A collection of <see cref="Reference"/> objects representing the references associated with the given name.
127128
/// If no references are found, an empty collection is returned.
128129
/// </returns>
129130
/// <exception cref="ArgumentException">Thrown when the provided name is null or empty.</exception>
130-
public IEnumerable<Reference> FindReferences(string name)
131+
public IEnumerable<Reference> FindReferences(string name, bool includeSubmembers = false)
131132
{
132133
if (string.IsNullOrEmpty(name))
133134
throw new ArgumentException("Name cannot be null or empty.", nameof(name));
134-
135+
135136
IndexIfRequired();
136-
137-
return _references.TryGetValue(name, out var references) ? references : [];
137+
138+
if (!includeSubmembers)
139+
return _references.TryGetValue(name, out var references) ? references : [];
140+
141+
var referenceKeys = _references.Keys.Where(key =>
142+
key.StartsWith($"{name}.", StringComparison.OrdinalIgnoreCase)
143+
|| string.Equals(key, name, StringComparison.OrdinalIgnoreCase));
144+
var allReferences = referenceKeys.SelectMany(key =>
145+
_references.TryGetValue(key, out var references) ? references : []);
146+
147+
return allReferences;
138148
}
139149

140150
/// <summary>

‎tests/L5Sharp.Tests.Core/L5XReferencesTests.cs‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FluentAssertions;
2+
using L5Sharp.Tests.Core.Data.Custom;
23

34

45
namespace L5Sharp.Tests.Core;
@@ -17,11 +18,40 @@ public void TagReferences_ProjectWithSimpleRungReferences_ShouldBeExpectedCount(
1718
.WithRung("XIC(MyTag)OTE(MyTag);")
1819
.Build());
1920

20-
var references = project.Tags.Get("MyTag").References().ToList();
21+
var references = project.Tags.Get("MyTag").References(true).ToList();
2122

2223
references.Should().HaveCount(3);
2324
}
2425

26+
[Test]
27+
public void TagReferences_RungWithBitAddressedDintTag_ShouldCountAllOccurrences()
28+
{
29+
var project = L5X.New("MyProject", "1756-L84E", "34.1")
30+
.Add(Tag.Named("SingleDint").WithValue(123).Build())
31+
.Add(Routine.Rll("MyRoutine").InProgram("MyProgram")
32+
.WithRung("XIC(SingleDint.0)TON(LocalTag,?,?);")
33+
.WithRung("XIC(SingleDint.1)OTE(SingleDint.2);")
34+
.Build());
35+
36+
var references = project.Tags.Get("SingleDint").References(true).ToList();
37+
38+
references.Should().HaveCount(3);
39+
}
40+
41+
[Test]
42+
public void TagReferences_RungWithTagSubmembers_ShouldCountAllOccurrences()
43+
{
44+
var project = L5X.New("MyProject", "1756-L84E", "34.1")
45+
.Add(Tag.Named("NestedTag").WithValue(new MyNestedData()).Build())
46+
.Add(Routine.Rll("MyRoutine").InProgram("MyProgram")
47+
.WithRung("XIC(NestedTag.Indy)OTE(NestedTag.Flags[0]);")
48+
.Build());
49+
50+
var references = project.Tags.Get("NestedTag").References(true).ToList();
51+
52+
references.Should().HaveCount(2);
53+
}
54+
2555
[Test]
2656
public void TagReferences_RungWithCommaSeparatedArgs_ShouldCountAllOccurrences()
2757
{

0 commit comments

Comments
 (0)