private static ParameterSyntax AsCallerMemberName(ParameterSyntax parameter, bool nullabilityAnnotationsEnabled)
        {
            parameter = parameter.AddAttributeLists(InpcFactory.CallerMemberNameAttributeList)
                        .WithDefault(SyntaxFactory.EqualsValueClause(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)));

            if (nullabilityAnnotationsEnabled)
            {
                parameter = parameter.WithType(InpcFactory.WithNullability(parameter.Type, nullable: true));
            }

            return(parameter);
        }
        protected override async Task <Document> GetChangedDocumentAsync(CancellationToken cancellationToken)
        {
            var editor = await DocumentEditor.CreateAsync(_document, cancellationToken);

            var syntaxGenerator = SyntaxGenerator.GetGenerator(_document);

            var memberName = $"{_methodDeclarationSyntax.Identifier.Text}_{_parameterSyntax.Identifier.Text}";

            var propertySyntax  = CreateGenPropertyDeclaration(syntaxGenerator, memberName, _genType, _parameterType, _parameterSyntax);
            var attributeSyntax = CreateMemberGenAttribute(syntaxGenerator, memberName);

            editor.InsertBefore(_methodDeclarationSyntax, propertySyntax);
            editor.ReplaceNode(_parameterSyntax, _parameterSyntax.AddAttributeLists(attributeSyntax));
            return(editor.GetChangedDocument());
        }
        public override TDeclarationNode AddAttributes <TDeclarationNode>(
            TDeclarationNode destination,
            IEnumerable <AttributeData> attributes,
            SyntaxToken?target,
            CodeGenerationOptions options,
            CancellationToken cancellationToken)
        {
            if (target.HasValue && !target.Value.IsValidAttributeTarget())
            {
                throw new ArgumentException("target");
            }

            var attributeSyntaxList = AttributeGenerator.GenerateAttributeLists(attributes.ToImmutableArray(), options, target).ToArray();

            return(destination switch
            {
                MemberDeclarationSyntax member => Cast <TDeclarationNode>(member.AddAttributeLists(attributeSyntaxList)),
                AccessorDeclarationSyntax accessor => Cast <TDeclarationNode>(accessor.AddAttributeLists(attributeSyntaxList)),
                CompilationUnitSyntax compilationUnit => Cast <TDeclarationNode>(compilationUnit.AddAttributeLists(attributeSyntaxList)),
                ParameterSyntax parameter => Cast <TDeclarationNode>(parameter.AddAttributeLists(attributeSyntaxList)),
                TypeParameterSyntax typeParameter => Cast <TDeclarationNode>(typeParameter.AddAttributeLists(attributeSyntaxList)),
                _ => destination,
            });
Esempio n. 4
0
 private static ParameterSyntax AsCallerMemberName(ParameterSyntax parameter)
 {
     return(parameter.AddAttributeLists(CallerMemberName)
            .WithDefault(SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression("null"))));
 }
 public static ParameterSyntax AddAttributes(this ParameterSyntax parameter, params string[] attributeNames)
 {
     return(parameter.AddAttributeLists(MakeAttributes(attributeNames)));
 }