public static async Task ComputeRefactoringAsync(RefactoringContext context, EnumDeclarationSyntax enumDeclaration)
        {
            if (!SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> .TryCreate(enumDeclaration.Members, context.Span, out SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selection))
            {
                return;
            }

            if (selection.Count > 1)
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.SortMemberDeclarations))
                {
                    await SortEnumMemberDeclarationsRefactoring.ComputeRefactoringAsync(context, enumDeclaration, selection).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.GenerateCombinedEnumMember))
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    GenerateCombinedEnumMemberRefactoring.ComputeRefactoring(context, enumDeclaration, selection, semanticModel);
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveEnumMemberValue))
            {
                RemoveEnumMemberValueRefactoring.ComputeRefactoring(context, enumDeclaration, selection);
            }
        }
        private static Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selectedMembers,
            string name,
            CancellationToken cancellationToken)
        {
            ExpressionSyntax expression = IdentifierName(selectedMembers.Last().Identifier.WithoutTrivia());

            for (int i = selectedMembers.LastIndex - 1; i >= selectedMembers.FirstIndex; i--)
            {
                expression = BitwiseOrExpression(
                    IdentifierName(selectedMembers.UnderlyingList[i].Identifier.WithoutTrivia()),
                    expression);
            }

            EnumMemberDeclarationSyntax newEnumMember = EnumMemberDeclaration(
                Identifier(name).WithRenameAnnotation(),
                EqualsValueClause(expression));

            EnumDeclarationSyntax newEnumDeclaration = enumDeclaration
                                                       .WithMembers(enumDeclaration.Members.Insert(selectedMembers.LastIndex + 1, newEnumMember));

            return(document.ReplaceNodeAsync(enumDeclaration, newEnumDeclaration, cancellationToken));
        }
Exemple #3
0
        public static async Task ComputeRefactoringAsync(
            RefactoringContext context,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selection)
        {
            ImmutableArray <EnumMemberDeclarationSyntax> selectedMembers = selection.SelectedItems;

            if (!EnumMemberDeclarationNameComparer.IsSorted(selectedMembers))
            {
                context.RegisterRefactoring(
                    "Sort enum members by name",
                    cancellationToken => SortByNameAsync(context.Document, enumDeclaration, selectedMembers, cancellationToken));
            }

            if (selectedMembers.Any(f => f.EqualsValue?.Value != null))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                if (!EnumMemberDeclarationValueComparer.IsSorted(selectedMembers, semanticModel, context.CancellationToken))
                {
                    context.RegisterRefactoring(
                        "Sort enum members by value",
                        cancellationToken => SortByValueAsync(context.Document, enumDeclaration, selectedMembers, cancellationToken));
                }
            }
        }
Exemple #4
0
        public static void ComputeRefactoring(
            RefactoringContext context,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selection,
            SemanticModel semanticModel)
        {
            INamedTypeSymbol enumSymbol = semanticModel.GetDeclaredSymbol(enumDeclaration, context.CancellationToken);

            if (enumSymbol?.IsEnumWithFlagsAttribute(semanticModel) == true)
            {
                object[] constantValues = selection
                                          .Select(f => semanticModel.GetDeclaredSymbol(f, context.CancellationToken))
                                          .Where(f => f.HasConstantValue)
                                          .Select(f => f.ConstantValue)
                                          .ToArray();

                object combinedValue = GetCombinedValue(constantValues, enumSymbol);

                if (combinedValue != null &&
                    !IsValueDefined(enumSymbol, combinedValue))
                {
                    string name = NameGenerator.Default.EnsureUniqueEnumMemberName(
                        string.Concat(selection.Select(f => f.Identifier.ValueText)),
                        enumSymbol);

                    EnumMemberDeclarationSyntax newEnumMember = CreateEnumMember(name, selection.SelectedItems);

                    int insertIndex = selection.EndIndex + 1;

                    context.RegisterRefactoring(
                        $"Generate enum member '{name}'",
                        cancellationToken => RefactorAsync(context.Document, enumDeclaration, newEnumMember, insertIndex, cancellationToken));
                }
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ArgumentListSyntax argumentList)
        {
            if (!context.IsAnyRefactoringEnabled(
                    RefactoringDescriptors.AddArgumentName,
                    RefactoringDescriptors.RemoveArgumentName))
            {
                return;
            }

            if (!SeparatedSyntaxListSelection <ArgumentSyntax> .TryCreate(argumentList.Arguments, context.Span, out SeparatedSyntaxListSelection <ArgumentSyntax> selection))
            {
                return;
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.AddArgumentName))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                AddArgumentNameRefactoring.ComputeRefactoring(context, argumentList, selection, semanticModel);
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.RemoveArgumentName))
            {
                RemoveArgumentNameRefactoring.ComputeRefactoring(context, argumentList, selection);
            }
        }
