2

I'm using Roslyn to develop a small tool that searches a code base. One functionality is I get to search if a method exists or not and if it does, I return the full method text to display on UI.

I'm using Visual Studio 2015 which has Roslyn integrated with it. A weird case that I've observed is the case when a particular method is the first method inside a #region.

Whenever I get the full text of that particular method, I'm getting an extra #endregion from the previous region end and also the current region start section.

Sample output that I'm getting for a method that is the first method inside a region.

#endregion      -- why is this coming?

#region Public Methods and Operators

/// <summary>
/// My Method's summary.
/// </summary>
/// <param name="param1">
/// The param1.
/// </param>
/// <returns>
/// returns something
/// </returns>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SampleMethod(Parameter param1)
{
    ....
....
sample code
    return this.RedirectToAction("sample_action");
}

Code that I've written to get the full method text is given below.

var MethodText = ((MethodDeclarationSyntax)method).ToFullString()

Is this how it is expected to work?

0

2 Answers 2

2

The parser has a general rule that "trivia on a line by itself gets attached to the next real token on some future line". So in this case, all the trivia before the method is getting attached to the next method. There isn't special-case logic for #endregion to do something else. Perhaps it should but it doesn't today.

You'll probably have to write some custom code to tweak your display. For example, you might want to exclude all trivia that isn't doc comment trivia. Consider this more contrived case:

#region Comment

// some long comment about the method below me

#endregion

void AbandonAllHopeYeWhoEntersHere() { ...}

Do you want to display that? Not really sure, to be honest.

Trivia is not trivial. It involves figuring out user intent in a lot cases, so we went with simple rules versus a huge pile of heuristics. (We often considered renaming trivia to "complexia" or something to warn people approaching from a distance, but decided not to.)

3
  • Nice explanation. Do you see "heuristics" being used down the line for things that might seem like what the user is expecting it to be like, e.g ToFullString() dependent on the type of Node or possibly extension based utility methods like node.WithoutRegions().ToFullString() or node.WithRegions().ToFullString() ? Aug 28, 2015 at 8:13
  • Thanks for explaining how the parser treats trivia. I liked the 'considered renaming to complexia' ;) At the moment, yes I did put few extra lines of code to remove unwanted stuff. And to be honest, I didn't really plan to specifically show the "long comment about my method" along with the full method text. I just used the method to get the full text. However, after seeing this case, even I feel having something like node.GetMethodText() that returns just the method text and not the comment and/or any leading/trailing trivia would be good. :) Cheers.
    – Amogh Natu
    Aug 28, 2015 at 13:54
  • 1
    It's unlikely that we will change lots of heuristics now because we might break extensions that expected the previous form. More likely we will add some helpers that "do the right thing" in more cases. Sometimes for a refactoring you still need to tinker and there's no way to avoid the low level stuff, but we might have some higher level APIs. Aug 28, 2015 at 16:36
2

From looking at the syntax visualizer, it doesn't look like it fits in right. I'm not entirely sure if this is expected behaviour, but it doesn't feel right as you say.

However, if you don't need any of the documentation you could use ToString(), which will just return the method and everything inside it.

ToFullString() includes leading and trailing trivia. It appears to go all the way up until the next leading SyntaxNode.

You could use something like this for now to remove the Trivia, before using ToFullString()

var trivia = method.DescendantTrivia()
                   .Where(t => t.IsKind(SyntaxKind.EndRegionDirectiveTrivia));

foreach (var t in trivia)
{
    method = method.ReplaceTrivia(t, trivia.ToSyntaxTriviaList()
                   .Except(st => st.IsKind(SyntaxKind.EndRegionDirectiveTrivia)));
}

method.ToFullString();

Snippet to use in Linqpad:

I can't seem to find any issue on the Roslyn Github issues.

EDIT: You also don't get the #endregion of the method. I would suggest removing any region syntax kind and then call .ToFullString()

3
  • Yup I removed region related SyntaxKind and then got the method the way I was looking for. I guess it is indeed going all the way up to the next SyntaxNode. But that does seems a bit odd though to get something that's not at all related to the required SyntaxNode. Thanks for the snippet and link :)
    – Amogh Natu
    Aug 27, 2015 at 18:57
  • 1
    ToFullString includes all leading and trailing trivia on the node, and the region directive is leading trivia on the public keyword (you should be able to see this in the Syntax Visualizer by expanding the PublicKeyword and clickiing on its "Lead: RegionDirectiveTrivia" child node), so it's expected that it would be included in the ToFullString representation. You could argue that there should be a better syntax tree shape in this case, but directives are represented as trivia, so they have to be attached to something and any other implementation could cause similar confusion. Aug 27, 2015 at 19:48
  • 1
    The syntax visualizer should be updated, it's misleading because it doesn't show everything, only once you click into the public keyword then it shows and highlights everything to the next SyntaxNode. Basically, the ToFullString() does what it's expected to do, but from a users perspective of wanting to get a string representation of a method, it's not exactly what a user might want. Aug 27, 2015 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.