private async Task <Document> RefactorAsync( Document document, MemberDeclarationSyntax member, CancellationToken cancellationToken = default(CancellationToken)) { if (member.IsParentKind(SyntaxKind.CompilationUnit)) { SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); string name = SyntaxUtility.GetUniqueName("Namespace", semanticModel, member.Span.Start); NamespaceDeclarationSyntax namespaceDeclaration = NamespaceDeclaration( IdentifierName(Identifier(name).WithRenameAnnotation()), default(SyntaxList <ExternAliasDirectiveSyntax>), default(SyntaxList <UsingDirectiveSyntax>), SingletonList(member)); SyntaxNode newRoot = root.ReplaceNode(member, namespaceDeclaration.WithFormatterAnnotation()); return(document.WithSyntaxRoot(newRoot)); } return(document); }
private static async Task <Document> IntroduceFieldToLockOnAsync( Document document, LockStatementSyntax lockStatement, CancellationToken cancellationToken = default(CancellationToken)) { SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); MemberDeclarationSyntax containingMember = lockStatement.FirstAncestorOrSelf <MemberDeclarationSyntax>(); if (containingMember != null) { var containingDeclaration = (MemberDeclarationSyntax)containingMember .Ancestors() .FirstOrDefault(f => f.IsKind( SyntaxKind.ClassDeclaration, SyntaxKind.InterfaceDeclaration, SyntaxKind.StructDeclaration)); if (containingDeclaration != null) { SyntaxList <MemberDeclarationSyntax> members = containingDeclaration.GetMembers(); int index = members.IndexOf(containingMember); string name = LockObjectName; if (document.SupportsSemanticModel) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); name = SyntaxUtility.GetUniqueName(name, semanticModel, lockStatement.Expression.Span.Start); } LockStatementSyntax newLockStatement = lockStatement .WithExpression(IdentifierName(name)); MemberDeclarationSyntax newContainingMember = containingMember .ReplaceNode(lockStatement, newLockStatement); bool isStatic = containingMember.GetModifiers().Contains(SyntaxKind.StaticKeyword); FieldDeclarationSyntax field = CreateField(name, isStatic).WithFormatterAnnotation(); SyntaxList <MemberDeclarationSyntax> newMembers = members .Replace(members[index], newContainingMember) .Insert(FindField(members, index) + 1, field); MemberDeclarationSyntax newNode = containingDeclaration.SetMembers(newMembers); SyntaxNode newRoot = root.ReplaceNode(containingDeclaration, newNode); return(document.WithSyntaxRoot(newRoot)); } } return(document); }