This isn't on the list of unimplemented features for Roslyn in C#, and I'm not positive I've found a bug either so I'm posting here.
In a code action, write the following
public CodeActionEdit GetEdit(CancellationToken cancellationToken) { var compilationUnit = (CompilationUnitSyntax)document.GetSyntaxRoot(); document = document.UpdateSyntaxRoot(compilationUnit.AddUsings(Syntax.UsingDirective(Syntax.ParseName("System.Linq")))); //document = document.UpdateSyntaxRoot(compilationUnit.AddMembers(Syntax.ClassDeclaration("TESTCLASS"))); return new CodeActionEdit(document.UpdateSyntaxRoot(compilationUnit.AddUsings(Syntax.UsingDirective(Syntax.ParseName("System"))).NormalizeWhitespace())); }
Your code action should successfully add System; and System.Linq;
Uncomment the commented out line and run again.. Only System; will be brought in.. The empty class will be created but it seems as though there is difficulty in calculating the delta between the UpdateSyntaxRoot calls when you use AddUsings (System.Linq; not brought in).
Also if you keep clicking the lightbulb you can get some pretty interesting behavior (again, whatever is keeping track here seems completely busted and you can see System.Linq; has not been added) :
ALSO, my unit tests pass :P maybe not smart to get all high and mighty about my unit tests vs a team of MS developers but when I do editor.Actions.First().GetEdit().UpdatedSolution.GetDocument(documentId).GetText().ToString(); I'm getting the correct
code back--it's only in true practice do I see these bugs.