internal static FieldDeclarationSyntax AddField(
            this DocumentEditor editor,
            TypeDeclarationSyntax containingType,
            string name,
            Accessibility accessibility,
            DeclarationModifiers modifiers,
            TypeSyntax type,
            CancellationToken cancellationToken)
        {
            var declaredSymbol = (INamedTypeSymbol)editor.SemanticModel.GetDeclaredSymbolSafe(containingType, cancellationToken);

            while (declaredSymbol.MemberNames.Contains(name))
            {
                name += "_";
            }

            var newField = (FieldDeclarationSyntax)editor.Generator.FieldDeclaration(
                name,
                accessibility: accessibility,
                modifiers: modifiers,
                type: type);

            _ = editor.AddField(containingType, newField);
            return(newField);
        }
Ejemplo n.º 2
0
        // ReSharper disable once UnusedMethodReturnValue.Local
        private static DocumentEditor AddDefaultFieldWithDocs(DocumentEditor editor, ClassDeclarationSyntax containingType)
        {
            var code = StringBuilderPool.Borrow()
                       .AppendLine(DefaultDocs)
                       .AppendLine(string.Format(DefaultFieldFormat, Modifier(containingType), containingType.Identifier.ValueText))
                       .Return();

            return(editor.AddField(containingType, ParseField(code))
                   .Seal(containingType));
        }
        internal static async Task <ExpressionSyntax> AddFieldAsync(this DocumentEditor editor, TypeDeclarationSyntax containingType, string name, Accessibility accessibility, DeclarationModifiers modifiers, TypeSyntax type, CancellationToken cancellationToken)
        {
            if (!name.StartsWith("_", StringComparison.Ordinal) &&
                await editor.OriginalDocument.UnderscoreFieldsAsync(cancellationToken).ConfigureAwait(false) == CodeStyleResult.Yes)
            {
                name = $"_{name}";
            }

            while (containingType.TryFindField(name, out _))
            {
                name += "_";
            }

            var field = (FieldDeclarationSyntax)editor.Generator.FieldDeclaration(
                name,
                accessibility: accessibility,
                modifiers: modifiers,
                type: type);

            _ = editor.AddField(containingType, field);
            var identifierNameSyntax = SyntaxFactory.IdentifierName(field.Declaration.Variables[0].Identifier);

            switch (await editor.OriginalDocument.QualifyFieldAccessAsync(cancellationToken).ConfigureAwait(false))
            {
            case CodeStyleResult.No:
            case CodeStyleResult.NotFound
                when name.StartsWith("_", StringComparison.Ordinal):
                return(identifierNameSyntax);

            case CodeStyleResult.Yes:
            case CodeStyleResult.Mixed:
            case CodeStyleResult.NotFound:
                return(SyntaxFactory.MemberAccessExpression(
                           SyntaxKind.SimpleMemberAccessExpression,
                           SyntaxFactory.ThisExpression(),
                           identifierNameSyntax));

            default:
                throw new InvalidOperationException("Unhandled code style.");
            }
        }
Ejemplo n.º 4
0
 // ReSharper disable once UnusedMethodReturnValue.Local
 private static DocumentEditor AddDefaultField(DocumentEditor editor, ClassDeclarationSyntax containingType)
 {
     return(editor.AddField(containingType, ParseField(string.Format(DefaultFieldFormat, Modifier(containingType), containingType.Identifier.ValueText)))
            .Seal(containingType));
 }