Created
February 23, 2020 17:31
-
-
Save Skamiplan/7ebc36c4f7f239e1e4767a78852035de to your computer and use it in GitHub Desktop.
Refactor Code with Roslyn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.CodeAnalysis; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using Microsoft.CodeAnalysis.CSharp; | |
using Microsoft.CodeAnalysis.CSharp.Syntax; | |
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | |
namespace POC | |
{ | |
public static class Special | |
{ | |
private static string programText = ""; | |
public static void Start() | |
{ | |
NameSyntax name = IdentifierName("System"); | |
name = QualifiedName(name, IdentifierName("Collections")); | |
name = QualifiedName(name, IdentifierName("Generic")); | |
SyntaxTree tree = CSharpSyntaxTree.ParseText( | |
@"using System; | |
using System.Collections; | |
using System.Linq; | |
using System.Text; | |
namespace HelloWorld | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string abc = ""Hey!""; | |
Console.WriteLine(abc); | |
} | |
} | |
}"); | |
var root = (CompilationUnitSyntax)tree.GetRoot(); | |
var oldUsing = root.Usings[1]; | |
var newUsing = oldUsing.WithName(name); | |
var nameSpaces = root.Members.Select(x => (NamespaceDeclarationSyntax)x).ToArray(); | |
for (int s = 0; s < nameSpaces.Length; s++) | |
{ | |
var namespaceDeclarationSyntax = nameSpaces[s]; | |
var newnamespaceDeclarationSyntax = namespaceDeclarationSyntax; | |
var progams = namespaceDeclarationSyntax.Members.Select(x => (ClassDeclarationSyntax)x).ToArray(); | |
for (int d = 0; d < progams.Count(); d++) | |
{ | |
var declarationSyntax = progams[d]; | |
var newdeclarationSyntax = declarationSyntax; | |
var mains = declarationSyntax.Members.Select(x => (MethodDeclarationSyntax)x).ToArray(); | |
for (int m = 0; m < mains.Count(); m++) | |
{ | |
var main = mains[m]; | |
var newMain = main; | |
var variables = main.DescendantNodes().Where(x => x is VariableDeclaratorSyntax) | |
.Select(x => (VariableDeclaratorSyntax)x).ToArray(); | |
for (int v = 0; v < variables.Count(); v++) | |
{ | |
var variable = variables[v]; | |
var usings = main.DescendantNodes() | |
.Where( | |
x => x is IdentifierNameSyntax && ((IdentifierNameSyntax)x).Identifier.ValueText | |
== variable.Identifier.ValueText).Select(x => (IdentifierNameSyntax)x).ToList(); | |
foreach (var identifierNameSyntax in usings) | |
{ | |
Console.WriteLine("Using found " + identifierNameSyntax); | |
} | |
SyntaxToken variableIdentifier = variable.Identifier; | |
VariableDeclaratorSyntax newVariable = variable.ReplaceToken( | |
variableIdentifier, | |
SyntaxFactory.Identifier("dfg")); | |
Console.WriteLine(variable); | |
variables[v] = variable; | |
main = main.ReplaceNode(variable, newVariable); | |
usings = main.DescendantNodes() | |
.Where( | |
x => x is IdentifierNameSyntax && ((IdentifierNameSyntax)x).Identifier.ValueText | |
== variable.Identifier.ValueText).Select(x => (IdentifierNameSyntax)x).ToList(); | |
for (var index = 0; index < usings.Count; index++) | |
{ | |
var identifierNameSyntax = usings[index]; | |
SyntaxToken usingIdentifier = identifierNameSyntax.Identifier; | |
IdentifierNameSyntax newusingIdentifier = identifierNameSyntax.ReplaceToken( | |
usingIdentifier, | |
SyntaxFactory.Identifier("dfg")); | |
var newIdentifierNameSyntax = identifierNameSyntax.ReplaceToken( | |
usingIdentifier, | |
newusingIdentifier.Identifier); | |
Console.WriteLine("Using found " + identifierNameSyntax); | |
main = main.ReplaceNode(identifierNameSyntax, newIdentifierNameSyntax); | |
} | |
declarationSyntax = declarationSyntax.ReplaceNode(newMain, main); | |
} | |
declarationSyntax = declarationSyntax.ReplaceNode(newdeclarationSyntax, declarationSyntax); | |
} | |
namespaceDeclarationSyntax = namespaceDeclarationSyntax.ReplaceNode(newdeclarationSyntax, declarationSyntax); | |
} | |
root = root.ReplaceNode(newnamespaceDeclarationSyntax, namespaceDeclarationSyntax); | |
} | |
root = root.ReplaceNode(oldUsing, newUsing); | |
var code = root.ToFullString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment