From a655bae331cfc16341e4e0548f5c6ed53cffa713 Mon Sep 17 00:00:00 2001 From: GrahamTheCoder Date: Sun, 25 Feb 2018 17:40:33 +0000 Subject: [PATCH] Separate language specifics --- .../CSharp/ProjectConversion.cs | 82 +++++++++---------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/ICSharpCode.CodeConverter/CSharp/ProjectConversion.cs b/ICSharpCode.CodeConverter/CSharp/ProjectConversion.cs index 3e6ec33..c7bae8e 100644 --- a/ICSharpCode.CodeConverter/CSharp/ProjectConversion.cs +++ b/ICSharpCode.CodeConverter/CSharp/ProjectConversion.cs @@ -17,38 +17,38 @@ namespace ICSharpCode.CodeConverter.CSharp public class ProjectConversion { private bool _methodBodyOnly; - private VisualBasicCompilation _compilation; - private IEnumerable _syntaxTreesToConvert; + private Compilation _sourceCompilation; + private IEnumerable _syntaxTreesToConvert; private static readonly AdhocWorkspace AdhocWorkspace = new AdhocWorkspace(); private readonly ConcurrentDictionary _errors = new ConcurrentDictionary(); private readonly Dictionary _firstPassResults = new Dictionary(); - private CSharpCompilation _cSharpCompilation; + private Compilation _targetCompilation; - private ProjectConversion(VisualBasicCompilation compilation, string solutionDir) - : this(compilation, compilation.SyntaxTrees.OfType().Where(t => t.FilePath.StartsWith(solutionDir))) + private ProjectConversion(Compilation sourceCompilation, string solutionDir) + : this(sourceCompilation, sourceCompilation.SyntaxTrees.Where(t => t.FilePath.StartsWith(solutionDir))) { } - private ProjectConversion(VisualBasicCompilation compilation, IEnumerable syntaxTreesToConvert) + private ProjectConversion(Compilation sourceCompilation, IEnumerable syntaxTreesToConvert) { - _compilation = compilation; + _sourceCompilation = sourceCompilation; _syntaxTreesToConvert = syntaxTreesToConvert; } public static ConversionResult ConvertText(string text, IReadOnlyCollection references) { - var visualBasicSyntaxTree = CreateTree(text); - var compilation = CreateCompilationFromTree(visualBasicSyntaxTree, references); - return ConvertSingle(compilation, visualBasicSyntaxTree, new TextSpan(0, 0)).GetAwaiter().GetResult(); + var syntaxTree = CreateTree(text); + var compilation = CreateCompilationFromTree(syntaxTree, references); + return ConvertSingle(compilation, syntaxTree, new TextSpan(0, 0)).GetAwaiter().GetResult(); } - public static async Task ConvertSingle(VisualBasicCompilation compilation, VisualBasicSyntaxTree syntaxTree, TextSpan selected) + public static async Task ConvertSingle(Compilation compilation, SyntaxTree syntaxTree, TextSpan selected) { var root = await syntaxTree.GetRootAsync(); if (selected.Length > 0) { var annotatedSyntaxTree = GetSyntaxTreeWithAnnotatedSelection(selected, root); compilation = compilation.ReplaceSyntaxTree(syntaxTree, annotatedSyntaxTree); - syntaxTree = (VisualBasicSyntaxTree) annotatedSyntaxTree; + syntaxTree = annotatedSyntaxTree; } var conversion = new ProjectConversion(compilation, new [] {syntaxTree}); @@ -68,7 +68,7 @@ namespace ICSharpCode.CodeConverter.CSharp var solutionDir = Path.GetDirectoryName(projects.First().Solution.FilePath); foreach (var project in projects) { var compilation = project.GetCompilationAsync().GetAwaiter().GetResult(); - var projectConversion = new ProjectConversion((VisualBasicCompilation)compilation, solutionDir); + var projectConversion = new ProjectConversion(compilation, solutionDir); foreach (var pathNodePair in projectConversion.Convert()) { yield return new ConversionResult(pathNodePair.Value.ToFullString()) {SourcePathOrNull = pathNodePair.Key}; } @@ -84,7 +84,7 @@ namespace ICSharpCode.CodeConverter.CSharp private Dictionary Convert() { FirstPass(); - _cSharpCompilation = CSharpCompilation.Create("Conversion", _firstPassResults.Values, _compilation.References); + _targetCompilation = CSharpCompilation.Create("Conversion", _firstPassResults.Values, _sourceCompilation.References); var secondPassByFilePath = _firstPassResults.ToDictionary(cs => cs.Key, SingleSecondPass); return secondPassByFilePath; } @@ -99,7 +99,7 @@ namespace ICSharpCode.CodeConverter.CSharp SingleFirstPass(tree, treeFilePath); } catch (NotImplementedOrRequiresSurroundingMethodDeclaration) - when (!_methodBodyOnly && _compilation.SyntaxTrees.Length == 1) + when (!_methodBodyOnly && _sourceCompilation.SyntaxTrees.Count() == 1) { SingleFirstPassSurroundedByClassAndMethod(tree); } @@ -110,37 +110,15 @@ namespace ICSharpCode.CodeConverter.CSharp } } - private SyntaxNode SingleSecondPass(KeyValuePair cs) - { - var secondPassNode = new CompilationErrorFixer(_cSharpCompilation, (CSharpSyntaxTree) cs.Value).Fix(); - if (_methodBodyOnly) secondPassNode = secondPassNode.DescendantNodes().OfType().First().Body; - return Formatter.Format(secondPassNode, AdhocWorkspace); - } - - private void SingleFirstPass(VisualBasicSyntaxTree tree, string treeFilePath) - { - var converted = VisualBasicConverter.ConvertCompilationTree(_compilation, tree); - _firstPassResults.Add(treeFilePath, SyntaxFactory.SyntaxTree(converted)); - } - private void SingleFirstPassSurroundedByClassAndMethod(SyntaxTree tree) { var newTree = CreateTree(WithSurroundingClassAndMethod(tree.GetText().ToString())); _methodBodyOnly = true; - _compilation = _compilation.AddSyntaxTrees(newTree); + _sourceCompilation = _sourceCompilation.AddSyntaxTrees(newTree); _syntaxTreesToConvert = new[] {newTree}; Convert(); } - private static string WithSurroundingClassAndMethod(string text) - { - return $@"Class SurroundingClass -Sub SurroundingSub() -{text} -End Sub -End Class"; - } - private static SyntaxTree GetSyntaxTreeWithAnnotatedSelection(TextSpan selected, SyntaxNode root) { var selectedNode = root.FindNode(selected); @@ -154,12 +132,34 @@ End Class"; return annotatedNode == null ? resultNode : Formatter.Format(annotatedNode, AdhocWorkspace); } - private static VisualBasicSyntaxTree CreateTree(string text) + private void SingleFirstPass(SyntaxTree tree, string treeFilePath) { - return (VisualBasicSyntaxTree) Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory.ParseSyntaxTree(SourceText.From(text)); + var converted = VisualBasicConverter.ConvertCompilationTree((VisualBasicCompilation)_sourceCompilation, (VisualBasicSyntaxTree)tree); + _firstPassResults.Add(treeFilePath, SyntaxFactory.SyntaxTree(converted)); } - private static VisualBasicCompilation CreateCompilationFromTree(VisualBasicSyntaxTree tree, IEnumerable references) + private SyntaxNode SingleSecondPass(KeyValuePair cs) + { + var secondPassNode = new CompilationErrorFixer((CSharpCompilation)_targetCompilation, (CSharpSyntaxTree)cs.Value).Fix(); + if (_methodBodyOnly) secondPassNode = secondPassNode.DescendantNodes().OfType().First().Body; + return Formatter.Format(secondPassNode, AdhocWorkspace); + } + + private static string WithSurroundingClassAndMethod(string text) + { + return $@"Class SurroundingClass +Sub SurroundingSub() +{text} +End Sub +End Class"; + } + + private static SyntaxTree CreateTree(string text) + { + return Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory.ParseSyntaxTree(SourceText.From(text)); + } + + private static Compilation CreateCompilationFromTree(SyntaxTree tree, IEnumerable references) { var compilationOptions = new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary) .WithRootNamespace("TestProject")