Exemple #6
0
        public static void ComputeRefactoring(
            RefactoringContext context,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selection)
        {
            int count = 0;

            for (int i = selection.StartIndex; i <= selection.EndIndex; i++)
            {
                if (selection.Items[i].EqualsValue?.Value != null)
                {
                    count++;

                    if (count == 2)
                    {
                        break;
                    }
                }
            }

            if (count == 0)
            {
                return;
            }

            context.RegisterRefactoring(
                (count == 1) ? "Remove enum value" : "Remove enum values",
                cancellationToken => RefactorAsync(context.Document, enumDeclaration, selection, cancellationToken));
        }
Exemple #7
0
        private static bool CanRefactor(
            SeparatedSyntaxListSelection <ArgumentSyntax> selection,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            for (int i = selection.StartIndex; i <= selection.EndIndex; i++)
            {
                ArgumentSyntax argument = selection.Items[i];

                NameColonSyntax nameColon = argument.NameColon;

                if (nameColon == null ||
                    nameColon.IsMissing)
                {
                    IParameterSymbol parameterSymbol = semanticModel.DetermineParameter(
                        argument,
                        allowParams: false,
                        cancellationToken: cancellationToken);

                    if (parameterSymbol != null)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
        public static void ComputeRefactoring(
            RefactoringContext context,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selectedMembers)
        {
            int count = 0;

            for (int i = 0; i < selectedMembers.Count; i++)
            {
                if (selectedMembers[i].EqualsValue?.Value != null)
                {
                    count++;

                    if (count == 2)
                    {
                        break;
                    }
                }
            }

            if (count == 0)
            {
                return;
            }

            context.RegisterRefactoring(
                (count == 1) ? "Remove enum value" : "Remove enum values",
                ct => RefactorAsync(context.Document, enumDeclaration, selectedMembers, keepCompositeValue: false, ct),
                RefactoringIdentifiers.RemoveEnumMemberValue);
        }
        public static void ComputeRefactoring(RefactoringContext context, ParameterListSyntax parameterList)
        {
            if (!SeparatedSyntaxListSelection <ParameterSyntax> .TryCreate(parameterList.Parameters, context.Span, out SeparatedSyntaxListSelection <ParameterSyntax> selection))
            {
                return;
            }

            ImmutableArray <ParameterSyntax> parameters = selection
                                                          .Where(IsValid)
                                                          .ToImmutableArray();

            if (!parameters.Any())
            {
                return;
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.IntroduceAndInitializeProperty))
            {
                IEnumerable <IntroduceAndInitializePropertyInfo> propertyInfos = parameters
                                                                                 .Select(parameter => new IntroduceAndInitializePropertyInfo(parameter, context.SupportsCSharp6));

                var refactoring = new IntroduceAndInitializePropertyRefactoring(propertyInfos);
                refactoring.RegisterRefactoring(context);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.IntroduceAndInitializeField))
            {
                IEnumerable <IntroduceAndInitializeFieldInfo> fieldInfos = parameters
                                                                           .Select(parameter => new IntroduceAndInitializeFieldInfo(parameter, context.Settings.PrefixFieldIdentifierWithUnderscore));

                var refactoring = new IntroduceAndInitializeFieldRefactoring(fieldInfos);
                refactoring.RegisterRefactoring(context);
            }
        }
Exemple #10
0
        private static Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selectedMembers,
            INamedTypeSymbol enumSymbol,
            string name,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            ExpressionSyntax            expression = null;
            EnumMemberDeclarationSyntax lastMember = null;

            using (IEnumerator <EnumMemberDeclarationSyntax> en = GetMembersToCombine().GetEnumerator())
            {
                if (en.MoveNext())
                {
                    lastMember = en.Current;
                    expression = IdentifierName(en.Current.Identifier.WithoutTrivia());

                    while (en.MoveNext())
                    {
                        expression = BitwiseOrExpression(
                            IdentifierName(en.Current.Identifier.WithoutTrivia()),
                            expression);
                    }
                }
            }

            EnumMemberDeclarationSyntax newEnumMember = EnumMemberDeclaration(
                Identifier(name).WithRenameAnnotation(),
                EqualsValueClause(expression));

            EnumDeclarationSyntax newEnumDeclaration = enumDeclaration
                                                       .WithMembers(enumDeclaration.Members.Insert(selectedMembers.UnderlyingList.IndexOf(lastMember) + 1, newEnumMember));

            return(document.ReplaceNodeAsync(enumDeclaration, newEnumDeclaration, cancellationToken));

            IEnumerable <EnumMemberDeclarationSyntax> GetMembersToCombine()
            {
                for (int i = selectedMembers.Count - 1; i >= 0; i--)
                {
                    IFieldSymbol symbol = semanticModel.GetDeclaredSymbol(selectedMembers[i], cancellationToken);

                    if (symbol.HasConstantValue)
                    {
                        ulong value = SymbolUtility.GetEnumValueAsUInt64(symbol.ConstantValue, enumSymbol);

                        if (!FlagsUtility <ulong> .Instance.IsComposite(value))
                        {
                            yield return(selectedMembers[i]);
                        }
                    }
                }
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ParameterListSyntax parameterList)
        {
            SeparatedSyntaxList <ParameterSyntax> parameters = parameterList.Parameters;

            if (parameters.Any())
            {
                if (context.IsRefactoringEnabled(RefactoringDescriptors.CopyParameter))
                {
                    var refactoring = new CopyParameterRefactoring(parameterList);
                    refactoring.ComputeRefactoring(context, RefactoringDescriptors.CopyParameter);
                }

                if (context.IsRefactoringEnabled(RefactoringDescriptors.CheckParameterForNull) &&
                    SeparatedSyntaxListSelection <ParameterSyntax> .TryCreate(parameterList.Parameters, context.Span, out SeparatedSyntaxListSelection <ParameterSyntax> selectedParameters))
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    CheckParameterForNullRefactoring.ComputeRefactoring(context, selectedParameters, semanticModel);
                }

                if (context.IsAnyRefactoringEnabled(
                        RefactoringDescriptors.IntroduceAndInitializeField,
                        RefactoringDescriptors.IntroduceAndInitializeProperty))
                {
                    IntroduceAndInitializeRefactoring.ComputeRefactoring(context, parameterList);
                }

                if (context.IsRefactoringEnabled(RefactoringDescriptors.WrapParameters) &&
                    (context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(parameterList)))
                {
                    if (parameterList.IsSingleLine())
                    {
                        if (parameters.Count > 1)
                        {
                            context.RegisterRefactoring(
                                "Wrap parameters",
                                ct => SyntaxFormatter.WrapParametersAsync(context.Document, parameterList, ct),
                                RefactoringDescriptors.WrapParameters);
                        }
                    }
                    else if (parameterList.DescendantTrivia(parameterList.Span).All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                    {
                        context.RegisterRefactoring(
                            "Unwrap parameters",
                            ct => SyntaxFormatter.UnwrapExpressionAsync(context.Document, parameterList, ct),
                            RefactoringDescriptors.WrapParameters);
                    }
                }
            }
        }
        private static bool CanRefactor(SeparatedSyntaxListSelection <ArgumentSyntax> selection)
        {
            for (int i = selection.StartIndex; i <= selection.EndIndex; i++)
            {
                NameColonSyntax nameColon = selection.Items[i].NameColon;

                if (nameColon?.IsMissing == false)
                {
                    return(true);
                }
            }

            return(false);
        }
        public static void ComputeRefactoring(
            RefactoringContext context,
            ArgumentListSyntax argumentList,
            SeparatedSyntaxListSelection <ArgumentSyntax> selection)
        {
            if (!CanRefactor(selection))
            {
                return;
            }

            context.RegisterRefactoring(
                "Remove parameter name",
                cancellationToken => RefactorAsync(context.Document, argumentList, selection, cancellationToken));
        }
        private static bool CanRefactor(SeparatedSyntaxListSelection <ArgumentSyntax> selection)
        {
            for (int i = 0; i < selection.Count; i++)
            {
                NameColonSyntax nameColon = selection[i].NameColon;

                if (nameColon?.IsMissing == false)
                {
                    return(true);
                }
            }

            return(false);
        }
        private static Task <Document> RefactorAsync(
            Document document,
            ArgumentListSyntax argumentList,
            SeparatedSyntaxListSelection <ArgumentSyntax> selection,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var rewriter = new RemoveParameterNameRewriter(selection.ToImmutableArray());

            SyntaxNode newNode = rewriter
                                 .Visit(argumentList)
                                 .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(argumentList, newNode, cancellationToken));
        }
