public static async Task ComputeRefactoringAsync(RefactoringContext context, EnumDeclarationSyntax enumDeclaration)
        {
            EnumMemberDeclarationSyntax[] selectedMembers = GetSelectedMembers(enumDeclaration, context.Span).ToArray();

            if (selectedMembers.Length > 1)
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                List <IFieldSymbol> fieldSymbols = GetFieldSymbols(selectedMembers, semanticModel, context.CancellationToken);

                if (fieldSymbols.Count > 1)
                {
                    var enumSymbol = semanticModel.GetDeclaredSymbol(enumDeclaration, context.CancellationToken) as INamedTypeSymbol;

                    if (enumSymbol != null)
                    {
                        object combinedValue = GetCombinedValue(fieldSymbols, enumSymbol);

                        if (combinedValue != null &&
                            !EnumHelper.IsValueDefined(enumSymbol, combinedValue))
                        {
                            string name = GetCombinedName(fieldSymbols);

                            EnumMemberDeclarationSyntax newEnumMember = GenerateEnumHelper.CreateEnumMember(enumSymbol, name, combinedValue);

                            int insertIndex = enumDeclaration.Members.IndexOf(selectedMembers.Last()) + 1;

                            context.RegisterRefactoring(
                                $"Generate enum member '{name}'",
                                cancellationToken => RefactorAsync(context.Document, enumDeclaration, newEnumMember, insertIndex, cancellationToken));
                        }
                    }
                }
            }
        }
        private static async Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            INamedTypeSymbol enumSymbol,
            object value,
            CancellationToken cancellationToken)
        {
            EnumMemberDeclarationSyntax newEnumMember = GenerateEnumHelper.CreateEnumMember(enumSymbol, "EnumMember", value);

            EnumDeclarationSyntax newNode = enumDeclaration.AddMembers(newEnumMember);

            return(await document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken).ConfigureAwait(false));
        }
Exemple #3
0
        private static Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            INamedTypeSymbol enumSymbol,
            object value,
            CancellationToken cancellationToken)
        {
            EnumMemberDeclarationSyntax newEnumMember = GenerateEnumHelper.CreateEnumMember(enumSymbol, Identifier.DefaultEnumMemberName, value);

            EnumDeclarationSyntax newNode = enumDeclaration.AddMembers(newEnumMember);

            return(document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken));
        }