From 5f6524ad445a1ac072429e4e3c42eab84c0a869d Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Thu, 14 Sep 2023 01:52:40 +0200 Subject: [PATCH] C#: Check if JetBrains Rider editor path is empty --- .../GodotTools/Ides/Rider/RiderPathManager.cs | 49 ++++++++++++------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/modules/mono/editor/GodotTools/GodotTools/Ides/Rider/RiderPathManager.cs b/modules/mono/editor/GodotTools/GodotTools/Ides/Rider/RiderPathManager.cs index fbbd01dafd8e..a0ab381b9bc0 100644 --- a/modules/mono/editor/GodotTools/GodotTools/Ides/Rider/RiderPathManager.cs +++ b/modules/mono/editor/GodotTools/GodotTools/Ides/Rider/RiderPathManager.cs @@ -5,10 +5,14 @@ using GodotTools.Internals; using JetBrains.Rider.PathLocator; +#nullable enable + namespace GodotTools.Ides.Rider { public static class RiderPathManager { + private const string EditorPathSettingName = "dotnet/editor/editor_path_optional"; + private static readonly RiderPathLocator RiderPathLocator; private static readonly RiderFileOpener RiderFileOpener; @@ -19,13 +23,14 @@ static RiderPathManager() RiderFileOpener = new RiderFileOpener(riderLocatorEnvironment); } - public static readonly string EditorPathSettingName = "dotnet/editor/editor_path_optional"; - - private static string GetRiderPathFromSettings() + private static string? GetRiderPathFromSettings() { var editorSettings = EditorInterface.Singleton.GetEditorSettings(); if (editorSettings.HasSetting(EditorPathSettingName)) + { return (string)editorSettings.GetSetting(EditorPathSettingName); + } + return null; } @@ -37,7 +42,7 @@ public static void Initialize() { if (!editorSettings.HasSetting(EditorPathSettingName)) { - Globals.EditorDef(EditorPathSettingName, "Optional"); + Globals.EditorDef(EditorPathSettingName, ""); editorSettings.AddPropertyInfo(new Godot.Collections.Dictionary { ["type"] = (int)Variant.Type.String, @@ -55,9 +60,10 @@ public static void Initialize() } var paths = RiderPathLocator.GetAllRiderPaths(); - - if (!paths.Any()) + if (paths.Length == 0) + { return; + } string newPath = paths.Last().Path; Globals.EditorDef(EditorPathSettingName, newPath); @@ -67,18 +73,16 @@ public static void Initialize() public static bool IsRider(string path) { - if (string.IsNullOrEmpty(path)) - return false; - if (path.IndexOfAny(Path.GetInvalidPathChars()) != -1) + { return false; + } var fileInfo = new FileInfo(path); - string filename = fileInfo.Name.ToLowerInvariant(); - return filename.StartsWith("rider", StringComparison.Ordinal); + return fileInfo.Name.StartsWith("rider", StringComparison.OrdinalIgnoreCase); } - private static string CheckAndUpdatePath(string riderPath) + private static string? CheckAndUpdatePath(string? riderPath) { if (File.Exists(riderPath)) { @@ -87,11 +91,14 @@ private static string CheckAndUpdatePath(string riderPath) var allInfos = RiderPathLocator.GetAllRiderPaths(); if (allInfos.Length == 0) + { return null; - var riderInfos = allInfos.Where(info => IsRider(info.Path)).ToArray(); - string newPath = riderInfos.Length > 0 - ? riderInfos[riderInfos.Length - 1].Path - : allInfos[allInfos.Length - 1].Path; + } + + // RiderPathLocator includes Rider and Fleet locations, prefer Rider when available. + var preferredInfo = allInfos.LastOrDefault(info => IsRider(info.Path), allInfos[allInfos.Length - 1]); + string newPath = preferredInfo.Path; + var editorSettings = EditorInterface.Singleton.GetEditorSettings(); editorSettings.SetSetting(EditorPathSettingName, newPath); Globals.EditorDef(EditorPathSettingName, newPath); @@ -100,8 +107,14 @@ private static string CheckAndUpdatePath(string riderPath) public static void OpenFile(string slnPath, string scriptPath, int line, int column) { - string pathFromSettings = GetRiderPathFromSettings(); - string path = CheckAndUpdatePath(pathFromSettings); + string? pathFromSettings = GetRiderPathFromSettings(); + string? path = CheckAndUpdatePath(pathFromSettings); + if (string.IsNullOrEmpty(path)) + { + GD.PushError($"Error when trying to run code editor: JetBrains Rider or Fleet. Could not find path to the editor."); + return; + } + RiderFileOpener.OpenFile(path, slnPath, scriptPath, line, column); } }