Example #1
0
        private static SyntaxNode SimplifyName(
            SyntaxNode node,
            SemanticModel semanticModel,
            OptionSet optionSet,
            CancellationToken cancellationToken)
        {
            SyntaxNode replacementNode;
            TextSpan   issueSpan;

            if (node.Kind() == SyntaxKind.QualifiedCref)
            {
                var crefSyntax = (QualifiedCrefSyntax)node;
                if (!crefSyntax.TryReduceOrSimplifyExplicitName(semanticModel, out var crefReplacement, out issueSpan, optionSet, cancellationToken))
                {
                    return(node);
                }

                replacementNode = crefReplacement;
            }
            else
            {
                var expressionSyntax = (ExpressionSyntax)node;
                if (!expressionSyntax.TryReduceOrSimplifyExplicitName(semanticModel, out var expressionReplacement, out issueSpan, optionSet, cancellationToken))
                {
                    return(node);
                }

                replacementNode = expressionReplacement;
            }

            node = node.CopyAnnotationsTo(replacementNode).WithAdditionalAnnotations(Formatter.Annotation);
            return(node.WithoutAnnotations(Simplifier.Annotation));
        }
        private static SyntaxNode SimplifyName(
            SyntaxNode node,
            SemanticModel semanticModel,
            OptionSet optionSet,
            CancellationToken cancellationToken)
        {
            SyntaxNode replacementNode;

            if (node.IsKind(SyntaxKind.QualifiedCref, out QualifiedCrefSyntax crefSyntax))
            {
                if (!QualifiedCrefSimplifier.Instance.TrySimplify(
                        crefSyntax, semanticModel, optionSet,
                        out var crefReplacement, out _, cancellationToken))
                {
                    return(node);
                }

                replacementNode = crefReplacement;
            }
            else
            {
                var expressionSyntax = (ExpressionSyntax)node;
                if (!ExpressionSimplifier.Instance.TrySimplify(expressionSyntax, semanticModel, optionSet, out var expressionReplacement, out _, cancellationToken))
                {
                    return(node);
                }

                replacementNode = expressionReplacement;
            }

            node = node.CopyAnnotationsTo(replacementNode).WithAdditionalAnnotations(Formatter.Annotation);
            return(node.WithoutAnnotations(Simplifier.Annotation));
        }
Example #3
0
        private static SyntaxNode SimplifyName(
            SyntaxNode node,
            SemanticModel semanticModel,
            OptionSet optionSet,
            CancellationToken cancellationToken)
        {
            SyntaxNode replacementNode;
            TextSpan issueSpan;

            if (node.Kind() == SyntaxKind.QualifiedCref)
            {
                var crefSyntax = (QualifiedCrefSyntax)node;
                if (!crefSyntax.TryReduceOrSimplifyExplicitName(semanticModel, out var crefReplacement, out issueSpan, optionSet, cancellationToken))
                {
                    return node;
                }

                replacementNode = crefReplacement;
            }
            else
            {
                var expressionSyntax = (ExpressionSyntax)node;
                if (!expressionSyntax.TryReduceOrSimplifyExplicitName(semanticModel, out var expressionReplacement, out issueSpan, optionSet, cancellationToken))
                {
                    return node;
                }

                replacementNode = expressionReplacement;
            }

            node = node.CopyAnnotationsTo(replacementNode).WithAdditionalAnnotations(Formatter.Annotation);
            return node.WithoutAnnotations(Simplifier.Annotation);
        }
 public T Accept(SyntaxNode node, bool addSourceMapping)
 {
     try {
         var converted = _wrappedVisitor.Visit(node);
         return(addSourceMapping ? node.CopyAnnotationsTo(converted).WithSourceMappingFrom(node)
             : WithoutSourceMapping(converted));
     } catch (Exception e) {
         var dummyStatement = SyntaxFactory.EmptyStatement();
         return(((T)(object)dummyStatement).WithVbTrailingErrorComment((CSharpSyntaxNode)node, e));
     }
 }
        private static T WithAnnotations<T>(SyntaxNode sourceNode, T destination) where T : SyntaxNode
        {
            destination = sourceNode.CopyAnnotationsTo(destination);

            var sourceChildAnnotations = new HashSet<SyntaxAnnotation>(sourceNode.ChildNodes()
                .SelectMany(n => n.GetAnnotations(AnnotationConstants.SelectedNodeAnnotationKind)));
            foreach (var annotation in destination.ChildNodes().SelectMany(n => n.GetAnnotations(AnnotationConstants.SelectedNodeAnnotationKind))) {
                sourceChildAnnotations.Remove(annotation);
            }
            return destination.WithAdditionalAnnotations(sourceChildAnnotations);
        }
Example #6
0
 /// <remarks>
 /// If lots of special cases, move to wrapping the wrappedVisitor in another visitor, but I'd rather use a simple switch here initially.
 /// </remarks>
 private static T WithSourceMapping <T>(SyntaxNode vbNode, T converted) where T : CS.CSharpSyntaxNode
 {
     switch (vbNode)
     {
     case VBSyntax.CompilationUnitSyntax vbCus when converted is CSSyntax.CompilationUnitSyntax csCus:
         converted = (T)(object)csCus.WithEndOfFileToken(
             csCus.EndOfFileToken.WithConvertedLeadingTriviaFrom(vbCus.EndOfFileToken).WithSourceMappingFrom(vbCus.EndOfFileToken)
             );
         break;
     }
     return(vbNode.CopyAnnotationsTo(converted).WithVbSourceMappingFrom(vbNode));
 }
