当前位置: 首页>>代码示例>>C#>>正文


C# SyntaxNode.GetLeadingTrivia方法代码示例

本文整理汇总了C#中SyntaxNode.GetLeadingTrivia方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.GetLeadingTrivia方法的具体用法?C# SyntaxNode.GetLeadingTrivia怎么用?C# SyntaxNode.GetLeadingTrivia使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SyntaxNode的用法示例。


在下文中一共展示了SyntaxNode.GetLeadingTrivia方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: HandleDeclarationAsync

        private async Task<Document> HandleDeclarationAsync(Document document, SyntaxNode root, SyntaxNode node, CancellationToken cancellationToken)
        {
            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
            var declarationContext = node.Parent;

            TypeSyntax typeSyntax = null;
            if (declarationContext is VariableDeclarationSyntax)
            {
                typeSyntax = ((VariableDeclarationSyntax)declarationContext).Type;
            }
            else if (declarationContext is ForEachStatementSyntax)
            {
                typeSyntax = ((ForEachStatementSyntax)declarationContext).Type;
            }
            else
            {
                Contract.Fail($"unhandled kind {declarationContext.Kind().ToString()}");
            }

            var typeSymbol = semanticModel.GetTypeInfo(typeSyntax).ConvertedType;

            var typeName = typeSymbol.GenerateTypeSyntax()
                                     .WithLeadingTrivia(node.GetLeadingTrivia())
                                     .WithTrailingTrivia(node.GetTrailingTrivia());

            Debug.Assert(!typeName.ContainsDiagnostics, "Explicit type replacement likely introduced an error in code");

            var newRoot = root.ReplaceNode(node, typeName);
            return document.WithSyntaxRoot(newRoot);
        }
开发者ID:CAPCHIK,项目名称:roslyn,代码行数:30,代码来源:UseExplicitTypeCodeFixProvider.cs

示例2: ReplaceTypeWithVarAsync

        private static Task<Document> ReplaceTypeWithVarAsync(Document document, SyntaxNode root, SyntaxNode node)
        {
            var implicitType = SyntaxFactory.IdentifierName("var")
                                            .WithLeadingTrivia(node.GetLeadingTrivia())
                                            .WithTrailingTrivia(node.GetTrailingTrivia());

            var newRoot = root.ReplaceNode(node, implicitType);
            return Task.FromResult(document.WithSyntaxRoot(newRoot));
        }
开发者ID:CAPCHIK,项目名称:roslyn,代码行数:9,代码来源:UseImplicitTypeCodeFixProvider.cs

示例3: GetUpdatedDocumentAsync

        internal override Task<Document> GetUpdatedDocumentAsync(
            Document document, SemanticModel model, SyntaxNode root,
            SyntaxNode nodeToFix, string diagnosticId, CancellationToken cancellationToken)
        {
            ArrayTypeSyntax arrayType = GetArrayType(nodeToFix);
            if (arrayType != null)
            {
                TypeSyntax elementType = arrayType.ElementType.WithoutLeadingTrivia().WithoutTrailingTrivia();
                if (arrayType.RankSpecifiers.Count > 1)
                {
                    elementType = SyntaxFactory.ArrayType(elementType, SyntaxFactory.List(arrayType.RankSpecifiers.Skip(1)));
                }

                InvocationExpressionSyntax syntax = InvokeStaticGenericParameterlessMethod(
                    model.Compilation.GetTypeByMetadataName("System.Array"),
                    EmptyArrayDiagnosticAnalyzer.ArrayEmptyMethodName,
                    elementType.WithoutLeadingTrivia().WithoutTrailingTrivia());

                if (nodeToFix.HasLeadingTrivia)
                {
                    syntax = syntax.WithLeadingTrivia(nodeToFix.GetLeadingTrivia());
                }

                if (nodeToFix.HasTrailingTrivia)
                {
                    syntax = syntax.WithTrailingTrivia(nodeToFix.GetTrailingTrivia());
                }

                if (syntax != null)
                {
                    root = root.ReplaceNode(nodeToFix, syntax);
                    document = document.WithSyntaxRoot(root);
                }
            }

            return Task.FromResult(document);
        }
开发者ID:GloryChou,项目名称:roslyn,代码行数:37,代码来源:CSharpEmptyArrayCodeFixProvider.cs

