<commit_before>﻿namespace ResXManager.VSIX
{
    using System;
    using System.Collections.Specialized;
    using System.Composition;
    using System.Linq;
    using Community.VisualStudio.Toolkit;
    using Microsoft.VisualStudio.Shell;

    using ResXManager.Infrastructure;
    using ResXManager.Model;
    using ResXManager.VSIX.Visuals;

    using TomsToolbox.Composition;

    using static Microsoft.VisualStudio.Shell.ThreadHelper;


    [Export]
    internal sealed class ErrorProvider : IDisposable
    {
        private readonly ResourceManager _resourceManager;
        private readonly VsixShellViewModel _shellViewModel;
        private readonly DteConfiguration _configuration;
        private readonly ErrorListProvider _errorListProvider;
        private readonly TaskProvider.TaskCollection _tasks;

        [ImportingConstructor]
        public ErrorProvider(
            [Import(nameof(VsPackage))][Ninject.Named(nameof(VsPackage))] IServiceProvider serviceProvider,
            ResourceManager resourceManager,
            VsixShellViewModel shellViewModel,
            ITracer tracer,
            DteConfiguration configuration)
        {
            _resourceManager = resourceManager;
            _shellViewModel = shellViewModel;
            _configuration = configuration;

            resourceManager.TableEntries.CollectionChanged += TableEntries_CollectionChanged;

            _errorListProvider = new ErrorListProvider(serviceProvider)
            {
                ProviderName = "ResX Resource Manager",
                ProviderGuid = new Guid("36C23699-DA00-4D2C-8233-5484A091BFD2")
            };

            _tasks = _errorListProvider.Tasks;

            VS.Events.BuildEvents.SolutionBuildStarted += BuildEvents_SolutionBuildStarted;
        }

        private void TableEntries_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action != NotifyCollectionChangedAction.Remove)
                return;

            foreach (var removed in e.OldItems.OfType<ResourceTableEntry>())
            {
                var task = _tasks.OfType<ResourceErrorTask>().FirstOrDefault(t => t.Entry == removed);

                if (task == null)
                    continue;

                try
                {
                    TryRemoveTask(task);
                }
                catch (MissingMethodException)
                {
                    // BUG in VS2022: missing method Microsoft.VisualStudio.Shell.TaskProvider.TaskCollection.Remove
                }
            }
        }

        private void TryRemoveTask(ResourceErrorTask task)
        {
            _tasks.Remove(task);
        }

        public static void Register(IExportProvider exportProvider)
        {
            try
            {
                exportProvider.GetExportedValue<ErrorProvider>();
            }
            catch
            {
                // we had loader errors, ignore this... 
            }
        }

        private void BuildEvents_SolutionBuildStarted(object sender, EventArgs e)
        {
            try
            {
                BuildStarted();
            }<commit_msg>make issues easier to reproduce<commit_after>﻿namespace ResXManager.VSIX
{
    using System;
    using System.Collections.Specialized;
    using System.Composition;
    using System.Linq;
    using Community.VisualStudio.Toolkit;
    using Microsoft.VisualStudio.Shell;

    using ResXManager.Infrastructure;
    using ResXManager.Model;
    using ResXManager.VSIX.Visuals;

    using TomsToolbox.Composition;

    using static Microsoft.VisualStudio.Shell.ThreadHelper;


    [Export]
    internal sealed class ErrorProvider : IDisposable
    {
        private readonly ResourceManager _resourceManager;
        private readonly VsixShellViewModel _shellViewModel;
        private readonly DteConfiguration _configuration;
        private readonly ErrorListProvider _errorListProvider;
        private readonly TaskProvider.TaskCollection _tasks;

        [ImportingConstructor]
        public ErrorProvider(
            [Import(nameof(VsPackage))][Ninject.Named(nameof(VsPackage))] IServiceProvider serviceProvider,
            ResourceManager resourceManager,
            VsixShellViewModel shellViewModel,
            ITracer tracer,
            DteConfiguration configuration)
        {
            _resourceManager = resourceManager;
            _shellViewModel = shellViewModel;
            _configuration = configuration;

            resourceManager.TableEntries.CollectionChanged += TableEntries_CollectionChanged;

            _errorListProvider = new ErrorListProvider(serviceProvider)
            {
                ProviderName = "ResX Resource Manager",
                ProviderGuid = new Guid("36C23699-DA00-4D2C-8233-5484A091BFD2")
            };

            _tasks = _errorListProvider.Tasks;

            VS.Events.BuildEvents.SolutionBuildStarted += BuildEvents_SolutionBuildStarted;
        }

        private void TableEntries_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
        {
            try
            {
                TableEntries_CollectionChanged(e);
            }
            catch (MissingMethodException)
            {
                // BUG in VS2022: missing method Microsoft.VisualStudio.Shell.TaskProvider.TaskCollection.Remove
            }
        }

        private void TableEntries_CollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (e.Action != NotifyCollectionChangedAction.Remove)
                return;

            foreach (var removed in e.OldItems.OfType<ResourceTableEntry>())
            {
                var task = _tasks.OfType<ResourceErrorTask>().FirstOrDefault(t => t.Entry == removed);

                if (task == null)
                    continue;

                _tasks.Remove(task);
            }
        }

        public static void Register(IExportProvider exportProvider)
        {
            try
            {
                exportProvider.GetExportedValue<ErrorProvider>();
            }
            catch
            {
                // we had loader errors, ignore this... 
            }
        }

        private void BuildEvents_SolutionBuildStarted(object sender, EventArgs e)
        {
            try
            {
                BuildStarted();
            }
{'fraction_non_alphanumeric': 0.06347211311861745, 'fraction_numerical': 0.008483896307934015, 'mean_word_length': 1.943134535367545, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '3816021', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1446, 'n_words': 300}