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


C# SyntaxNode.InsertNodesAfter方法代码示例

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


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

示例1: MakeAutoPropertyAsync

 public async static Task<Solution> MakeAutoPropertyAsync(Document document, SyntaxNode root, PropertyDeclarationSyntax property, CancellationToken cancellationToken)
 {
     var semanticModel = await document.GetSemanticModelAsync(cancellationToken);
     var getterReturn = (ReturnStatementSyntax)property.AccessorList.Accessors.First(a => a.Keyword.ValueText == "get").Body.Statements.First();
     var returnIdentifier = (IdentifierNameSyntax)(getterReturn.Expression is MemberAccessExpressionSyntax ? ((MemberAccessExpressionSyntax)getterReturn.Expression).Name : getterReturn.Expression);
     var returnIdentifierSymbol = semanticModel.GetSymbolInfo(returnIdentifier).Symbol;
     var variableDeclarator = (VariableDeclaratorSyntax)returnIdentifierSymbol.DeclaringSyntaxReferences.First().GetSyntax();
     var fieldDeclaration = variableDeclarator.FirstAncestorOfType<FieldDeclarationSyntax>();
     root = root.TrackNodes(returnIdentifier, fieldDeclaration, property);
     document = document.WithSyntaxRoot(root);
     root = await document.GetSyntaxRootAsync(cancellationToken);
     semanticModel = await document.GetSemanticModelAsync(cancellationToken);
     returnIdentifier = root.GetCurrentNode(returnIdentifier);
     returnIdentifierSymbol = semanticModel.GetSymbolInfo(returnIdentifier).Symbol;
     var newProperty = GetSimpleProperty(property, variableDeclarator)
         .WithTriviaFrom(property)
         .WithAdditionalAnnotations(Formatter.Annotation);
     var newSolution = await Renamer.RenameSymbolAsync(document.Project.Solution, returnIdentifierSymbol, property.Identifier.ValueText, document.Project.Solution.Workspace.Options, cancellationToken);
     document = newSolution.GetDocument(document.Id);
     root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
     root = root.InsertNodesAfter(root.GetCurrentNode(property), new[] { newProperty });
     var multipleVariableDeclaration = fieldDeclaration.Declaration.Variables.Count > 1;
     if (multipleVariableDeclaration)
     {
         var newfieldDeclaration = fieldDeclaration.WithDeclaration(fieldDeclaration.Declaration.RemoveNode(variableDeclarator, SyntaxRemoveOptions.KeepNoTrivia));
         root = root.RemoveNode(root.GetCurrentNode<SyntaxNode>(property), SyntaxRemoveOptions.KeepNoTrivia);
         root = root.ReplaceNode(root.GetCurrentNode(fieldDeclaration), newfieldDeclaration);
     }
     else
     {
         root = root.RemoveNodes(root.GetCurrentNodes<SyntaxNode>(new SyntaxNode[] { fieldDeclaration, property }), SyntaxRemoveOptions.KeepNoTrivia);
     }
     document = document.WithSyntaxRoot(root);
     return document.Project.Solution;
 }
开发者ID:haroldhues,项目名称:code-cracker,代码行数:35,代码来源:SwitchToAutoPropCodeFixProvider.cs

示例2: CreateRootAddingDisposeToEndOfMethod

 private static SyntaxNode CreateRootAddingDisposeToEndOfMethod(SyntaxNode root, ExpressionStatementSyntax statement, ILocalSymbol identitySymbol)
 {
     var method = statement.FirstAncestorOrSelf<MethodDeclarationSyntax>();
     var newDispose = ImplementsDisposableExplicitly(identitySymbol.Type)
         ? SyntaxFactory.ExpressionStatement(SyntaxFactory.InvocationExpression(
             SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
             SyntaxFactory.ParenthesizedExpression(SyntaxFactory.CastExpression(SyntaxFactory.ParseName("System.IDisposable").WithAdditionalAnnotations(Simplifier.Annotation), SyntaxFactory.IdentifierName(identitySymbol.Name))),
             SyntaxFactory.IdentifierName("Dispose"))))
         : SyntaxFactory.ParseStatement($"{identitySymbol.Name}.Dispose();");
     newDispose = newDispose.WithAdditionalAnnotations(Formatter.Annotation);
     var last = method.Body.Statements.Last();
     var newRoot = root.InsertNodesAfter(method.Body.Statements.Last(), new[] { newDispose });
     return newRoot;
 }
