<commit_before>﻿// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Wrapping.SeparatedSyntaxList;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Wrapping.InitializerExpression
{
    internal abstract partial class AbstractInitializerExpressionWrapper<TListSyntax, TListItemSyntax>
    {
        private class InitializerExpressionCodeActionComputer : AbstractCodeActionComputer<AbstractInitializerExpressionWrapper<TListSyntax, TListItemSyntax>>
        {
            private readonly TListSyntax _listSyntax;
            private readonly SeparatedSyntaxList<TListItemSyntax> _listItems;
            private readonly SyntaxTrivia _afterOpenTokenIndentationTrivia;
            private readonly SyntaxTrivia _singleIndentationTrivia;
            private readonly SyntaxTrivia _braceIndentationTrivia;
            private readonly bool _doWrapInitializerOpenBrace;

            public InitializerExpressionCodeActionComputer(
                AbstractInitializerExpressionWrapper<TListSyntax, TListItemSyntax> service,
                Document document, SourceText sourceText, DocumentOptionSet options,
                TListSyntax listSyntax, SeparatedSyntaxList<TListItemSyntax> listItems,
                bool doWrapInitializerOpenBrace, CancellationToken cancellationToken)
                : base(service, document, sourceText, options, cancellationToken)
            {
                _listSyntax = listSyntax;
                _listItems = listItems;

                var generator = SyntaxGenerator.GetGenerator(OriginalDocument);

                _afterOpenTokenIndentationTrivia = generator.Whitespace(GetAfterOpenTokenIdentation());
                _singleIndentationTrivia = generator.Whitespace(GetSingleIdentation());
                _braceIndentationTrivia = generator.Whitespace(GetBraceTokenIndentation());

                _doWrapInitializerOpenBrace = doWrapInitializerOpenBrace;
            }

            private string GetAfterOpenTokenIdentation()
            {
                var openToken = _listSyntax.GetFirstToken();
                var afterOpenTokenOffset = OriginalSourceText.GetOffset(openToken.Span.End);

                var indentString = afterOpenTokenOffset.CreateIndentationString(UseTabs, TabSize);
                return indentString;
            }

            private string GetBraceTokenIndentation()
            {
                var generator = SyntaxGenerator.GetGenerator(OriginalDocument);
                var initialStatement = generator.GetDeclaration(_listSyntax);

                if (initialStatement == null)
                {
                    return string.Empty;
                }

                var initialStatementOffset = initialStatement.SpanStart;
                if (generator.GetDeclarationKind(initialStatement) == DeclarationKind.Method)
                {
                    // If we didn't find some form of declaration and are in a method body
                    // the initializer must be in a return statement. Therefore, add a tab
                    // to align the end brace with the return keyword
                    initialStatementOffset += TabSize;
                }


                var afterInitialStatementOffset = OriginalSourceText.GetOffset(initialStatementOffset);

                var indentString = afterInitialStatementOffset.CreateIndentationString(UseTabs, TabSize);
                return indentString;
            }

            private string GetSingleIdentation()
            {
                // Insert a newline after the open token of the list.  Then ask the
                // ISynchronousIndentationService where it thinks that the next line should be<commit_msg>Use the indentation service to place the open brace<commit_after>﻿// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Wrapping.SeparatedSyntaxList;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Wrapping.InitializerExpression
{
    internal abstract partial class AbstractInitializerExpressionWrapper<TListSyntax, TListItemSyntax>
    {
        private class InitializerExpressionCodeActionComputer : AbstractCodeActionComputer<AbstractInitializerExpressionWrapper<TListSyntax, TListItemSyntax>>
        {
            private readonly TListSyntax _listSyntax;
            private readonly SeparatedSyntaxList<TListItemSyntax> _listItems;
            private readonly SyntaxTrivia _afterOpenTokenIndentationTrivia;
            private readonly SyntaxTrivia _singleIndentationTrivia;
            private readonly SyntaxTrivia _braceIndentationTrivia;
            private readonly bool _doWrapInitializerOpenBrace;

            public InitializerExpressionCodeActionComputer(
                AbstractInitializerExpressionWrapper<TListSyntax, TListItemSyntax> service,
                Document document, SourceText sourceText, DocumentOptionSet options,
                TListSyntax listSyntax, SeparatedSyntaxList<TListItemSyntax> listItems,
                bool doWrapInitializerOpenBrace, CancellationToken cancellationToken)
                : base(service, document, sourceText, options, cancellationToken)
            {
                _listSyntax = listSyntax;
                _listItems = listItems;

                var generator = SyntaxGenerator.GetGenerator(OriginalDocument);

                _afterOpenTokenIndentationTrivia = generator.Whitespace(GetAfterOpenTokenIdentation());
                _singleIndentationTrivia = generator.Whitespace(GetSingleIdentation());
                _braceIndentationTrivia = generator.Whitespace(GetBraceTokenIndentation());

                _doWrapInitializerOpenBrace = doWrapInitializerOpenBrace;
            }

            private string GetAfterOpenTokenIdentation()
            {
                var openToken = _listSyntax.GetFirstToken();
                var afterOpenTokenOffset = OriginalSourceText.GetOffset(openToken.Span.End);

                var indentString = afterOpenTokenOffset.CreateIndentationString(UseTabs, TabSize);
                return indentString;
            }

            private string GetBraceTokenIndentation()
            {
                var previousToken = _listSyntax.GetFirstToken().GetPreviousToken();
                return GetSmartIndentationAfter(previousToken);
            }

            private string GetSingleIdentation()
            {
                // Insert a newline after the open token of the list.  Then ask the
                // ISynchronousIndentationService where it thinks that the next line should be
{'fraction_non_alphanumeric': 0.052157548057534614, 'fraction_numerical': 0.0005377066810055115, 'mean_word_length': 2.7785678009141694, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3756638', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1665, 'n_words': 405}