Last active
September 14, 2024 04:06
-
-
Save Creta5164/244fcb11a3b2d5d65ef196ca0e2d08ed to your computer and use it in GitHub Desktop.
Adds the ability to open with VSCode to the right-click menu to Godot's file system dock.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if TOOLS | |
using System.IO; | |
using System.Linq; | |
using Godot; | |
using static Godot.GD; | |
namespace CretaPark.GodotTools; | |
[Tool] | |
public partial class OpenInVSCode : EditorPlugin { | |
private const int POPUP_MENU_ID = 5164; | |
private const string NODE_PATH_CONTEXT_MENU = "@PopupMenu@5811"; //use @5651 after 4.3-beta.1, there's breaking changes. | |
private PopupMenu _fileSystemDockContextMenu; | |
public override void _EnterTree() { | |
var fileSystemDock = EditorInterface.Singleton.GetFileSystemDock(); | |
if (!IsInstanceValid(_fileSystemDockContextMenu)) { | |
_fileSystemDockContextMenu = fileSystemDock.GetNode(NODE_PATH_CONTEXT_MENU) as PopupMenu; | |
if (!IsInstanceValid(_fileSystemDockContextMenu)) | |
return; | |
} | |
_fileSystemDockContextMenu.AboutToPopup += PopupMenu_AboutToPopup; | |
_fileSystemDockContextMenu.IdPressed += PopupMenu_IdPressed; | |
} | |
public override void _ExitTree() { | |
if (IsInstanceValid(_fileSystemDockContextMenu)) { | |
_fileSystemDockContextMenu.AboutToPopup -= PopupMenu_AboutToPopup; | |
_fileSystemDockContextMenu.IdPressed -= PopupMenu_IdPressed; | |
} | |
_fileSystemDockContextMenu = null; | |
} | |
private void PopupMenu_AboutToPopup() { | |
int registeredItemIndex = _fileSystemDockContextMenu.GetItemIndex(POPUP_MENU_ID); | |
if (registeredItemIndex != -1) | |
_fileSystemDockContextMenu.RemoveItem(registeredItemIndex); | |
Theme editorTheme = EditorInterface.Singleton.GetEditorTheme(); | |
Texture2D codeEditIcon = editorTheme.GetIcon("CodeEdit", "EditorIcons"); | |
_fileSystemDockContextMenu.AddIconItem(codeEditIcon, "Open in VSCode", POPUP_MENU_ID); | |
} | |
private void PopupMenu_IdPressed(long id) { | |
if (id != POPUP_MENU_ID) | |
return; | |
string externalCodePath = EditorInterface.Singleton | |
.GetEditorSettings() | |
.GetSetting("text_editor/external/exec_path") | |
.AsString(); | |
if (!Path.GetDirectoryName(externalCodePath).EndsWith("Microsoft VS Code")) { | |
Print( | |
$"[{nameof(OpenInVSCode)}] Please ensure setting \"text_editor/external/exec_path\"" | |
+ " is set to the path of VSCode in Editor settings." | |
); | |
return; | |
} | |
var selectedPaths = EditorInterface.Singleton.GetSelectedPaths() | |
.Select(ProjectSettings.GlobalizePath); | |
OS.Execute(externalCodePath, [ "-r", ..selectedPaths ]); | |
} | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[plugin] | |
name="OpenInVSCode" | |
description="Adds the ability to open with VSCode to the right-click menu to Godot's file system dock." | |
author="Creta Park" | |
version="1.0.0" | |
language="C-sharp" | |
script="OpenInVSCode.cs" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Heya, awesome plugin! Do you mind if I upload my GDScript adaptation to the Godot asset store? I have your name included as well in the authors and I can link back to this Gist if you like @Creta5164