private ImmutableArray <Edit> GetWrapEachEdits(
                WrappingStyle wrappingStyle, SyntaxTrivia indentationTrivia)
            {
                var result = ArrayBuilder <Edit> .GetInstance();

                AddTextChangeBetweenOpenAndFirstItem(wrappingStyle, result);

                var itemsAndSeparators = _listItems.GetWithSeparators();

                for (var i = 0; i < itemsAndSeparators.Count; i += 2)
                {
                    var item = itemsAndSeparators[i].AsNode();
                    if (i < itemsAndSeparators.Count - 1)
                    {
                        // intermediary item
                        var comma = itemsAndSeparators[i + 1].AsToken();
                        result.Add(Edit.DeleteBetween(item, comma));

                        // Always wrap between this comma and the next item.
                        result.Add(Edit.UpdateBetween(
                                       comma, NewLineTrivia, indentationTrivia, itemsAndSeparators[i + 2]));
                    }
                }

                // last item.  Delete whatever is between it and the close token of the list.
                result.Add(Edit.DeleteBetween(_listItems.Last(), _listSyntax.GetLastToken()));

                return(result.ToImmutableAndFree());
            }
 private void AddTextChangeBetweenOpenAndFirstItem(
     WrappingStyle wrappingStyle, ArrayBuilder <Edit> result)
 {
     result.Add(wrappingStyle == WrappingStyle.WrapFirst_IndentRest
         ? Edit.UpdateBetween(_listSyntax.GetFirstToken(), NewLineTrivia, _singleIndentationTrivia, _listItems[0])
         : Edit.DeleteBetween(_listSyntax.GetFirstToken(), _listItems[0]));
 }
            private ImmutableArray <Edit> GetUnwrapAllEdits(WrappingStyle wrappingStyle)
            {
                using var _ = ArrayBuilder <Edit> .GetInstance(out var result);

                if (_shouldMoveOpenBraceToNewLine)
                {
                    result.Add(Edit.DeleteBetween(_listSyntax.GetFirstToken().GetPreviousToken(), _listSyntax.GetFirstToken()));
                }

                AddTextChangeBetweenOpenAndFirstItem(wrappingStyle, result);

                foreach (var comma in _listItems.GetSeparators())
                {
                    result.Add(Edit.DeleteBetween(comma.GetPreviousToken(), comma));
                    result.Add(Edit.DeleteBetween(comma, comma.GetNextToken()));
                }

                var last = _listItems.GetWithSeparators().Last();

                if (last.IsNode)
                {
                    result.Add(Edit.DeleteBetween(last, _listSyntax.GetLastToken()));
                }

                return(result.ToImmutable());
            }
            private async Task <WrapItemsAction> GetWrapEveryNestedCodeActionAsync(
                string parentTitle, WrappingStyle wrappingStyle)
            {
                var indentationTrivia = GetIndentationTrivia(wrappingStyle);

                var edits = GetWrapEachEdits(wrappingStyle, indentationTrivia);
                var title = GetNestedCodeActionTitle(wrappingStyle);

                return(await TryCreateCodeActionAsync(edits, parentTitle, title).ConfigureAwait(false));
            }
            private string GetNestedCodeActionTitle(WrappingStyle wrappingStyle)
            {
                switch (wrappingStyle)
                {
                case WrappingStyle.WrapFirst_IndentRest: return(Wrapper.Indent_all_items);

                case WrappingStyle.UnwrapFirst_AlignRest: return(Wrapper.Align_wrapped_items);

                case WrappingStyle.UnwrapFirst_IndentRest: return(Wrapper.Indent_wrapped_items);

                default:
                    throw ExceptionUtilities.UnexpectedValue(wrappingStyle);
                }
            }
            private async Task <WrapItemsAction> GetUnwrapAllCodeActionAsync(
                string parentTitle,
                WrappingStyle wrappingStyle
                )
            {
                var edits = GetUnwrapAllEdits(wrappingStyle);
                var title =
                    wrappingStyle == WrappingStyle.WrapFirst_IndentRest
                        ? Wrapper.Unwrap_and_indent_all_items
                        : Wrapper.Unwrap_all_items;

                return(await TryCreateCodeActionAsync(edits, parentTitle, title)
                       .ConfigureAwait(false));
            }
            private ImmutableArray <Edit> GetUnwrapAllEdits(WrappingStyle wrappingStyle)
            {
                var result = ArrayBuilder <Edit> .GetInstance();

                AddTextChangeBetweenOpenAndFirstItem(wrappingStyle, result);

                foreach (var comma in _listItems.GetSeparators())
                {
                    result.Add(Edit.DeleteBetween(comma.GetPreviousToken(), comma));
                    result.Add(Edit.DeleteBetween(comma, comma.GetNextToken()));
                }

                result.Add(Edit.DeleteBetween(_listItems.Last(), _listSyntax.GetLastToken()));
                return(result.ToImmutableAndFree());
            }
            private ImmutableArray <Edit> GetWrapLongLinesEdits(
                WrappingStyle wrappingStyle, SyntaxTrivia indentationTrivia)
            {
                var result = ArrayBuilder <Edit> .GetInstance();

                AddTextChangeBetweenOpenAndFirstItem(wrappingStyle, result);

                var currentOffset = wrappingStyle == WrappingStyle.WrapFirst_IndentRest
                    ? indentationTrivia.FullWidth()
                    : _afterOpenTokenIndentationTrivia.FullWidth();
                var itemsAndSeparators = _listItems.GetWithSeparators();

                for (var i = 0; i < itemsAndSeparators.Count; i += 2)
                {
                    var item = itemsAndSeparators[i].AsNode();

                    // Figure out where we'd be after this item.
                    currentOffset += item.Span.Length;

                    if (i > 0)
                    {
                        if (currentOffset < WrappingColumn)
                        {
                            // this item would not make us go pass our preferred wrapping column. So
                            // keep it on this line, making sure there's a space between the previous
                            // comma and us.
                            result.Add(Edit.UpdateBetween(itemsAndSeparators[i - 1], SingleWhitespaceTrivia, NoTrivia, item));
                            currentOffset += " ".Length;
                        }
                        else
                        {
                            // not the first item on the line and this item makes us go past the wrapping
                            // limit.  We want to wrap before this item.
                            result.Add(Edit.UpdateBetween(itemsAndSeparators[i - 1], NewLineTrivia, indentationTrivia, item));
                            currentOffset = indentationTrivia.FullWidth() + item.Span.Length;
                        }
                    }

                    // Get rid of any spaces between the list item and the following token (a
                    // comma or close token).
                    var nextToken = item.GetLastToken().GetNextToken();

                    result.Add(Edit.DeleteBetween(item, nextToken));
                    currentOffset += nextToken.Span.Length;
                }

                return(result.ToImmutableAndFree());
            }
            private ImmutableArray <Edit> GetWrapEachEdits(
                WrappingStyle wrappingStyle, SyntaxTrivia indentationTrivia)
            {
                using var _ = ArrayBuilder <Edit> .GetInstance(out var result);

                if (_shouldMoveOpenBraceToNewLine)
                {
                    result.Add(Edit.UpdateBetween(_listSyntax.GetFirstToken().GetPreviousToken(), NewLineTrivia, _braceIndentationTrivia, _listSyntax.GetFirstToken()));
                }

                AddTextChangeBetweenOpenAndFirstItem(wrappingStyle, result);

                var itemsAndSeparators = _listItems.GetWithSeparators();

                for (var i = 1; i < itemsAndSeparators.Count; i += 2)
                {
                    var comma = itemsAndSeparators[i].AsToken();

                    var item = itemsAndSeparators[i - 1];
                    result.Add(Edit.DeleteBetween(item, comma));

                    if (i < itemsAndSeparators.Count - 1)
                    {
                        // Always wrap between this comma and the next item.
                        result.Add(Edit.UpdateBetween(
                                       comma, NewLineTrivia, indentationTrivia, itemsAndSeparators[i + 1]));
                    }
                }

                if (_shouldMoveCloseBraceToNewLine)
                {
                    result.Add(Edit.UpdateBetween(itemsAndSeparators.Last(), NewLineTrivia, _braceIndentationTrivia, _listSyntax.GetLastToken()));
                }
                else
                {
                    // last item.  Delete whatever is between it and the close token of the list.
                    result.Add(Edit.DeleteBetween(itemsAndSeparators.Last(), _listSyntax.GetLastToken()));
                }

                return(result.ToImmutable());
            }
 private SyntaxTrivia GetIndentationTrivia(WrappingStyle wrappingStyle)
 {
     return(wrappingStyle == WrappingStyle.UnwrapFirst_AlignRest
         ? _afterOpenTokenIndentationTrivia
         : _singleIndentationTrivia);
 }
