diff --git a/GitTreeFilter/Commands/CommandRegistrar.cs b/GitTreeFilter/Commands/CommandRegistrar.cs index a89b2cd..df5ab75 100644 --- a/GitTreeFilter/Commands/CommandRegistrar.cs +++ b/GitTreeFilter/Commands/CommandRegistrar.cs @@ -22,9 +22,11 @@ public static async Task InitializeAsync(IGitFiltersPackage package) try { await RegisterAsync(package, commandService); + GitTreeFilterOutput.WriteLine($"Initialized all commands"); } catch (Exception ex) { + GitTreeFilterOutput.WriteLine($"Failed to register commands: {ex}"); ActivityLog.LogError(nameof(CommandRegistrar), $"Failed to register commands - {ex}"); throw ex; } @@ -50,19 +52,38 @@ private static async Task RegisterAsync(IGitFiltersPackage package, IMenuCommand private static void RegisterCommand(IMenuCommandService commandService, int rawCommandId, GitFiltersCommand command) { - var commandId = new CommandID(GitFiltersControls.GitFiltersControlsCmdSet, rawCommandId); - var menuCommand = new OleMenuCommand(command.OnExecute, commandId); + GitTreeFilterOutput.WriteLine($"Registering command {command.GetType()} with id {rawCommandId}"); - menuCommand.BeforeQueryStatus += delegate (object sender, EventArgs e) + var commandId = new CommandID(GitFiltersControls.GitFiltersControlsCmdSet, rawCommandId); + if (commandService.FindCommand(commandId) == null) { - menuCommand.Visible = command.IsVisible; - }; + var menuCommand = new OleMenuCommand(command.OnExecute, commandId); - command.VisibilityChanged += delegate - { - menuCommand.Visible = command.IsVisible; - }; + menuCommand.BeforeQueryStatus += delegate (object sender, EventArgs e) + { + ThreadHelper.JoinableTaskFactory.Run(() => + { + menuCommand.Visible = command.IsVisible; + return Task.CompletedTask; + }); + }; + + command.VisibilityChanged += delegate + { + ThreadHelper.JoinableTaskFactory.Run(() => + { + menuCommand.Visible = command.IsVisible; + return Task.CompletedTask; + }); + }; - commandService.AddCommand(menuCommand); + commandService.AddCommand(menuCommand); + + GitTreeFilterOutput.WriteLine($"Command {command.GetType()} with id {rawCommandId} registered"); + } + else + { + GitTreeFilterOutput.WriteLine($"Command {command.GetType()} with id {rawCommandId} already registered"); + } } } diff --git a/GitTreeFilter/GitTreeFilter.csproj b/GitTreeFilter/GitTreeFilter.csproj index ebb0682..0adb659 100644 --- a/GitTreeFilter/GitTreeFilter.csproj +++ b/GitTreeFilter/GitTreeFilter.csproj @@ -62,6 +62,7 @@ + diff --git a/GitTreeFilter/GitTreeFilterOutput.cs b/GitTreeFilter/GitTreeFilterOutput.cs new file mode 100644 index 0000000..4a51423 --- /dev/null +++ b/GitTreeFilter/GitTreeFilterOutput.cs @@ -0,0 +1,33 @@ +using Microsoft.VisualStudio.Shell; +using Microsoft.VisualStudio.Shell.Interop; +using System; + +public static class GitTreeFilterOutput +{ + private static IVsOutputWindowPane _outputPane; + + public static void Initialize(IServiceProvider serviceProvider) + { + ThreadHelper.ThrowIfNotOnUIThread(); + if (_outputPane == null) + { + var outputWindow = serviceProvider.GetService(typeof(SVsOutputWindow)) as IVsOutputWindow; + if (outputWindow != null) + { + Guid customPaneGuid = Guid.NewGuid(); + outputWindow.CreatePane(ref customPaneGuid, nameof(GitTreeFilter), 1, 1); + outputWindow.GetPane(ref customPaneGuid, out _outputPane); + } + } + } + + public static void WriteLine(string message) + { + ThreadHelper.ThrowIfNotOnUIThread(); + + if (_outputPane != null) + { + _outputPane.OutputStringThreadSafe(message + Environment.NewLine); + } + } +} diff --git a/GitTreeFilter/GitTreeFilterService.cs b/GitTreeFilter/GitTreeFilterService.cs index 59602cd..9847837 100644 --- a/GitTreeFilter/GitTreeFilterService.cs +++ b/GitTreeFilter/GitTreeFilterService.cs @@ -77,7 +77,7 @@ public ISessionSettings SessionSettings public bool IsFilterApplied { - get => _isFilterApplied; + get => _isFilterApplied; // true set { _isFilterApplied = value; @@ -119,6 +119,7 @@ public async Task InitializeAsync(CancellationToken cancellationToken) { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); + GitTreeFilterOutput.Initialize(ServiceProvider.GlobalProvider); _gitExt = ServiceProvider.GlobalProvider.GetService(typeof(IGitExt)) as IGitExt; _dte = await _package.GetServiceAsync(); @@ -151,6 +152,7 @@ private async Task SetUpAsync() if (!TryGetRootRepositoryPath(out string repositoryRootPath)) { + GitTreeFilterOutput.WriteLine("Could not identify GIT repository to use, deactivating the plugin for now"); PluginState = PluginLifecycleState.INACTIVE; Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Could not identify GIT repository to use, deactivating the plugin for now")); return; @@ -174,11 +176,10 @@ private async Task SetUpAsync() LoadSessionSettings(); ItemTagManager.CreateTagTables(); - if (PluginState == PluginLifecycleState.LOADING) - { - await CommandRegistrar.InitializeAsync(_package); - } + GitTreeFilterOutput.WriteLine($"Initializing commands"); + await CommandRegistrar.InitializeAsync(_package); + GitTreeFilterOutput.WriteLine($"Initialization done"); PluginState = PluginLifecycleState.RUNNING; }