Skip to content

Commit

Permalink
Merge pull request #69 from lilith/main
Browse files Browse the repository at this point in the history
Add TomlPropertyMetadata.Span and populate
  • Loading branch information
xoofx authored Jan 31, 2024
2 parents ffcc734 + c25ccfe commit 20971a1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
41 changes: 40 additions & 1 deletion src/Tomlyn.Tests/SyntaxTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -96,5 +96,44 @@ public void Sample()
var value = (bool) ((TomlTable) table["mytable"]!)["val"]!;
Console.WriteLine($"key = {key}, val = {value}");
}

class PropMetaTestDoc : ITomlMetadataProvider{
public PropMetaTestTable? Mytable {get; set;}
public TomlPropertiesMetadata? PropertiesMetadata { get; set; }
}
class PropMetaTestTable : ITomlMetadataProvider{
public int Key {get; set;}
public bool Val {get; set;}
public TomlPropertiesMetadata? PropertiesMetadata { get; set; }
}

[Test]
public void TestPropertySpan()
{
var input = """
[mytable]
key = 15
val = true
""";
var doc = Toml.Parse(input);

var instance = doc.ToModel<PropMetaTestDoc>();
Assert.True(instance.Mytable?.PropertiesMetadata?.ContainsProperty("key"));
if (instance.Mytable?.PropertiesMetadata?.TryGetProperty("key", out var keyMeta) == true){
Assert.That(keyMeta.Span.Start.Line, Is.EqualTo(1));
Assert.That(keyMeta.Span.Start.Column, Is.EqualTo(0));
Assert.That(keyMeta.Span.End.Line, Is.EqualTo(1));
// End of line may differ depending on line endings in source control, don't assert
}

var table = doc.ToModel();
var myTable = (TomlTable) table["mytable"]!;
var myTableProperties = myTable.PropertiesMetadata;

Assert.True(myTableProperties?.ContainsProperty("key"));
if (myTableProperties?.TryGetProperty("key", out var keyMeta2) == true){
Assert.That(keyMeta2.Span.ToString().StartsWith("(2,1)-(2,"));
}
}
}
}
4 changes: 3 additions & 1 deletion src/Tomlyn/Model/SyntaxToModelTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,9 @@ public override void Visit(InlineTableSyntax inlineTable)
metadata.TrailingTriviaAfterEndOfLine = trivias;
}
}

metadata ??= new TomlPropertyMetadata();
metadata.Span = syntax.Span;

return metadata;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Tomlyn/Model/TomlPropertiesMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class TomlPropertyMetadata
/// Gets the trailing trivia attached to this node. Might be null if no trailing trivias.
/// </summary>
public List<TomlSyntaxTriviaMetadata>? TrailingTriviaAfterEndOfLine { get; set; }

public SourceSpan Span {get; set;}
}

public record struct TomlSyntaxTriviaMetadata(TokenKind Kind, string? Text)
Expand Down

0 comments on commit 20971a1

Please sign in to comment.