diff --git a/Editor/NvimScriptEditor.cs b/Editor/NvimScriptEditor.cs index 695106b..92b1b69 100644 --- a/Editor/NvimScriptEditor.cs +++ b/Editor/NvimScriptEditor.cs @@ -149,7 +149,7 @@ static string HandledExtensionsString public string ReplaceTemplate(String templateStr, String pipePath, String path, int line, int column) { templateStr = templateStr.Replace("${pipePath}", pipePath); - templateStr = templateStr.Replace("${filePath}", path); + templateStr = templateStr.Replace("${filePath}", path.Replace(" ", "\\ ")); templateStr = templateStr.Replace("${line}", Math.Max(line, 1).ToString()); templateStr = templateStr.Replace("${column}", Math.Max(column, 0).ToString()); return templateStr; @@ -287,7 +287,8 @@ public bool OpenProject(string path, int line, int column) }; Process.Start(nvrStartInfo); - Process process = Process.GetProcesses().FirstOrDefault(x => x.Id == EditorPid); + // Guard agains PID recycling issues (when a PID is reused for another app, then the below still checks that the app getting opened is Neovide, and not something else) + Process process = Process.GetProcesses().FirstOrDefault(x => x.Id == EditorPid && x.ProcessName.IndexOf("neovide", StringComparison.OrdinalIgnoreCase) >= 0); if (process == null || process.HasExited) { @@ -312,6 +313,21 @@ public bool OpenProject(string path, int line, int column) else { // ForceForegroundWindow(process.MainWindowHandle); + if (IsOSX) + { + try + { + ActivateOSXApp(ClientCmd); + } + catch (Exception e) + { + UnityEngine.Debug.LogWarning($"[NvimScriptEditor] Failed to activate {ClientCmd}: {e.Message}"); + } + } + else + { + ActivateWindowsApp(process); + } } return true; @@ -383,6 +399,68 @@ static bool SupportsExtension(string path) return HandledExtensions.Contains(extension.TrimStart('.')); } + +#if UNITY_EDITOR_WIN + [System.Runtime.InteropServices.DllImport("user32.dll")] + static extern bool SetForegroundWindow(IntPtr hWnd); +#endif + + // Allow windows to put app front and center + static void ActivateWindowsApp(Process process) + { +#if UNITY_EDITOR_WIN + if (process != null && process.MainWindowHandle != IntPtr.Zero) + { + SetForegroundWindow(process.MainWindowHandle); + } +#endif + } + + // Allow for OSX to put the app front and center, even when it is already running + static void ActivateOSXApp(string clientCmd) + { + if (string.IsNullOrEmpty(clientCmd)) + { + return; + } + + // clientCmd is the CLI binary path (e.g. /opt/homebrew/bin/neovide). + // LaunchServices/AppleScript activation needs the app bundle name, not the binary path. + var appName = Path.GetFileNameWithoutExtension(clientCmd); + if (string.IsNullOrEmpty(appName)) + { + return; + } + + appName = char.ToUpper(appName[0]) + appName.Substring(1); // neovide -> Neovide + appName = appName.Replace("\"", "\\\"").Replace("'", "\\'"); // guard against quote injection + + var psi = new ProcessStartInfo + { + FileName = "osascript", + Arguments = $"-e 'tell application \"{appName}\" to activate'", + CreateNoWindow = true, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + }; + + var proc = Process.Start(psi); + + if (proc != null) + { + proc.ErrorDataReceived += (sender, args) => + { + if (!string.IsNullOrEmpty(args.Data)) + { + UnityEngine.Debug.LogWarning($"[NvimScriptEditor] osascript: {args.Data}"); + } + }; + + proc.BeginErrorReadLine(); + } + } + public CodeEditor.Installation[] Installations => installations; protected static CodeEditor.Installation[] installations; diff --git a/package.json b/package.json index 72fbc8f..e714132 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "com.unity.nvim", "displayName": "Neovim Editor", "description": "Code editor integration for supporting Neovim as code editor for unity. Adds support for generating csproj files for intellisense purposes, etc.", - "version": "1.2.5", + "version": "1.3.5", "unity": "2019.2", "unityRelease": "0a12", "upmCi": { @@ -12,5 +12,9 @@ "url": "https://github.com/Unity-Technologies/com.unity.ide.vscode.git", "type": "git", "revision": "b0740c80bfc2440527c317109f7c3d9100132722" + }, + "author": { + "name": "FREEZX", + "url": "https://github.com/FREEZX/com.unity.nvim" } }