Exemple #16
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ParameterListSyntax parameterList)
        {
            SeparatedSyntaxList <ParameterSyntax> parameters = parameterList.Parameters;

            if (parameters.Any())
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.DuplicateParameter))
                {
                    var refactoring = new DuplicateParameterRefactoring(parameterList);
                    refactoring.ComputeRefactoring(context, RefactoringIdentifiers.DuplicateParameter);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.CheckParameterForNull) &&
                    SeparatedSyntaxListSelection <ParameterSyntax> .TryCreate(parameterList.Parameters, context.Span, out SeparatedSyntaxListSelection <ParameterSyntax> selectedParameters))
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    CheckParameterForNullRefactoring.ComputeRefactoring(context, selectedParameters, semanticModel);
                }

                if (context.IsAnyRefactoringEnabled(
                        RefactoringIdentifiers.IntroduceAndInitializeField,
                        RefactoringIdentifiers.IntroduceAndInitializeProperty))
                {
                    IntroduceAndInitializeRefactoring.ComputeRefactoring(context, parameterList);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatParameterList) &&
                    (context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(parameterList)))
                {
                    if (parameterList.IsSingleLine())
                    {
                        if (parameters.Count > 1)
                        {
                            context.RegisterRefactoring(
                                "Format parameters on separate lines",
                                cancellationToken => SyntaxFormatter.ToMultiLineAsync(context.Document, parameterList, cancellationToken),
                                RefactoringIdentifiers.FormatParameterList);
                        }
                    }
                    else if (parameterList.DescendantTrivia(parameterList.Span).All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                    {
                        context.RegisterRefactoring(
                            "Format parameters on a single line",
                            cancellationToken => SyntaxFormatter.ToSingleLineAsync(context.Document, parameterList, cancellationToken),
                            RefactoringIdentifiers.FormatParameterList);
                    }
                }
            }
        }
