Skip to content

Commit 6fdaa00

Browse files
committed
refactor: settings
1 parent 3a11515 commit 6fdaa00

15 files changed

Lines changed: 261 additions & 140 deletions

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111
- Support for VimR on macOS.
12+
- Setting class to define launcher settings.
13+
- Unregister Easy Editor before the assembly reloads.
1214
- Documentation.
1315

1416
### Changed
17+
- Launchers inherit from abstract class (Launcher) instead of interface (ILauncher).
1518
- Fix changelog links.
1619
- Clarify comments on former changelogs.
1720

Editor/ExternalCodeEditor.cs

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ namespace EasyEditor
1010
[InitializeOnLoad]
1111
internal class ExternalCodeEditor : IExternalCodeEditor
1212
{
13+
public static readonly ExternalCodeEditor instance;
14+
1315
static ExternalCodeEditor()
1416
{
1517
LauncherRegistry.Load();
16-
CodeEditor.Register(new ExternalCodeEditor());
18+
instance = new ExternalCodeEditor();
19+
CodeEditor.Register(instance);
20+
AssemblyReloadEvents.beforeAssemblyReload += OnBeforeAssemblyReload;
1721
}
1822

1923
public CodeEditor.Installation[] Installations { get; }
@@ -36,14 +40,19 @@ private static bool SupportsExtension(string path)
3640
return !string.IsNullOrEmpty(extension) && DefaultExtensions.Contains(extension.TrimStart('.'));
3741
}
3842

43+
private static void OnBeforeAssemblyReload()
44+
{
45+
CodeEditor.Unregister(instance);
46+
}
47+
3948
public ExternalCodeEditor()
4049
{
4150
Installations = LauncherRegistry.Installations;
4251
}
4352

4453
public void Initialize(string editorInstallationPath)
4554
{
46-
if (Preferences.IsActive && Preferences.AutoSync)
55+
if (Preferences.IsActive && Preferences.Settings.autoSync.GetBool())
4756
{
4857
SyncUtil.Sync();
4958
}
@@ -57,42 +66,44 @@ public void OnGUI()
5766
}
5867

5968
EditorGUI.BeginDisabledGroup(!Preferences.IsActive || !SyncVS.IsValid || SyncUtil.IsReloading || EditorApplication.isCompiling || EditorApplication.isUpdating);
60-
EditorGUI.BeginChangeCheck();
61-
GUIContent syncContent = new GUIContent(
62-
"Sync solution and project files",
63-
"Forces .sln and .csproj files to be generated and kept in sync.");
64-
bool v = EditorGUILayout.Toggle(syncContent, Preferences.AutoSync);
65-
if (EditorGUI.EndChangeCheck())
6669
{
67-
Preferences.AutoSync = v;
68-
if (v)
70+
EditorGUI.BeginChangeCheck();
71+
Setting s = Preferences.Settings.autoSync;
72+
GUIContent c = new GUIContent(s.description, s.tooltip);
73+
bool v = EditorGUILayout.Toggle(c, s.GetBool());
74+
if (EditorGUI.EndChangeCheck())
6975
{
70-
SyncUtil.Sync();
76+
s.SetBool(v);
77+
if (v)
78+
{
79+
SyncUtil.Sync();
80+
}
81+
Event.current.Use();
7182
}
72-
Event.current.Use();
7383
}
7484
if (!SyncVS.IsValid)
7585
{
7686
EditorGUILayout.HelpBox("Couldn't retrieve synchronization members. Please contact this package's author.", MessageType.Warning);
7787
}
7888

