1

I am using Roslyn within a source generator to find certain c# method calls in a syntax tree. When found, I want to note the line and column number.

This answer works for the line number but not the column number.

This is how I get the LineSpan for an InvocationExpression node:

syntaxTree.GetLineSpan(node.Span, cancellationToken)

In my case, the returned FileLinePositionSpan's starting Character is 5. Accounting for it being zero-based, it's character number 4. This matches Visual Studio's

Ch value shown below.

Visual Studio Code Editor status bar

Col, however, is 13. VS figures that out by multiplying each tab character by 5. Two tabs and three spaces lands you on column 13. 5+5+3 = 13.

How can I accurately get the Column number with Roslyn?

p.s. There seemed to be cases where GetLineSpan returned Character values that did not coincide with Visual Studio, but I am unable to reproduce that now.

  • 1
    I'd expect those to match, other than the IDE shows one-based numbers and the Roslyn API is zero based. What difference are you seeing? – Jason Malinowski Aug 04 '22 at 20:31
  • Hmm. The Roslyn character number does not match the visual studio column number. For example, if the first character is a tab, the end of that will be column 5 (with my IDE settings. I rechecked Roslyn's character numbers and they are correct, assuming the line number is zero-based as well. When I looked at it before, the start character was correct, but the ending character was far beyond the end of the line. – Christopher J. Grace Aug 05 '22 at 01:55
  • Does not match because you're using tabs and those have an IDE setting set by the user that can be different from user to user. Roslyn identifies the position in the file where the tabe is 1 character. – Paulo Morgado Aug 05 '22 at 09:43
  • I wonder how mono cecil does it. Somehow it can look at a compiled assembly and reflect out the exact line and column in the original source code file. – Christopher J. Grace Aug 05 '22 at 15:18
  • mono cecil provides the column number by using the pdb. – Christopher J. Grace Aug 05 '22 at 15:44
  • So the Roslyn thing should match (ignoring zero- versus one-based) the "Ch" in there. Note that the "column" number depends on the tab width setting, which the compiler doesn't even know, so it cannot use. I'm guessing Mono.Cecil is simply decompiling the code and happens to be showing it in a text editor with the same tab width settings as your VS IDE. Try changing your tab width in VS and see if it still matches. – Jason Malinowski Aug 06 '22 at 00:27
  • @JasonMalinowski, I wonder if the pdb includes something to indicate what VS's tab width setting was. Probably not since the source files could have been edited with anything. Does this post mean Roslyn does not consider the pdb at all when building the syntax tree? https://stackoverflow.com/questions/39456994/is-it-possible-to-create-a-microsoft-codeanalysis-csharp-csharpcompilation-from – Christopher J. Grace Aug 09 '22 at 17:44
  • Ah, that question is now out of date -- we absolutely can now reconstruct a Compilation from certain PDBs since we now embed more information into the PDB. But still, there's no tab setting anywhere. Remember, the compiler that's actually building your code is not running inside Visual Studio. There's no requirement you even have Visual Studio installed to build. – Jason Malinowski Aug 15 '22 at 18:41
  • In order to match the output of the pre-existing reflection-based solution I'm replacing, I may have to assume five character tabs. But there doesn't appear to be a way to get the characters in whitespace trivia from the syntax tree. – Christopher J. Grace Sep 08 '22 at 18:13

0 Answers0