Tomlyn icon indicating copy to clipboard operation
Tomlyn copied to clipboard

Is there any way to retrieve the line ?

Open JeanArhancet opened this issue 2 years ago • 3 comments

Hey there,

Thanks for your awesome Toml parser.

Is there a way to retrieve the line in which a toml element is located? For example, how can I find out the list line?

var toml = @"global = ""this is a string""
# This is a comment of a table
[my_table]
key = 1 # Comment a key
value = true
list = [4, 5, 6]
";

// Converts the TOML string to a `TomlTable`
var model = Toml.ToModel(toml);
// Prints "this is a string"
Console.WriteLine(model["global"]);

Is it possible to retrieve a line from a tomlArray...etc?

JeanArhancet avatar Aug 18 '23 16:08 JeanArhancet

Line is accessible by parsing (but not converting to a model) and then through DocumentSyntax, you can access the Span property of the various elements. The model is a step that is compressing the information and would not be able to recover all the details that you get through the syntax model.

xoofx avatar Sep 08 '23 05:09 xoofx

https://github.com/xoofx/Tomlyn/pull/69 might help, although you'll have to add public TomlPropertiesMetadata? PropertiesMetadata { get; set; } to your classes.

lilith avatar Jan 30 '24 00:01 lilith

TomlTable.PropertiesMetadata.TryGetProperty provides TomlPropertyMetadata which has .Span, which is a SourceSpan. This will give you the line for list. This was added to main recently, so probably the next release?

lilith avatar Jan 31 '24 22:01 lilith