Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VisitClassDeclaration node.Identifier in Roslyn

Tags:

c#

.net

roslyn

How can I find what line number in the source file the declaration was found on?

like image 289
Doug Finke Avatar asked Oct 26 '11 18:10

Doug Finke


1 Answers

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.

like image 128
Kevin Pilch Avatar answered Nov 10 '22 03:11

Kevin Pilch