Exemplo n.º 1
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SyntaxNode node)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.WrapInRegion,
                    RefactoringIdentifiers.WrapInIfDirective))
            {
                //XPERF:
                TextLineCollectionSelection selectedLines = await SelectedLinesRefactoring.GetSelectedLinesAsync(context).ConfigureAwait(false);

                if (selectedLines != null)
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInRegion))
                    {
                        context.RegisterRefactoring(
                            "Wrap in region",
                            ct => WrapInRegionRefactoring.Instance.RefactorAsync(context.Document, selectedLines, ct));
                    }

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

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveEmptyLines) &&
                await RemoveEmptyLinesRefactoring.CanRefactorAsync(context, node).ConfigureAwait(false))
            {
                context.RegisterRefactoring(
                    "Remove empty lines",
                    ct => RemoveEmptyLinesRefactoring.RefactorAsync(context.Document, context.Span, ct));
            }
        }
Exemplo n.º 2
0
        public Task <Document> RefactorAsync(
            Document document,
            TextLineCollectionSelection selectedLines,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ImmutableArray <TextChange> textChanges = GetTextChanges(selectedLines);

            return(document.WithTextChangesAsync(textChanges, cancellationToken));
        }
        public static async Task <TextLineCollectionSelection> GetSelectedLinesAsync(RefactoringContext context)
        {
            TextSpan span = context.Span;

            if (IsValidSpan(context.Root, span))
            {
                SourceText sourceText = await context.Document.GetTextAsync(context.CancellationToken).ConfigureAwait(false);

                return(TextLineCollectionSelection.Create(sourceText.Lines, span));
            }

            return(null);
        }
Exemplo n.º 4
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SyntaxNode node)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.WrapInRegion,
                    RefactoringIdentifiers.WrapInIfDirective))
            {
                TextLineCollectionSelection selectedLines = await SelectedLinesRefactoring.GetSelectedLinesAsync(context).ConfigureAwait(false);

                if (selectedLines?.Any() == true)
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInRegion))
                    {
                        context.RegisterRefactoring(
                            "Wrap in region",
                            cancellationToken =>
                        {
                            var refactoring = new WrapInRegionRefactoring();

                            return(refactoring.RefactorAsync(context.Document, selectedLines, cancellationToken));
                        });
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapInIfDirective))
                    {
                        context.RegisterRefactoring(
                            "Wrap in #if",
                            cancellationToken =>
                        {
                            var refactoring = new WrapInIfDirectiveRefactoring();

                            return(refactoring.RefactorAsync(context.Document, selectedLines, cancellationToken));
                        });
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveEmptyLines) &&
                await RemoveEmptyLinesRefactoring.CanRefactorAsync(context, node).ConfigureAwait(false))
            {
                context.RegisterRefactoring(
                    "Remove empty lines",
                    cancellationToken =>
                {
                    return(RemoveEmptyLinesRefactoring.RefactorAsync(
                               context.Document,
                               context.Span,
                               cancellationToken));
                });
            }
        }
Exemplo n.º 5
0
        public static async Task <TextLineCollectionSelection> GetSelectedLinesAsync(RefactoringContext context)
        {
            if (!IsValidSpan(context.Root, context.Span))
            {
                return(null);
            }

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

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

            return(selectedLines);
        }
Exemplo n.º 6
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);
                    }
                }
            }
        }
 internal EnumeratorImpl(TextLineCollectionSelection selection)
 {
     _en = new Enumerator(selection);
 }
 internal Enumerator(TextLineCollectionSelection selection)
 {
     _selection = selection;
     _index     = -1;
 }