コード例 #1
0
        public static Task <Document> MergeAsync(
            Document document,
            MemberDeclarationSyntax member,
            AttributeListSyntax[] attributeLists,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxList <AttributeListSyntax> lists = member.GetAttributeLists();

            var newLists = new List <AttributeListSyntax>(lists.Count - attributeLists.Length + 1);

            int index = lists.IndexOf(attributeLists[0]);

            for (int i = 0; i < index; i++)
            {
                newLists.Add(lists[i]);
            }

            newLists.Add(AttributeRefactoring.MergeAttributes(attributeLists));

            for (int i = index + attributeLists.Length; i < lists.Count; i++)
            {
                newLists.Add(lists[i]);
            }

            return(document.ReplaceNodeAsync(
                       member,
                       member.SetAttributeLists(newLists.ToSyntaxList()),
                       cancellationToken));
        }
コード例 #2
0
        public static void ComputeRefactorings(RefactoringContext context, MemberDeclarationSyntax member)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.SplitAttributes,
                    RefactoringIdentifiers.MergeAttributes) &&
                !member.IsKind(SyntaxKind.NamespaceDeclaration) &&
                SyntaxListSelection <AttributeListSyntax> .TryCreate(member.GetAttributeLists(), context.Span, out SyntaxListSelection <AttributeListSyntax> selectedAttributeLists))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.SplitAttributes) &&
                    selectedAttributeLists.Any(f => f.Attributes.Count > 1))
                {
                    context.RegisterRefactoring(
                        "Split attributes",
                        ct => SplitAsync(context.Document, member, selectedAttributeLists.ToArray(), ct),
                        RefactoringIdentifiers.SplitAttributes);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeAttributes) &&
                    selectedAttributeLists.Count > 1)
                {
                    context.RegisterRefactoring(
                        "Merge attributes",
                        ct => MergeAsync(context.Document, member, selectedAttributeLists.ToArray(), ct),
                        RefactoringIdentifiers.MergeAttributes);
                }
            }
        }
コード例 #3
0
        public static Task <Document> SplitAsync(
            Document document,
            MemberDeclarationSyntax member,
            AttributeListSyntax[] attributeLists,
            CancellationToken cancellationToken = default)
        {
            SyntaxList <AttributeListSyntax> lists = member.GetAttributeLists();

            var newLists = new List <AttributeListSyntax>();

            int index = lists.IndexOf(attributeLists[0]);

            for (int i = 0; i < index; i++)
            {
                newLists.Add(lists[i]);
            }

            newLists.AddRange(attributeLists.SelectMany(f => SyntaxRefactorings.SplitAttributeList(f)).Select(f => f.WithFormatterAnnotation()));

            for (int i = index + attributeLists.Length; i < lists.Count; i++)
            {
                newLists.Add(lists[i]);
            }

            return(document.ReplaceNodeAsync(
                       member,
                       member.WithAttributeLists(newLists.ToSyntaxList()),
                       cancellationToken));
        }
コード例 #4
0
        public static async Task <Document> MergeAsync(
            Document document,
            MemberDeclarationSyntax member,
            AttributeListSyntax[] attributeLists,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            SyntaxList <AttributeListSyntax> lists = member.GetAttributeLists();

            var newLists = new List <AttributeListSyntax>(lists.Count - attributeLists.Length + 1);

            int index = lists.IndexOf(attributeLists[0]);

            for (int i = 0; i < index; i++)
            {
                newLists.Add(lists[i]);
            }

            newLists.Add(AttributeRefactoring.MergeAttributes(attributeLists));

            for (int i = index + attributeLists.Length; i < lists.Count; i++)
            {
                newLists.Add(lists[i]);
            }

            SyntaxNode newRoot = root.ReplaceNode(
                member,
                member.SetAttributeLists(newLists.ToSyntaxList()));

            return(document.WithSyntaxRoot(newRoot));
        }
コード例 #5
0
        private static async Task <Document> RefactorAsync(
            Document document,
            MemberDeclarationSyntax member,
            AttributeListSyntax[] attributeLists,
            CancellationToken cancellationToken)
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken);

            SyntaxList <AttributeListSyntax> lists = member.GetAttributeLists();

            int index = lists.IndexOf(attributeLists[0]);

            for (int i = attributeLists.Length - 1; i >= 1; i--)
            {
                lists = lists.RemoveAt(index);
            }

            AttributeListSyntax list = AttributeRefactoring.MergeAttributes(attributeLists)
                                       .WithAdditionalAnnotations(Formatter.Annotation);

            lists = lists.Replace(lists[index], list);

            root = root.ReplaceNode(member, member.SetAttributeLists(lists));

            return(document.WithSyntaxRoot(root));
        }
