How can I find what line number in the source file the declaration was found on?
Disclaimer: I work for Microsoft on the Roslyn team.
You can use the ISyntaxTree.GetLineSpan()
method to convert to a line number. For example, given an ISymbol
"symbol", you can get the start location of the first definition with:
var loc = symbol.Locations.First();
var lineSpan = loc.SourceTree.GetLineSpan(loc.SourceSpan,
usePreprocessorDirectives: false);
var line = lineSpan.StartLinePosition.Line;
var character = lineSpan.StartLinePosition.Character;
From the title, it looks like you're starting with a SyntaxNode
, so you can just use the Span
property directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With