Exemple #17
0
        public static void ComputeRefactoring(
            RefactoringContext context,
            ArgumentListSyntax argumentList,
            SeparatedSyntaxListSelection <ArgumentSyntax> selection)
        {
            if (!CanRefactor(selection))
            {
                return;
            }

            context.RegisterRefactoring(
                "Remove argument name",
                ct => RefactorAsync(context.Document, argumentList, selection, ct),
                RefactoringDescriptors.RemoveArgumentName);
        }
Exemple #18
0
        public static void ComputeRefactoring(
            RefactoringContext context,
            ArgumentListSyntax argumentList,
            SeparatedSyntaxListSelection <ArgumentSyntax> selection,
            SemanticModel semanticModel)
        {
            if (!CanRefactor(selection, semanticModel, context.CancellationToken))
            {
                return;
            }

            context.RegisterRefactoring(
                "Add parameter name",
                cancellationToken => RefactorAsync(context.Document, argumentList, selection, cancellationToken),
                RefactoringIdentifiers.AddParameterNameToArgument);
        }
        private static Task <Document> SortByNameAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selectedMembers,
            CancellationToken cancellationToken)
        {
            IEnumerable <EnumMemberDeclarationSyntax> sorted = selectedMembers.OrderBy(f => f, EnumMemberDeclarationNameComparer.Instance);

            SeparatedSyntaxList <EnumMemberDeclarationSyntax> newMembers = enumDeclaration
                                                                           .Members
                                                                           .ReplaceRange(selectedMembers.FirstIndex, selectedMembers.Count, sorted);

            MemberDeclarationSyntax newNode = enumDeclaration.WithMembers(newMembers);

            return(document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken));
        }
Exemple #20
0
        private static async Task <Document> RefactorAsync(
            Document document,
            ArgumentListSyntax argumentList,
            SeparatedSyntaxListSelection <ArgumentSyntax> selection,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var rewriter = new AddParameterNameRewriter(selection.SelectedItems, semanticModel);

            SyntaxNode newNode = rewriter
                                 .Visit(argumentList)
                                 .WithFormatterAnnotation();

            return(await document.ReplaceNodeAsync(argumentList, newNode, cancellationToken).ConfigureAwait(false));
        }