示例4: GetLeadingWhitespaceLength

        private static int GetLeadingWhitespaceLength(SyntaxNode node)
        {
            if (!(node is IXmlElement))
            {
                return 0;
            }

            var leadingTrivia = node.GetLeadingTrivia();
            if (leadingTrivia == null)
            {
                return 0;
            }

            int totalLength = 0;
            foreach (var child in leadingTrivia.ChildNodes)
            {
                if (child.Kind == SyntaxKind.WhitespaceTrivia)
                {
                    totalLength += child.FullWidth;
                }
            }

            return totalLength;
        }
开发者ID:akrisiun,项目名称:Microsoft.Language.Xml,代码行数:24,代码来源:SmartIndent.cs

示例5: CollectCommentRegions

        public static void CollectCommentRegions(SyntaxNode node, List<OutliningSpan> spans)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            var triviaList = node.GetLeadingTrivia();

            CollectCommentRegions(triviaList, spans);
        }
开发者ID:GloryChou,项目名称:roslyn,代码行数:11,代码来源:CSharpOutliningHelpers.cs

示例6: CollectCommentBlockSpans

        public static void CollectCommentBlockSpans(
            SyntaxNode node, ArrayBuilder<BlockSpan> spans)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            var triviaList = node.GetLeadingTrivia();

            CollectCommentBlockSpans(triviaList, spans);
        }
开发者ID:jkotas,项目名称:roslyn,代码行数:12,代码来源:CSharpStructureHelpers.cs

示例7: IsAutogenerated

 public static bool IsAutogenerated(SyntaxNode contents)
 {
     return contents.GetLeadingTrivia().Any(x => x.ToFullString().Contains("<auto-generated"));
 }
开发者ID:grokys,项目名称:StyleCopMagic,代码行数:4,代码来源:RuleRewriter.cs

示例8: Visit

            public override SyntaxNode Visit(SyntaxNode node)
            {
                _cancellationToken.ThrowIfCancellationRequested();

                if (node == null)
                {
                    return node;
                }

                // Process children first
                node = base.Visit(node);

                // Check the leading trivia for doc comments.
                if (node.GetLeadingTrivia().Any(SyntaxKind.SingleLineDocumentationCommentTrivia))
                {
                    var newLeadingTrivia = new List<SyntaxTrivia>();

                    foreach (var trivia in node.GetLeadingTrivia())
                    {
                        if (trivia.Kind() == SyntaxKind.SingleLineDocumentationCommentTrivia)
                        {
                            newLeadingTrivia.Add(SyntaxFactory.Comment("//"));
                            newLeadingTrivia.Add(SyntaxFactory.ElasticCarriageReturnLineFeed);

                            var structuredTrivia = (DocumentationCommentTriviaSyntax)trivia.GetStructure();
                            newLeadingTrivia.AddRange(ConvertDocCommentToRegularComment(structuredTrivia));
                        }
                        else
                        {
                            newLeadingTrivia.Add(trivia);
                        }
                    }

                    node = node.WithLeadingTrivia(newLeadingTrivia);
                }

                return node;
            }
开发者ID:noahfalk,项目名称:roslyn,代码行数:38,代码来源:CSharpMetadataAsSourceService.cs

示例9: RemoveComments

        private static SyntaxNode RemoveComments(SyntaxNode ele)
        {
            if (ele.HasLeadingTrivia)
            {
                var oldCommentsTrivias = from trivia in ele.GetLeadingTrivia().Where(
                    x => (x.Kind == SyntaxKind.MultiLineCommentTrivia || x.Kind == SyntaxKind.SingleLineCommentTrivia))
                                         select trivia;
                foreach (var comment in oldCommentsTrivias)
                {
                    ele = ele.ReplaceTrivia(comment, SyntaxTriviaList.Empty);                    

                }
            }
            if (ele.DescendantTrivia() != null && ele.DescendantTrivia().Count() > 0)
            {
                var containingComments = from trivia in ele.DescendantTrivia().Where(
                    x => (x.Kind == SyntaxKind.MultiLineCommentTrivia || x.Kind == SyntaxKind.SingleLineCommentTrivia))
                                         select trivia;
                foreach (var comment in containingComments)
                {
                    ele = ele.ReplaceTrivia(comment, SyntaxTriviaList.Empty);
                }
            }

            return ele;
        }
开发者ID:ankushraizada,项目名称:Dynamo,代码行数:26,代码来源:DSTestCaseUtil.cs


注:本文中的SyntaxNode.GetLeadingTrivia方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。