Example #7
0
        private static SyntaxList <T> WithSourceMappingFrom <T>(this SyntaxList <T> converted, SyntaxNode node) where T : SyntaxNode
        {
            if (!converted.Any())
            {
                return(converted);
            }
            var origLinespan = node.SyntaxTree.GetLineSpan(node.Span);
            var first        = converted.First();

            converted = converted.Replace(first, node.CopyAnnotationsTo(first).WithSourceStartLineAnnotation(origLinespan));
            var last = converted.Last();

            return(converted.Replace(last, last.WithSourceEndLineAnnotation(origLinespan)));
        }
Example #8
0
 /// <remarks>
 /// If lots of special cases, move to wrapping the wrappedVisitor in another visitor, but I'd rather use a simple switch here initially.
 /// </remarks>
 private static T WithSourceMapping <T>(SyntaxNode vbNode, T converted) where T : CS.CSharpSyntaxNode
 {
     converted = vbNode.CopyAnnotationsTo(converted);
     switch (vbNode)
     {
     case VBSyntax.CompilationUnitSyntax vbCus when converted is CSSyntax.CompilationUnitSyntax csCus:
         return((T)(object)csCus
                .WithSourceStartLineAnnotation(vbNode.SyntaxTree.GetLineSpan(new TextSpan(0, 0)))
                .WithEndOfFileToken(csCus.EndOfFileToken
                                    .WithConvertedLeadingTriviaFrom(vbCus.EndOfFileToken).WithSourceMappingFrom(vbCus.EndOfFileToken)
                                    ));
     }
     return(converted.WithVbSourceMappingFrom(vbNode));
 }
Example #9
0
    /// <remarks>
    /// If lots of special cases, move to wrapping the wrappedVisitor in another visitor, but I'd rather use a simple switch here initially.
    /// </remarks>
    private static T WithSourceMapping(SyntaxNode csNode, T converted)
    {
        switch (csNode)
        {
        case CSSyntax.AttributeListSyntax _:
            converted = converted.WithPrependedLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed);
            break;

        case CSSyntax.CompilationUnitSyntax csCus when converted is VBSyntax.CompilationUnitSyntax vbCus:
            converted = (T)(object)vbCus.WithEndOfFileToken(
                vbCus.EndOfFileToken.WithConvertedLeadingTriviaFrom(csCus.EndOfFileToken).WithSourceMappingFrom(csCus.EndOfFileToken)
                );
            break;
        }
        return(csNode.CopyAnnotationsTo(converted).WithCsSourceMappingFrom(csNode));
    }
Example #10
0
    /// <remarks>
    /// If lots of special cases, move to wrapping the wrappedVisitor in another visitor, but I'd rather use a simple switch here initially.
    /// </remarks>
    private static T WithSourceMapping <T>(SyntaxNode vbNode, T converted) where T : CSharpSyntaxNode
    {
        converted = vbNode.CopyAnnotationsTo(converted);
        switch (vbNode)
        {
        case VBSyntax.CompilationUnitSyntax vbCus when converted is CSSyntax.CompilationUnitSyntax csCus:
            return((T)(object)csCus
                   .WithSourceStartLineAnnotation(vbNode.SyntaxTree.GetLineSpan(new TextSpan(0, 0)))
                   .WithEndOfFileToken(csCus.EndOfFileToken
                                       .WithConvertedLeadingTriviaFrom(vbCus.EndOfFileToken).WithSourceMappingFrom(vbCus.EndOfFileToken)
                                       ));

        case VBSyntax.XmlElementSyntax vbXes when converted is CSSyntax.ExpressionSyntax csXes:
            var lastToken = csXes.GetLastToken();
            return((T)(object)csXes
                   .ReplaceToken(lastToken, lastToken.WithConvertedLeadingTriviaFrom(vbXes.EndTag.GetFirstToken()))
                   .WithConvertedLeadingTriviaFrom(vbXes.GetFirstToken())
                   .WithConvertedTrailingTriviaFrom(vbXes.GetLastToken()));
        }
        return(converted.WithVbSourceMappingFrom(vbNode));
    }
Example #11
0
        public static TNode CopyAnnotations <TNode>(SyntaxNode from, TNode to) where TNode : SyntaxNode
        {
            // Because we are removing a node that may have annotations (i.e. formatting), we need
            // to copy those annotations to the new node. However, we can only copy all annotations
            // which will mean that the new node will include a ParenthesesSimplification annotation,
            // even if didn't have one before. That results in potentially removing parentheses that
            // weren't annotated by the user. To address this, we add *another* annotation to indicate
            // that the new node shouldn't be simplified. This is to work around the
            // fact that there is no way to remove an annotation from a node in the current API. If
            // that gets added, we can clean this up.

            var dontSimplifyResult = !to.HasAnnotation(Simplifier.Annotation);

            to = from.CopyAnnotationsTo(to);

            if (dontSimplifyResult)
            {
                to = to.WithAdditionalAnnotations(DontSimplifyAnnotation);
            }

            return(to);
        }
 private static T CopyAnnotationsFrom <T>(T node, SyntaxNode other) where T : SyntaxNode
 {
     return(other.CopyAnnotationsTo(node));
 }