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


C# SyntaxNode.InsertNodesBefore方法代码示例

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


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

示例1: Visit

        public override SyntaxNode Visit(SyntaxNode node)
        {
            // Check each node until we find the property's parent. When  we've found it, let's add a private field to it
            if (_propertyParent.IsEquivalentTo(node))
            {
                var variableDeclarator = SyntaxFactory.VariableDeclarator(
                                                    SyntaxFactory.Identifier(this.PrivateFieldName));

                if (_crunchedProperty.IsExpressionProperty())
                {
                    variableDeclarator = variableDeclarator.WithInitializer(
                        SyntaxFactory.EqualsValueClause(
                            SyntaxFactory.Token(SyntaxKind.EqualsToken),
                            _crunchedProperty.ExpressionBody.Expression));
                }

                // Create the private "backing field" of the property
                var privateField = SyntaxFactory.FieldDeclaration(
                                        SyntaxFactory.VariableDeclaration(_crunchedProperty.Type, // Property Type
                                            SyntaxFactory.SeparatedList(
                                                new[] { variableDeclarator }))) // Field Name
                    .AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword)); // Modifier (always private)

                // Insert the backing field just ahead of the original property
                var newParent = node.InsertNodesBefore(_crunchedProperty, new[] { privateField });

                return base.Visit(newParent); // Return the new parent which replaces the original.
            }
            return base.Visit(node);
        }
开发者ID:NielsFilter,项目名称:PropertyExplosion,代码行数:30,代码来源:PropertyExploder.cs

示例2: MoveMember

        private static SyntaxNode MoveMember(SyntaxNode syntaxRoot, MemberDeclarationSyntax member, MemberDeclarationSyntax targetMember, IndentationOptions indentationOptions)
        {
            var firstToken = syntaxRoot.GetFirstToken();
            var fileHeader = GetFileHeader(firstToken.LeadingTrivia);
            syntaxRoot = syntaxRoot.TrackNodes(member, targetMember, firstToken.Parent);
            var memberToMove = syntaxRoot.GetCurrentNode(member);
            var targetMemberTracked = syntaxRoot.GetCurrentNode(targetMember);
            if (!memberToMove.HasLeadingTrivia)
            {
                var targetIndentationLevel = IndentationHelper.GetIndentationSteps(indentationOptions, targetMember);
                var indentationString = IndentationHelper.GenerateIndentationString(indentationOptions, targetIndentationLevel);
                memberToMove = memberToMove.WithLeadingTrivia(SyntaxFactory.Whitespace(indentationString));
            }

            if (!HasLeadingBlankLines(targetMember)
                && HasLeadingBlankLines(member))
            {
                memberToMove = memberToMove.WithTrailingTrivia(memberToMove.GetTrailingTrivia().Add(SyntaxFactory.CarriageReturnLineFeed));
                memberToMove = memberToMove.WithLeadingTrivia(GetLeadingTriviaWithoutLeadingBlankLines(memberToMove));
            }

            syntaxRoot = syntaxRoot.InsertNodesBefore(targetMemberTracked, new[] { memberToMove });
            var fieldToMoveTracked = syntaxRoot.GetCurrentNodes(member).Last();
            syntaxRoot = syntaxRoot.RemoveNode(fieldToMoveTracked, SyntaxRemoveOptions.KeepNoTrivia);
            if (fileHeader.Any())
            {
                var oldFirstToken = syntaxRoot.GetCurrentNode(firstToken.Parent).ChildTokens().First();
                syntaxRoot = syntaxRoot.ReplaceToken(oldFirstToken, oldFirstToken.WithLeadingTrivia(StripFileHeader(oldFirstToken.LeadingTrivia)));
                var newFirstToken = syntaxRoot.GetFirstToken();
                syntaxRoot = syntaxRoot.ReplaceToken(newFirstToken, newFirstToken.WithLeadingTrivia(fileHeader.AddRange(newFirstToken.LeadingTrivia)));
            }

            return syntaxRoot;
        }
开发者ID:Noryoko,项目名称:StyleCopAnalyzers,代码行数:34,代码来源:ElementOrderCodeFixProvider.cs

