Skip to content

Commit

Permalink
Add TomlPropertyMetadata.Span and populate
Browse files Browse the repository at this point in the history
  • Loading branch information
lilith committed Aug 4, 2023
1 parent 3c221dd commit ce792b5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/Tomlyn.Tests/SyntaxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,43 @@ 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));
// TextPosition adds 1 to line and column in ToString
Assert.That(keyMeta.Span.ToString(), Is.EqualTo("(2,1)-(2,9)"));
}

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(), Is.EqualTo("(2,1)-(2,9)"));
}
}
}
}
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 ce792b5

Please sign in to comment.