Skip to content

Commit

Permalink
Add the ability to set a custom C# editor
Browse files Browse the repository at this point in the history
This allows users to still use the built-in Godot editor for GDScript.
  • Loading branch information
dmitsuki authored and YuriSizov committed Apr 18, 2023
1 parent 3db4035 commit c2b97ec
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public enum ExternalEditorId : long
VisualStudioForMac, // Mac-only
MonoDevelop,
VsCode,
Rider
Rider,
CustomEditor
}
}
87 changes: 84 additions & 3 deletions modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public partial class GodotSharpEditor : EditorPlugin, ISerializationListener
public static class Settings
{
public const string ExternalEditor = "dotnet/editor/external_editor";
public const string CustomExecPath = "dotnet/editor/custom_exec_path";
public const string CustomExecPathArgs = "dotnet/editor/custom_exec_path_args";
public const string VerbosityLevel = "dotnet/build/verbosity_level";
public const string NoConsoleLogging = "dotnet/build/no_console_logging";
public const string CreateBinaryLog = "dotnet/build/create_binary_log";
Expand Down Expand Up @@ -185,6 +187,66 @@ public Error OpenInExternalEditor(Script script, int line, int col)
case ExternalEditorId.None:
// Not an error. Tells the caller to fallback to the global external editor settings or the built-in editor.
return Error.Unavailable;
case ExternalEditorId.CustomEditor:
{
string file = ProjectSettings.GlobalizePath(script.ResourcePath);
var execCommand = _editorSettings.GetSetting(Settings.CustomExecPath).As<string>();
var execArgs = _editorSettings.GetSetting(Settings.CustomExecPathArgs).As<string>();
var args = new List<string>();
var from = 0;
var numChars = 0;
var insideQuotes = false;
var hasFileFlag = false;

execArgs = execArgs.ReplaceN("{line}", line.ToString());
execArgs = execArgs.ReplaceN("{col}", col.ToString());
execArgs = execArgs.StripEdges(true, true);
execArgs = execArgs.Replace("\\\\", "\\");

for (int i = 0; i < execArgs.Length; ++i)
{
if ((execArgs[i] == '"' && (i == 0 || execArgs[i - 1] != '\\')) && i != execArgs.Length - 1)
{
if (!insideQuotes)
{
from++;
}
insideQuotes = !insideQuotes;
}
else if ((execArgs[i] == ' ' && !insideQuotes) || i == execArgs.Length - 1)
{
if (i == execArgs.Length - 1 && !insideQuotes)
{
numChars++;
}

var arg = execArgs.Substr(from, numChars);
if (arg.Contains("{file}"))
{
hasFileFlag = true;
}

arg = arg.ReplaceN("{file}", file);
args.Add(arg);

from = i + 1;
numChars = 0;
}
else
{
numChars++;
}
}

if (!hasFileFlag)
{
args.Add(file);
}

OS.RunProcess(execCommand, args);

break;
}
case ExternalEditorId.VisualStudio:
{
string scriptPath = ProjectSettings.GlobalizePath(script.ResourcePath);
Expand Down Expand Up @@ -463,6 +525,8 @@ public override void _EnablePlugin()

// External editor settings
EditorDef(Settings.ExternalEditor, Variant.From(ExternalEditorId.None));
EditorDef(Settings.CustomExecPath, "");
EditorDef(Settings.CustomExecPathArgs, "");
EditorDef(Settings.VerbosityLevel, Variant.From(VerbosityLevelId.Normal));
EditorDef(Settings.NoConsoleLogging, false);
EditorDef(Settings.CreateBinaryLog, false);
Expand All @@ -474,20 +538,23 @@ public override void _EnablePlugin()
settingsHintStr += $",Visual Studio:{(int)ExternalEditorId.VisualStudio}" +
$",MonoDevelop:{(int)ExternalEditorId.MonoDevelop}" +
$",Visual Studio Code:{(int)ExternalEditorId.VsCode}" +
$",JetBrains Rider:{(int)ExternalEditorId.Rider}";
$",JetBrains Rider:{(int)ExternalEditorId.Rider}" +
$",Custom:{(int)ExternalEditorId.CustomEditor}";
}
else if (OS.IsMacOS)
{
settingsHintStr += $",Visual Studio:{(int)ExternalEditorId.VisualStudioForMac}" +
$",MonoDevelop:{(int)ExternalEditorId.MonoDevelop}" +
$",Visual Studio Code:{(int)ExternalEditorId.VsCode}" +
$",JetBrains Rider:{(int)ExternalEditorId.Rider}";
$",JetBrains Rider:{(int)ExternalEditorId.Rider}" +
$",Custom:{(int)ExternalEditorId.CustomEditor}";
}
else if (OS.IsUnixLike)
{
settingsHintStr += $",MonoDevelop:{(int)ExternalEditorId.MonoDevelop}" +
$",Visual Studio Code:{(int)ExternalEditorId.VsCode}" +
$",JetBrains Rider:{(int)ExternalEditorId.Rider}";
$",JetBrains Rider:{(int)ExternalEditorId.Rider}" +
$",Custom:{(int)ExternalEditorId.CustomEditor}";
}

_editorSettings.AddPropertyInfo(new Godot.Collections.Dictionary
Expand All @@ -498,6 +565,20 @@ public override void _EnablePlugin()
["hint_string"] = settingsHintStr
});

_editorSettings.AddPropertyInfo(new Godot.Collections.Dictionary
{
["type"] = (int)Variant.Type.String,
["name"] = Settings.CustomExecPath,
["hint"] = (int)PropertyHint.GlobalFile,
});

_editorSettings.AddPropertyInfo(new Godot.Collections.Dictionary
{
["type"] = (int)Variant.Type.String,
["name"] = Settings.CustomExecPathArgs,
});
_editorSettings.SetInitialValue(Settings.CustomExecPathArgs, "{file}", false);

var verbosityLevels = Enum.GetValues<VerbosityLevelId>().Select(level => $"{Enum.GetName(level)}:{(int)level}");
_editorSettings.AddPropertyInfo(new Godot.Collections.Dictionary
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ private string GetExternalEditorIdentity(ExternalEditorId editorId)
return "VisualStudioForMac";
case ExternalEditorId.MonoDevelop:
return "MonoDevelop";
case ExternalEditorId.CustomEditor:
return "CustomEditor";
default:
throw new NotImplementedException();
}
Expand Down Expand Up @@ -105,6 +107,7 @@ private void LaunchIde(ExternalEditorId editorId, string editorIdentity)
case ExternalEditorId.VisualStudio:
case ExternalEditorId.VsCode:
case ExternalEditorId.Rider:
case ExternalEditorId.CustomEditor:
throw new NotSupportedException();
case ExternalEditorId.VisualStudioForMac:
goto case ExternalEditorId.MonoDevelop;
Expand Down

0 comments on commit c2b97ec

Please sign in to comment.