Esempio n. 1
0
        public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            var(document, classDeclarationSyntax) = await context.FindSyntaxForCurrentSpan <ClassDeclarationSyntax>();

            if (document == null || classDeclarationSyntax == null)
            {
                return;
            }

            if (!ClassDeclarationSyntaxAnalysis.IsRecordLike(classDeclarationSyntax))
            {
                return;
            }

            var properties = ClassDeclarationSyntaxAnalysis.GetPropertyDeclarations(classDeclarationSyntax).ToList();

            if (properties.Count == 0 ||
                properties.Any(PropertyDeclarationSyntaxExtensions.IsStatic))
            {
                return;
            }

            context.RegisterRefactoring(
                new DelegateCodeAction(
                    "Generate to string from parameters",
                    (c) => GenerateToString(document, classDeclarationSyntax, properties, c)));

            return;
        }
Esempio n. 2
0
        public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            var(document, classDeclarationSyntax) = await context.FindSyntaxForCurrentSpan <ClassDeclarationSyntax>();

            if (document == null || classDeclarationSyntax == null)
            {
                return;
            }

            if (!ClassDeclarationSyntaxAnalysis.IsRecordLike(classDeclarationSyntax))
            {
                return;
            }

            var(atMostOneConstructor, nonTrivialConstructorCandidate) =
                ClassDeclarationSyntaxAnalysis.HasAtMostOneNoneTrivialConstructor(classDeclarationSyntax);

            if (!atMostOneConstructor || nonTrivialConstructorCandidate == null)
            {
                return;
            }

            var properties = ClassDeclarationSyntaxAnalysis.GetPropertyDeclarations(classDeclarationSyntax).ToList();

            if (properties.Count == 0 ||
                properties.Any(PropertyDeclarationSyntaxExtensions.IsStatic))
            {
                return;
            }

            var cancellationToken = context.CancellationToken;
            var semanticModel     = await document.GetSemanticModelAsync(cancellationToken);

            var constructorSymbol = semanticModel.GetDeclaredSymbol(nonTrivialConstructorCandidate);
            var propertySymbols   = properties.Select(p => semanticModel.GetDeclaredSymbol(p, cancellationToken)).ToArray();

            var analyser = new ConstructorPropertyRelationshipAnalyser(
                Array.Empty <IFieldSymbol>(),
                propertySymbols);

            var result = analyser.Analyze(semanticModel, nonTrivialConstructorCandidate);

            var(idxMappings, isExhaustive) = result.GetIndexMapping(propertySymbols, constructorSymbol);
            if (!isExhaustive)
            {
                return;
            }

            context.RegisterRefactoring(
                new DelegateCodeAction(
                    "Generate with methods for parameters",
                    (c) => GenerateWithMethods(document, classDeclarationSyntax, properties, idxMappings, c)));

            return;
        }