Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 80 additions & 2 deletions Editor/NvimScriptEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines 149 to 155
Expand Down Expand Up @@ -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);
Comment on lines +290 to +291

if (process == null || process.HasExited)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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,
};
Comment on lines +438 to +446

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;
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
}
}