I'm working on a little piece of roslyn code to try to do some simple refactorings, inside a Visual Studio for MAC extension project.
I want to refactor this code:
public class Test
{
private const object a = null;
public const int c = 0;
}
into this code
public class Test
{
public const int c = 0;
private const object a = null;
}
So far I managed to get a List of ISymbol representing my constants, order them properly, and then transfom them into a list of SyntaxNode like this
var orderedMembers = orderer.OrderAll(classMembers);
var nodes = orderedMembers.Select(o => o.DeclaringSyntaxReferences.FirstOrDefault().GetSyntax())
.Where(node => node != null)
.ToList();
So far so good. The output is like this
public class Test
{
public const int c = 0;
private const object a = null;
}
Now my last step, is adding a few lines after my last constant, to define a separation between my constants and what might follow.
It seems I can do this by adding a Trailing/Leading trivia to one of my SyntaxNode. So I tried this (hard coded for simplicity right now):
nodes[1] = nodes[1].WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
Then I insert my new nodes into my "Test" class declaration node, and make that into a string (here "editor" is an instance of a Microsoft.CodeAnalysis.Editing.DocumentEditor representing a file I opened with vs for mac IDE):
editor.InsertMembers(classNode, 0, nodes);
var newDocument = editor.GetChangedDocument();
(await newDocument.GetTextAsync()).ToString();
and this is what it generates:
public class Test
{
public const int c = 0;
}
So the SyntaxNode I tried to add Trivia to, does not get generated when calling the GetTextAsync on my newly edited Document.
This is the first time I've been playing with Roslyn so I guess I'm missing something important about using Trivia, but I can't seem to find what that is. Any ideas on how to add some new Lines after a defined SyntaxNode ?