Exemplo n.º 1
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SyntaxNode node)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.WrapInRegion,
                    RefactoringIdentifiers.WrapInIfDirective,
                    RefactoringIdentifiers.RemoveEmptyLines))
            {
                SyntaxNode root = context.Root;
                TextSpan   span = context.Span;

                if (!IsFullLineSpan(node, span))
                {
                    return;
                }

                Document   document   = context.Document;
                SourceText sourceText = await document.GetTextAsync(context.CancellationToken).ConfigureAwait(false);

                if (!TextLineCollectionSelection.TryCreate(sourceText.Lines, span, out TextLineCollectionSelection selectedLines))
                {
                    return;
                }

                if (!IsInMultiLineDocumentationComment(root, span.Start) &&
                    !IsInMultiLineDocumentationComment(root, span.End))
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInRegion))
                    {
                        context.RegisterRefactoring(
                            "Wrap in region",
                            ct => WrapInRegionRefactoring.Instance.RefactorAsync(document, selectedLines, ct),
                            RefactoringIdentifiers.WrapInRegion);
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInIfDirective))
                    {
                        context.RegisterRefactoring(
                            "Wrap in #if",
                            ct => WrapInIfDirectiveRefactoring.Instance.RefactorAsync(document, selectedLines, ct),
                            RefactoringIdentifiers.WrapInIfDirective);
                    }
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveEmptyLines))
                {
                    bool containsEmptyLine = false;

                    foreach (TextLine line in selectedLines)
                    {
                        context.ThrowIfCancellationRequested();

                        if (line.IsEmptyOrWhiteSpace() &&
                            root.FindTrivia(line.End, findInsideTrivia: true).IsEndOfLineTrivia())
                        {
                            containsEmptyLine = true;
                            break;
                        }
                    }

                    if (containsEmptyLine)
                    {
                        context.RegisterRefactoring(
                            "Remove empty lines",
                            ct =>
                        {
                            ct.ThrowIfCancellationRequested();

                            IEnumerable <TextChange> textChanges = selectedLines
                                                                   .Where(line => line.IsEmptyOrWhiteSpace() && root.FindTrivia(line.End, findInsideTrivia: true).IsEndOfLineTrivia())
                                                                   .Select(line => new TextChange(line.SpanIncludingLineBreak, ""));

                            return(document.WithTextChangesAsync(textChanges, ct));
                        },
                            RefactoringIdentifiers.RemoveEmptyLines);
                    }
                }
            }
        }