Line of Code API?

Topics: APIs
Apr 30, 2014 at 6:42 PM
Does any know how to reference the code as a "Line Of Code" rather than syntax tree / nodes?
LineOfCode := IEnumerable< SyntaxNode >
SourceCode := IEnumerable< LineOfCode >
Say I need to produce a Code-Fix the is dependant what the following line of is also?
Dim x As Integer
x = 100
rewritten by code-fix to
Dim x As Integer = 100
Developer
Apr 30, 2014 at 9:50 PM
You could use something like the following to get the line of a given SyntaxNode
Dim node As SyntaxNode = 'Something
Dim line As FileLinePositionSpan = node.SyntaxTree.GetLineSpan(node.Span)
Or do
 Dim tree As SyntaxTree = 'Something
 Dim text As SourceText = Await tree.GetTextAsync()
 Dim lines As TextLineCollection = text.Lines
To get all lines of a file.

However, what if the code you were analyzing was actually like this:
Dim x As Integer 
' Comment about something important
x = 100
In this case you would want to find LocalDeclarationStatementSyntax nodes and check for assignment. If they are not assigned, you would then check for SimpleAssignmentStatementSyntax nodes and verify that they are the same symbol using a SemanticModel.