Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Preprocessor to make C# code for WPF compatible with NoesisGUI (replace System.Windows namespaces)
public static SyntaxNode ProcessSyntaxTree(
SyntaxTree syntaxTree,
ConcurrentStack<Diagnostic> syntaxTreeProcessingMessages)
{
var replacementsDict = new Dictionary<SyntaxNode, SyntaxNode>(0);
var root = (CompilationUnitSyntax)syntaxTree.GetRoot();
foreach (var usingNode in root.DescendantNodes().OfType<UsingDirectiveSyntax>())
{
var ns = usingNode.Name.GetText().ToString();
if (ns.StartsWith("System.Windows", StringComparison.Ordinal))
{
SyntaxNode replacement = usingNode.WithName(SyntaxFactory.IdentifierName("Noesis"));
replacementsDict.Add(usingNode, replacement);
continue;
}
}
var qualifiedNameNodes = root.DescendantNodes().OfType<QualifiedNameSyntax>();
foreach (var qualifiedNameNode in qualifiedNameNodes)
{
var nsLeft = qualifiedNameNode.Left.GetText().ToString();
if (nsLeft.StartsWith("System.Windows", StringComparison.Ordinal))
{
var replacement =
SyntaxFactory.IdentifierName("Noesis");
replacementsDict.Add(qualifiedNameNode, replacement);
continue;
}
}
if (replacementsDict.Count <= 0)
{
// not modified
return null;
}
root = root.ReplaceNodes(replacementsDict.Keys, (n1, n2) => replacementsDict[n1]);
return root;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment