Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify some duplicated codes and make them testable/mockable #3823

Merged
merged 23 commits into from Mar 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5a2bffe
Reorganize the classes pertaining to VBE runtime and settings together.
bclothier Mar 4, 2018
41106f0
Add previoulsy versioned files now unversioned... (wth?)
bclothier Mar 4, 2018
f52c309
Remove a redundant folder
bclothier Mar 4, 2018
6e223a7
Enable VBERuntimeAccessor to take version from the VBE.Version via VB…
bclothier Mar 4, 2018
14f4913
Simplify the ctor, avoid the newing of VBESettings.
bclothier Mar 4, 2018
423349a
Streamline the version check logic
bclothier Mar 4, 2018
4864939
Setup the new UiContext class whose sole responsibility is to affirm …
bclothier Mar 4, 2018
1b123d1
Register the singleton UIContext with the IoC
bclothier Mar 4, 2018
e9363f7
Make ComMessagePumper a non-static class and provide an interface
bclothier Mar 4, 2018
5027d0b
Remove unneeded ComMessagePumper static invocations.
bclothier Mar 4, 2018
e28b597
Add TaskScheduler to the UiContext
bclothier Mar 4, 2018
6924cab
Provide a better name and update references
bclothier Mar 4, 2018
e603df2
Make UiDispatcher a non-static class
bclothier Mar 4, 2018
6af85b0
Update tests to mock the IUiDispatcher
bclothier Mar 4, 2018
489b073
Fix the check on the UiDispatcher; it does not need the CheckContext.
bclothier Mar 4, 2018
9ef93be
Make ComMessagePumper take IVBERuntime as an injected dependency inst…
bclothier Mar 5, 2018
af33e82
Correct the logic for checking the thread in the pumper.
bclothier Mar 6, 2018
fcd3dca
Remove unused using declaration
bclothier Mar 10, 2018
8575191
Merge branch 'next' of https://github.com/rubberduck-vba/Rubberduck i…
bclothier Mar 14, 2018
b6cc24e
Clean up the botched merge
bclothier Mar 14, 2018
606f828
Address feedbacks and restore some missing code from merge.
bclothier Mar 15, 2018
195684d
Make VBEEvents mockable, thanks to Mug
bclothier Mar 15, 2018
1a5e59f
Make the guard clauses in RPS explicit, add ignore attribute. Add exp…
bclothier Mar 15, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 12 additions & 7 deletions Rubberduck.API/API/VBA/ParserState.cs
Expand Up @@ -11,7 +11,9 @@
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.UIContext;
using Rubberduck.VBEditor.ComManagement;
using Rubberduck.VBEditor.Events;
using Rubberduck.VBEditor.SafeComWrappers.VBA;
using Rubberduck.VBEditor.Utility;

namespace Rubberduck.API.VBA
{
Expand Down Expand Up @@ -50,11 +52,13 @@ public sealed class ParserState : IParserState, IDisposable
private AttributeParser _attributeParser;
private ParseCoordinator _parser;
private VBE _vbe;
private IVBEEvents _vbeEvents;
private readonly IUiDispatcher _dispatcher;

public ParserState()
{
UiDispatcher.Initialize();
ComMessagePumper.Initialize();
UiContextProvider.Initialize();
_dispatcher = new UiDispatcher(UiContextProvider.Instance());
}

public void Initialize(Microsoft.Vbe.Interop.VBE vbe)
Expand All @@ -65,9 +69,10 @@ public void Initialize(Microsoft.Vbe.Interop.VBE vbe)
}

_vbe = new VBE(vbe);
_vbeEvents = VBEEvents.Initialize(_vbe);
var declarationFinderFactory = new ConcurrentlyConstructedDeclarationFinderFactory();
var projectRepository = new ProjectsRepository(_vbe);
_state = new RubberduckParserState(null, projectRepository, declarationFinderFactory);
_state = new RubberduckParserState(null, projectRepository, declarationFinderFactory, _vbeEvents);
_state.StateChanged += _state_StateChanged;

