Created
February 16, 2017 06:15
-
-
Save aienabled/d20c5d34e4ec945003dda4be8b76bd09 to your computer and use it in GitHub Desktop.
Preprocessor to make C# code for WPF compatible with NoesisGUI (replace System.Windows namespaces)
This file contains hidden or 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
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