Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Format.HasNested checks for existing Placeholder in Items #416

Merged
merged 2 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Format.HasNested checks for existing Placeholder
Closes  #415
* `Format.HasNested` checks Items for existing `Placeholder`
* Update Format Class Initialization (mark overload with arg `bool hasNested` as obsolete
  • Loading branch information
axunonb committed Jun 8, 2024
commit baa7f4813abdb8d9ab740c4aa4a70eae5065d3a5
16 changes: 6 additions & 10 deletions src/SmartFormat/Core/Parsing/Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,12 @@
/// <param name="baseString">The base format string-</param>
/// <param name="startIndex">The start index within the format base string.</param>
/// <param name="endIndex">The end index within the format base string.</param>
/// <param name="hasNested"><see langword="true"/> if the nested formats exist.</param>
/// <param name="hasNested"><see langword="true"/> if the format at least one nested <see cref="Placeholder"/>.</param>
/// <returns>This <see cref="Format"/> instance.</returns>
[Obsolete("Use the overload without 'hasNested' instead.")]
public Format Initialize(SmartSettings smartSettings, string baseString, int startIndex, int endIndex, bool hasNested)
{
base.Initialize(smartSettings, null, baseString, startIndex, endIndex);
ParentPlaceholder = null;
HasNested = hasNested;

Initialize(smartSettings, baseString, startIndex, endIndex);

Check warning on line 91 in src/SmartFormat/Core/Parsing/Format.cs

View check run for this annotation

Codecov / codecov/patch

src/SmartFormat/Core/Parsing/Format.cs#L91

Added line #L91 was not covered by tests
return this;
}

Expand All @@ -104,7 +102,6 @@
Clear();

ParentPlaceholder = null;
HasNested = false;

#pragma warning disable S3267 // Don't use LINQ in favor of less GC
// Return and clear FormatItems we own
Expand Down Expand Up @@ -143,11 +140,11 @@
/// Gets the <see cref="List{T}"/> of <see cref="FormatItem"/>s.
/// </summary>
public List<FormatItem> Items { get; } = new();

/// <summary>
/// Returns <see langword="true"/>, if the <see cref="Format"/> is nested.
/// Returns <see langword="true"/>, if the <see cref="Items"/> contain at least one nested <see cref="Placeholder"/>.
/// </summary>
public bool HasNested { get; internal set; }
public bool HasNested => Items.Exists(i => i is Placeholder);

#endregion

Expand Down Expand Up @@ -199,7 +196,6 @@
else
{
// item is a placeholder -- we can't split a placeholder though.
substring.HasNested = true;
}

substring.Items.Add(newItem);
Expand Down
1 change: 0 additions & 1 deletion src/SmartFormat/Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ private void CreateNewPlaceholder(ref int nestedDepth, out Placeholder newPlaceh
nestedDepth++;
newPlaceholder = PlaceholderPool.Instance.Get().Initialize(_resultFormat, _index.Current, nestedDepth);
_resultFormat.Items.Add(newPlaceholder);
_resultFormat.HasNested = true;
_index.Operator = _index.SafeAdd(_index.Current, 1);
_index.Selector = 0;
_index.NamedFormatterStart = PositionUndefined;
Expand Down
2 changes: 1 addition & 1 deletion src/SmartFormat/Extensions/ListFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
// The format is not nested,
// so we will treat it as an ItemFormat:
var newItemFormat = FormatPool.Instance.Get().Initialize(_smartSettings, itemFormat.BaseString,
itemFormat.StartIndex, itemFormat.EndIndex, true);
itemFormat.StartIndex, itemFormat.EndIndex);
itemFormat.ParentPlaceholder = formattingInfo.Placeholder;

var newPlaceholder = PlaceholderPool.Instance.Get().Initialize(newItemFormat, itemFormat.StartIndex, 0);
Expand Down