Ejemplo n.º 1
0
        public static async Task <SyntaxNode> ConvertCompilationTree(Document document,
                                                                     CSharpCompilation csharpViewOfVbSymbols, Project csharpReferenceProject)
        {
            document = await document.WithExpandedRootAsync();

            var root = await document.GetSyntaxRootAsync() as VBSyntax.CompilationUnitSyntax ??
                       throw new InvalidOperationException(NullRootError(document));

            var compilation = await document.Project.GetCompilationAsync();

            var tree = await document.GetSyntaxTreeAsync();


            var csSyntaxGenerator        = SyntaxGenerator.GetGenerator(csharpReferenceProject);
            var semanticModel            = compilation.GetSemanticModel(tree, true);
            var visualBasicSyntaxVisitor = new
                                           DeclarationNodeVisitor(document, compilation, semanticModel, csharpViewOfVbSymbols, csSyntaxGenerator);
            var converted = (CSS.CompilationUnitSyntax) await root.AcceptAsync(visualBasicSyntaxVisitor.TriviaConvertingDeclarationVisitor);

            try {
                converted = (CSS.CompilationUnitSyntax)Formatter.Format(converted, document.Project.Solution.Workspace);
                return(LineTriviaMapper.MapSourceTriviaToTarget(root, converted));
            } catch (Exception) { //TODO log
                return(converted);
            }
        }
Ejemplo n.º 2
0
        public static async Task <SyntaxNode> ConvertCompilationTree(Document document,
                                                                     CSharpCompilation csharpViewOfVbSymbols, Project csharpReferenceProject, CancellationToken cancellationToken)
        {
            document = await document.WithExpandedRootAsync(cancellationToken);

            var root = await document.GetSyntaxRootAsync(cancellationToken) as VBSyntax.CompilationUnitSyntax ??
                       throw new InvalidOperationException(NullRootError(document));

            var compilation = await document.Project.GetCompilationAsync(cancellationToken);

            var tree = await document.GetSyntaxTreeAsync(cancellationToken);


            var csSyntaxGenerator        = SyntaxGenerator.GetGenerator(csharpReferenceProject);
            var semanticModel            = compilation.GetSemanticModel(tree, true);
            var visualBasicSyntaxVisitor = new
                                           DeclarationNodeVisitor(document, compilation, semanticModel, csharpViewOfVbSymbols, csSyntaxGenerator);
            var converted = (CSS.CompilationUnitSyntax) await root.AcceptAsync(visualBasicSyntaxVisitor.TriviaConvertingDeclarationVisitor);

            try {
                // This call is very expensive for large documents. Should look for a more performant version, e.g. Is NormalizeWhitespace good enough?
                converted = (CSS.CompilationUnitSyntax)Formatter.Format(converted, document.Project.Solution.Workspace, cancellationToken: cancellationToken);
                return(LineTriviaMapper.MapSourceTriviaToTarget(root, converted));
            } catch (Exception) { //TODO log
                return(converted);
            }
        }
Ejemplo n.º 3
0
        public static async Task <SyntaxNode> ConvertCompilationTree(Document document,
                                                                     VisualBasicCompilation vbViewOfCsSymbols, Project vbReferenceProject, CancellationToken cancellationToken)
        {
            document = await document.WithExpandedRootAsync(cancellationToken);

            var compilation = await document.Project.GetCompilationAsync(cancellationToken);

            var tree = await document.GetSyntaxTreeAsync(cancellationToken);

            var semanticModel = compilation.GetSemanticModel(tree, true);
            var root          = await document.GetSyntaxRootAsync(cancellationToken) as CSS.CompilationUnitSyntax ??
                                throw new InvalidOperationException(NullRootError(document));

            var vbSyntaxGenerator = SyntaxGenerator.GetGenerator(vbReferenceProject);
            var numberOfLines     = tree.GetLineSpan(root.FullSpan).EndLinePosition.Line;

            var visualBasicSyntaxVisitor = new NodesVisitor(document, (CS.CSharpCompilation)compilation, semanticModel, vbViewOfCsSymbols, vbSyntaxGenerator, numberOfLines);
            var converted = (VBSyntax.CompilationUnitSyntax)root.Accept(visualBasicSyntaxVisitor.TriviaConvertingVisitor);

            try {
                // This call is very expensive for large documents. Should look for a more performant version, e.g. Is NormalizeWhitespace good enough?
                converted = (VBSyntax.CompilationUnitSyntax)Formatter.Format(converted, document.Project.Solution.Workspace, cancellationToken: cancellationToken);
                return(LineTriviaMapper.MapSourceTriviaToTarget(root, converted));
            } catch (Exception) { //TODO log
                return(converted);
            }
        }
Ejemplo n.º 4
0
 public SyntaxNode MapSourceTriviaToTargetHandled <TSource, TTarget>(TSource root,
                                                                     TTarget converted, Document document)
     where TSource : SyntaxNode, ICompilationUnitSyntax where TTarget : SyntaxNode, ICompilationUnitSyntax
 {
     try
     {
         converted = (TTarget)Format(converted, document);
         return(LineTriviaMapper.MapSourceTriviaToTarget(root, converted));
     }
     catch (Exception e)
     {
         _progress.Report(new ConversionProgress($"Error while formatting and converting comments: {e}"));
         return(converted);
     }
 }
Ejemplo n.º 5
0
        public static async Task <SyntaxNode> ConvertCompilationTree(Document document,
                                                                     VisualBasicCompilation vbViewOfCsSymbols, Project vbReferenceProject)
        {
            document = await document.WithExpandedRootAsync();

            var compilation = await document.Project.GetCompilationAsync();

            var tree = await document.GetSyntaxTreeAsync();

            var semanticModel = compilation.GetSemanticModel(tree, true);
            var root          = await document.GetSyntaxRootAsync() as CSS.CompilationUnitSyntax ??
                                throw new InvalidOperationException(NullRootError(document));

            var vbSyntaxGenerator = SyntaxGenerator.GetGenerator(vbReferenceProject);
            var numberOfLines     = tree.GetLineSpan(root.FullSpan).EndLinePosition.Line;

            var visualBasicSyntaxVisitor = new NodesVisitor(document, (CS.CSharpCompilation)compilation, semanticModel, vbViewOfCsSymbols, vbSyntaxGenerator, numberOfLines);
            var converted = root.Accept(visualBasicSyntaxVisitor.TriviaConvertingVisitor);

            var formattedConverted = (VBSyntax.CompilationUnitSyntax)Formatter.Format(converted, document.Project.Solution.Workspace);


            return(LineTriviaMapper.MapSourceTriviaToTarget(root, formattedConverted));
        }