<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.Globalization;
using System.IO;
using System.Linq;
using System.Xml;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeMetrics;

namespace Metrics
{
    internal static class MetricsOutputWriter
    {
        private const string Version = "1.0";

        public static void WriteMetricFile(ImmutableArray<(string, CodeAnalysisMetricData)> data, XmlTextWriter writer)
        {
            writer.Formatting = Formatting.Indented;

            writer.WriteStartDocument();
            writer.WriteStartElement("CodeMetricsReport");
            writer.WriteAttributeString("Version", Version);
            writer.WriteStartElement("Targets");
            writeMetrics();
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();

            return;

            void writeMetrics()
            {
                foreach (var kvp in data)
                {
                    string filePath = kvp.Item1;
                    CodeAnalysisMetricData metric = kvp.Item2;

                    writer.WriteStartElement("Target");
                    writer.WriteAttributeString("Name", Path.GetFileName(filePath));

                    WriteMetricData(metric, writer);

                    writer.WriteEndElement();
                }
            }
        }

        private static void WriteMetricData(CodeAnalysisMetricData data, XmlTextWriter writer)
        {
            writeHeader();
            writeMetrics();
            writeChildren();
            writer.WriteEndElement();

            return;

            void writeHeader()
            {
                writer.WriteStartElement(data.Symbol.Kind.ToString());
                switch (data.Symbol.Kind)
                {
                    case SymbolKind.NamedType:
                        writer.WriteAttributeString("Name", data.Symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat));
                        break;

                    case SymbolKind.Method:
                    case SymbolKind.Field:<commit_msg>Update type name calculation<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.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeMetrics;

namespace Metrics
{
    internal static class MetricsOutputWriter
    {
        private const string Version = "1.0";

        public static void WriteMetricFile(ImmutableArray<(string, CodeAnalysisMetricData)> data, XmlTextWriter writer)
        {
            writer.Formatting = Formatting.Indented;

            writer.WriteStartDocument();
            writer.WriteStartElement("CodeMetricsReport");
            writer.WriteAttributeString("Version", Version);
            writer.WriteStartElement("Targets");
            writeMetrics();
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();

            return;

            void writeMetrics()
            {
                foreach (var kvp in data)
                {
                    string filePath = kvp.Item1;
                    CodeAnalysisMetricData metric = kvp.Item2;

                    writer.WriteStartElement("Target");
                    writer.WriteAttributeString("Name", Path.GetFileName(filePath));

                    WriteMetricData(metric, writer);

                    writer.WriteEndElement();
                }
            }
        }

        private static void WriteMetricData(CodeAnalysisMetricData data, XmlTextWriter writer)
        {
            writeHeader();
            writeMetrics();
            writeChildren();
            writer.WriteEndElement();

            return;

            void writeHeader()
            {
                writer.WriteStartElement(data.Symbol.Kind.ToString());
                switch (data.Symbol.Kind)
                {
                    case SymbolKind.NamedType:
                        var minimalTypeName = new StringBuilder(data.Symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));

                        var containingType = data.Symbol.ContainingType;
                        while (containingType != null)
                        {
                            minimalTypeName.Insert(0, ".");
                            minimalTypeName.Insert(0, containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
                        }

                        writer.WriteAttributeString("Name", minimalTypeName.ToString());
                        break;

                    case SymbolKind.Method:
                    case SymbolKind.Field:
{'fraction_non_alphanumeric': 0.07919621749408984, 'fraction_numerical': 0.0027580772261623326, 'mean_word_length': 1.7712882096069869, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1775156', 'n_tokens_mistral': 1162, 'n_tokens_neox': 1095, 'n_words': 243}