79-
EditorGUI.BeginChangeCheck();
80-
GUIContent matchCompilerContent = new GUIContent(
81-
"Match compiler version",
82-
"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() + ". ");
83-
v = EditorGUILayout.Toggle(matchCompilerContent, Preferences.MatchCompilerVersion);
84-
if (EditorGUI.EndChangeCheck())
8589
{
86-
Preferences.MatchCompilerVersion = v;
87-
if (v)
90+
EditorGUI.BeginChangeCheck();
91+
Setting s = Preferences.Settings.matchCompilerVersion;
92+
GUIContent c = new GUIContent(s.description, s.tooltip);
93+
bool v = EditorGUILayout.Toggle(c, s.GetBool());
94+
if (EditorGUI.EndChangeCheck())
8895
{
89-
SyncUtil.Sync();
96+
s.SetBool(v);
97+
if (v)
98+
{
99+
SyncUtil.Sync();
100+
}
101+
Event.current.Use();
90102
}
91-
Event.current.Use();
92103
}
93104

94105
string editorPath = CodeEditor.CurrentEditorInstallation.Trim();
95-
ILauncher launcher = LauncherRegistry.GetLauncher(editorPath);
106+
Launcher launcher = LauncherRegistry.GetLauncher(editorPath);
96107
if (launcher != null)
97108
{
98109
launcher.OnGUI();
@@ -120,9 +131,9 @@ public bool OpenProject(string filePath = "", int line = -1, int column = -1)
120131

121132
string editorPath = CodeEditor.CurrentEditorInstallation.Trim();
122133

123-
LaunchDescriptor descriptor = new LaunchDescriptor(filePath, line, column, Preferences.ProjectPath, Preferences.ProjectName);
134+
LaunchDescriptor descriptor = new LaunchDescriptor(filePath, line, column, Preferences.projectPath, Preferences.projectName);
124135

125-
ILauncher launcher = LauncherRegistry.GetLauncher(editorPath);
136+
Launcher launcher = LauncherRegistry.GetLauncher(editorPath);
126137
if (launcher != null)
127138
{
128139
_ = launcher.Launch(editorPath, descriptor);
@@ -135,7 +146,7 @@ public void SyncAll()
135146
{
136147
SyncUtil.Sync();
137148
string editorPath = CodeEditor.CurrentEditorInstallation.Trim();
138-
ILauncher launcher = LauncherRegistry.GetLauncher(editorPath);
149+
Launcher launcher = LauncherRegistry.GetLauncher(editorPath);
139150
if (launcher != null)
140151
{
141152
launcher.SyncAll();
@@ -146,7 +157,7 @@ public void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] mo
146157
{
147158
SyncUtil.Sync();
148159
string editorPath = CodeEditor.CurrentEditorInstallation.Trim();
149-
ILauncher launcher = LauncherRegistry.GetLauncher(editorPath);
160+
Launcher launcher = LauncherRegistry.GetLauncher(editorPath);
150161
if (launcher != null)
151162
{
152163
launcher.SyncIfNeeded(addedFiles, deletedFiles, movedFiles, movedFromFiles, importedFiles);

Editor/ILauncher.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

Editor/Launcher.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace EasyEditor
2+
{
3+
using Unity.CodeEditor;
4+
5+
internal abstract class Launcher
6+
{
7+
public abstract CodeEditor.Installation[] Installations { get; }
8+
9+
public abstract bool Launch(string editorPath, LaunchDescriptor descriptor);
10+
public abstract void SyncAll();
11+
public abstract void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles);
12+
public abstract bool MatchesExecutable(string editorPath);
13+
14+
public abstract void OnGUI();
15+
}
16+
}
17+

Editor/LauncherRegistry.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ internal static class LauncherRegistry
1111
{
1212
public static CodeEditor.Installation[] Installations { get; set; }
1313
public static string LoadErrors { get; private set; }
14-
private static readonly Dictionary<string, ILauncher> launchers = new Dictionary<string, ILauncher>();
14+
private static readonly Dictionary<string, Launcher> launchers = new Dictionary<string, Launcher>();
1515

1616
static LauncherRegistry()
1717
{
1818
}
1919

20-
public static ILauncher GetLauncher(string editorPath)
20+
public static Launcher GetLauncher(string editorPath)
2121
{
22-
_ = launchers.TryGetValue(editorPath, out ILauncher launcher);
22+
_ = launchers.TryGetValue(editorPath, out Launcher launcher);
2323
return launcher;
2424
}
2525

@@ -31,9 +31,9 @@ public static void Load()
3131
Assembly assembly = System.AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == "EasyEditor.Launchers");
3232
Debug.Assert(assembly != null, "Couldn't find the EasyEditor.Launchers assembly");
3333

34-
foreach (System.Type type in assembly.GetTypes().Where(t => t.IsClass && typeof(ILauncher).IsAssignableFrom(t)))
34+
foreach (System.Type type in assembly.GetTypes().Where(t => t.IsClass && typeof(Launcher).IsAssignableFrom(t)))
3535
{
36-
ILauncher instance = (ILauncher)System.Activator.CreateInstance(type);
36+
Launcher instance = (Launcher)System.Activator.CreateInstance(type);
3737

3838
if (instance.Installations == null)
3939
{

Editor/Launchers/GVim.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ namespace EasyEditor.Launchers
77
using UnityEditor;
88
using UnityEngine;
99

10-
internal class GVim : ILauncher
10+
internal class GVim : Launcher
1111
{
12-
private static readonly string PrefPrefix = $"{typeof(GVim).FullName}";
13-
private static readonly string PrefRestartOmniSharp = $"{PrefPrefix}.RestartOmniSharp";
12+
[InitializeOnLoad]
13+
private static class Settings
14+
{
15+
public static readonly Setting restartOmniSharp = new Setting(
16+
"RestartOmniSharp",
17+
"Restart OmniSharp server on sync",
18+
"Upon synchronization, send a command (--remote-send) to the gVim server to trigger a OmniSharp server reload. omnisharp-vim required.",
19+
"GVim");
20+
}
1421

15-
public CodeEditor.Installation[] Installations => new CodeEditor.Installation[]
22+
public override CodeEditor.Installation[] Installations => new CodeEditor.Installation[]
1623
{
1724
#if UNITY_EDITOR_WIN
1825
new CodeEditor.Installation()
@@ -28,7 +35,7 @@ internal class GVim : ILauncher
2835
#endif
2936
};
3037

31-
public bool Launch(string editorPath, LaunchDescriptor descriptor)
38+
public override bool Launch(string editorPath, LaunchDescriptor descriptor)
3239
{
3340
string args = string.Format("--servername \"{0}\" --remote-silent +$(Line) $(File)", descriptor.ProjectName);
3441

@@ -51,7 +58,7 @@ public bool Launch(string editorPath, LaunchDescriptor descriptor)
5158

5259
private void ReloadOmniSharpServer()
5360
{
54-
string args = string.Format("--servername \"{0}\" --remote-send '<C-\\><C-N>:silent OmniSharpRestartServer<CR>'", Preferences.ProjectName);
61+
string args = string.Format("--servername \"{0}\" --remote-send '<C-\\><C-N>:silent OmniSharpRestartServer<CR>'", Preferences.projectName);
5562

5663
Process process = new Process
5764
{
@@ -68,23 +75,23 @@ private void ReloadOmniSharpServer()
6875
_ = process.Start();
6976
}
7077

71-
public void SyncAll()
78+
public override void SyncAll()
7279
{
73-
if (Preferences.IsActive && EditorPrefs.GetBool(PrefRestartOmniSharp))
80+
if (Preferences.IsActive && Settings.restartOmniSharp.GetBool())
7481
{
7582
ReloadOmniSharpServer();
7683
}
7784
}
7885

79-
public void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles)
86+
public override void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles)
8087
{
81-
if (Preferences.IsActive && EditorPrefs.GetBool(PrefRestartOmniSharp))
88+
if (Preferences.IsActive && Settings.restartOmniSharp.GetBool())
8289
{
8390
ReloadOmniSharpServer();
8491
}
8592
}
8693

87-
public bool MatchesExecutable(string editorPath)
94+
public override bool MatchesExecutable(string editorPath)
8895
{
8996
string vimPath = Path.Combine(Path.GetDirectoryName(editorPath), "vim.exe");
9097
Process process = new Process
@@ -109,16 +116,15 @@ public bool MatchesExecutable(string editorPath)
109116
return output.StartsWith("VIM - Vi IMproved");
110117
}
111118

112-
public void OnGUI()
119+
public override void OnGUI()
113120
{
114121
EditorGUI.BeginChangeCheck();
115-
GUIContent restartOmniSharpContent = new GUIContent(
116-
"Restart OmniSharp server on sync",
117-
"Upon synchronization, send a command (--remote-send) to the gVim server to trigger a OmniSharp server reload. omnisharp-vim required.");
118-
bool b = EditorGUILayout.Toggle(restartOmniSharpContent, EditorPrefs.GetBool(PrefRestartOmniSharp));
122+
Setting s = Settings.restartOmniSharp;
123+
GUIContent c = new GUIContent(s.description, s.tooltip);
124+
bool b = EditorGUILayout.Toggle(c, s.GetBool());
119125
if (EditorGUI.EndChangeCheck())
120126
{
121-
EditorPrefs.SetBool(PrefRestartOmniSharp, b);
127+
s.SetBool(b);
122128
Event.current.Use();
123129
}
124130
}

Editor/Launchers/MacVim.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ namespace EasyEditor.Launchers
66
using UnityEditor;
77
using UnityEngine;
88

9-
internal class MacVim : ILauncher
9+
internal class MacVim : Launcher
1010
{
11-
private static readonly string PrefPrefix = $"{typeof(MacVim).FullName}";
12-
private static readonly string PrefRestartOmniSharp = $"{PrefPrefix}.RestartOmniSharp";
11+
[InitializeOnLoad]
12+
private static class Settings
13+
{
14+
public static readonly Setting restartOmniSharp = new Setting(
15+
"RestartOmniSharp",
16+
"Restart OmniSharp server on sync",
17+
"Upon synchronization, send a command (--remote-send) to the Vim server to trigger a OmniSharp server reload. omnisharp-vim required.",
18+
"MacVim");
19+
}
1320

14-
public CodeEditor.Installation[] Installations => new CodeEditor.Installation[]
21+
public override CodeEditor.Installation[] Installations => new CodeEditor.Installation[]
1522
{
1623
#if UNITY_EDITOR_OSX
1724
new CodeEditor.Installation()
@@ -22,7 +29,7 @@ internal class MacVim : ILauncher
2229
#endif
2330
};
2431

25-
public bool Launch(string editorPath, LaunchDescriptor descriptor)
32+
public override bool Launch(string editorPath, LaunchDescriptor descriptor)
2633
{
2734
string args = string.Format("--servername \"{0}\" --remote-silent '+call cursor($(Line),$(Column))' $(File)", descriptor.ProjectName);
2835

@@ -45,7 +52,7 @@ public bool Launch(string editorPath, LaunchDescriptor descriptor)
4552

4653
private void ReloadOmniSharpServer()
4754
{
48-
string args = string.Format("--servername \"{0}\" --remote-send '<C-\\><C-N>:silent OmniSharpRestartServer<CR>'", Preferences.ProjectName);
55+
string args = string.Format("--servername \"{0}\" --remote-send '<C-\\><C-N>:silent OmniSharpRestartServer<CR>'", Preferences.projectName);
4956

5057
Process process = new Process
5158
{
@@ -62,23 +69,23 @@ private void ReloadOmniSharpServer()
6269
_ = process.Start();
6370
}
6471

65-
public void SyncAll()
72+
public override void SyncAll()
6673
{
67-
if (Preferences.IsActive && EditorPrefs.GetBool(PrefRestartOmniSharp))
74+
if (Preferences.IsActive && Settings.restartOmniSharp.GetBool())
6875
{
6976
ReloadOmniSharpServer();
7077
}
7178
}
7279

73-
public void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles)
80+
public override void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles)
7481
{
75-
if (Preferences.IsActive && EditorPrefs.GetBool(PrefRestartOmniSharp))
82+
if (Preferences.IsActive && Settings.restartOmniSharp.GetBool())
7683
{
7784
ReloadOmniSharpServer();
7885
}
7986
}
8087

81-
public bool MatchesExecutable(string editorPath)
88+
public override bool MatchesExecutable(string editorPath)
8289
{
8390
Process process = new Process
8491
{
@@ -102,16 +109,15 @@ public bool MatchesExecutable(string editorPath)
102109
return output.StartsWith("VIM - Vi IMproved");
103110
}
104111

105-
public void OnGUI()
112+
public override void OnGUI()
106113
{
107114
EditorGUI.BeginChangeCheck();
108-
GUIContent restartOmniSharpContent = new GUIContent(
109-
"Restart OmniSharp server on sync",
110-
"Upon synchronization, send a command (--remote-send) to the Vim server to trigger a OmniSharp server reload. omnisharp-vim required.");
111-
bool b = EditorGUILayout.Toggle(restartOmniSharpContent, EditorPrefs.GetBool(PrefRestartOmniSharp));
115+
Setting s = Settings.restartOmniSharp;
116+
GUIContent c = new GUIContent(s.description, s.tooltip);
117+
bool b = EditorGUILayout.Toggle(c, s.GetBool());
112118
if (EditorGUI.EndChangeCheck())
113119
{
114-
EditorPrefs.SetBool(PrefRestartOmniSharp, b);
120+
s.SetBool(b);
115121
Event.current.Use();
116122
}
117123
}

0 commit comments

Comments
 (0)