Exemple #21
0
        public static void ComputeRefactoring(
            RefactoringContext context,
            ArgumentListSyntax argumentList,
            SeparatedSyntaxListSelection <ArgumentSyntax> selection,
            SemanticModel semanticModel)
        {
            if (!CanRefactor(selection, semanticModel, context.CancellationToken))
            {
                return;
            }

            context.RegisterRefactoring(
                "Add argument name",
                ct => RefactorAsync(context.Document, argumentList, selection, ct),
                RefactoringDescriptors.AddArgumentName);
        }
        public static void ComputeRefactoring(
            RefactoringContext context,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selectedMembers,
            SemanticModel semanticModel)
        {
            INamedTypeSymbol enumSymbol = semanticModel.GetDeclaredSymbol(enumDeclaration, context.CancellationToken);

            if (enumSymbol?.HasAttribute(MetadataNames.System_FlagsAttribute) != true)
            {
                return;
            }

            ImmutableArray <ulong> constantValues = selectedMembers
                                                    .Select(f => semanticModel.GetDeclaredSymbol(f, context.CancellationToken))
                                                    .Where(f => f.HasConstantValue)
                                                    .Select(f => SymbolUtility.GetEnumValueAsUInt64(f.ConstantValue, enumSymbol))
                                                    .ToImmutableArray();

            Optional <ulong> optionalValue = FlagsUtility <ulong> .Instance.TryCompose(constantValues);

            if (!optionalValue.HasValue)
            {
                return;
            }

            if (IsValueDefined(enumSymbol, optionalValue))
            {
                return;
            }

            string name = NameGenerator.Default.EnsureUniqueMemberName(
                string.Concat(selectedMembers.Select(f => f.Identifier.ValueText)),
                enumSymbol);

            context.RegisterRefactoring(
                $"Generate enum member '{name}'",
                ct => RefactorAsync(context.Document, enumDeclaration, selectedMembers, name, ct),
                RefactoringIdentifiers.GenerateCombinedEnumMember);
        }
        public static async Task ComputeRefactoringAsync(RefactoringContext context, ParameterListSyntax parameterList)
        {
            if (!SeparatedSyntaxListSelection <ParameterSyntax> .TryCreate(parameterList.Parameters, context.Span, out SeparatedSyntaxListSelection <ParameterSyntax> selection))
            {
                return;
            }

            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

            ImmutableArray <ParameterSyntax> parameters = selection
                                                          .Where(parameter => IsValid(parameter) && CanRefactor(parameter, semanticModel, context.CancellationToken))
                                                          .ToImmutableArray();

            if (parameters.Length == 1)
            {
                RegisterRefactoring(context, parameters[0]);
            }
            else if (parameters.Length > 0)
            {
                RegisterRefactoring(context, parameters, "parameters");
            }
        }
        private static Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selection,
            CancellationToken cancellationToken)
        {
            SeparatedSyntaxList <EnumMemberDeclarationSyntax> newMembers = enumDeclaration.Members.ModifyRange(
                selection.FirstIndex,
                selection.Count,
                enumMember =>
            {
                return(enumMember
                       .WithEqualsValue(null)
                       .WithTrailingTrivia(enumMember.GetTrailingTrivia())
                       .WithFormatterAnnotation());
            })
                                                                           .ToSeparatedSyntaxList();

            EnumDeclarationSyntax newEnumDeclaration = enumDeclaration.WithMembers(newMembers);

            return(document.ReplaceNodeAsync(enumDeclaration, newEnumDeclaration, cancellationToken));
        }
        private static async Task <Document> SortByValueAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selectedMembers,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            SpecialType enumSpecialType = semanticModel.GetDeclaredSymbol(enumDeclaration, cancellationToken).EnumUnderlyingType.SpecialType;

            var comparer = new EnumMemberDeclarationValueComparer(EnumValueComparer.GetInstance(enumSpecialType), semanticModel, cancellationToken);

            IEnumerable <EnumMemberDeclarationSyntax> sorted = selectedMembers.OrderBy(f => f, comparer);

            SeparatedSyntaxList <EnumMemberDeclarationSyntax> newMembers = enumDeclaration
                                                                           .Members
                                                                           .ReplaceRange(selectedMembers.FirstIndex, selectedMembers.Count, sorted);

            MemberDeclarationSyntax newNode = enumDeclaration.WithMembers(newMembers);

            return(await document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken).ConfigureAwait(false));
        }
