Skip to content

Commit

Permalink
refactor: settings
Browse files Browse the repository at this point in the history
  • Loading branch information
frarees committed Oct 5, 2020
1 parent 3a11515 commit 6fdaa00
Show file tree
Hide file tree
Showing 15 changed files with 261 additions and 140 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
65 changes: 38 additions & 27 deletions Editor/ExternalCodeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -36,14 +40,19 @@ 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;
}

public void Initialize(string editorInstallationPath)
{
if (Preferences.IsActive && Preferences.AutoSync)
if (Preferences.IsActive && Preferences.Settings.autoSync.GetBool())
{
SyncUtil.Sync();
}
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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);
Expand Down
17 changes: 0 additions & 17 deletions Editor/ILauncher.cs

This file was deleted.

17 changes: 17 additions & 0 deletions Editor/Launcher.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}

File renamed without changes.
10 changes: 5 additions & 5 deletions Editor/LauncherRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ILauncher> launchers = new Dictionary<string, ILauncher>();
private static readonly Dictionary<string, Launcher> launchers = new Dictionary<string, Launcher>();

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;
}

Expand All @@ -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)
{
Expand Down
40 changes: 23 additions & 17 deletions Editor/Launchers/GVim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);

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

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

Process process = new Process
{
Expand All @@ -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
Expand All @@ -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();
}
}
Expand Down
40 changes: 23 additions & 17 deletions Editor/Launchers/MacVim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);

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

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

Process process = new Process
{
Expand All @@ -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
{
Expand All @@ -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();
}
}
Expand Down
Loading

0 comments on commit 6fdaa00

Please sign in to comment.