From 8916d3c406200770db87ca25a03b2cccadc37e00 Mon Sep 17 00:00:00 2001 From: programming-with-ia Date: Sun, 22 Dec 2024 23:43:44 +0500 Subject: [PATCH 1/6] add: contextMenu items for `Run.Plugin.VSCodeWorkspaces` --- .../Main.cs | 100 +++++++++++++++++- .../Properties/Resources.resx | 9 ++ 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs index 577d0a19de58..8b7f2c2cda82 100644 --- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs +++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs @@ -7,16 +7,19 @@ using System.ComponentModel; using System.Diagnostics; using System.Linq; +using System.Windows; using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.Properties; using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.RemoteMachinesHelper; using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper; using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper; + +using Wox.Infrastructure; using Wox.Plugin; namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces { - public class Main : IPlugin, IPluginI18n + public class Main : IPlugin, IPluginI18n, IContextMenu { private PluginInitContext _context; @@ -174,6 +177,101 @@ public void Init(PluginInitContext context) _context = context; } + public List LoadContextMenus(Result selectedResult) + { + if (selectedResult?.ContextData is not VSCodeWorkspace workspace) + { + return new List(); + } + + string realPath = SystemPath.RealPath(workspace.RelativePath); + + return new List + { + CreateContextMenuResult( + title: $"{Resources.CopyPath} (Ctrl+C)", + glyph: "\xE8C8", // Copy + acceleratorKey: Key.C, + acceleratorModifiers: ModifierKeys.Control, + action: () => CopyToClipboard(realPath) + ), + CreateContextMenuResult( + title: $"{Resources.OpenInExplorer} (Ctrl+Shift+F)", + glyph: "\xEC50", // File Explorer + acceleratorKey: Key.F, + acceleratorModifiers: ModifierKeys.Control | ModifierKeys.Shift, + action: () => OpenInExplorer(realPath) + ) + CreateContextMenuResult( + title: $"{Resources.OpenInConsole} (Ctrl+Shift+C)", + glyph: "\xE756", // Command Prompt + acceleratorKey: Key.C, + acceleratorModifiers: ModifierKeys.Control | ModifierKeys.Shift, + action: () => OpenInConsole(realPath) + ), + }; + } + + private ContextMenuResult CreateContextMenuResult(string title, string glyph, Key acceleratorKey, ModifierKeys acceleratorModifiers, Func action) + { + return new ContextMenuResult + { + PluginName = Name, + Title = title, + Glyph = glyph, + FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets", + AcceleratorKey = acceleratorKey, + AcceleratorModifiers = acceleratorModifiers, + Action = context => action() + }; + } + + private bool CopyToClipboard(string path) + { + try + { + Clipboard.SetText(path); + return true; + } + catch (Exception) + { + ShowErrorMessage("Can't copy to clipboard"); + return false; + } + } + + private bool OpenInConsole(string path) + { + try + { + Helper.OpenInConsole(path); + return true; + } + catch (Exception) + { + ShowErrorMessage($"Unable to open the specified path in the console: {path}"); + return false; + } + } + + private bool OpenInExplorer(string path) + { + if (!Helper.OpenInShell("explorer.exe", $"\"{path}\"")) + { + ShowErrorMessage($"Failed to open folder in Explorer at path: {path}"); + return false; + } + + return true; + } + + private void ShowErrorMessage(string message) + { + var pluginName = $"Plugin: {_context.CurrentPluginMetadata.Name}"; + _context.API.ShowMsg(pluginName, message, string.Empty); + } + + public string GetTranslatedPluginTitle() { return Resources.PluginTitle; diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Properties/Resources.resx b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Properties/Resources.resx index 01778a0beb61..de0254afd3b8 100644 --- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Properties/Resources.resx +++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Properties/Resources.resx @@ -151,4 +151,13 @@ Project Folder It refers to the Visual Studio Code Project Folders + + Copy path + + + Open in console + + + Open in Explorer + \ No newline at end of file From 80b00687766011d6c2160f12169692adffeace0c Mon Sep 17 00:00:00 2001 From: programming-with-ia Date: Mon, 23 Dec 2024 01:30:06 +0500 Subject: [PATCH 2/6] logs using `Wox.Plugin.Logger.Log` fix errors --- .../Main.cs | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs index 8b7f2c2cda82..76672a3f45bd 100644 --- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs +++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs @@ -16,6 +16,7 @@ using Wox.Infrastructure; using Wox.Plugin; +using Wox.Plugin.Logger; namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces { @@ -79,11 +80,9 @@ public List Query(Query query) hide = true; } - catch (Win32Exception) + catch (Win32Exception ex) { - var name = $"Plugin: {_context.CurrentPluginMetadata.Name}"; - var msg = "Can't Open this file"; - _context.API.ShowMsg(name, msg, string.Empty); + LogError("Can't Open this file", ex); hide = false; } @@ -127,11 +126,9 @@ public List Query(Query query) hide = true; } - catch (Win32Exception) + catch (Win32Exception ex) { - var name = $"Plugin: {_context.CurrentPluginMetadata.Name}"; - var msg = "Can't Open this file"; - _context.API.ShowMsg(name, msg, string.Empty); + LogError("Can't Open this file", ex); hide = false; } @@ -201,14 +198,14 @@ public List LoadContextMenus(Result selectedResult) acceleratorKey: Key.F, acceleratorModifiers: ModifierKeys.Control | ModifierKeys.Shift, action: () => OpenInExplorer(realPath) - ) + ), CreateContextMenuResult( title: $"{Resources.OpenInConsole} (Ctrl+Shift+C)", glyph: "\xE756", // Command Prompt acceleratorKey: Key.C, acceleratorModifiers: ModifierKeys.Control | ModifierKeys.Shift, action: () => OpenInConsole(realPath) - ), + ) }; } @@ -233,9 +230,9 @@ private bool CopyToClipboard(string path) Clipboard.SetText(path); return true; } - catch (Exception) + catch (Exception ex) { - ShowErrorMessage("Can't copy to clipboard"); + LogError("Can't copy to clipboard", ex); return false; } } @@ -247,9 +244,9 @@ private bool OpenInConsole(string path) Helper.OpenInConsole(path); return true; } - catch (Exception) + catch (Exception ex) { - ShowErrorMessage($"Unable to open the specified path in the console: {path}"); + LogError($"Unable to open the specified path in the console: {path}", ex); return false; } } @@ -258,19 +255,26 @@ private bool OpenInExplorer(string path) { if (!Helper.OpenInShell("explorer.exe", $"\"{path}\"")) { - ShowErrorMessage($"Failed to open folder in Explorer at path: {path}"); + LogError($"Failed to open folder in Explorer at path: {path}"); return false; } return true; } - private void ShowErrorMessage(string message) + public void LogError(string msg, Exception exception = null) { - var pluginName = $"Plugin: {_context.CurrentPluginMetadata.Name}"; - _context.API.ShowMsg(pluginName, message, string.Empty); - } + string fullMessage = $"Plugin: {_context.CurrentPluginMetadata.Name}, Message: {msg}"; + if (exception != null) + { + Log.Exception(fullMessage, exception, exception.GetType()); + } + else + { + Log.Error(fullMessage, typeof(VSCodeWorkspaces.Main)); + } + } public string GetTranslatedPluginTitle() { From f4b81506f7240bf70dfcc3056ad384e77b52cf39 Mon Sep 17 00:00:00 2001 From: programming-with-ia Date: Mon, 23 Dec 2024 02:28:05 +0500 Subject: [PATCH 3/6] Add System.Windows.Input for key handling --- .../Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs index 76672a3f45bd..90065f318f92 100644 --- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs +++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs @@ -8,6 +8,7 @@ using System.Diagnostics; using System.Linq; using System.Windows; +using System.Windows.Input; using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.Properties; using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.RemoteMachinesHelper; From fa03d592bbeab644267bb724eb592910e0960f65 Mon Sep 17 00:00:00 2001 From: programming-with-ia Date: Mon, 23 Dec 2024 14:44:42 +0500 Subject: [PATCH 4/6] ... manually edit Resources.Designer.cs fix: trailing comma in multi-line initializers bump plugin version --- .../Main.cs | 4 +-- .../Properties/Resources.Designer.cs | 27 +++++++++++++++++++ .../plugin.json | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs index 90065f318f92..339bc12a7ae9 100644 --- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs +++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs @@ -206,7 +206,7 @@ public List LoadContextMenus(Result selectedResult) acceleratorKey: Key.C, acceleratorModifiers: ModifierKeys.Control | ModifierKeys.Shift, action: () => OpenInConsole(realPath) - ) + ), }; } @@ -220,7 +220,7 @@ private ContextMenuResult CreateContextMenuResult(string title, string glyph, Ke FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets", AcceleratorKey = acceleratorKey, AcceleratorModifiers = acceleratorModifiers, - Action = context => action() + Action = context => action(), }; } diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Properties/Resources.Designer.cs b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Properties/Resources.Designer.cs index 9f7d4d920fe5..45914a338f20 100644 --- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Properties/Resources.Designer.cs +++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Properties/Resources.Designer.cs @@ -140,5 +140,32 @@ internal static string Workspace { return ResourceManager.GetString("Workspace", resourceCulture); } } + + /// + /// Looks up a localized string similar to Copy Path. + /// + internal static string CopyPath { + get { + return ResourceManager.GetString("CopyPath", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Open in console. + /// + internal static string OpenInConsole { + get { + return ResourceManager.GetString("OpenInConsole", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Open in Explorer. + /// + internal static string OpenInExplorer { + get { + return ResourceManager.GetString("OpenInExplorer", resourceCulture); + } + } } } diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/plugin.json b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/plugin.json index 2c6d9cb70d70..c5f0a7b84c9d 100644 --- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/plugin.json +++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/plugin.json @@ -4,7 +4,7 @@ "ActionKeyword": "{", "Name": "VS Code Workspaces", "Author": "ricardosantos9521", - "Version": "1.1.0", + "Version": "1.2.0", "Language": "csharp", "Website": "https://github.com/ricardosantos9521/PowerToys/", "ExecuteFileName": "Community.PowerToys.Run.Plugin.VSCodeWorkspaces.dll", From 25526acbce3d560dc4387a9ab4a7fa0a018f2c54 Mon Sep 17 00:00:00 2001 From: programming-with-ia Date: Mon, 23 Dec 2024 20:30:00 +0500 Subject: [PATCH 5/6] format file --- .../Main.cs | 39 +++++++++---------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs index 339bc12a7ae9..b48fc8013c80 100644 --- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs +++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs @@ -143,23 +143,23 @@ public List Query(Query query) results = results.Where(a => a.Title.Contains(query.Search, StringComparison.InvariantCultureIgnoreCase)).ToList(); results.ForEach(x => - { - if (x.Score == 0) - { - x.Score = 100; - } + { + if (x.Score == 0) + { + x.Score = 100; + } - // intersect the title with the query - var intersection = Convert.ToInt32(x.Title.ToLowerInvariant().Intersect(query.Search.ToLowerInvariant()).Count() * query.Search.Length); - var differenceWithQuery = Convert.ToInt32((x.Title.Length - intersection) * query.Search.Length * 0.7); - x.Score = x.Score - differenceWithQuery + intersection; + // intersect the title with the query + var intersection = Convert.ToInt32(x.Title.ToLowerInvariant().Intersect(query.Search.ToLowerInvariant()).Count() * query.Search.Length); + var differenceWithQuery = Convert.ToInt32((x.Title.Length - intersection) * query.Search.Length * 0.7); + x.Score = x.Score - differenceWithQuery + intersection; - // if is a remote machine give it 12 extra points - if (x.ContextData is VSCodeRemoteMachine) - { - x.Score = Convert.ToInt32(x.Score + (intersection * 2)); - } - }); + // if is a remote machine give it 12 extra points + if (x.ContextData is VSCodeRemoteMachine) + { + x.Score = Convert.ToInt32(x.Score + (intersection * 2)); + } + }); results = results.OrderByDescending(x => x.Score).ToList(); if (query.Search == string.Empty || query.Search.Replace(" ", string.Empty) == string.Empty) @@ -191,22 +191,19 @@ public List LoadContextMenus(Result selectedResult) glyph: "\xE8C8", // Copy acceleratorKey: Key.C, acceleratorModifiers: ModifierKeys.Control, - action: () => CopyToClipboard(realPath) - ), + action: () => CopyToClipboard(realPath)), CreateContextMenuResult( title: $"{Resources.OpenInExplorer} (Ctrl+Shift+F)", glyph: "\xEC50", // File Explorer acceleratorKey: Key.F, acceleratorModifiers: ModifierKeys.Control | ModifierKeys.Shift, - action: () => OpenInExplorer(realPath) - ), + action: () => OpenInExplorer(realPath)), CreateContextMenuResult( title: $"{Resources.OpenInConsole} (Ctrl+Shift+C)", glyph: "\xE756", // Command Prompt acceleratorKey: Key.C, acceleratorModifiers: ModifierKeys.Control | ModifierKeys.Shift, - action: () => OpenInConsole(realPath) - ), + action: () => OpenInConsole(realPath)), }; } From 36f44d2f3c422cc2b758bf0da216a3e7e9c05a96 Mon Sep 17 00:00:00 2001 From: programming-with-ia Date: Tue, 24 Dec 2024 17:08:04 +0500 Subject: [PATCH 6/6] feat: show error messages --- .../Main.cs | 85 ++++++++++--------- 1 file changed, 45 insertions(+), 40 deletions(-) diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs index b48fc8013c80..fcb71374d8f8 100644 --- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs +++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.VSCodeWorkspaces/Main.cs @@ -83,7 +83,7 @@ public List Query(Query query) } catch (Win32Exception ex) { - LogError("Can't Open this file", ex); + HandleError("Can't Open this file", ex, showMsg: true); hide = false; } @@ -129,7 +129,7 @@ public List Query(Query query) } catch (Win32Exception ex) { - LogError("Can't Open this file", ex); + HandleError("Can't Open this file", ex, showMsg: true); hide = false; } @@ -186,40 +186,39 @@ public List LoadContextMenus(Result selectedResult) return new List { - CreateContextMenuResult( - title: $"{Resources.CopyPath} (Ctrl+C)", - glyph: "\xE8C8", // Copy - acceleratorKey: Key.C, - acceleratorModifiers: ModifierKeys.Control, - action: () => CopyToClipboard(realPath)), - CreateContextMenuResult( - title: $"{Resources.OpenInExplorer} (Ctrl+Shift+F)", - glyph: "\xEC50", // File Explorer - acceleratorKey: Key.F, - acceleratorModifiers: ModifierKeys.Control | ModifierKeys.Shift, - action: () => OpenInExplorer(realPath)), - CreateContextMenuResult( - title: $"{Resources.OpenInConsole} (Ctrl+Shift+C)", - glyph: "\xE756", // Command Prompt - acceleratorKey: Key.C, - acceleratorModifiers: ModifierKeys.Control | ModifierKeys.Shift, - action: () => OpenInConsole(realPath)), + new ContextMenuResult + { + PluginName = Name, + Title = $"{Resources.CopyPath} (Ctrl+C)", + Glyph = "\xE8C8", // Copy + FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets", + AcceleratorKey = Key.C, + AcceleratorModifiers = ModifierKeys.Control, + Action = context => CopyToClipboard(realPath), + }, + new ContextMenuResult + { + PluginName = Name, + Title = $"{Resources.OpenInExplorer} (Ctrl+Shift+F)", + Glyph = "\xEC50", // File Explorer + FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets", + AcceleratorKey = Key.F, + AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift, + Action = context => OpenInExplorer(realPath), + }, + new ContextMenuResult + { + PluginName = Name, + Title = $"{Resources.OpenInConsole} (Ctrl+Shift+C)", + Glyph = "\xE756", // Command Prompt + FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets", + AcceleratorKey = Key.C, + AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift, + Action = context => OpenInConsole(realPath), + }, }; } - private ContextMenuResult CreateContextMenuResult(string title, string glyph, Key acceleratorKey, ModifierKeys acceleratorModifiers, Func action) - { - return new ContextMenuResult - { - PluginName = Name, - Title = title, - Glyph = glyph, - FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets", - AcceleratorKey = acceleratorKey, - AcceleratorModifiers = acceleratorModifiers, - Action = context => action(), - }; - } private bool CopyToClipboard(string path) { @@ -230,7 +229,7 @@ private bool CopyToClipboard(string path) } catch (Exception ex) { - LogError("Can't copy to clipboard", ex); + HandleError("Can't copy to clipboard", ex, showMsg: true); return false; } } @@ -244,7 +243,7 @@ private bool OpenInConsole(string path) } catch (Exception ex) { - LogError($"Unable to open the specified path in the console: {path}", ex); + HandleError($"Unable to open the specified path in the console: {path}", ex, showMsg: true); return false; } } @@ -253,24 +252,30 @@ private bool OpenInExplorer(string path) { if (!Helper.OpenInShell("explorer.exe", $"\"{path}\"")) { - LogError($"Failed to open folder in Explorer at path: {path}"); + HandleError($"Failed to open folder in Explorer at path: {path}", showMsg: true); return false; } return true; } - public void LogError(string msg, Exception exception = null) + public void HandleError(string msg, Exception exception = null, bool showMsg = false) { - string fullMessage = $"Plugin: {_context.CurrentPluginMetadata.Name}, Message: {msg}"; if (exception != null) { - Log.Exception(fullMessage, exception, exception.GetType()); + Log.Exception(msg, exception, exception.GetType()); } else { - Log.Error(fullMessage, typeof(VSCodeWorkspaces.Main)); + Log.Error(msg, typeof(VSCodeWorkspaces.Main)); + } + + if (showMsg) + { + _context.API.ShowMsg( + $"Plugin: {_context.CurrentPluginMetadata.Name}", + msg); } }