From 6fdaa00d180ef7d81fe45fc23706fcf27c9efabe Mon Sep 17 00:00:00 2001 From: Francisco Requena Date: Mon, 5 Oct 2020 13:16:25 +0200 Subject: [PATCH] refactor: settings --- CHANGELOG.md | 3 + Editor/ExternalCodeEditor.cs | 65 +++++++++-------- Editor/ILauncher.cs | 17 ----- Editor/Launcher.cs | 17 +++++ .../{ILauncher.cs.meta => Launcher.cs.meta} | 0 Editor/LauncherRegistry.cs | 10 +-- Editor/Launchers/GVim.cs | 40 ++++++----- Editor/Launchers/MacVim.cs | 40 ++++++----- Editor/Launchers/SublimeText.cs | 38 +++++----- Editor/Launchers/VimR.cs | 40 ++++++----- Editor/Postprocessor.cs | 4 +- Editor/Preferences.cs | 43 ++++++------ Editor/Setting.cs | 69 +++++++++++++++++++ Editor/Setting.cs.meta | 11 +++ Editor/SyncUtil.cs | 4 +- 15 files changed, 261 insertions(+), 140 deletions(-) delete mode 100644 Editor/ILauncher.cs create mode 100644 Editor/Launcher.cs rename Editor/{ILauncher.cs.meta => Launcher.cs.meta} (100%) create mode 100644 Editor/Setting.cs create mode 100644 Editor/Setting.cs.meta diff --git a/CHANGELOG.md b/CHANGELOG.md index e2071fd..a9cdba9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,9 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Support for VimR on macOS. +- Setting class to define launcher settings. +- Unregister Easy Editor before the assembly reloads. - Documentation. ### Changed +- Launchers inherit from abstract class (Launcher) instead of interface (ILauncher). - Fix changelog links. - Clarify comments on former changelogs. diff --git a/Editor/ExternalCodeEditor.cs b/Editor/ExternalCodeEditor.cs index 7e22708..6284bb7 100644 --- a/Editor/ExternalCodeEditor.cs +++ b/Editor/ExternalCodeEditor.cs @@ -10,10 +10,14 @@ namespace EasyEditor [InitializeOnLoad] internal class ExternalCodeEditor : IExternalCodeEditor { + public static readonly ExternalCodeEditor instance; + static ExternalCodeEditor() { LauncherRegistry.Load(); - CodeEditor.Register(new ExternalCodeEditor()); + instance = new ExternalCodeEditor(); + CodeEditor.Register(instance); + AssemblyReloadEvents.beforeAssemblyReload += OnBeforeAssemblyReload; } public CodeEditor.Installation[] Installations { get; } @@ -36,6 +40,11 @@ private static bool SupportsExtension(string path) return !string.IsNullOrEmpty(extension) && DefaultExtensions.Contains(extension.TrimStart('.')); } + private static void OnBeforeAssemblyReload() + { + CodeEditor.Unregister(instance); + } + public ExternalCodeEditor() { Installations = LauncherRegistry.Installations; @@ -43,7 +52,7 @@ public ExternalCodeEditor() public void Initialize(string editorInstallationPath) { - if (Preferences.IsActive && Preferences.AutoSync) + if (Preferences.IsActive && Preferences.Settings.autoSync.GetBool()) { SyncUtil.Sync(); } @@ -57,42 +66,44 @@ public void OnGUI() } EditorGUI.BeginDisabledGroup(!Preferences.IsActive || !SyncVS.IsValid || SyncUtil.IsReloading || EditorApplication.isCompiling || EditorApplication.isUpdating); - EditorGUI.BeginChangeCheck(); - GUIContent syncContent = new GUIContent( - "Sync solution and project files", - "Forces .sln and .csproj files to be generated and kept in sync."); - bool v = EditorGUILayout.Toggle(syncContent, Preferences.AutoSync); - if (EditorGUI.EndChangeCheck()) { - Preferences.AutoSync = v; - if (v) + EditorGUI.BeginChangeCheck(); + Setting s = Preferences.Settings.autoSync; + GUIContent c = new GUIContent(s.description, s.tooltip); + bool v = EditorGUILayout.Toggle(c, s.GetBool()); + if (EditorGUI.EndChangeCheck()) { - SyncUtil.Sync(); + s.SetBool(v); + if (v) + { + SyncUtil.Sync(); + } + Event.current.Use(); } - Event.current.Use(); } if (!SyncVS.IsValid) { EditorGUILayout.HelpBox("Couldn't retrieve synchronization members. Please contact this package's author.", MessageType.Warning); } - EditorGUI.BeginChangeCheck(); - GUIContent matchCompilerContent = new GUIContent( - "Match compiler version", - "When Unity creates or updates .csproj files, it defines LangVersion as 'latest'. This can create inconsistencies with other .NET platforms (e.g. OmniSharp), which could resolve 'latest' as a different version. By matching compiler version, 'latest' will get resolved as " + Preferences.GetLangVersion() + ". "); - v = EditorGUILayout.Toggle(matchCompilerContent, Preferences.MatchCompilerVersion); - if (EditorGUI.EndChangeCheck()) { - Preferences.MatchCompilerVersion = v; - if (v) + EditorGUI.BeginChangeCheck(); + Setting s = Preferences.Settings.matchCompilerVersion; + GUIContent c = new GUIContent(s.description, s.tooltip); + bool v = EditorGUILayout.Toggle(c, s.GetBool()); + if (EditorGUI.EndChangeCheck()) { - SyncUtil.Sync(); + s.SetBool(v); + if (v) + { + SyncUtil.Sync(); + } + Event.current.Use(); } - Event.current.Use(); } string editorPath = CodeEditor.CurrentEditorInstallation.Trim(); - ILauncher launcher = LauncherRegistry.GetLauncher(editorPath); + Launcher launcher = LauncherRegistry.GetLauncher(editorPath); if (launcher != null) { launcher.OnGUI(); @@ -120,9 +131,9 @@ public bool OpenProject(string filePath = "", int line = -1, int column = -1) string editorPath = CodeEditor.CurrentEditorInstallation.Trim(); - LaunchDescriptor descriptor = new LaunchDescriptor(filePath, line, column, Preferences.ProjectPath, Preferences.ProjectName); + LaunchDescriptor descriptor = new LaunchDescriptor(filePath, line, column, Preferences.projectPath, Preferences.projectName); - ILauncher launcher = LauncherRegistry.GetLauncher(editorPath); + Launcher launcher = LauncherRegistry.GetLauncher(editorPath); if (launcher != null) { _ = launcher.Launch(editorPath, descriptor); @@ -135,7 +146,7 @@ public void SyncAll() { SyncUtil.Sync(); string editorPath = CodeEditor.CurrentEditorInstallation.Trim(); - ILauncher launcher = LauncherRegistry.GetLauncher(editorPath); + Launcher launcher = LauncherRegistry.GetLauncher(editorPath); if (launcher != null) { launcher.SyncAll(); @@ -146,7 +157,7 @@ public void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] mo { SyncUtil.Sync(); string editorPath = CodeEditor.CurrentEditorInstallation.Trim(); - ILauncher launcher = LauncherRegistry.GetLauncher(editorPath); + Launcher launcher = LauncherRegistry.GetLauncher(editorPath); if (launcher != null) { launcher.SyncIfNeeded(addedFiles, deletedFiles, movedFiles, movedFromFiles, importedFiles); diff --git a/Editor/ILauncher.cs b/Editor/ILauncher.cs deleted file mode 100644 index 95ea255..0000000 --- a/Editor/ILauncher.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace EasyEditor -{ - using Unity.CodeEditor; - - internal interface ILauncher - { - CodeEditor.Installation[] Installations { get; } - - bool Launch(string editorPath, LaunchDescriptor descriptor); - void SyncAll(); - void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles); - bool MatchesExecutable(string editorPath); - - void OnGUI(); - } -} - diff --git a/Editor/Launcher.cs b/Editor/Launcher.cs new file mode 100644 index 0000000..fe39974 --- /dev/null +++ b/Editor/Launcher.cs @@ -0,0 +1,17 @@ +namespace EasyEditor +{ + using Unity.CodeEditor; + + internal abstract class Launcher + { + public abstract CodeEditor.Installation[] Installations { get; } + + public abstract bool Launch(string editorPath, LaunchDescriptor descriptor); + public abstract void SyncAll(); + public abstract void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles); + public abstract bool MatchesExecutable(string editorPath); + + public abstract void OnGUI(); + } +} + diff --git a/Editor/ILauncher.cs.meta b/Editor/Launcher.cs.meta similarity index 100% rename from Editor/ILauncher.cs.meta rename to Editor/Launcher.cs.meta diff --git a/Editor/LauncherRegistry.cs b/Editor/LauncherRegistry.cs index 4a019c4..653995c 100644 --- a/Editor/LauncherRegistry.cs +++ b/Editor/LauncherRegistry.cs @@ -11,15 +11,15 @@ internal static class LauncherRegistry { public static CodeEditor.Installation[] Installations { get; set; } public static string LoadErrors { get; private set; } - private static readonly Dictionary launchers = new Dictionary(); + private static readonly Dictionary launchers = new Dictionary(); static LauncherRegistry() { } - public static ILauncher GetLauncher(string editorPath) + public static Launcher GetLauncher(string editorPath) { - _ = launchers.TryGetValue(editorPath, out ILauncher launcher); + _ = launchers.TryGetValue(editorPath, out Launcher launcher); return launcher; } @@ -31,9 +31,9 @@ public static void Load() Assembly assembly = System.AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == "EasyEditor.Launchers"); Debug.Assert(assembly != null, "Couldn't find the EasyEditor.Launchers assembly"); - foreach (System.Type type in assembly.GetTypes().Where(t => t.IsClass && typeof(ILauncher).IsAssignableFrom(t))) + foreach (System.Type type in assembly.GetTypes().Where(t => t.IsClass && typeof(Launcher).IsAssignableFrom(t))) { - ILauncher instance = (ILauncher)System.Activator.CreateInstance(type); + Launcher instance = (Launcher)System.Activator.CreateInstance(type); if (instance.Installations == null) { diff --git a/Editor/Launchers/GVim.cs b/Editor/Launchers/GVim.cs index 5532a6e..41e428a 100644 --- a/Editor/Launchers/GVim.cs +++ b/Editor/Launchers/GVim.cs @@ -7,12 +7,19 @@ namespace EasyEditor.Launchers using UnityEditor; using UnityEngine; - internal class GVim : ILauncher + internal class GVim : Launcher { - private static readonly string PrefPrefix = $"{typeof(GVim).FullName}"; - private static readonly string PrefRestartOmniSharp = $"{PrefPrefix}.RestartOmniSharp"; + [InitializeOnLoad] + private static class Settings + { + public static readonly Setting restartOmniSharp = new Setting( + "RestartOmniSharp", + "Restart OmniSharp server on sync", + "Upon synchronization, send a command (--remote-send) to the gVim server to trigger a OmniSharp server reload. omnisharp-vim required.", + "GVim"); + } - public CodeEditor.Installation[] Installations => new CodeEditor.Installation[] + public override CodeEditor.Installation[] Installations => new CodeEditor.Installation[] { #if UNITY_EDITOR_WIN new CodeEditor.Installation() @@ -28,7 +35,7 @@ internal class GVim : ILauncher #endif }; - public bool Launch(string editorPath, LaunchDescriptor descriptor) + public override bool Launch(string editorPath, LaunchDescriptor descriptor) { string args = string.Format("--servername \"{0}\" --remote-silent +$(Line) $(File)", descriptor.ProjectName); @@ -51,7 +58,7 @@ public bool Launch(string editorPath, LaunchDescriptor descriptor) private void ReloadOmniSharpServer() { - string args = string.Format("--servername \"{0}\" --remote-send ':silent OmniSharpRestartServer'", Preferences.ProjectName); + string args = string.Format("--servername \"{0}\" --remote-send ':silent OmniSharpRestartServer'", Preferences.projectName); Process process = new Process { @@ -68,23 +75,23 @@ private void ReloadOmniSharpServer() _ = process.Start(); } - public void SyncAll() + public override void SyncAll() { - if (Preferences.IsActive && EditorPrefs.GetBool(PrefRestartOmniSharp)) + if (Preferences.IsActive && Settings.restartOmniSharp.GetBool()) { ReloadOmniSharpServer(); } } - public void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles) + public override void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles) { - if (Preferences.IsActive && EditorPrefs.GetBool(PrefRestartOmniSharp)) + if (Preferences.IsActive && Settings.restartOmniSharp.GetBool()) { ReloadOmniSharpServer(); } } - public bool MatchesExecutable(string editorPath) + public override bool MatchesExecutable(string editorPath) { string vimPath = Path.Combine(Path.GetDirectoryName(editorPath), "vim.exe"); Process process = new Process @@ -109,16 +116,15 @@ public bool MatchesExecutable(string editorPath) return output.StartsWith("VIM - Vi IMproved"); } - public void OnGUI() + public override void OnGUI() { EditorGUI.BeginChangeCheck(); - GUIContent restartOmniSharpContent = new GUIContent( - "Restart OmniSharp server on sync", - "Upon synchronization, send a command (--remote-send) to the gVim server to trigger a OmniSharp server reload. omnisharp-vim required."); - bool b = EditorGUILayout.Toggle(restartOmniSharpContent, EditorPrefs.GetBool(PrefRestartOmniSharp)); + Setting s = Settings.restartOmniSharp; + GUIContent c = new GUIContent(s.description, s.tooltip); + bool b = EditorGUILayout.Toggle(c, s.GetBool()); if (EditorGUI.EndChangeCheck()) { - EditorPrefs.SetBool(PrefRestartOmniSharp, b); + s.SetBool(b); Event.current.Use(); } } diff --git a/Editor/Launchers/MacVim.cs b/Editor/Launchers/MacVim.cs index a66ce61..42e6f1c 100644 --- a/Editor/Launchers/MacVim.cs +++ b/Editor/Launchers/MacVim.cs @@ -6,12 +6,19 @@ namespace EasyEditor.Launchers using UnityEditor; using UnityEngine; - internal class MacVim : ILauncher + internal class MacVim : Launcher { - private static readonly string PrefPrefix = $"{typeof(MacVim).FullName}"; - private static readonly string PrefRestartOmniSharp = $"{PrefPrefix}.RestartOmniSharp"; + [InitializeOnLoad] + private static class Settings + { + public static readonly Setting restartOmniSharp = new Setting( + "RestartOmniSharp", + "Restart OmniSharp server on sync", + "Upon synchronization, send a command (--remote-send) to the Vim server to trigger a OmniSharp server reload. omnisharp-vim required.", + "MacVim"); + } - public CodeEditor.Installation[] Installations => new CodeEditor.Installation[] + public override CodeEditor.Installation[] Installations => new CodeEditor.Installation[] { #if UNITY_EDITOR_OSX new CodeEditor.Installation() @@ -22,7 +29,7 @@ internal class MacVim : ILauncher #endif }; - public bool Launch(string editorPath, LaunchDescriptor descriptor) + public override bool Launch(string editorPath, LaunchDescriptor descriptor) { string args = string.Format("--servername \"{0}\" --remote-silent '+call cursor($(Line),$(Column))' $(File)", descriptor.ProjectName); @@ -45,7 +52,7 @@ public bool Launch(string editorPath, LaunchDescriptor descriptor) private void ReloadOmniSharpServer() { - string args = string.Format("--servername \"{0}\" --remote-send ':silent OmniSharpRestartServer'", Preferences.ProjectName); + string args = string.Format("--servername \"{0}\" --remote-send ':silent OmniSharpRestartServer'", Preferences.projectName); Process process = new Process { @@ -62,23 +69,23 @@ private void ReloadOmniSharpServer() _ = process.Start(); } - public void SyncAll() + public override void SyncAll() { - if (Preferences.IsActive && EditorPrefs.GetBool(PrefRestartOmniSharp)) + if (Preferences.IsActive && Settings.restartOmniSharp.GetBool()) { ReloadOmniSharpServer(); } } - public void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles) + public override void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles) { - if (Preferences.IsActive && EditorPrefs.GetBool(PrefRestartOmniSharp)) + if (Preferences.IsActive && Settings.restartOmniSharp.GetBool()) { ReloadOmniSharpServer(); } } - public bool MatchesExecutable(string editorPath) + public override bool MatchesExecutable(string editorPath) { Process process = new Process { @@ -102,16 +109,15 @@ public bool MatchesExecutable(string editorPath) return output.StartsWith("VIM - Vi IMproved"); } - public void OnGUI() + public override void OnGUI() { EditorGUI.BeginChangeCheck(); - GUIContent restartOmniSharpContent = new GUIContent( - "Restart OmniSharp server on sync", - "Upon synchronization, send a command (--remote-send) to the Vim server to trigger a OmniSharp server reload. omnisharp-vim required."); - bool b = EditorGUILayout.Toggle(restartOmniSharpContent, EditorPrefs.GetBool(PrefRestartOmniSharp)); + Setting s = Settings.restartOmniSharp; + GUIContent c = new GUIContent(s.description, s.tooltip); + bool b = EditorGUILayout.Toggle(c, s.GetBool()); if (EditorGUI.EndChangeCheck()) { - EditorPrefs.SetBool(PrefRestartOmniSharp, b); + s.SetBool(b); Event.current.Use(); } } diff --git a/Editor/Launchers/SublimeText.cs b/Editor/Launchers/SublimeText.cs index 66ea170..7ca079e 100644 --- a/Editor/Launchers/SublimeText.cs +++ b/Editor/Launchers/SublimeText.cs @@ -6,12 +6,19 @@ namespace EasyEditor.Launchers using UnityEditor; using UnityEngine; - internal class SublimeText : ILauncher + internal class SublimeText : Launcher { - private static readonly string PrefPrefix = $"{typeof(SublimeText).FullName}"; - private static readonly string PrefRestartOmniSharp = $"{PrefPrefix}.RestartOmniSharp"; + [InitializeOnLoad] + private static class Settings + { + public static readonly Setting restartOmniSharp = new Setting( + "RestartOmniSharp", + "Restart OmniSharp server on sync", + "Upon synchronization, send a command (--command) to Sublime Text to trigger a OmniSharp server reload. omnisharp-sublime required.", + "SublimeText"); + } - public CodeEditor.Installation[] Installations => new CodeEditor.Installation[] + public override CodeEditor.Installation[] Installations => new CodeEditor.Installation[] { #if UNITY_EDITOR_OSX new CodeEditor.Installation() @@ -28,7 +35,7 @@ internal class SublimeText : ILauncher #endif }; - public bool Launch(string editorPath, LaunchDescriptor descriptor) + public override bool Launch(string editorPath, LaunchDescriptor descriptor) { string args = string.Format("$(File):$(Line):$(Column)", descriptor.ProjectName); @@ -68,23 +75,23 @@ private void ReloadOmniSharpServer() _ = process.Start(); } - public void SyncAll() + public override void SyncAll() { - if (Preferences.IsActive && EditorPrefs.GetBool(PrefRestartOmniSharp)) + if (Preferences.IsActive && Settings.restartOmniSharp.GetBool()) { ReloadOmniSharpServer(); } } - public void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles) + public override void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles) { - if (Preferences.IsActive && EditorPrefs.GetBool(PrefRestartOmniSharp)) + if (Preferences.IsActive && Settings.restartOmniSharp.GetBool()) { ReloadOmniSharpServer(); } } - public bool MatchesExecutable(string editorPath) + public override bool MatchesExecutable(string editorPath) { Process process = new Process { @@ -108,16 +115,15 @@ public bool MatchesExecutable(string editorPath) return output.StartsWith("Sublime Text"); } - public void OnGUI() + public override void OnGUI() { EditorGUI.BeginChangeCheck(); - GUIContent restartOmniSharpContent = new GUIContent( - "Restart OmniSharp server on sync", - "Upon synchronization, send a command (--command) to Sublime Text to trigger a OmniSharp server reload. omnisharp-sublime required."); - bool b = EditorGUILayout.Toggle(restartOmniSharpContent, EditorPrefs.GetBool(PrefRestartOmniSharp)); + Setting s = Settings.restartOmniSharp; + GUIContent c = new GUIContent(s.description, s.tooltip); + bool b = EditorGUILayout.Toggle(c, s.GetBool()); if (EditorGUI.EndChangeCheck()) { - EditorPrefs.SetBool(PrefRestartOmniSharp, b); + s.SetBool(b); Event.current.Use(); } } diff --git a/Editor/Launchers/VimR.cs b/Editor/Launchers/VimR.cs index 9dd9ff6..b5fd808 100644 --- a/Editor/Launchers/VimR.cs +++ b/Editor/Launchers/VimR.cs @@ -6,12 +6,19 @@ namespace EasyEditor.Launchers using UnityEditor; using UnityEngine; - internal class VimR : ILauncher + internal class VimR : Launcher { - private static readonly string PrefPrefix = $"{typeof(VimR).FullName}"; - private static readonly string PrefRestartOmniSharp = $"{PrefPrefix}.RestartOmniSharp"; + [InitializeOnLoad] + private static class Settings + { + public static readonly Setting restartOmniSharp = new Setting( + "RestartOmniSharp", + "Restart OmniSharp server on sync", + "Upon synchronization, execute a command (--cmd) on a new neovim instance (--nvim) to trigger a OmniSharp server reload. omnisharp-vim required.", + "VimR"); + } - public CodeEditor.Installation[] Installations => new CodeEditor.Installation[] + public override CodeEditor.Installation[] Installations => new CodeEditor.Installation[] { #if UNITY_EDITOR_OSX new CodeEditor.Installation() @@ -22,7 +29,7 @@ internal class VimR : ILauncher #endif }; - public bool Launch(string editorPath, LaunchDescriptor descriptor) + public override bool Launch(string editorPath, LaunchDescriptor descriptor) { Process process = new Process { @@ -48,7 +55,7 @@ private void ReloadOmniSharpServer() StartInfo = new ProcessStartInfo { FileName = CodeEditor.CurrentEditorInstallation.Trim(), - Arguments = "-c OmniSharpRestartServer", + Arguments = "--nvim --cmd OmniSharpRestartServer", WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true, UseShellExecute = false, @@ -58,23 +65,23 @@ private void ReloadOmniSharpServer() _ = process.Start(); } - public void SyncAll() + public override void SyncAll() { - if (Preferences.IsActive && EditorPrefs.GetBool(PrefRestartOmniSharp)) + if (Preferences.IsActive && Settings.restartOmniSharp.GetBool()) { ReloadOmniSharpServer(); } } - public void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles) + public override void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles) { - if (Preferences.IsActive && EditorPrefs.GetBool(PrefRestartOmniSharp)) + if (Preferences.IsActive && Settings.restartOmniSharp.GetBool()) { ReloadOmniSharpServer(); } } - public bool MatchesExecutable(string editorPath) + public override bool MatchesExecutable(string editorPath) { Process process = new Process { @@ -98,16 +105,15 @@ public bool MatchesExecutable(string editorPath) return output.StartsWith("usage: vimr"); } - public void OnGUI() + public override void OnGUI() { EditorGUI.BeginChangeCheck(); - GUIContent restartOmniSharpContent = new GUIContent( - "Restart OmniSharp server on sync", - "Upon synchronization, execute a command (-c) on VimR's neovim instance to trigger a OmniSharp server reload. omnisharp-vim required."); - bool b = EditorGUILayout.Toggle(restartOmniSharpContent, EditorPrefs.GetBool(PrefRestartOmniSharp)); + Setting s = Settings.restartOmniSharp; + GUIContent c = new GUIContent(s.description, s.tooltip); + bool b = EditorGUILayout.Toggle(c, s.GetBool()); if (EditorGUI.EndChangeCheck()) { - EditorPrefs.SetBool(PrefRestartOmniSharp, b); + s.SetBool(b); Event.current.Use(); } } diff --git a/Editor/Postprocessor.cs b/Editor/Postprocessor.cs index 8fc9f12..10890da 100644 --- a/Editor/Postprocessor.cs +++ b/Editor/Postprocessor.cs @@ -13,7 +13,7 @@ private static void OnGeneratedCSProjectFiles() return; } - if (Preferences.MatchCompilerVersion) + if (Preferences.Settings.matchCompilerVersion.GetBool()) { WriteLangVersions(); } @@ -21,7 +21,7 @@ private static void OnGeneratedCSProjectFiles() private static void WriteLangVersions() { - foreach (string path in Directory.GetFiles(Preferences.ProjectPath, "*.csproj")) + foreach (string path in Directory.GetFiles(Preferences.projectPath, "*.csproj")) { XmlDocument document = new XmlDocument(); document.Load(path); diff --git a/Editor/Preferences.cs b/Editor/Preferences.cs index 20dde87..f695b7f 100644 --- a/Editor/Preferences.cs +++ b/Editor/Preferences.cs @@ -8,14 +8,33 @@ namespace EasyEditor [InitializeOnLoad] internal static class Preferences { + [InitializeOnLoad] + public static class Settings + { + public static readonly Setting autoSync; + public static readonly Setting matchCompilerVersion; + + static Settings() + { + autoSync = new Setting( + "AutoSync", + "Sync solution and project files", + "Forces .sln and .csproj files to be generated and kept in sync."); + matchCompilerVersion = new Setting( + "MatchCompilerVersion", + "Match compiler version", + "When Unity creates or updates .csproj files, it defines LangVersion as 'latest'. This can create inconsistencies with other .NET platforms (e.g. OmniSharp), which could resolve 'latest' as a different version. By matching compiler version, 'latest' will get resolved as " + GetLangVersion() + ". "); + } + } + static Preferences() { - ProjectPath = Application.dataPath.Substring(0, Application.dataPath.Length - 6); - ProjectName = Path.GetFileName(Path.GetDirectoryName(ProjectPath)); + projectPath = Application.dataPath.Substring(0, Application.dataPath.Length - 6); + projectName = Path.GetFileName(Path.GetDirectoryName(projectPath)); } - public static string ProjectPath { get; private set; } - public static string ProjectName { get; private set; } + public static readonly string projectPath; + public static readonly string projectName; public static bool IsActive => CodeEditor.CurrentEditor is ExternalCodeEditor; @@ -27,22 +46,6 @@ public static string GetLangVersion() return "7.3"; #endif } - - public const string prefPrefix = "EasyEditor."; - - private const string prefMatchCompilerVersion = prefPrefix + "MatchCompilerVersion"; - public static bool MatchCompilerVersion - { - get => EditorPrefs.GetBool(prefMatchCompilerVersion); - set => EditorPrefs.SetBool(prefMatchCompilerVersion, value); - } - - private const string prefAutoSync = prefPrefix + "AutoSync"; - public static bool AutoSync - { - get => EditorPrefs.GetBool(prefAutoSync); - set => EditorPrefs.SetBool(prefAutoSync, value); - } } } diff --git a/Editor/Setting.cs b/Editor/Setting.cs new file mode 100644 index 0000000..38d774e --- /dev/null +++ b/Editor/Setting.cs @@ -0,0 +1,69 @@ +namespace EasyEditor +{ + using UnityEditor; + + internal readonly struct Setting + { + public readonly string id; + public readonly string description; + public readonly string tooltip; + + public readonly string key; + + public Setting(string id, string description, string tooltip, string scope) + { + this.id = id; + this.description = description; + this.tooltip = tooltip; + key = $"{nameof(EasyEditor)}.{scope}.{id}"; + } + + public Setting(string id, string description, string tooltip) + { + this.id = id; + this.description = description; + this.tooltip = tooltip; + key = $"{nameof(EasyEditor)}.{id}"; + } + + public void SetBool(bool value) + { + EditorPrefs.SetBool(key, value); + } + + public bool GetBool() + { + return EditorPrefs.GetBool(key); + } + + public void SetInt(int value) + { + EditorPrefs.SetInt(key, value); + } + + public int GetInt() + { + return EditorPrefs.GetInt(key); + } + + public void SetFloat(float value) + { + EditorPrefs.SetFloat(key, value); + } + + public float GetFloat() + { + return EditorPrefs.GetFloat(key); + } + + public void SetString(string value) + { + EditorPrefs.SetString(key, value); + } + + public string GetString() + { + return EditorPrefs.GetString(key); + } + } +} diff --git a/Editor/Setting.cs.meta b/Editor/Setting.cs.meta new file mode 100644 index 0000000..3e41675 --- /dev/null +++ b/Editor/Setting.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 26b03e9668a88489fbed54a66cae92f1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/SyncUtil.cs b/Editor/SyncUtil.cs index e21fd1d..247fda0 100644 --- a/Editor/SyncUtil.cs +++ b/Editor/SyncUtil.cs @@ -16,7 +16,7 @@ static SyncUtil() if (!SyncVS.IsValid) { - Preferences.AutoSync = false; + Preferences.Settings.autoSync.SetBool(false); } } @@ -29,7 +29,7 @@ private static void OnAfterAssemblyReload() { IsReloading = false; - if (Preferences.AutoSync) + if (Preferences.Settings.autoSync.GetBool()) { Sync(); }