Ejemplo n.º 11
0
 private string GetNestedCodeActionTitle(WrappingStyle wrappingStyle)
 => wrappingStyle switch
 {
        private void SetWrappingStyle(WrappingStyle? style)
        {
            this.UncheckWrappingStyleToggleButtons();
            if (style == null)
            {
                this.wrappingStyleInLineWithTextButton.IsChecked = true;
            }
            else
            {
                switch (style.Value)
                {
                    case WrappingStyle.TopAndBottom:
                        this.wrappingStyleTopAndBottomButton.IsChecked = true;
                        break;
                    case WrappingStyle.Square:
                        this.wrappingStyleSquareButton.IsChecked = true;
                        break;
                    case WrappingStyle.BehindText:
                        this.wrappingStyleBehindTextButton.IsChecked = true;
                        break;
                    case WrappingStyle.InFrontOfText:
                        this.wrappingStyleInFrontOfTextButton.IsChecked = true;
                        break;
                    default:
                        throw new NotImplementedException("Unknown WrappingStyle: " + style.Value.ToString());
                }

            }
        }
            private ImmutableArray <Edit> GetWrapLongLinesEdits(
                WrappingStyle wrappingStyle, SyntaxTrivia indentationTrivia)
            {
                using var _ = ArrayBuilder <Edit> .GetInstance(out var result);

                if (_shouldMoveOpenBraceToNewLine)
                {
                    result.Add(Edit.UpdateBetween(_listSyntax.GetFirstToken().GetPreviousToken(), NewLineTrivia, _braceIndentationTrivia, _listSyntax.GetFirstToken()));
                }

                AddTextChangeBetweenOpenAndFirstItem(wrappingStyle, result);

                var currentOffset = wrappingStyle == WrappingStyle.WrapFirst_IndentRest
                    ? indentationTrivia.FullWidth()
                    : _afterOpenTokenIndentationTrivia.FullWidth();
                var itemsAndSeparators = _listItems.GetWithSeparators();

                for (var i = 0; i < itemsAndSeparators.Count; i += 2)
                {
                    var item = itemsAndSeparators[i].AsNode() !;

                    // Figure out where we'd be after this item.
                    currentOffset += item.Span.Length;

                    if (i > 0)
                    {
                        if (currentOffset < Options.WrappingColumn)
                        {
                            // this item would not make us go pass our preferred wrapping column. So
                            // keep it on this line, making sure there's a space between the previous
                            // comma and us.
                            result.Add(Edit.UpdateBetween(itemsAndSeparators[i - 1], SingleWhitespaceTrivia, NoTrivia, item));
                            currentOffset += " ".Length;
                        }
                        else
                        {
                            // not the first item on the line and this item makes us go past the wrapping
                            // limit.  We want to wrap before this item.
                            result.Add(Edit.UpdateBetween(itemsAndSeparators[i - 1], NewLineTrivia, indentationTrivia, item));
                            currentOffset = indentationTrivia.FullWidth() + item.Span.Length;
                        }
                    }

                    // Get rid of any spaces between the list item and the following comma token
                    if (i + 1 < itemsAndSeparators.Count)
                    {
                        var comma = itemsAndSeparators[i + 1];
                        Contract.ThrowIfFalse(comma.IsToken);
                        result.Add(Edit.DeleteBetween(item, comma));
                        currentOffset += comma.Span.Length;
                    }
                }

                if (this.Wrapper.ShouldMoveCloseBraceToNewLine)
                {
                    result.Add(Edit.UpdateBetween(itemsAndSeparators.Last(), NewLineTrivia, _braceIndentationTrivia, _listSyntax.GetLastToken()));
                }
                else
                {
                    result.Add(Edit.DeleteBetween(itemsAndSeparators.Last(), _listSyntax.GetLastToken()));
                }

                return(result.ToImmutable());
            }
 internal void WrappingModeUpdated(WrappingStyle? wrappingStyle)
 {
     if (wrappingStyle != null)
     {
         this.SetIsEnabled(true);
         this.UpdateRadioPositionTypes();
     }
     else
     {
         this.SetIsEnabled(false);
     }
 }