Is there a way to know what Line and "Column" a code block statement starts at in code

Is there a way to know what Line and "Column" a code block statement starts at in code?

For example when modifing the Body of a IMethodDeclaration which has a collection of "Statements"; Is there a was to know how indented a part of the code is.
Like "Col 75" is where a string variable starts

public void StartProcess()
{
     string example = "This is the string that I want to know which column this string literal starts at";
}

How can I find out that the code is at Ln 3, Col 24?

2 comments
Comment actions Permalink

The line and column information isn't cached in the syntax tree, because it depends on the preceding code, and any change to that code would require recalculating the line and column information for all following nodes in the syntax tree. However, you should be able to use the ITreeNode.GetDocumentRange extension method (ITreeNode is the root interface of all elements in the syntax tree). This will return you a DocumentRange, from which you should be able to call range.Document.GetCoordsByOffset(documentRange.TextRange.StartOffset). I'm not sure on the performance characteristics of this call - I think the document caches this information, but not sure, so be careful of doing this in a hot code path without measuring.

0
Comment actions Permalink

Thank you. That worked great.

0

Please sign in to leave a comment.