<commit_before>﻿using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Tree;
using Repository.EditorServices.Internal.SyntaxHighlighting;
using Repository.EditorServices.SyntaxHighlighting;
using static Repository.EditorServices.Internal.Languages.Java.SyntaxHighlighting.JavaParser;

namespace Repository.EditorServices.Internal.Languages.Java.SyntaxHighlighting
{
    internal partial class JavaSyntaxHighlighter
    {
        private partial class Visitor
        {
            private static SyntaxReplacement AnnotationNameReplacement { get; } =
                SyntaxReplacement.Create(NodePath.Create(typeof(QualifiedNameContext), typeof(TerminalNodeImpl)), SyntaxKind.Annotation);

            private static SyntaxReplacement AnnotationTypeDeclarationReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.TypeDeclaration);

            private static SyntaxReplacement ClassDeclarationReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.TypeDeclaration);

            private static SyntaxReplacement ClassOrInterfaceTypeReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.TypeIdentifier);

            private static SyntaxReplacement ConstructorDeclarationReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.ConstructorDeclaration);

            private static SyntaxReplacement CreatedNameReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.TypeIdentifier);

            private static SyntaxReplacement EnumDeclarationReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.TypeDeclaration);

            private static SyntaxReplacement InterfaceDeclarationReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.TypeDeclaration);

            private static SyntaxReplacement MethodDeclarationReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.MethodDeclaration);

            private static ImmutableArray<SyntaxReplacement> MethodInvocationReplacements { get; } =
                ImmutableArray.Create(
                    SyntaxReplacement.Create(NodePath.Create(typeof(PrimaryExpressionContext), typeof(PrimaryContext), typeof(TerminalNodeImpl)), SyntaxKind.MethodIdentifier),
                    SyntaxReplacement.Create(NodePath.Create(typeof(MemberAccessContext), typeof(TerminalNodeImpl)), SyntaxKind.MethodIdentifier));

            private static SyntaxReplacement TypeParameterReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.TypeIdentifier);

            private static SyntaxReplacement VariableDeclaratorIdReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.ParameterDeclaration);

            public override object VisitAnnotationName([NotNull] AnnotationNameContext context)
                => VisitChildren(context, AnnotationNameReplacement);

            // TODO: Do annotation type declarations, which use '@interface', need special treatment?
            public override object VisitAnnotationTypeDeclaration([NotNull] AnnotationTypeDeclarationContext context)
                => VisitChildren(context, AnnotationTypeDeclarationReplacement);

            public override object VisitClassDeclaration([NotNull] ClassDeclarationContext context)
                => VisitChildren(context, ClassDeclarationReplacement);

            public override object VisitClassOrInterfaceType([NotNull] ClassOrInterfaceTypeContext context)
                => VisitChildren(context, ClassOrInterfaceTypeReplacement);

            public override object VisitConstructorDeclaration([NotNull] ConstructorDeclarationContext context)
                => VisitChildren(context, ConstructorDeclarationReplacement);

            public override object VisitCreatedName([NotNull] CreatedNameContext context)
                => VisitChildren(context, CreatedNameReplacement);

            public override object VisitEnumDeclaration([NotNull] EnumDeclarationContext context)
                => VisitChildren(context, EnumDeclarationReplacement);

            public override object VisitInterfaceDeclaration([NotNull] InterfaceDeclarationContext context)
                => VisitChildren(context, InterfaceDeclarationReplacement);

            public override object VisitMethodDeclaration([NotNull] MethodDeclarationContext context)
                => VisitChildren(context, MethodDeclarationReplacement);

            public override object VisitMethodInvocation([NotNull] MethodInvocationContext context)
                => VisitChildren(context, MethodInvocationReplacements);

            public override object VisitTerminal(ITerminalNode node)
            {
                var replacementKind = FindTerminalReplacement(node).Kind;
                Advance(node, replacementKind);
                return null;
            }

            public override object VisitTypeParameter([NotNull] TypeParameterContext context)
                => VisitChildren(context, TypeParameterReplacement);

            // TODO: Affects declarators in try-with-resources and enhanced for loop, not only parameters.
            public override object VisitVariableDeclaratorId([NotNull] VariableDeclaratorIdContext context)
                => VisitChildren(context, VariableDeclaratorIdReplacement);

            private SyntaxReplacement FindTerminalReplacement(ITerminalNode node)
            {
                var nodePath = NodePath.GetRelativePath(_lastAncestor, node);
                // The last replacements are the ones that were added most recently, by closer ancestors. Give precedence to those.
                for (int i = _replacements.Length - 1; i >= 0; i--)
                {
                    var replacement = _replacements[i];
                    if (nodePath.Equals(replacement.Path))
                    {
                        return replacement;
                    }
                }

                return SyntaxReplacement.None;
            }

            private ImmutableArray<SyntaxReplacement>.Builder GetApplicableReplacements(IParseTree tree)
            {
                var treePath = NodePath.GetRelativePath(_lastAncestor, tree);
                var builder = ImmutableArray.CreateBuilder<SyntaxReplacement>();
<commit_msg>[Java.SyntaxHighlighting] Only parameters should be colored orange<commit_after>﻿using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Tree;
using Repository.EditorServices.Internal.SyntaxHighlighting;
using Repository.EditorServices.SyntaxHighlighting;
using static Repository.EditorServices.Internal.Languages.Java.SyntaxHighlighting.JavaParser;

namespace Repository.EditorServices.Internal.Languages.Java.SyntaxHighlighting
{
    internal partial class JavaSyntaxHighlighter
    {
        private partial class Visitor
        {
            private static SyntaxReplacement AnnotationNameReplacement { get; } =
                SyntaxReplacement.Create(NodePath.Create(typeof(QualifiedNameContext), typeof(TerminalNodeImpl)), SyntaxKind.Annotation);

            private static SyntaxReplacement AnnotationTypeDeclarationReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.TypeDeclaration);

            private static SyntaxReplacement ClassDeclarationReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.TypeDeclaration);

            private static SyntaxReplacement ClassOrInterfaceTypeReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.TypeIdentifier);

            private static SyntaxReplacement ConstructorDeclarationReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.ConstructorDeclaration);

            private static SyntaxReplacement CreatedNameReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.TypeIdentifier);

            private static SyntaxReplacement EnumDeclarationReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.TypeDeclaration);

            private static SyntaxReplacement FormalParameterReplacement { get; } =
                SyntaxReplacement.Create(NodePath.Create(typeof(VariableDeclaratorIdContext), typeof(TerminalNodeImpl)), SyntaxKind.ParameterDeclaration);

            private static SyntaxReplacement InterfaceDeclarationReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.TypeDeclaration);

            private static SyntaxReplacement LastFormalParameterReplacement { get; } =
                SyntaxReplacement.Create(NodePath.Create(typeof(VariableDeclaratorIdContext), typeof(TerminalNodeImpl)), SyntaxKind.ParameterDeclaration);

            private static SyntaxReplacement MethodDeclarationReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.MethodDeclaration);

            private static ImmutableArray<SyntaxReplacement> MethodInvocationReplacements { get; } =
                ImmutableArray.Create(
                    SyntaxReplacement.Create(NodePath.Create(typeof(PrimaryExpressionContext), typeof(PrimaryContext), typeof(TerminalNodeImpl)), SyntaxKind.MethodIdentifier),
                    SyntaxReplacement.Create(NodePath.Create(typeof(MemberAccessContext), typeof(TerminalNodeImpl)), SyntaxKind.MethodIdentifier));

            private static SyntaxReplacement TypeParameterReplacement { get; } =
                SyntaxReplacement.Terminal(SyntaxKind.TypeIdentifier);

            public override object VisitAnnotationName([NotNull] AnnotationNameContext context)
                => VisitChildren(context, AnnotationNameReplacement);

            // TODO: Do annotation type declarations, which use '@interface', need special treatment?
            public override object VisitAnnotationTypeDeclaration([NotNull] AnnotationTypeDeclarationContext context)
                => VisitChildren(context, AnnotationTypeDeclarationReplacement);

            public override object VisitClassDeclaration([NotNull] ClassDeclarationContext context)
                => VisitChildren(context, ClassDeclarationReplacement);

            public override object VisitClassOrInterfaceType([NotNull] ClassOrInterfaceTypeContext context)
                => VisitChildren(context, ClassOrInterfaceTypeReplacement);

            public override object VisitConstructorDeclaration([NotNull] ConstructorDeclarationContext context)
                => VisitChildren(context, ConstructorDeclarationReplacement);

            public override object VisitCreatedName([NotNull] CreatedNameContext context)
                => VisitChildren(context, CreatedNameReplacement);

            public override object VisitEnumDeclaration([NotNull] EnumDeclarationContext context)
                => VisitChildren(context, EnumDeclarationReplacement);

            public override object VisitFormalParameter([NotNull] FormalParameterContext context)
                => VisitChildren(context, FormalParameterReplacement);

            public override object VisitInterfaceDeclaration([NotNull] InterfaceDeclarationContext context)
                => VisitChildren(context, InterfaceDeclarationReplacement);

            public override object VisitLastFormalParameter([NotNull] LastFormalParameterContext context)
                => VisitChildren(context, LastFormalParameterReplacement);

            public override object VisitMethodDeclaration([NotNull] MethodDeclarationContext context)
                => VisitChildren(context, MethodDeclarationReplacement);

            public override object VisitMethodInvocation([NotNull] MethodInvocationContext context)
                => VisitChildren(context, MethodInvocationReplacements);

            public override object VisitTerminal(ITerminalNode node)
            {
                var replacementKind = FindTerminalReplacement(node).Kind;
                Advance(node, replacementKind);
                return null;
            }

            public override object VisitTypeParameter([NotNull] TypeParameterContext context)
                => VisitChildren(context, TypeParameterReplacement);

            private SyntaxReplacement FindTerminalReplacement(ITerminalNode node)
            {
                var nodePath = NodePath.GetRelativePath(_lastAncestor, node);
                // The last replacements are the ones that were added most recently, by closer ancestors. Give precedence to those.
                for (int i = _replacements.Length - 1; i >= 0; i--)
                {
                    var replacement = _replacements[i];
                    if (nodePath.Equals(replacement.Path))
                    {
                        return replacement;
                    }
                }

                return SyntaxReplacement.None;
            }

            private ImmutableArray<SyntaxReplacement>.Builder GetApplicableReplacements(IParseTree tree)
            {
                var treePath = NodePath.GetRelativePath(_lastAncestor, tree);
                var builder = ImmutableArray.CreateBuilder<SyntaxReplacement>();

{'fraction_non_alphanumeric': 0.06323606631160453, 'fraction_numerical': 0.0007501312729727703, 'mean_word_length': 3.0522796352583588, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 36, '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': '2503525', 'n_tokens_mistral': 2940, 'n_tokens_neox': 2749, 'n_words': 610}