From 5701fdf5fa46cda28211cf36b47ca29654cb23e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Krawczyk?= Date: Wed, 29 May 2019 08:01:55 +0200 Subject: [PATCH 1/4] fix after code review --- CocoJumper/Commands/CocoJumperCommandPackage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CocoJumper/Commands/CocoJumperCommandPackage.cs b/CocoJumper/Commands/CocoJumperCommandPackage.cs index fa0a426..e6a390d 100644 --- a/CocoJumper/Commands/CocoJumperCommandPackage.cs +++ b/CocoJumper/Commands/CocoJumperCommandPackage.cs @@ -24,8 +24,8 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke await CocoJumperMultiSearchCommand.InitializeAsync(this); await CocoJumperSingleSearchCommand.InitializeAsync(this); await CocoJumperSingleSearchHighlightCommand.InitializeAsync(this); + await CocoJumperWordSearchCommand.InitializeAsync(this); await base.InitializeAsync(cancellationToken, progress); - await CocoJumper.Commands.CocoJumperWordSearchCommand.InitializeAsync(this); } public int LimitResults From d6fab0c792b62d46bbad24eda0256ca01cf95c9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Krawczyk?= Date: Wed, 29 May 2019 08:24:14 +0200 Subject: [PATCH 2/4] refactor commands and package --- .../Commands/CocoJumperCommandPackage.cs | 53 ++++++++++--------- .../Commands/CocoJumperMultiSearchCommand.cs | 13 +++-- CocoJumper/Commands/CocoJumperOptions.cs | 2 +- .../Commands/CocoJumperSingleSearchCommand.cs | 13 +++-- .../CocoJumperSingleSearchHighlightCommand.cs | 13 +++-- .../Commands/CocoJumperWordSearchCommand.cs | 42 +-------------- CocoJumper/Logic/CocoJumperLogic.cs | 2 +- 7 files changed, 55 insertions(+), 83 deletions(-) diff --git a/CocoJumper/Commands/CocoJumperCommandPackage.cs b/CocoJumper/Commands/CocoJumperCommandPackage.cs index e6a390d..4146096 100644 --- a/CocoJumper/Commands/CocoJumperCommandPackage.cs +++ b/CocoJumper/Commands/CocoJumperCommandPackage.cs @@ -11,84 +11,85 @@ namespace CocoJumper.Commands [PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)] [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] [ProvideMenuResource("Menus.ctmenu", 1)] - [ProvideOptionPage(typeof(CocoJumperOptions), "Environment\\Keyboard", "CocoJumper", 0, 0, true, ProvidesLocalizedCategoryName = false)] + [ProvideOptionPage(typeof(CocoJumperOptions), "Environment\\Keyboard", "CocoJumper", + 0, 0, true, ProvidesLocalizedCategoryName = false)] [Guid(PackageGuidString)] public sealed class CocoJumperCommandPackage : AsyncPackage { public const string PackageGuidString = "cd8f3565-1f57-4c09-b5c1-01fe488ab080"; - protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress progress) - { - await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); - MefProvider.ComponentModel = await GetServiceAsync(typeof(SComponentModel)) as IComponentModel; - await CocoJumperMultiSearchCommand.InitializeAsync(this); - await CocoJumperSingleSearchCommand.InitializeAsync(this); - await CocoJumperSingleSearchHighlightCommand.InitializeAsync(this); - await CocoJumperWordSearchCommand.InitializeAsync(this); - await base.InitializeAsync(cancellationToken, progress); - } - - public int LimitResults + public int AutomaticallyExitInterval { get { CocoJumperOptions page = (CocoJumperOptions)GetDialogPage(typeof(CocoJumperOptions)); - return page.LimitResults; + return page.AutomaticallyExitInterval; } } - public int TimerInterval + public bool DisableHighlightForMultiSearch { get { CocoJumperOptions page = (CocoJumperOptions)GetDialogPage(typeof(CocoJumperOptions)); - return page.TimerInterval; + return page.DisableHighlightForMultiSearch; } } - public bool JumpAfterChoosedElement + public bool DisableHighlightForSingleHighlight { get { CocoJumperOptions page = (CocoJumperOptions)GetDialogPage(typeof(CocoJumperOptions)); - return page.JumpAfterChoosedElement; + return page.DisableHighlightForSingleHighlight; } } - public int AutomaticallyExitInterval + public bool DisableHighlightForSingleSearch { get { CocoJumperOptions page = (CocoJumperOptions)GetDialogPage(typeof(CocoJumperOptions)); - return page.AutomaticallyExitInterval; + return page.DisableHighlightForSingleSearch; } } - public bool DisableHighlightForMultiSearch + public bool JumpAfterChosenElement { get { CocoJumperOptions page = (CocoJumperOptions)GetDialogPage(typeof(CocoJumperOptions)); - return page.DisableHighlightForMultiSearch; + return page.JumpAfterChosenElement; } } - public bool DisableHighlightForSingleHighlight + public int LimitResults { get { CocoJumperOptions page = (CocoJumperOptions)GetDialogPage(typeof(CocoJumperOptions)); - return page.DisableHighlightForSingleHighlight; + return page.LimitResults; } } - public bool DisableHighlightForSingleSearch + public int TimerInterval { get { CocoJumperOptions page = (CocoJumperOptions)GetDialogPage(typeof(CocoJumperOptions)); - return page.DisableHighlightForSingleSearch; + return page.TimerInterval; } } + + protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress progress) + { + await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); + MefProvider.ComponentModel = await GetServiceAsync(typeof(SComponentModel)) as IComponentModel; + await CocoJumperMultiSearchCommand.InitializeAsync(this); + await CocoJumperSingleSearchCommand.InitializeAsync(this); + await CocoJumperSingleSearchHighlightCommand.InitializeAsync(this); + await CocoJumperWordSearchCommand.InitializeAsync(this); + await base.InitializeAsync(cancellationToken, progress); + } } } \ No newline at end of file diff --git a/CocoJumper/Commands/CocoJumperMultiSearchCommand.cs b/CocoJumper/Commands/CocoJumperMultiSearchCommand.cs index 30e1301..d5f9e8d 100644 --- a/CocoJumper/Commands/CocoJumperMultiSearchCommand.cs +++ b/CocoJumper/Commands/CocoJumperMultiSearchCommand.cs @@ -29,12 +29,14 @@ internal class CocoJumperMultiSearchCommand private InputListener _inputListener; private ICocoJumperLogic _logic; - private CocoJumperMultiSearchCommand(AsyncPackage package, OleMenuCommandService commandService, IVsTextManager textManager, IVsEditorAdaptersFactoryService editorAdaptersFactoryService, IEventAggregator eventAggregator) + private CocoJumperMultiSearchCommand(AsyncPackage package, OleMenuCommandService commandService, + IVsTextManager textManager, IVsEditorAdaptersFactoryService editorAdaptersFactoryService, + IEventAggregator eventAggregator) { - this._package = package ?? throw new ArgumentNullException(nameof(package)); + _package = package ?? throw new ArgumentNullException(nameof(package)); commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); _vsTextManager = textManager ?? throw new ArgumentNullException(nameof(textManager)); - this._editorAdaptersFactoryService = editorAdaptersFactoryService ?? throw new ArgumentNullException(nameof(editorAdaptersFactoryService)); + _editorAdaptersFactoryService = editorAdaptersFactoryService ?? throw new ArgumentNullException(nameof(editorAdaptersFactoryService)); eventAggregator.AddListener(new DelegateListener(OnExit), true); CommandID menuCommandId = new CommandID(CommandSet, CommandId); @@ -48,7 +50,10 @@ public static CocoJumperMultiSearchCommand Instance private set; } - private IAsyncServiceProvider ServiceProvider => _package; + private IAsyncServiceProvider ServiceProvider + { + get { return _package; } + } public static async Task InitializeAsync(AsyncPackage package) { diff --git a/CocoJumper/Commands/CocoJumperOptions.cs b/CocoJumper/Commands/CocoJumperOptions.cs index bff74d9..ab64f28 100644 --- a/CocoJumper/Commands/CocoJumperOptions.cs +++ b/CocoJumper/Commands/CocoJumperOptions.cs @@ -38,7 +38,7 @@ public class CocoJumperOptions : DialogPage [DisplayName("Jump after choosed element")] [Description("If set to True, logic will move caret to the end of choosed element.")] [DefaultValue(false)] - public bool JumpAfterChoosedElement { get; set; } = false; + public bool JumpAfterChosenElement { get; set; } = false; [Category(GeneralCategory)] [DisplayName("Limit results")] diff --git a/CocoJumper/Commands/CocoJumperSingleSearchCommand.cs b/CocoJumper/Commands/CocoJumperSingleSearchCommand.cs index 7658c1b..1bae90d 100644 --- a/CocoJumper/Commands/CocoJumperSingleSearchCommand.cs +++ b/CocoJumper/Commands/CocoJumperSingleSearchCommand.cs @@ -30,12 +30,14 @@ internal sealed class CocoJumperSingleSearchCommand private InputListener _inputListener; private ICocoJumperLogic _logic; - private CocoJumperSingleSearchCommand(AsyncPackage package, OleMenuCommandService commandService, IVsTextManager textManager, IVsEditorAdaptersFactoryService editorAdaptersFactoryService, IEventAggregator eventAggregator) + private CocoJumperSingleSearchCommand(AsyncPackage package, OleMenuCommandService commandService, + IVsTextManager textManager, IVsEditorAdaptersFactoryService editorAdaptersFactoryService, + IEventAggregator eventAggregator) { - this._package = package ?? throw new ArgumentNullException(nameof(package)); + _package = package ?? throw new ArgumentNullException(nameof(package)); commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); _vsTextManager = textManager ?? throw new ArgumentNullException(nameof(textManager)); - this._editorAdaptersFactoryService = editorAdaptersFactoryService ?? throw new ArgumentNullException(nameof(editorAdaptersFactoryService)); + _editorAdaptersFactoryService = editorAdaptersFactoryService ?? throw new ArgumentNullException(nameof(editorAdaptersFactoryService)); eventAggregator.AddListener(new DelegateListener(OnExit), true); CommandID menuCommandId = new CommandID(CommandSet, CommandId); @@ -51,10 +53,7 @@ public static CocoJumperSingleSearchCommand Instance private IAsyncServiceProvider ServiceProvider { - get - { - return _package; - } + get { return _package; } } public static async Task InitializeAsync(AsyncPackage package) diff --git a/CocoJumper/Commands/CocoJumperSingleSearchHighlightCommand.cs b/CocoJumper/Commands/CocoJumperSingleSearchHighlightCommand.cs index cbb7655..0c0d9d2 100644 --- a/CocoJumper/Commands/CocoJumperSingleSearchHighlightCommand.cs +++ b/CocoJumper/Commands/CocoJumperSingleSearchHighlightCommand.cs @@ -29,12 +29,14 @@ internal sealed class CocoJumperSingleSearchHighlightCommand private InputListener _inputListener; private ICocoJumperLogic _logic; - private CocoJumperSingleSearchHighlightCommand(AsyncPackage package, OleMenuCommandService commandService, IVsTextManager textManager, IVsEditorAdaptersFactoryService editorAdaptersFactoryService, IEventAggregator eventAggregator) + private CocoJumperSingleSearchHighlightCommand(AsyncPackage package, OleMenuCommandService commandService, + IVsTextManager textManager, IVsEditorAdaptersFactoryService editorAdaptersFactoryService, + IEventAggregator eventAggregator) { - this._package = package ?? throw new ArgumentNullException(nameof(package)); + _package = package ?? throw new ArgumentNullException(nameof(package)); commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); _vsTextManager = textManager ?? throw new ArgumentNullException(nameof(textManager)); - this._editorAdaptersFactoryService = editorAdaptersFactoryService ?? throw new ArgumentNullException(nameof(editorAdaptersFactoryService)); + _editorAdaptersFactoryService = editorAdaptersFactoryService ?? throw new ArgumentNullException(nameof(editorAdaptersFactoryService)); eventAggregator.AddListener(new DelegateListener(OnExit), true); CommandID menuCommandId = new CommandID(CommandSet, CommandId); @@ -48,7 +50,10 @@ public static CocoJumperSingleSearchHighlightCommand Instance private set; } - private IAsyncServiceProvider ServiceProvider => _package; + private IAsyncServiceProvider ServiceProvider + { + get { return _package; } + } public static async Task InitializeAsync(AsyncPackage package) { diff --git a/CocoJumper/Commands/CocoJumperWordSearchCommand.cs b/CocoJumper/Commands/CocoJumperWordSearchCommand.cs index 87dd38c..69ed7df 100644 --- a/CocoJumper/Commands/CocoJumperWordSearchCommand.cs +++ b/CocoJumper/Commands/CocoJumperWordSearchCommand.cs @@ -18,24 +18,12 @@ namespace CocoJumper.Commands { - /// - /// Command handler - /// internal sealed class CocoJumperWordSearchCommand { - /// - /// Command ID. - /// public const int CommandId = 4131; - /// - /// Command menu group (command set GUID). - /// public static readonly Guid CommandSet = new Guid("29fda481-672d-4ce9-9793-0bebf8b4c6c8"); - /// - /// VS Package that provides this command, not null. - /// private readonly IVsEditorAdaptersFactoryService _editorAdaptersFactoryService; private readonly AsyncPackage _package; @@ -43,12 +31,6 @@ internal sealed class CocoJumperWordSearchCommand private InputListener _inputListener; private ICocoJumperLogic _logic; - /// - /// Initializes a new instance of the class. - /// Adds our command handlers for menu (commands must exist in the command table file) - /// - /// Owner package, not null. - /// Command service to add command to, not null. private CocoJumperWordSearchCommand(AsyncPackage package, OleMenuCommandService commandService, IVsTextManager textManager, IVsEditorAdaptersFactoryService editorAdaptersFactoryService, IEventAggregator eventAggregator) @@ -65,30 +47,17 @@ private CocoJumperWordSearchCommand(AsyncPackage package, OleMenuCommandService commandService.AddCommand(menuItem); } - /// - /// Gets the instance of the command. - /// public static CocoJumperWordSearchCommand Instance { get; private set; } - /// - /// Gets the service provider from the owner package. - /// - private Microsoft.VisualStudio.Shell.IAsyncServiceProvider ServiceProvider + private IAsyncServiceProvider ServiceProvider { - get - { - return _package; - } + get { return _package; } } - /// - /// Initializes the singleton instance of the command. - /// - /// Owner package, not null. public static async Task InitializeAsync(AsyncPackage package) { OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService; @@ -111,13 +80,6 @@ private void CleanupLogicAndInputListener() _inputListener = null; } - /// - /// This function is the callback used to execute the command when the menu item is clicked. - /// See the constructor to see how the menu item is associated with this function using - /// OleMenuCommandService service and MenuCommand class. - /// - /// Event sender. - /// Event args. private void Execute(object sender, EventArgs e) { ThreadHelper.ThrowIfNotOnUIThread(); diff --git a/CocoJumper/Logic/CocoJumperLogic.cs b/CocoJumper/Logic/CocoJumperLogic.cs index 3d381c1..7cccbb8 100644 --- a/CocoJumper/Logic/CocoJumperLogic.cs +++ b/CocoJumper/Logic/CocoJumperLogic.cs @@ -40,7 +40,7 @@ public CocoJumperLogic(IWpfViewProvider renderer, CocoJumperCommandPackage packa _disableMultiSearchHighlight = package.DisableHighlightForMultiSearch; _disableSingleSearchHighlight = package.DisableHighlightForSingleSearch; _disableSingleSearchSelectHighlight = package.DisableHighlightForSingleHighlight; - _jumpAfterChosenElement = package.JumpAfterChoosedElement; + _jumpAfterChosenElement = package.JumpAfterChosenElement; _timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(package.TimerInterval) }; _autoExitDispatcherTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(package.AutomaticallyExitInterval) }; _autoExitDispatcherTimer.Tick += OnAutoExitTimerEvent; From d01d4911cbfd24f90c04c2977d219872e2139c9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Krawczyk?= Date: Wed, 29 May 2019 09:17:14 +0200 Subject: [PATCH 3/4] create base command, refactor wordsearchcommand --- CocoJumper.Base/CocoJumper.Base.csproj | 1 + .../Exception/NotFoundException.cs | 9 ++ CocoJumper/CocoJumper.csproj | 1 + CocoJumper/Commands/CocoJumperBaseCommand.cs | 114 ++++++++++++++++++ .../Commands/CocoJumperWordSearchCommand.cs | 85 ++----------- 5 files changed, 133 insertions(+), 77 deletions(-) create mode 100644 CocoJumper.Base/Exception/NotFoundException.cs create mode 100644 CocoJumper/Commands/CocoJumperBaseCommand.cs diff --git a/CocoJumper.Base/CocoJumper.Base.csproj b/CocoJumper.Base/CocoJumper.Base.csproj index 90fb5e0..d48def6 100644 --- a/CocoJumper.Base/CocoJumper.Base.csproj +++ b/CocoJumper.Base/CocoJumper.Base.csproj @@ -80,6 +80,7 @@ + diff --git a/CocoJumper.Base/Exception/NotFoundException.cs b/CocoJumper.Base/Exception/NotFoundException.cs new file mode 100644 index 0000000..d8eb312 --- /dev/null +++ b/CocoJumper.Base/Exception/NotFoundException.cs @@ -0,0 +1,9 @@ +namespace CocoJumper.Base.Exception +{ + public class NotFoundException : System.Exception + { + public NotFoundException(string message) : base(message) + { + } + } +} \ No newline at end of file diff --git a/CocoJumper/CocoJumper.csproj b/CocoJumper/CocoJumper.csproj index 47de93b..ade8c57 100644 --- a/CocoJumper/CocoJumper.csproj +++ b/CocoJumper/CocoJumper.csproj @@ -64,6 +64,7 @@ + diff --git a/CocoJumper/Commands/CocoJumperBaseCommand.cs b/CocoJumper/Commands/CocoJumperBaseCommand.cs new file mode 100644 index 0000000..86d278f --- /dev/null +++ b/CocoJumper/Commands/CocoJumperBaseCommand.cs @@ -0,0 +1,114 @@ +using CocoJumper.Base.Enum; +using CocoJumper.Base.Events; +using CocoJumper.Base.Exception; +using CocoJumper.Base.Logic; +using CocoJumper.Events; +using CocoJumper.Extensions; +using CocoJumper.Listeners; +using CocoJumper.Logic; +using CocoJumper.Provider; +using Microsoft.VisualStudio.ComponentModelHost; +using Microsoft.VisualStudio.Editor; +using Microsoft.VisualStudio.Shell; +using Microsoft.VisualStudio.Text.Editor; +using Microsoft.VisualStudio.TextManager.Interop; +using System; +using System.ComponentModel.Design; +using System.Threading.Tasks; + +namespace CocoJumper.Commands +{ + internal abstract class CocoJumperBaseCommand + { + protected ICocoJumperLogic Logic; + private readonly IVsEditorAdaptersFactoryService _editorAdaptersFactoryService; + private readonly AsyncPackage _package; + private readonly IVsTextManager _vsTextManager; + private InputListener _inputListener; + + protected CocoJumperBaseCommand(AsyncPackage package, + OleMenuCommandService commandService, + IVsTextManager textManager, + IVsEditorAdaptersFactoryService editorAdaptersFactoryService, + IEventAggregator eventAggregator, + Guid commandSet, + int commandId) + { + _package = package ?? throw new ArgumentNullException(nameof(package)); + + _vsTextManager = textManager ?? throw new ArgumentNullException(nameof(textManager)); + _editorAdaptersFactoryService = editorAdaptersFactoryService ?? throw new ArgumentNullException(nameof(editorAdaptersFactoryService)); + + eventAggregator.AddListener(new DelegateListener(OnExit), true); + + commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); + CommandID menuCommandID = new CommandID(commandSet, commandId); + MenuCommand menuItem = new MenuCommand(Execute, menuCommandID); + commandService.AddCommand(menuItem); + } + + protected IAsyncServiceProvider ServiceProvider + { + get { return _package; } + } + + protected static async + Task<(OleMenuCommandService commandService, + IVsTextManager vsTextManager, + IVsEditorAdaptersFactoryService editor, + IEventAggregator eventAggregator)> GetServicesAsync(AsyncPackage package) + { + OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService; + IVsTextManager vsTextManager = await package.GetServiceAsync(typeof(SVsTextManager)) as IVsTextManager; + IComponentModel componentModel = await package.GetServiceAsync(typeof(SComponentModel)) as IComponentModel + ?? throw new NotFoundException($"{nameof(IComponentModel)} not found."); + IVsEditorAdaptersFactoryService editor = componentModel.GetService(); + IEventAggregator eventAggregator = componentModel.GetService(); + + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); + + return (commandService, vsTextManager, editor, eventAggregator); + } + + protected void Execute(object sender, EventArgs e) + { + ThreadHelper.ThrowIfNotOnUIThread(); + IVsTextView textView = _vsTextManager.GetActiveView(); + IWpfTextView wpfTextView = _editorAdaptersFactoryService.GetWpfTextView(textView); + CocoJumperCommandPackage cocoJumperCommandPackage = (CocoJumperCommandPackage)_package; + + CleanupLogicAndInputListener(); + WpfViewProvider renderer = new WpfViewProvider(wpfTextView); + + Logic = new CocoJumperLogic(renderer, cocoJumperCommandPackage); + _inputListener = new InputListener(textView); + _inputListener.KeyPressEvent += OnKeyboardAction; + + ExecutePostAction(); + } + + protected abstract void ExecutePostAction(); + + protected void OnExit(ExitEvent e) + { + CleanupLogicAndInputListener(); + } + + protected void OnKeyboardAction(object oSender, char? key, KeyEventType eventType) + { + Logic = Logic ?? throw new Exception($"{nameof(OnKeyboardAction)} in {nameof(CocoJumperBaseCommand)}, {nameof(Logic)} is null"); + if (Logic.KeyboardAction(key, eventType) == CocoJumperKeyboardActionResult.Finished) + { + CleanupLogicAndInputListener(); + } + } + + private void CleanupLogicAndInputListener() + { + Logic?.Dispose(); + _inputListener?.Dispose(); + Logic = null; + _inputListener = null; + } + } +} \ No newline at end of file diff --git a/CocoJumper/Commands/CocoJumperWordSearchCommand.cs b/CocoJumper/Commands/CocoJumperWordSearchCommand.cs index 69ed7df..1b6f6b6 100644 --- a/CocoJumper/Commands/CocoJumperWordSearchCommand.cs +++ b/CocoJumper/Commands/CocoJumperWordSearchCommand.cs @@ -1,50 +1,25 @@ using CocoJumper.Base.Enum; -using CocoJumper.Base.Events; -using CocoJumper.Base.Logic; using CocoJumper.Events; -using CocoJumper.Extensions; -using CocoJumper.Listeners; -using CocoJumper.Logic; -using CocoJumper.Provider; -using Microsoft; -using Microsoft.VisualStudio.ComponentModelHost; using Microsoft.VisualStudio.Editor; using Microsoft.VisualStudio.Shell; -using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.TextManager.Interop; using System; -using System.ComponentModel.Design; using Task = System.Threading.Tasks.Task; namespace CocoJumper.Commands { - internal sealed class CocoJumperWordSearchCommand + internal sealed class CocoJumperWordSearchCommand : CocoJumperBaseCommand { public const int CommandId = 4131; public static readonly Guid CommandSet = new Guid("29fda481-672d-4ce9-9793-0bebf8b4c6c8"); - private readonly IVsEditorAdaptersFactoryService _editorAdaptersFactoryService; - - private readonly AsyncPackage _package; - private readonly IVsTextManager _vsTextManager; - private InputListener _inputListener; - private ICocoJumperLogic _logic; - private CocoJumperWordSearchCommand(AsyncPackage package, OleMenuCommandService commandService, IVsTextManager textManager, IVsEditorAdaptersFactoryService editorAdaptersFactoryService, IEventAggregator eventAggregator) + : base(package, commandService, textManager, editorAdaptersFactoryService, + eventAggregator, CommandSet, CommandId) { - _package = package ?? throw new ArgumentNullException(nameof(package)); - commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); - - _vsTextManager = textManager ?? throw new ArgumentNullException(nameof(textManager)); - _editorAdaptersFactoryService = editorAdaptersFactoryService ?? throw new ArgumentNullException(nameof(editorAdaptersFactoryService)); - eventAggregator.AddListener(new DelegateListener(OnExit), true); - - CommandID menuCommandID = new CommandID(CommandSet, CommandId); - MenuCommand menuItem = new MenuCommand(Execute, menuCommandID); - commandService.AddCommand(menuItem); } public static CocoJumperWordSearchCommand Instance @@ -53,62 +28,18 @@ public static CocoJumperWordSearchCommand Instance private set; } - private IAsyncServiceProvider ServiceProvider - { - get { return _package; } - } - public static async Task InitializeAsync(AsyncPackage package) { - OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService; - IVsTextManager vsTextManager = await package.GetServiceAsync(typeof(SVsTextManager)) as IVsTextManager; - IComponentModel componentModel = await package.GetServiceAsync(typeof(SComponentModel)) as IComponentModel; - Assumes.Present(componentModel); - IVsEditorAdaptersFactoryService editor = componentModel.GetService(); - IEventAggregator eventAggregator = componentModel.GetService(); - - await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); - Instance = new CocoJumperWordSearchCommand(package, commandService, vsTextManager, editor, - eventAggregator); - } + var (commandService, vsTextManager, editor, eventAggregator) = await GetServicesAsync(package); - private void CleanupLogicAndInputListener() - { - _logic?.Dispose(); - _inputListener?.Dispose(); - _logic = null; - _inputListener = null; + Instance = new CocoJumperWordSearchCommand(package, commandService, + vsTextManager, editor, eventAggregator); } - private void Execute(object sender, EventArgs e) + protected override void ExecutePostAction() { - ThreadHelper.ThrowIfNotOnUIThread(); - IVsTextView textView = _vsTextManager.GetActiveView(); - IWpfTextView wpfTextView = _editorAdaptersFactoryService.GetWpfTextView(textView); - CocoJumperCommandPackage cocoJumperCommandPackage = (CocoJumperCommandPackage)_package; - - CleanupLogicAndInputListener(); - WpfViewProvider renderer = new WpfViewProvider(wpfTextView); - - _logic = new CocoJumperLogic(renderer, cocoJumperCommandPackage); - _inputListener = new InputListener(textView); - _inputListener.KeyPressEvent += OnKeyboardAction; - _logic.ActivateSearching(false, false, true); + Logic.ActivateSearching(false, false, true); OnKeyboardAction(this, null, KeyEventType.KeyPress); } - - private void OnExit(ExitEvent e) - { - CleanupLogicAndInputListener(); - } - - private void OnKeyboardAction(object oSender, char? key, KeyEventType eventType) - { - _logic = _logic ?? throw new Exception($"{nameof(OnKeyboardAction)} in {nameof(CocoJumperWordSearchCommand)}, {nameof(_logic)} is null"); - if (_logic.KeyboardAction(key, eventType) == CocoJumperKeyboardActionResult.Finished) - { - CleanupLogicAndInputListener(); - } - } } } \ No newline at end of file From fdd524db66173b05dee4fa3c01d373332f2f22a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Krawczyk?= Date: Wed, 29 May 2019 10:02:22 +0200 Subject: [PATCH 4/4] refactor rest of the commands --- .../Commands/CocoJumperMultiSearchCommand.cs | 84 ++---------------- .../Commands/CocoJumperSingleSearchCommand.cs | 85 ++----------------- .../CocoJumperSingleSearchHighlightCommand.cs | 84 ++---------------- 3 files changed, 27 insertions(+), 226 deletions(-) diff --git a/CocoJumper/Commands/CocoJumperMultiSearchCommand.cs b/CocoJumper/Commands/CocoJumperMultiSearchCommand.cs index d5f9e8d..3ff6940 100644 --- a/CocoJumper/Commands/CocoJumperMultiSearchCommand.cs +++ b/CocoJumper/Commands/CocoJumperMultiSearchCommand.cs @@ -1,47 +1,24 @@ -using CocoJumper.Base.Enum; -using CocoJumper.Base.Events; -using CocoJumper.Base.Logic; -using CocoJumper.Events; -using CocoJumper.Extensions; -using CocoJumper.Listeners; -using CocoJumper.Logic; -using CocoJumper.Provider; -using Microsoft; -using Microsoft.VisualStudio.ComponentModelHost; +using CocoJumper.Events; using Microsoft.VisualStudio.Editor; using Microsoft.VisualStudio.Shell; -using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.TextManager.Interop; using System; -using System.ComponentModel.Design; using Task = System.Threading.Tasks.Task; namespace CocoJumper.Commands { - internal class CocoJumperMultiSearchCommand + internal class CocoJumperMultiSearchCommand : CocoJumperBaseCommand { public const int CommandId = 0x0100; public static readonly Guid CommandSet = new Guid("29fda481-672d-4ce9-9793-0bebf8b4c6c8"); - private readonly IVsEditorAdaptersFactoryService _editorAdaptersFactoryService; - private readonly AsyncPackage _package; - private readonly IVsTextManager _vsTextManager; - private InputListener _inputListener; - private ICocoJumperLogic _logic; private CocoJumperMultiSearchCommand(AsyncPackage package, OleMenuCommandService commandService, IVsTextManager textManager, IVsEditorAdaptersFactoryService editorAdaptersFactoryService, IEventAggregator eventAggregator) + : base(package, commandService, textManager, editorAdaptersFactoryService, + eventAggregator, CommandSet, CommandId) { - _package = package ?? throw new ArgumentNullException(nameof(package)); - commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); - _vsTextManager = textManager ?? throw new ArgumentNullException(nameof(textManager)); - _editorAdaptersFactoryService = editorAdaptersFactoryService ?? throw new ArgumentNullException(nameof(editorAdaptersFactoryService)); - eventAggregator.AddListener(new DelegateListener(OnExit), true); - - CommandID menuCommandId = new CommandID(CommandSet, CommandId); - MenuCommand menuItem = new MenuCommand(Execute, menuCommandId); - commandService.AddCommand(menuItem); } public static CocoJumperMultiSearchCommand Instance @@ -50,60 +27,17 @@ public static CocoJumperMultiSearchCommand Instance private set; } - private IAsyncServiceProvider ServiceProvider - { - get { return _package; } - } - public static async Task InitializeAsync(AsyncPackage package) { - OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService; - IVsTextManager vsTextManager = await package.GetServiceAsync(typeof(SVsTextManager)) as IVsTextManager; - IComponentModel componentModel = await package.GetServiceAsync(typeof(SComponentModel)) as IComponentModel; - Assumes.Present(componentModel); - IVsEditorAdaptersFactoryService editor = componentModel.GetService(); - IEventAggregator eventAggregator = componentModel.GetService(); + var (commandService, vsTextManager, editor, eventAggregator) = await GetServicesAsync(package); - await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); - Instance = new CocoJumperMultiSearchCommand(package, commandService, vsTextManager, editor, eventAggregator); - } - - private void CleanupLogicAndInputListener() - { - _logic?.Dispose(); - _inputListener?.Dispose(); - _logic = null; - _inputListener = null; - } - - private void Execute(object sender, EventArgs e) - { - ThreadHelper.ThrowIfNotOnUIThread(); - IVsTextView textView = _vsTextManager.GetActiveView(); - IWpfTextView wpfTextView = _editorAdaptersFactoryService.GetWpfTextView(textView); - CocoJumperCommandPackage cocoJumperCommandPackage = (CocoJumperCommandPackage)_package; - - CleanupLogicAndInputListener(); - WpfViewProvider renderer = new WpfViewProvider(wpfTextView); - _logic = new CocoJumperLogic(renderer, cocoJumperCommandPackage); - - _inputListener = new InputListener(textView); - _inputListener.KeyPressEvent += OnKeyboardAction; - _logic.ActivateSearching(false, false, false); - } - - private void OnExit(ExitEvent e) - { - CleanupLogicAndInputListener(); + Instance = new CocoJumperMultiSearchCommand(package, commandService, + vsTextManager, editor, eventAggregator); } - private void OnKeyboardAction(object oSender, char? key, KeyEventType eventType) + protected override void ExecutePostAction() { - _logic = _logic ?? throw new Exception($"{nameof(OnKeyboardAction)} in {nameof(CocoJumperMultiSearchCommand)}, {nameof(_logic)} is null"); - if (_logic.KeyboardAction(key, eventType) == CocoJumperKeyboardActionResult.Finished) - { - CleanupLogicAndInputListener(); - } + Logic.ActivateSearching(false, false, false); } } } \ No newline at end of file diff --git a/CocoJumper/Commands/CocoJumperSingleSearchCommand.cs b/CocoJumper/Commands/CocoJumperSingleSearchCommand.cs index 1bae90d..3528ace 100644 --- a/CocoJumper/Commands/CocoJumperSingleSearchCommand.cs +++ b/CocoJumper/Commands/CocoJumperSingleSearchCommand.cs @@ -1,48 +1,24 @@ -using CocoJumper.Base.Enum; -using CocoJumper.Base.Events; -using CocoJumper.Base.Logic; -using CocoJumper.Events; -using CocoJumper.Extensions; -using CocoJumper.Listeners; -using CocoJumper.Logic; -using CocoJumper.Provider; -using Microsoft; -using Microsoft.VisualStudio.ComponentModelHost; +using CocoJumper.Events; using Microsoft.VisualStudio.Editor; using Microsoft.VisualStudio.Shell; -using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.TextManager.Interop; using System; -using System.ComponentModel.Design; using Task = System.Threading.Tasks.Task; namespace CocoJumper.Commands { - internal sealed class CocoJumperSingleSearchCommand + internal sealed class CocoJumperSingleSearchCommand : CocoJumperBaseCommand { public const int CommandId = 4129; public static readonly Guid CommandSet = new Guid("29fda481-672d-4ce9-9793-0bebf8b4c6c8"); - private readonly IVsEditorAdaptersFactoryService _editorAdaptersFactoryService; - private readonly AsyncPackage _package; - private readonly IVsTextManager _vsTextManager; - private InputListener _inputListener; - private ICocoJumperLogic _logic; - private CocoJumperSingleSearchCommand(AsyncPackage package, OleMenuCommandService commandService, IVsTextManager textManager, IVsEditorAdaptersFactoryService editorAdaptersFactoryService, IEventAggregator eventAggregator) + : base(package, commandService, textManager, editorAdaptersFactoryService, + eventAggregator, CommandSet, CommandId) { - _package = package ?? throw new ArgumentNullException(nameof(package)); - commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); - _vsTextManager = textManager ?? throw new ArgumentNullException(nameof(textManager)); - _editorAdaptersFactoryService = editorAdaptersFactoryService ?? throw new ArgumentNullException(nameof(editorAdaptersFactoryService)); - eventAggregator.AddListener(new DelegateListener(OnExit), true); - - CommandID menuCommandId = new CommandID(CommandSet, CommandId); - MenuCommand menuItem = new MenuCommand(Execute, menuCommandId); - commandService.AddCommand(menuItem); } public static CocoJumperSingleSearchCommand Instance @@ -51,60 +27,17 @@ public static CocoJumperSingleSearchCommand Instance private set; } - private IAsyncServiceProvider ServiceProvider - { - get { return _package; } - } - public static async Task InitializeAsync(AsyncPackage package) { - OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService; - IVsTextManager vsTextManager = await package.GetServiceAsync(typeof(SVsTextManager)) as IVsTextManager; - IComponentModel componentModel = await package.GetServiceAsync(typeof(SComponentModel)) as IComponentModel; - Assumes.Present(componentModel); - IVsEditorAdaptersFactoryService editor = componentModel.GetService(); - IEventAggregator eventAggregator = componentModel.GetService(); - - await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); - Instance = new CocoJumperSingleSearchCommand(package, commandService, vsTextManager, editor, eventAggregator); - } - - private void CleanupLogicAndInputListener() - { - _logic?.Dispose(); - _inputListener?.Dispose(); - _logic = null; - _inputListener = null; - } - - private void Execute(object sender, EventArgs e) - { - ThreadHelper.ThrowIfNotOnUIThread(); - IVsTextView textView = _vsTextManager.GetActiveView(); - IWpfTextView wpfTextView = _editorAdaptersFactoryService.GetWpfTextView(textView); - CocoJumperCommandPackage cocoJumperCommandPackage = (CocoJumperCommandPackage)_package; + var (commandService, vsTextManager, editor, eventAggregator) = await GetServicesAsync(package); - CleanupLogicAndInputListener(); - WpfViewProvider renderer = new WpfViewProvider(wpfTextView); - - _logic = new CocoJumperLogic(renderer, cocoJumperCommandPackage); - _inputListener = new InputListener(textView); - _inputListener.KeyPressEvent += OnKeyboardAction; - _logic.ActivateSearching(true, false, false); - } - - private void OnExit(ExitEvent e) - { - CleanupLogicAndInputListener(); + Instance = new CocoJumperSingleSearchCommand(package, commandService, + vsTextManager, editor, eventAggregator); } - private void OnKeyboardAction(object oSender, char? key, KeyEventType eventType) + protected override void ExecutePostAction() { - _logic = _logic ?? throw new Exception($"{nameof(OnKeyboardAction)} in {nameof(CocoJumperMultiSearchCommand)}, {nameof(_logic)} is null"); - if (_logic.KeyboardAction(key, eventType) == CocoJumperKeyboardActionResult.Finished) - { - CleanupLogicAndInputListener(); - } + Logic.ActivateSearching(true, false, false); } } } \ No newline at end of file diff --git a/CocoJumper/Commands/CocoJumperSingleSearchHighlightCommand.cs b/CocoJumper/Commands/CocoJumperSingleSearchHighlightCommand.cs index 0c0d9d2..f3c4ebb 100644 --- a/CocoJumper/Commands/CocoJumperSingleSearchHighlightCommand.cs +++ b/CocoJumper/Commands/CocoJumperSingleSearchHighlightCommand.cs @@ -1,47 +1,24 @@ -using CocoJumper.Base.Enum; -using CocoJumper.Base.Events; -using CocoJumper.Base.Logic; -using CocoJumper.Events; -using CocoJumper.Extensions; -using CocoJumper.Listeners; -using CocoJumper.Logic; -using CocoJumper.Provider; -using Microsoft; -using Microsoft.VisualStudio.ComponentModelHost; +using CocoJumper.Events; using Microsoft.VisualStudio.Editor; using Microsoft.VisualStudio.Shell; -using Microsoft.VisualStudio.Text.Editor; using Microsoft.VisualStudio.TextManager.Interop; using System; -using System.ComponentModel.Design; using Task = System.Threading.Tasks.Task; namespace CocoJumper.Commands { - internal sealed class CocoJumperSingleSearchHighlightCommand + internal sealed class CocoJumperSingleSearchHighlightCommand : CocoJumperBaseCommand { public const int CommandId = 4130; public static readonly Guid CommandSet = new Guid("29fda481-672d-4ce9-9793-0bebf8b4c6c8"); - private readonly IVsEditorAdaptersFactoryService _editorAdaptersFactoryService; - private readonly AsyncPackage _package; - private readonly IVsTextManager _vsTextManager; - private InputListener _inputListener; - private ICocoJumperLogic _logic; private CocoJumperSingleSearchHighlightCommand(AsyncPackage package, OleMenuCommandService commandService, IVsTextManager textManager, IVsEditorAdaptersFactoryService editorAdaptersFactoryService, IEventAggregator eventAggregator) + : base(package, commandService, textManager, editorAdaptersFactoryService, + eventAggregator, CommandSet, CommandId) { - _package = package ?? throw new ArgumentNullException(nameof(package)); - commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); - _vsTextManager = textManager ?? throw new ArgumentNullException(nameof(textManager)); - _editorAdaptersFactoryService = editorAdaptersFactoryService ?? throw new ArgumentNullException(nameof(editorAdaptersFactoryService)); - eventAggregator.AddListener(new DelegateListener(OnExit), true); - - CommandID menuCommandId = new CommandID(CommandSet, CommandId); - MenuCommand menuItem = new MenuCommand(Execute, menuCommandId); - commandService.AddCommand(menuItem); } public static CocoJumperSingleSearchHighlightCommand Instance @@ -50,60 +27,17 @@ public static CocoJumperSingleSearchHighlightCommand Instance private set; } - private IAsyncServiceProvider ServiceProvider - { - get { return _package; } - } - public static async Task InitializeAsync(AsyncPackage package) { - OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService; - IVsTextManager vsTextManager = await package.GetServiceAsync(typeof(SVsTextManager)) as IVsTextManager; - IComponentModel componentModel = await package.GetServiceAsync(typeof(SComponentModel)) as IComponentModel; - Assumes.Present(componentModel); - IVsEditorAdaptersFactoryService editor = componentModel.GetService(); - IEventAggregator eventAggregator = componentModel.GetService(); + var (commandService, vsTextManager, editor, eventAggregator) = await GetServicesAsync(package); - await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); - Instance = new CocoJumperSingleSearchHighlightCommand(package, commandService, vsTextManager, editor, eventAggregator); - } - - private void CleanupLogicAndInputListener() - { - _logic?.Dispose(); - _inputListener?.Dispose(); - _logic = null; - _inputListener = null; - } - - private void Execute(object sender, EventArgs e) - { - ThreadHelper.ThrowIfNotOnUIThread(); - IVsTextView textView = _vsTextManager.GetActiveView(); - IWpfTextView wpfTextView = _editorAdaptersFactoryService.GetWpfTextView(textView); - CocoJumperCommandPackage cocoJumperCommandPackage = (CocoJumperCommandPackage)_package; - - CleanupLogicAndInputListener(); - WpfViewProvider renderer = new WpfViewProvider(wpfTextView); - - _logic = new CocoJumperLogic(renderer, cocoJumperCommandPackage); - _inputListener = new InputListener(textView); - _inputListener.KeyPressEvent += OnKeyboardAction; - _logic.ActivateSearching(true, true, false); - } - - private void OnExit(ExitEvent e) - { - CleanupLogicAndInputListener(); + Instance = new CocoJumperSingleSearchHighlightCommand(package, commandService, + vsTextManager, editor, eventAggregator); } - private void OnKeyboardAction(object oSender, char? key, KeyEventType eventType) + protected override void ExecutePostAction() { - _logic = _logic ?? throw new Exception($"{nameof(OnKeyboardAction)} in {nameof(CocoJumperMultiSearchCommand)}, {nameof(_logic)} is null"); - if (_logic.KeyboardAction(key, eventType) == CocoJumperKeyboardActionResult.Finished) - { - CleanupLogicAndInputListener(); - } + Logic.ActivateSearching(true, true, false); } } } \ No newline at end of file