示例3: AddBefore

 internal static SyntaxNode AddBefore(SyntaxNode root, SyntaxNode loationToAddBefore, SyntaxNode nodeToAdd)
 {
     return root.InsertNodesBefore(
         root.GetCurrentNode(loationToAddBefore)
         , new List<SyntaxNode> { nodeToAdd });
 }
开发者ID:ceddlyburge,项目名称:RefactoringEssentials,代码行数:6,代码来源:Manipulations.cs

示例4: ProcessObjectInitializer


//.........这里部分代码省略.........
                }
                else
                {
                    newObjectCreation =
                        objectCreation.WithInitializer(null).WithArgumentList(objectCreation.ArgumentList);
                }

                var local = SyntaxFactory.LocalDeclarationStatement(
                                SyntaxFactory.VariableDeclaration(
                                    target.Type,
                                    SyntaxFactory.SeparatedList(new[]
                            {
                                SyntaxFactory.VariableDeclarator(
                                    SyntaxFactory.Identifier(tempName),
                                    null,
                                    SyntaxFactory.EqualsValueClause(newObjectCreation))
                            })
                                )
                            ).NormalizeWhitespace();

                statements.Add(local);
                statements.AddRange(assignments.Cast<StatementSyntax>());

                var listS = SyntaxFactory.List(statements);

                var newList2 = new List<ObjectCreationExpressionSyntax>();
                var union2 = originalObjectExpressionSyntax.Union(new SyntaxNode[] { target });
                foreach (var statementSyntax in union2)
                {
                    try
                    {
                        newList2.Add((ObjectCreationExpressionSyntax)newRoot.GetCurrentNode(statementSyntax));
                    }
                    catch (Exception ex)
                    {
                    }
                }

                if (!newList2.Contains(target))
                {
                    //                 
                    originalObjectExpressionSyntax =
                        originalObjectExpressionSyntax.Union(new SyntaxNode[] { target }).ToArray();
                }

                originalObjectExpressionSyntax = originalObjectExpressionSyntax.Where(l => l != null).ToArray();
                newRoot = newRoot.TrackNodes(originalObjectExpressionSyntax);
                try
                {
                    var currentNode = newRoot.GetCurrentNode(target);

                    var lastStatement = currentNode.Ancestors().OfType<StatementSyntax>().First();

                    newRoot = newRoot.InsertNodesBefore(lastStatement, listS);

                    //  var currentNodes = newRoot.GetCurrentNodes<SyntaxNode>(originalObjectExpressionSyntax);
                    newRoot = newRoot.TrackNodes(originalObjectExpressionSyntax);

                    currentNode = newRoot.GetCurrentNode(target);
                    var newName = SyntaxFactory.ParseExpression(tempName);

                    var nDict = new Dictionary<SyntaxNode, SyntaxNode>();
                    foreach (var replacedNode in replacedNodes)
                        nDict[replacedNode.Key] = replacedNode.Value;

                    nDict[currentNode] = newName;
                    replacedNodes = nDict;
                    // newRoot = newRoot;
                    newRoot = newRoot.ReplaceNode(currentNode, newName);

                    var newList = new List<ObjectCreationExpressionSyntax>();
                    var union = originalObjectExpressionSyntax.Union(nDict.Values); //.Union(new SyntaxNode[]{newName});
                    foreach (var statementSyntax in union)
                    {
                        try
                        {
                            //                          if(statementSyntax==newName)
                            //                          {
                            //                              newList.Add((ObjectCreationExpressionSyntax) (newName));
                            //                              continue;
                            //                          }

                            var syntaxNode = newRoot.GetCurrentNode(statementSyntax);
                            newList.Add((ObjectCreationExpressionSyntax)syntaxNode);
                        }
                        catch (Exception ex)
                        {
                        }
                    }

                    originalObjectExpressionSyntax = newList.ToArray();
                    newRoot = newRoot.TrackNodes(originalObjectExpressionSyntax);
                }
                catch (Exception ex)
                {
                }
            }

            return lastTemporaryIndex;
        }
开发者ID:mortezabarzkar,项目名称:SharpNative,代码行数:101,代码来源:Program.cs


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