var exporter = new ModuleExporter();
Expand Down Expand Up @@ -144,7 +149,7 @@ public void Parse()
public void BeginParse()
{
// non-blocking call
UiDispatcher.Invoke(() => _state.OnParseRequested(this));
_dispatcher.Invoke(() => _state.OnParseRequested(this));
}

public event Action OnParsed;
Expand All @@ -164,19 +169,19 @@ private void _state_StateChanged(object sender, EventArgs e)
var errorHandler = OnError;
if (_state.Status == Parsing.VBA.ParserState.Error && errorHandler != null)
{
UiDispatcher.Invoke(errorHandler.Invoke);
_dispatcher.Invoke(errorHandler.Invoke);
}

var parsedHandler = OnParsed;
if (_state.Status == Parsing.VBA.ParserState.Parsed && parsedHandler != null)
{
UiDispatcher.Invoke(parsedHandler.Invoke);
_dispatcher.Invoke(parsedHandler.Invoke);
}

var readyHandler = OnReady;
if (_state.Status == Parsing.VBA.ParserState.Ready && readyHandler != null)
{
UiDispatcher.Invoke(readyHandler.Invoke);
_dispatcher.Invoke(readyHandler.Invoke);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Rubberduck.Core/App.cs
Expand Up @@ -15,6 +15,7 @@
using Rubberduck.UI.Command;
using Rubberduck.VBEditor.ComManagement;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
using Rubberduck.VBEditor.Utility;
using Rubberduck.VersionCheck;
using Application = System.Windows.Forms.Application;

Expand Down Expand Up @@ -50,8 +51,7 @@ public sealed class App : IDisposable

_configService.SettingsChanged += _configService_SettingsChanged;

UiDispatcher.Initialize();
ComMessagePumper.Initialize();
UiContextProvider.Initialize();
}

private void _configService_SettingsChanged(object sender, ConfigurationChangedEventArgs e)
Expand Down
14 changes: 10 additions & 4 deletions Rubberduck.Core/Navigation/CodeExplorer/CodeExplorerViewModel.cs
Expand Up @@ -14,7 +14,6 @@
using Rubberduck.UI;
using Rubberduck.UI.CodeExplorer.Commands;
using Rubberduck.UI.Command;
using Rubberduck.UI.Command.MenuItems;
using Rubberduck.VBEditor;
using Rubberduck.VBEditor.SafeComWrappers;
using System.Windows;
Expand All @@ -32,17 +31,24 @@ public sealed class CodeExplorerViewModel : ViewModelBase, IDisposable
private readonly IConfigProvider<WindowSettings> _windowSettingsProvider;
private readonly GeneralSettings _generalSettings;
private readonly WindowSettings _windowSettings;
private readonly IUiDispatcher _uiDispatcher;

private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

