<commit_before>﻿using JetBrains.Application.Threading;
using JetBrains.DataFlow;
using JetBrains.Lifetimes;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Plugins.Unity.Settings;
using JetBrains.ReSharper.Psi.Util;

namespace JetBrains.ReSharper.Plugins.Unity.Rider
{
    [SolutionComponent]
    public class UnitySettingsSynchronizer
    {
        public UnitySettingsSynchronizer(Lifetime lifetime, ISolution solution, UnityHost host,
                                         IApplicationWideContextBoundSettingStore settingsStore)
        {
            var boundStore = settingsStore.BoundSettingsStore;
            var entry = boundStore.Schema.GetScalarEntry((UnitySettings s) => s.EnableShaderLabHippieCompletion);
            boundStore.GetValueProperty<bool>(lifetime, entry, null).Change.Advise_HasNew(lifetime, args =>
            {
                solution.Locks.ExecuteOrQueueEx(lifetime, "EnableShaderLabHippieCompletion", () =>
                    host.PerformModelAction(rd => rd.BackendSettings.EnableShaderLabHippieCompletion.Value = args.New));
            });

            var useYamlMergeSetting = boundStore.Schema.GetScalarEntry((UnitySettings s) => s.UseUnityYamlMerge);
            boundStore.GetValueProperty<bool>(lifetime, useYamlMergeSetting, null).Change.Advise_HasNew(lifetime, args =>
            {
                solution.Locks.ExecuteOrQueueEx(lifetime, "UseUnityYamlMerge", () =>
                    host.PerformModelAction(rd => rd.BackendSettings.UseUnityYamlMerge.Value = args.New));
            });

            var mergeParametersSetting = boundStore.Schema.GetScalarEntry((UnitySettings s) => s.MergeParameters);
            boundStore.GetValueProperty<string>(lifetime, mergeParametersSetting, null).Change.Advise_HasNew(lifetime, args =>
            {
                solution.Locks.ExecuteOrQueueEx(lifetime, "MergeParameters", () =>
                    host.PerformModelAction(rd => rd.BackendSettings.MergeParameters.Value = args.New));
            });

            var debuggerExtensionsEnabledSetting =
                boundStore.Schema.GetScalarEntry((UnitySettings s) => s.EnableDebuggerExtensions);
            boundStore.GetValueProperty<bool>(lifetime, debuggerExtensionsEnabledSetting, null).Change.Advise_HasNew(lifetime, args =>
            {
                solution.Locks.ExecuteOrQueueEx(lifetime, "DebuggerExtensionsEnabled", () =>
                    host.PerformModelAction(rd => rd.BackendSettings.EnableDebuggerExtensions.Value = args.New));
            });
        }
    }
}<commit_msg>Refactor UnitySettingsSynchronizer<commit_after>﻿using System;
using System.Linq.Expressions;
using JetBrains.Application.Settings;
using JetBrains.Application.Threading;
using JetBrains.DataFlow;
using JetBrains.Lifetimes;
using JetBrains.ProjectModel;
using JetBrains.Reflection;
using JetBrains.ReSharper.Plugins.Unity.Settings;
using JetBrains.ReSharper.Psi.Util;
using JetBrains.Rider.Model.Unity.FrontendBackend;

namespace JetBrains.ReSharper.Plugins.Unity.Rider
{
    [SolutionComponent]
    public class UnitySettingsSynchronizer
    {
        public UnitySettingsSynchronizer(Lifetime lifetime, ISolution solution, UnityHost host,
                                         IApplicationWideContextBoundSettingStore settingsStore)
        {
            var boundStore = settingsStore.BoundSettingsStore;
            BindSettingToProperty(lifetime, solution, host, boundStore,
                (UnitySettings s) => s.EnableShaderLabHippieCompletion,
                (model, args) => model.BackendSettings.EnableShaderLabHippieCompletion.Value = args.New);

            BindSettingToProperty(lifetime, solution, host, boundStore,
                (UnitySettings s) => s.UseUnityYamlMerge,
                (model, args) => model.BackendSettings.UseUnityYamlMerge.Value = args.New);
            BindSettingToProperty(lifetime, solution, host, boundStore,
                (UnitySettings s) => s.MergeParameters,
                (model, args) => model.BackendSettings.MergeParameters.Value = args.New);

            BindSettingToProperty(lifetime, solution, host, boundStore,
                (UnitySettings s) => s.EnableDebuggerExtensions,
                (model, args) => model.BackendSettings.EnableDebuggerExtensions.Value = args.New);
        }

        private static void BindSettingToProperty<TKeyClass, TEntryMemberType>(Lifetime lifetime, ISolution solution, UnityHost host,
                                                                               IContextBoundSettingsStoreLive boundStore,
                                                                               Expression<Func<TKeyClass, TEntryMemberType>> entry,
                                                                               Action<FrontendBackendModel, PropertyChangedEventArgs<TEntryMemberType>> action)
        {
            var name = entry.GetInstanceMemberName();
            var setting = boundStore.Schema.GetScalarEntry(entry);
            boundStore.GetValueProperty<TEntryMemberType>(lifetime, setting, null).Change.Advise_HasNew(lifetime,
                args =>
                {
                    solution.Locks.ExecuteOrQueueEx(lifetime, name, () =>
                    {
                        host.PerformModelAction(m => action(m, args));
                    });
                });
        }
    }
}
{'fraction_non_alphanumeric': 0.08672994602642844, 'fraction_numerical': 0.0, 'mean_word_length': 2.4626288659793816, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '677962', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1319, 'n_words': 204}