<commit_before>﻿using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Windows.Data;
using Microsoft.VisualStudio.Imaging;
using Microsoft.VisualStudio.Imaging.Interop;
using Microsoft.VisualStudio.Shell;

namespace GitHub.VisualStudio.Views.GitHubPane
{
    internal sealed class FileNameToImageMonikerConverter : IValueConverter
    {
        private const string shellFileAssociations = "ShellFileAssociations";
        private const string defaultIconMoniker = "DefaultIconMoniker";
        private const string knownMonikersPrefix = "KnownMonikers.";

        private readonly Package package;

        public FileNameToImageMonikerConverter()
        {
            package = (Package)Services.GitHubServiceProvider.GetService(typeof(Package));
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return TryGetIcon((string)value) ?? KnownMonikers.Document;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        private ImageMoniker? TryGetIcon(string fileName)
        {
            var extension = Path.GetExtension(fileName);

            if (string.IsNullOrEmpty(extension))
                return null;

            using (var key = package.ApplicationRegistryRoot.OpenSubKey(shellFileAssociations + "\\" + extension))
            {
                if (key?.GetValue(defaultIconMoniker) is string str)
                    return TryParseImageMoniker(str);
            }

            return null;
        }

        private static ImageMoniker? TryParseImageMoniker(string str)
        {
            // This is the common case: KnownMonikers.Foo

            if (str.StartsWith(knownMonikersPrefix, StringComparison.Ordinal))
            {
                var propertyName = str.Substring(knownMonikersPrefix.Length);
                var property = typeof(KnownMonikers).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Static);

                return property?.GetValue(null) as ImageMoniker?;
            }

            // Custom icon - this will look like: cb4a8fc6-efe7-424a-b611-23adf22b568e:6

            if (ImagingUtilities.TryParseImageMoniker(str, out var imageMoniker))
                return imageMoniker;

            return null;
        }
    }
}<commit_msg>Removing C# 7 language features<commit_after>﻿using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Windows.Data;
using Microsoft.VisualStudio.Imaging;
using Microsoft.VisualStudio.Imaging.Interop;
using Microsoft.VisualStudio.Shell;

namespace GitHub.VisualStudio.Views.GitHubPane
{
    internal sealed class FileNameToImageMonikerConverter : IValueConverter
    {
        private const string shellFileAssociations = "ShellFileAssociations";
        private const string defaultIconMoniker = "DefaultIconMoniker";
        private const string knownMonikersPrefix = "KnownMonikers.";

        private readonly Package package;

        public FileNameToImageMonikerConverter()
        {
            package = (Package)Services.GitHubServiceProvider.GetService(typeof(Package));
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return TryGetIcon((string)value) ?? KnownMonikers.Document;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        private ImageMoniker? TryGetIcon(string fileName)
        {
            var extension = Path.GetExtension(fileName);

            if (string.IsNullOrEmpty(extension))
                return null;

            using (var key = package.ApplicationRegistryRoot.OpenSubKey(shellFileAssociations + "\\" + extension))
            {
                var str = key?.GetValue(defaultIconMoniker) as string;
                if (str != null)
                    return TryParseImageMoniker(str);
            }

            return null;
        }

        private static ImageMoniker? TryParseImageMoniker(string str)
        {
            // This is the common case: KnownMonikers.Foo

            if (str.StartsWith(knownMonikersPrefix, StringComparison.Ordinal))
            {
                var propertyName = str.Substring(knownMonikersPrefix.Length);
                var property = typeof(KnownMonikers).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Static);

                return property?.GetValue(null) as ImageMoniker?;
            }

            // Custom icon - this will look like: cb4a8fc6-efe7-424a-b611-23adf22b568e:6

            ImageMoniker imageMoniker;
            if (ImagingUtilities.TryParseImageMoniker(str, out imageMoniker))
                return imageMoniker;

            return null;
        }
    }
}
{'fraction_non_alphanumeric': 0.07477009196321471, 'fraction_numerical': 0.00739704118352659, 'mean_word_length': 2.452726017943409, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6002301', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1217, 'n_words': 286}