开发者ID:JeanLLopes,项目名称:code-cracker,代码行数:14,代码来源:DisposableVariableNotDisposedCodeFixProvider.cs

示例3: MakePropertiesAsync

		private Task<Document> MakePropertiesAsync(Document document, SyntaxNode root, TextSpan span, CancellationToken cancellationToken)
		{
			var field = root.FindNode(span) as FieldDeclarationSyntax;

			var semicolon = SyntaxFactory.Token(SyntaxKind.SemicolonToken);

			// { get; }
			var getAccessor = SyntaxFactory.AccessorList(SyntaxFactory.List(new[] {
				SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(semicolon),
			}));
			// { get; set; }
			var getSetAccessor = getAccessor.AddAccessors(SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(semicolon));

			var propertySyntaxes = field.Declaration.Variables.Select(v =>
			{
				var isReadOnly = field.Modifiers.Any(SyntaxKind.ReadOnlyKeyword);
				var accessors = isReadOnly ? getAccessor : getSetAccessor;

				var initializer = v.ChildNodes().OfType<EqualsValueClauseSyntax>().FirstOrDefault();
				var property = SyntaxFactory.PropertyDeclaration(
					attributeLists: field.AttributeLists,
					modifiers: isReadOnly ? field.Modifiers.Remove(field.Modifiers.Where(x => x.IsKind(SyntaxKind.ReadOnlyKeyword)).First()) : field.Modifiers,
					type: field.Declaration.Type,
					explicitInterfaceSpecifier: null,
					identifier: v.Identifier,
					accessorList: accessors,
					expressionBody: null,
					initializer: initializer
				);

				// int Foo { get; set; }\n  or  int Bar { get; set; } = 1;\n
				return (initializer != null ? property.WithSemicolonToken(semicolon) : property)
					.WithTrailingTrivia(SyntaxFactory.SyntaxTrivia(SyntaxKind.EndOfLineTrivia, Environment.NewLine))
					.WithAdditionalAnnotations(Formatter.Annotation);
			});

			var inserted = root.InsertNodesAfter(field, propertySyntaxes);
			var removingNode = inserted.FindNode(span);
			var newRoot = inserted.RemoveNode(removingNode, SyntaxRemoveOptions.KeepNoTrivia);
			return Task.FromResult(document.WithSyntaxRoot(newRoot));
		}
开发者ID:occar421,项目名称:MyAnalyzerSamples,代码行数:41,代码来源:PublicFieldCodeFixProvider.cs

示例4: Handle

        static CodeAction Handle(Document document, Microsoft.CodeAnalysis.Text.TextSpan span, SyntaxNode root, SyntaxNode node, ExpressionSyntax iterateOver, bool replaceNode)
        {
            return CodeActionFactory.Create(
                span,
                DiagnosticSeverity.Info,
                "Iterate via 'foreach'",
                ct =>
                {
                    ForEachStatementSyntax foreachStmt = BuildForeach(iterateOver);

                    SyntaxNode newRoot;
                    var ancestor = node.GetAncestor<StatementSyntax>();
                    if (replaceNode)
                    {
                        newRoot = root.ReplaceNode(ancestor, new[] { foreachStmt.WithTrailingTrivia(ancestor.GetTrailingTrivia()) });
                    }
                    else
                    {
                        newRoot = root.InsertNodesAfter(ancestor, new[] { foreachStmt.WithTrailingTrivia(ancestor.GetTrailingTrivia()) });
                    }
                    return Task.FromResult(document.WithSyntaxRoot(newRoot));
                }
            );
        }
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:24,代码来源:IterateViaForeachAction.cs


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