コード例 #6
0
        public static void Refactor(CodeRefactoringContext context, MemberDeclarationSyntax member)
        {
            if (member == null)
            {
                throw new ArgumentNullException(nameof(member));
            }

            SyntaxList <AttributeListSyntax> attributeLists = member.GetAttributeLists();

            if (attributeLists.Count > 0)
            {
                AttributeListSyntax[] lists = AttributeRefactoring.GetSelectedAttributeLists(attributeLists, context.Span).ToArray();

                if (lists.Length > 1)
                {
                    context.RegisterRefactoring(
                        "Merge attributes",
                        cancellationToken =>
                    {
                        return(RefactorAsync(
                                   context.Document,
                                   member,
                                   lists,
                                   cancellationToken));
                    });
                }
            }
        }
コード例 #7
0
        public static async Task <Document> SplitAsync(
            Document document,
            MemberDeclarationSyntax member,
            AttributeListSyntax[] attributeLists,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxList <AttributeListSyntax> lists = member.GetAttributeLists();

            var newLists = new List <AttributeListSyntax>();

            int index = lists.IndexOf(attributeLists[0]);

            for (int i = 0; i < index; i++)
            {
                newLists.Add(lists[i]);
            }

            newLists.AddRange(attributeLists.SelectMany(f => AttributeRefactoring.SplitAttributes(f)));

            for (int i = index + attributeLists.Length; i < lists.Count; i++)
            {
                newLists.Add(lists[i]);
            }

            return(await document.ReplaceNodeAsync(
                       member,
                       member.SetAttributeLists(newLists.ToSyntaxList()),
                       cancellationToken).ConfigureAwait(false));
        }
コード例 #8
0
        private static async Task <Document> SplitAttributesAsync(
            Document document,
            MemberDeclarationSyntax member,
            AttributeListSyntax[] attributeLists,
            CancellationToken cancellationToken)
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

            SyntaxList <AttributeListSyntax> lists = member.GetAttributeLists();

            int index = lists.IndexOf(attributeLists[attributeLists.Length - 1]);

            for (int i = attributeLists.Length - 1; i >= 0; i--)
            {
                lists = lists.ReplaceRange(
                    lists[index],
                    AttributeRefactoring.SplitAttributes(attributeLists[i])
                    .Select(f => f.WithAdditionalAnnotations(Formatter.Annotation)));

                index--;
            }

            SyntaxNode newRoot = oldRoot.ReplaceNode(member, member.SetAttributeLists(lists));

            return(document.WithSyntaxRoot(newRoot));
        }
コード例 #9
0
        public static void ComputeRefactorings(RefactoringContext context, MemberDeclarationSyntax member)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.SplitAttributes,
                    RefactoringIdentifiers.MergeAttributes) &&
                !member.IsKind(SyntaxKind.NamespaceDeclaration))
            {
                SyntaxList <AttributeListSyntax> lists = member.GetAttributeLists();

                if (lists.Any())
                {
                    var selectedLists = new SelectedNodeCollection <AttributeListSyntax>(lists, context.Span);

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.SplitAttributes) &&
                        selectedLists.Any(f => f.Attributes.Count > 1))
                    {
                        context.RegisterRefactoring(
                            "Split attributes",
                            cancellationToken =>
                        {
                            return(SplitAsync(
                                       context.Document,
                                       member,
                                       selectedLists.ToArray(),
                                       cancellationToken));
                        });
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeAttributes) &&
                        selectedLists.IsMultiple)
                    {
                        context.RegisterRefactoring(
                            "Merge attributes",
                            cancellationToken =>
                        {
                            return(MergeAsync(
                                       context.Document,
                                       member,
                                       selectedLists.ToArray(),
                                       cancellationToken));
                        });
                    }
                }
            }
        }
コード例 #10
0
        public static void ComputeRefactorings(RefactoringContext context, MemberDeclarationSyntax member)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.SplitAttributes,
                    RefactoringIdentifiers.MergeAttributes))
            {
                SyntaxList <AttributeListSyntax> lists = member.GetAttributeLists();

                if (lists.Count > 0)
                {
                    var info = new SelectedNodesInfo <AttributeListSyntax>(lists, context.Span);

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.SplitAttributes) &&
                        info.SelectedNodes().Any(f => f.Attributes.Count > 1))
                    {
                        context.RegisterRefactoring(
                            "Split attributes",
                            cancellationToken =>
                        {
                            return(SplitAsync(
                                       context.Document,
                                       member,
                                       info.SelectedNodes().ToArray(),
                                       cancellationToken));
                        });
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeAttributes) &&
                        info.AreManySelected)
                    {
                        context.RegisterRefactoring(
                            "Merge attributes",
                            cancellationToken =>
                        {
                            return(MergeAsync(
                                       context.Document,
                                       member,
                                       info.SelectedNodes().ToArray(),
                                       cancellationToken));
                        });
                    }
                }
            }
        }