<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
    {
        internal const string TakeTopEquivalenceKey = nameof(TakeTopEquivalenceKey);
        internal const string TakeBottomEquivalenceKey = nameof(TakeBottomEquivalenceKey);
        internal 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 lines = text.Lines;

            var position = context.Span.Start;
            if (!ShouldFix(root, text, position, out var startLine, out var middleLine, out var endLine))
                return;

            RegisterCodeFixes(context, startLine, middleLine, endLine);
        }

        private bool ShouldFix(
            SyntaxNode root, SourceText text, int position,
            out TextLine startLine, out TextLine middleLine, out TextLine endLine)
        {
            startLine = default;
            middleLine = default;
            endLine = default;

            var lines = text.Lines;
            var conflictLine = lines.GetLineFromPosition(position);
            if (position != conflictLine.Start)
            {
                Debug.Assert(false, "All conflict markers should start at the beginning of a line.");
                return false;
            }

            if (!TryGetConflictLines(text, position, out startLine, out middleLine, out endLine))
                return false;

            var startTrivia = root.FindTrivia(startLine.Start);
            var middleTrivia = root.FindTrivia(middleLine.Start);<commit_msg>Remove local<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
    {
        internal const string TakeTopEquivalenceKey = nameof(TakeTopEquivalenceKey);
        internal const string TakeBottomEquivalenceKey = nameof(TakeBottomEquivalenceKey);
        internal 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 position = context.Span.Start;
            if (!ShouldFix(root, text, position, out var startLine, out var middleLine, out var endLine))
                return;

            RegisterCodeFixes(context, startLine, middleLine, endLine);
        }

        private bool ShouldFix(
            SyntaxNode root, SourceText text, int position,
            out TextLine startLine, out TextLine middleLine, out TextLine endLine)
        {
            startLine = default;
            middleLine = default;
            endLine = default;

            var lines = text.Lines;
            var conflictLine = lines.GetLineFromPosition(position);
            if (position != conflictLine.Start)
            {
                Debug.Assert(false, "All conflict markers should start at the beginning of a line.");
                return false;
            }

            if (!TryGetConflictLines(text, position, out startLine, out middleLine, out endLine))
                return false;

            var startTrivia = root.FindTrivia(startLine.Start);
            var middleTrivia = root.FindTrivia(middleLine.Start);
{'fraction_non_alphanumeric': 0.06564790269764541, 'fraction_numerical': 0.0, 'mean_word_length': 2.961704756022236, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 7, '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': '2375733', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1522, 'n_words': 441}