Exemple #26
0
        private static Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selection,
            CancellationToken cancellationToken)
        {
            SeparatedSyntaxList <EnumMemberDeclarationSyntax> members    = enumDeclaration.Members;
            SeparatedSyntaxList <EnumMemberDeclarationSyntax> newMembers = members;

            for (int i = selection.StartIndex; i <= selection.EndIndex; i++)
            {
                EnumMemberDeclarationSyntax newMember = members[i]
                                                        .WithEqualsValue(null)
                                                        .WithTrailingTrivia(members[i].GetTrailingTrivia())
                                                        .WithFormatterAnnotation();

                newMembers = newMembers.ReplaceAt(i, newMember);
            }

            EnumDeclarationSyntax newEnumDeclaration = enumDeclaration.WithMembers(newMembers);

            return(document.ReplaceNodeAsync(enumDeclaration, newEnumDeclaration, cancellationToken));
        }
Exemple #27
0
        public static void ComputeRefactoring(RefactoringContext context, SeparatedSyntaxListSelection <ParameterSyntax> selectedParameters, SemanticModel semanticModel)
        {
            ParameterSyntax singleParameter = null;

            ImmutableArray <ParameterSyntax> .Builder builder = default;

            foreach (ParameterSyntax parameter in selectedParameters)
            {
                if (IsValid(parameter) &&
                    CanRefactor(parameter, semanticModel, context.CancellationToken))
                {
                    if (singleParameter == null)
                    {
                        singleParameter = parameter;
                    }
                    else
                    {
                        if (builder == null)
                        {
                            builder = ImmutableArray.CreateBuilder <ParameterSyntax>(selectedParameters.Count);
                        }

                        builder.Add(singleParameter);
                        builder.Add(parameter);
                    }
                }
            }

            if (builder != null)
            {
                RegisterRefactoring(context, builder.ToImmutableArray(), "parameters", semanticModel);
            }
            else if (singleParameter != null)
            {
                RegisterRefactoring(context, singleParameter, semanticModel);
            }
        }
Exemple #28
0
        public static void ComputeRefactoring(
            RefactoringContext context,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selectedMembers,
            SemanticModel semanticModel)
        {
            if (!enumDeclaration.AttributeLists.Any())
            {
                return;
            }

            INamedTypeSymbol enumSymbol = semanticModel.GetDeclaredSymbol(enumDeclaration, context.CancellationToken);

            if (enumSymbol?.HasAttribute(MetadataNames.System_FlagsAttribute) != true)
            {
                return;
            }

            IEnumerable <(IFieldSymbol symbol, ulong value)> symbolsValues = selectedMembers
                                                                             .Select(f => semanticModel.GetDeclaredSymbol(f, context.CancellationToken))
                                                                             .Where(f => f.HasConstantValue)
                                                                             .Select(f => (symbol: f, value: SymbolUtility.GetEnumValueAsUInt64(f.ConstantValue, enumSymbol)))
                                                                             .Where(f => !FlagsUtility <ulong> .Instance.IsComposite(f.value));

            ImmutableArray <ulong> constantValues = symbolsValues.Select(f => f.value).ToImmutableArray();

            if (constantValues.Length <= 1)
            {
                return;
            }

            Optional <ulong> optionalValue = FlagsUtility <ulong> .Instance.TryCompose(constantValues);

            if (!optionalValue.HasValue)
            {
                return;
            }

            if (IsValueDefined(optionalValue))
            {
                return;
            }

            string name = NameGenerator.Default.EnsureUniqueEnumMemberName(
                string.Concat(symbolsValues.Select(f => f.symbol.Name)),
                enumSymbol);

            context.RegisterRefactoring(
                $"Generate member '{name}'",
                ct => RefactorAsync(context.Document, enumDeclaration, selectedMembers, enumSymbol, name, semanticModel, ct),
                RefactoringIdentifiers.GenerateCombinedEnumMember);

            bool IsValueDefined(object value)
            {
                foreach (ISymbol member in enumSymbol.GetMembers())
                {
                    if (member.Kind == SymbolKind.Field)
                    {
                        var fieldSymbol = (IFieldSymbol)member;

                        if (fieldSymbol.HasConstantValue &&
                            object.Equals(fieldSymbol.ConstantValue, value))
                        {
                            return(true);
                        }
                    }
                }

                return(false);
            }
        }
 internal EnumeratorImpl(SeparatedSyntaxListSelection <TNode> selection)
 {
     _en = new Enumerator(selection);
 }
 internal Enumerator(SeparatedSyntaxListSelection <TNode> selection)
 {
     _selection = selection;
     _index     = -1;
 }