<commit_before>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.ConflictMarkerResolution
{
    internal abstract partial class AbstractResolveConflictMarkerCodeFixProvider : CodeFixProvider
    {
        private const string TakeTopEquivalenceKey = nameof(TakeTopEquivalenceKey);
        private const string TakeBottomEquivalenceKey = nameof(TakeBottomEquivalenceKey);
        private const string TakeBothEquivalenceKey = nameof(TakeBothEquivalenceKey);

        private static readonly int s_mergeConflictLength = "<<<<<<<".Length;

        private readonly ISyntaxKinds _syntaxKinds;

        protected AbstractResolveConflictMarkerCodeFixProvider(
            ISyntaxKinds syntaxKinds, string diagnosticId)
        {
            FixableDiagnosticIds = ImmutableArray.Create(diagnosticId);
            _syntaxKinds = syntaxKinds;
        }

        public override ImmutableArray<string> FixableDiagnosticIds { get; }

        public override FixAllProvider GetFixAllProvider()
            => new ConflictMarkerFixAllProvider(this);

        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var cancellationToken = context.CancellationToken;
            var document = context.Document;
            var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
            var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);

            var startTrivia = root.FindTrivia(context.Span.Start);
            if (!IsConflictMarker(text, startTrivia, '<'))
                return;

            var conflictTrivia = TryGetConflictTrivia(text, startTrivia);
            if (conflictTrivia == null)
                return;

            var (equalsTrivia, endTrivia) = conflictTrivia.Value;
            RegisterCodeFixes(context, startTrivia, equalsTrivia, endTrivia);
        }

        private (SyntaxTrivia equalsTrivia, SyntaxTrivia endTrivia)? TryGetConflictTrivia(
            SourceText text, SyntaxTrivia startTrivia)
        {
            var token = startTrivia.Token;

            while (true)
            {
                var index = GetEqualsConflictMarkerIndex(text, token, afterPosition: startTrivia.SpanStart);
                if (index >= 0)
                {
                    var leadingTrivia = token.LeadingTrivia;

                    if (index + 3 < token.LeadingTrivia.Count)
                    {
                        // normal case where there us =====, then dead code, then >>>>>>

                        var equalsTrivia = leadingTrivia[index];
                        var endOfLineTrivia = leadingTrivia[index + 1];
                        var disabledTrivia = leadingTrivia[index + 2];
                        var endTrivia = leadingTrivia[index + 3];

                        if (_syntaxKinds.EndOfLineTrivia == endOfLineTrivia.RawKind &&
                            _syntaxKinds.DisabledTextTrivia == disabledTrivia.RawKind &&
                            IsConflictMarker(text, endTrivia, '>'))
                        {
                            return (equalsTrivia, endTrivia);
                        }
                    }<commit_msg>Simplify<commit_after>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.ConflictMarkerResolution
{
    internal abstract partial class AbstractResolveConflictMarkerCodeFixProvider : CodeFixProvider
    {
        private const string TakeTopEquivalenceKey = nameof(TakeTopEquivalenceKey);
        private const string TakeBottomEquivalenceKey = nameof(TakeBottomEquivalenceKey);
        private const string TakeBothEquivalenceKey = nameof(TakeBothEquivalenceKey);

        private static readonly int s_mergeConflictLength = "<<<<<<<".Length;

        private readonly ISyntaxKinds _syntaxKinds;

        protected AbstractResolveConflictMarkerCodeFixProvider(
            ISyntaxKinds syntaxKinds, string diagnosticId)
        {
            FixableDiagnosticIds = ImmutableArray.Create(diagnosticId);
            _syntaxKinds = syntaxKinds;
        }

        public override ImmutableArray<string> FixableDiagnosticIds { get; }

        public override FixAllProvider GetFixAllProvider()
            => new ConflictMarkerFixAllProvider(this);

        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var cancellationToken = context.CancellationToken;
            var document = context.Document;
            var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
            var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);

            var startTrivia = root.FindTrivia(context.Span.Start);
            if (!IsConflictMarker(text, startTrivia, '<'))
                return;

            var conflictTrivia = TryGetConflictTrivia(text, startTrivia);
            if (conflictTrivia == null)
                return;

            var (equalsTrivia, endTrivia) = conflictTrivia.Value;
            RegisterCodeFixes(context, startTrivia, equalsTrivia, endTrivia);
        }

        private (SyntaxTrivia equalsTrivia, SyntaxTrivia endTrivia)? TryGetConflictTrivia(SourceText text, SyntaxTrivia startTrivia)
        {
            var token = startTrivia.Token;

            while (true)
            {
                var index = GetEqualsConflictMarkerIndex(text, token, afterPosition: startTrivia.SpanStart);
                if (index >= 0)
                {
                    var leadingTrivia = token.LeadingTrivia;

                    if (index + 3 < token.LeadingTrivia.Count)
                    {
                        // normal case where there us =====, then dead code, then >>>>>>

                        var equalsTrivia = leadingTrivia[index];
                        var endOfLineTrivia = leadingTrivia[index + 1];
                        var disabledTrivia = leadingTrivia[index + 2];
                        var endTrivia = leadingTrivia[index + 3];

                        if (_syntaxKinds.EndOfLineTrivia == endOfLineTrivia.RawKind &&
                            _syntaxKinds.DisabledTextTrivia == disabledTrivia.RawKind &&
                            IsConflictMarker(text, endTrivia, '>'))
                        {
                            return (equalsTrivia, endTrivia);
                        }
                    }
{'fraction_non_alphanumeric': 0.07037427517132314, 'fraction_numerical': 0.0013178703215603585, 'mean_word_length': 2.3788958147818344, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '3877779', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1873, 'n_words': 443}