public CodeExplorerViewModel(FolderHelper folderHelper, RubberduckParserState state, List<CommandBase> commands,
IConfigProvider<GeneralSettings> generalSettingsProvider, IConfigProvider<WindowSettings> windowSettingsProvider)
public CodeExplorerViewModel(
FolderHelper folderHelper,
RubberduckParserState state,
List<CommandBase> commands,
IConfigProvider<GeneralSettings> generalSettingsProvider,
IConfigProvider<WindowSettings> windowSettingsProvider,
IUiDispatcher uiDispatcher)
{
_folderHelper = folderHelper;
_state = state;
_state.StateChanged += HandleStateChanged;
_state.ModuleStateChanged += ParserState_ModuleStateChanged;
_windowSettingsProvider = windowSettingsProvider;
_uiDispatcher = uiDispatcher;

if (generalSettingsProvider != null)
{
Expand Down Expand Up @@ -388,7 +394,7 @@ private void ParserState_ModuleStateChanged(object sender, ParseProgressEventArg
var projectName = componentProject.Name;
var folderNode = projectNode.Items.FirstOrDefault(f => f is CodeExplorerCustomFolderViewModel && f.Name == projectName);

UiDispatcher.Invoke(() =>
_uiDispatcher.Invoke(() =>
{
try
{
Expand Down
3 changes: 0 additions & 3 deletions Rubberduck.Core/Rubberduck.Core.csproj
Expand Up @@ -475,9 +475,6 @@
<Compile Include="UnitTesting\IFakes.cs" />
<Compile Include="UnitTesting\ReturnTypeConverter.cs" />
<Compile Include="UnitTesting\PermissiveObjectComparer.cs" />
<Compile Include="VBERuntime\RegistryWrapper.cs" />
<Compile Include="VBERuntime\IVBESettings.cs" />
<Compile Include="VBERuntime\VBESettings.cs" />
<Compile Include="VersionCheck\IVersionCheck.cs" />
<Compile Include="UI\Command\MenuItems\CommandBars\AppCommandBarBase.cs" />
<Compile Include="UI\Command\MenuItems\CommandBars\ContextSelectionLabelMenuItem.cs" />
Expand Down
6 changes: 4 additions & 2 deletions Rubberduck.Core/UI/Command/FindAllImplementationsCommand.cs
Expand Up @@ -28,12 +28,13 @@ public class FindAllImplementationsCommand : CommandBase, IDisposable
private readonly ISearchResultsWindowViewModel _viewModel;
private readonly SearchResultPresenterInstanceManager _presenterService;
private readonly IVBE _vbe;
private readonly IUiDispatcher _uiDispatcher;

private new static readonly Logger Logger = LogManager.GetCurrentClassLogger();

public FindAllImplementationsCommand(INavigateCommand navigateCommand, IMessageBox messageBox,
RubberduckParserState state, IVBE vbe, ISearchResultsWindowViewModel viewModel,
SearchResultPresenterInstanceManager presenterService)
SearchResultPresenterInstanceManager presenterService, IUiDispatcher uiDispatcher)
: base(LogManager.GetCurrentClassLogger())
{
_navigateCommand = navigateCommand;
Expand All @@ -42,6 +43,7 @@ public class FindAllImplementationsCommand : CommandBase, IDisposable
_vbe = vbe;
_viewModel = viewModel;
_presenterService = presenterService;
_uiDispatcher = uiDispatcher;

_state.StateChanged += _state_StateChanged;
}
Expand All @@ -62,7 +64,7 @@ private void _state_StateChanged(object sender, ParserStateEventArgs e)

if (_viewModel == null) { return; }

UiDispatcher.InvokeAsync(UpdateTab);
_uiDispatcher.InvokeAsync(UpdateTab);
}

private void UpdateTab()
Expand Down
7 changes: 4 additions & 3 deletions Rubberduck.Core/UI/Command/FindAllReferencesCommand.cs
Expand Up @@ -6,7 +6,6 @@
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.UIContext;
using Rubberduck.Parsing.VBA;
using Rubberduck.UI.Command.MenuItems;
using Rubberduck.UI.Controls;
using Rubberduck.VBEditor;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
Expand All @@ -25,10 +24,11 @@ public class FindAllReferencesCommand : CommandBase, IDisposable
private readonly ISearchResultsWindowViewModel _viewModel;
private readonly SearchResultPresenterInstanceManager _presenterService;
private readonly IVBE _vbe;
private readonly IUiDispatcher _uiDispatcher;

public FindAllReferencesCommand(INavigateCommand navigateCommand, IMessageBox messageBox,
RubberduckParserState state, IVBE vbe, ISearchResultsWindowViewModel viewModel,
SearchResultPresenterInstanceManager presenterService)
SearchResultPresenterInstanceManager presenterService, IUiDispatcher uiDispatcher)
: base(LogManager.GetCurrentClassLogger())
{
_navigateCommand = navigateCommand;
Expand All @@ -37,6 +37,7 @@ public class FindAllReferencesCommand : CommandBase, IDisposable
_vbe = vbe;
_viewModel = viewModel;
_presenterService = presenterService;
_uiDispatcher = uiDispatcher;

_state.StateChanged += _state_StateChanged;
}
Expand All @@ -57,7 +58,7 @@ private void _state_StateChanged(object sender, ParserStateEventArgs e)

if (_viewModel == null) { return; }

UiDispatcher.InvokeAsync(UpdateTab);
_uiDispatcher.InvokeAsync(UpdateTab);
}

private void UpdateTab()
Expand Down
Expand Up @@ -16,13 +16,15 @@ public abstract class AppCommandBarBase : IAppCommandBar
private readonly string _name;
private readonly CommandBarPosition _position;
private readonly IDictionary<ICommandMenuItem, ICommandBarControl> _items;
protected readonly IUiDispatcher _uiDispatcher;
protected static readonly Logger Logger = LogManager.GetCurrentClassLogger();

protected AppCommandBarBase(string name, CommandBarPosition position, IEnumerable<ICommandMenuItem> items)
protected AppCommandBarBase(string name, CommandBarPosition position, IEnumerable<ICommandMenuItem> items, IUiDispatcher uiDispatcher)
{
_name = name;
_position = position;
_items = items.ToDictionary(item => item, item => null as ICommandBarControl);
_uiDispatcher = uiDispatcher;
}

protected ICommandMenuItem FindChildByTag(string tag)
Expand Down Expand Up @@ -53,7 +55,7 @@ public void Localize()
try
{
var item = kvp;
UiDispatcher.Invoke(() => LocalizeInternal(item, kvp));
_uiDispatcher.Invoke(() => LocalizeInternal(item, kvp));

}
catch (Exception e)
Expand Down
Expand Up @@ -16,8 +16,8 @@ public class RubberduckCommandBar : AppCommandBarBase, IDisposable
private readonly IParseCoordinator _parser;
private readonly ISelectionChangeService _selectionService;

public RubberduckCommandBar(IParseCoordinator parser, IEnumerable<ICommandMenuItem> items, IContextFormatter formatter, ISelectionChangeService selectionService)
: base("Rubberduck", CommandBarPosition.Top, items)
public RubberduckCommandBar(IParseCoordinator parser, IEnumerable<ICommandMenuItem> items, IContextFormatter formatter, ISelectionChangeService selectionService, IUiDispatcher uiDispatcher)
: base("Rubberduck", CommandBarPosition.Top, items, uiDispatcher)
{
_parser = parser;
_formatter = formatter;
Expand Down Expand Up @@ -106,7 +106,7 @@ private void SetStatusLabelCaption(string caption, int? errorCount = null)
var showErrorsCommandButton = FindChildByTag(typeof(ShowParserErrorsCommandMenuItem).FullName) as ShowParserErrorsCommandMenuItem;
if (showErrorsCommandButton == null) { return; }

UiDispatcher.Invoke(() =>
_uiDispatcher.Invoke(() =>
{
try
{
Expand All @@ -132,7 +132,7 @@ private void SetContextSelectionCaption(string caption, int contextReferenceCoun
var contextReferences = FindChildByTag(typeof(ReferenceCounterLabelMenuItem).FullName) as ReferenceCounterLabelMenuItem;
var contextDescription = FindChildByTag(typeof(ContextDescriptionLabelMenuItem).FullName) as ContextDescriptionLabelMenuItem;

UiDispatcher.Invoke(() =>
_uiDispatcher.Invoke(() =>
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion Rubberduck.Core/UI/Command/ReparseCommand.cs
Expand Up @@ -8,10 +8,10 @@
using Rubberduck.Settings;
using Rubberduck.SettingsProvider;
using Rubberduck.UI.CodeExplorer.Commands;
using Rubberduck.VBERuntime;
using Rubberduck.VBEditor.ComManagement.TypeLibsAPI;
using Rubberduck.VBEditor.SafeComWrappers;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
using Rubberduck.VBEditor.VBERuntime.Settings;

namespace Rubberduck.UI.Command
{
Expand Down
10 changes: 6 additions & 4 deletions Rubberduck.Core/UI/Command/ShowParserErrorsCommand.cs
Expand Up @@ -7,7 +7,6 @@
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.UIContext;
using Rubberduck.Parsing.VBA;
using Rubberduck.UI.Command.MenuItems;
using Rubberduck.UI.Controls;
using Rubberduck.VBEditor;

Expand All @@ -22,17 +21,20 @@ public class ShowParserErrorsCommand : CommandBase, IShowParserErrorsCommand
private readonly RubberduckParserState _state;
private readonly ISearchResultsWindowViewModel _viewModel;
private readonly SearchResultPresenterInstanceManager _presenterService;
private readonly IUiDispatcher _uiDispatcher;

public ShowParserErrorsCommand(INavigateCommand navigateCommand,
RubberduckParserState state,
ISearchResultsWindowViewModel viewModel,
SearchResultPresenterInstanceManager presenterService)
SearchResultPresenterInstanceManager presenterService,
IUiDispatcher uiDispatcher)
: base(LogManager.GetCurrentClassLogger())
{
_navigateCommand = navigateCommand;
_state = state;
_viewModel = viewModel;
_presenterService = presenterService;
_uiDispatcher = uiDispatcher;

_state.StateChanged += _state_StateChanged;
}
Expand All @@ -42,8 +44,8 @@ private void _state_StateChanged(object sender, ParserStateEventArgs e)
if (_viewModel == null) { return; }

if (_state.Status != ParserState.Error && _state.Status != ParserState.Parsed) { return; }
UiDispatcher.InvokeAsync(UpdateTab);

_uiDispatcher.InvokeAsync(UpdateTab);
}

private void UpdateTab()
Expand Down
19 changes: 14 additions & 5 deletions Rubberduck.Core/UI/Inspections/InspectionResultsViewModel.cs
Expand Up @@ -15,7 +15,6 @@
using Rubberduck.Parsing.VBA;
using Rubberduck.Settings;
using Rubberduck.UI.Command;
using Rubberduck.UI.Command.MenuItems;
using Rubberduck.UI.Controls;
using Rubberduck.UI.Settings;

Expand All @@ -41,12 +40,20 @@ public sealed class InspectionResultsViewModel : ViewModelBase, INavigateSelecti
private readonly IClipboardWriter _clipboard;
private readonly IGeneralConfigService _configService;
private readonly ISettingsFormFactory _settingsFormFactory;
private readonly IUiDispatcher _uiDispatcher;

private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

public InspectionResultsViewModel(RubberduckParserState state, IInspector inspector, IQuickFixProvider quickFixProvider,
INavigateCommand navigateCommand, ReparseCommand reparseCommand,
IClipboardWriter clipboard, IGeneralConfigService configService, ISettingsFormFactory settingsFormFactory)
public InspectionResultsViewModel(
RubberduckParserState state,
IInspector inspector,
IQuickFixProvider quickFixProvider,
INavigateCommand navigateCommand,
ReparseCommand reparseCommand,
IClipboardWriter clipboard,
IGeneralConfigService configService,
ISettingsFormFactory settingsFormFactory,
IUiDispatcher uiDispatcher)
{
_state = state;
_inspector = inspector;
Expand All @@ -55,6 +62,8 @@ public sealed class InspectionResultsViewModel : ViewModelBase, INavigateSelecti
_clipboard = clipboard;
_configService = configService;
_settingsFormFactory = settingsFormFactory;
_uiDispatcher = uiDispatcher;

RefreshCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(),
o =>
{
Expand Down Expand Up @@ -326,7 +335,7 @@ private async void RefreshInspections(CancellationToken token)

Results = new ObservableCollection<IInspectionResult>(results);

UiDispatcher.Invoke(() =>
_uiDispatcher.Invoke(() =>
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion Rubberduck.Core/UI/Settings/GeneralSettingsViewModel.cs
Expand Up @@ -8,7 +8,7 @@
using NLog;
using Rubberduck.SettingsProvider;
using Rubberduck.UI.Command;
using Rubberduck.VBERuntime;
using Rubberduck.VBEditor.VBERuntime.Settings;

namespace Rubberduck.UI.Settings
{
Expand Down
3 changes: 1 addition & 2 deletions Rubberduck.Core/UI/Settings/SettingsForm.cs
@@ -1,10 +1,9 @@
using System.Windows.Forms;
using Rubberduck.Settings;
using Rubberduck.Common;
using Rubberduck.VBERuntime;
using Rubberduck.VBEditor.VBERuntime.Settings;
using System.Collections.Generic;
using System;
using System.Collections.ObjectModel;

namespace Rubberduck.UI.Settings
{
Expand Down