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

Add more filters for CodeExplorer import command #5731

Open
wants to merge 8 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public sealed class CodeExplorerViewModel : ViewModelBase
private readonly RemoveCommand _externalRemoveCommand;
private readonly IConfigurationService<GeneralSettings> _generalSettingsProvider;
private readonly IConfigurationService<WindowSettings> _windowSettingsProvider;
private readonly IConfigurationService<ProjectSettings> _projectSettingsProvider;
private readonly IUiDispatcher _uiDispatcher;
private readonly IVBE _vbe;
private readonly ITemplateProvider _templateProvider;
Expand All @@ -52,7 +53,8 @@ public CodeExplorerViewModel(
RubberduckParserState state,
RemoveCommand removeCommand,
IConfigurationService<GeneralSettings> generalSettingsProvider,
IConfigurationService<WindowSettings> windowSettingsProvider,
IConfigurationService<WindowSettings> windowSettingsProvider,
IConfigurationService<ProjectSettings> projectSettingsProvider,
IUiDispatcher uiDispatcher,
IVBE vbe,
ITemplateProvider templateProvider,
Expand All @@ -66,8 +68,10 @@ public CodeExplorerViewModel(
_generalSettingsProvider = generalSettingsProvider;
_generalSettingsProvider.SettingsChanged += GeneralSettingsChanged;
RefreshDragAndDropSetting();

_projectSettingsProvider = projectSettingsProvider;

_windowSettingsProvider = windowSettingsProvider;
_windowSettingsProvider = windowSettingsProvider;
_uiDispatcher = uiDispatcher;
_vbe = vbe;
_templateProvider = templateProvider;
Expand Down
363 changes: 200 additions & 163 deletions Rubberduck.Core/Properties/Settings.Designer.cs

Large diffs are not rendered by default.

370 changes: 202 additions & 168 deletions Rubberduck.Core/Properties/Settings.settings

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions Rubberduck.Core/Rubberduck.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,16 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<Compile Update="Properties\Settings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
</Project>
21 changes: 16 additions & 5 deletions Rubberduck.Core/Settings/ConfigurationLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ConfigurationLoader : IConfigurationService<Configuration>
private readonly IConfigurationService<UnitTestSettings> _unitTestProvider;
private readonly IConfigurationService<IndenterSettings> _indenterProvider;
private readonly IConfigurationService<WindowSettings> _windowProvider;
private readonly IConfigurationService<ProjectSettings> _projectSettingsProvider;

public ConfigurationLoader(IConfigurationService<GeneralSettings> generalProvider,
IConfigurationService<HotkeySettings> hotkeyProvider,
Expand All @@ -26,7 +27,8 @@ public ConfigurationLoader(IConfigurationService<GeneralSettings> generalProvide
IConfigurationService<CodeInspectionSettings> inspectionProvider,
IConfigurationService<UnitTestSettings> unitTestProvider,
IConfigurationService<IndenterSettings> indenterProvider,
IConfigurationService<WindowSettings> windowProvider)
IConfigurationService<WindowSettings> windowProvider,
IConfigurationService<ProjectSettings> projectSettingsProvider)
{
_generalProvider = generalProvider;
_hotkeyProvider = hotkeyProvider;
Expand All @@ -36,6 +38,7 @@ public ConfigurationLoader(IConfigurationService<GeneralSettings> generalProvide
_unitTestProvider = unitTestProvider;
_indenterProvider = indenterProvider;
_windowProvider = windowProvider;
_projectSettingsProvider = projectSettingsProvider;
}

/// <summary>
Expand All @@ -55,7 +58,8 @@ public virtual Configuration Read()
_inspectionProvider.Read(),
_unitTestProvider.Read(),
_indenterProvider.Read(),
_windowProvider.Read()
_windowProvider.Read(),
_projectSettingsProvider.Read()
)
};
return config;
Expand All @@ -74,7 +78,8 @@ public Configuration ReadDefaults()
_inspectionProvider.ReadDefaults(),
_unitTestProvider.ReadDefaults(),
_indenterProvider.ReadDefaults(),
_windowProvider.ReadDefaults()
_windowProvider.ReadDefaults(),
_projectSettingsProvider.ReadDefaults()
)
};
}
Expand All @@ -91,6 +96,10 @@ public void Save(Configuration toSerialize)
var newAutoCompleteSettings = toSerialize.UserSettings.AutoCompleteSettings;
var autoCompletesChanged = oldAutoCompleteSettings.Equals(newAutoCompleteSettings);

var oldProjectSettings = _projectSettingsProvider.Read();
var newProjectSettings = toSerialize.UserSettings.ProjectSettings;
var openFileDialogFilterIndexChanged = oldProjectSettings.OpenFileDialogFilterIndex != newProjectSettings.OpenFileDialogFilterIndex;

_generalProvider.Save(toSerialize.UserSettings.GeneralSettings);
_hotkeyProvider.Save(toSerialize.UserSettings.HotkeySettings);
_autoCompleteProvider.Save(toSerialize.UserSettings.AutoCompleteSettings);
Expand All @@ -99,8 +108,9 @@ public void Save(Configuration toSerialize)
_unitTestProvider.Save(toSerialize.UserSettings.UnitTestSettings);
_indenterProvider.Save(toSerialize.UserSettings.IndenterSettings);
_windowProvider.Save(toSerialize.UserSettings.WindowSettings);
_projectSettingsProvider.Save(toSerialize.UserSettings.ProjectSettings);

OnSettingsChanged(new ConfigurationChangedEventArgs(inspectOnReparse, langChanged, inspectionsChanged, autoCompletesChanged));
OnSettingsChanged(new ConfigurationChangedEventArgs(inspectOnReparse, langChanged, inspectionsChanged, autoCompletesChanged, openFileDialogFilterIndexChanged));
}

public event EventHandler<ConfigurationChangedEventArgs> SettingsChanged;
Expand All @@ -122,7 +132,8 @@ public Configuration Import(string fileName)
_inspectionProvider.Import(fileName),
_unitTestProvider.Import(fileName),
_indenterProvider.Import(fileName),
_windowProvider.Import(fileName)
_windowProvider.Import(fileName),
_projectSettingsProvider.Import(fileName)
)
};
}
Expand Down
36 changes: 36 additions & 0 deletions Rubberduck.Core/Settings/ProjectSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Configuration;
using System.Xml.Serialization;

namespace Rubberduck.Settings
{
public interface IProjectSettings
{
int OpenFileDialogFilterIndex { get; set; }
}

[SettingsSerializeAs(SettingsSerializeAs.Xml)]
[XmlType(AnonymousType = true)]
public class ProjectSettings : IProjectSettings, IEquatable<ProjectSettings>
{
[XmlElement(Type = typeof(int))]
public int OpenFileDialogFilterIndex { get; set; } = 1;

/// <Summary>
/// Default constructor required for XML serialization.
/// </Summary>
public ProjectSettings()
{
}

public ProjectSettings(int openFileDialogFilterIndex)
{
OpenFileDialogFilterIndex = openFileDialogFilterIndex;
}

public bool Equals(ProjectSettings other)
{
return OpenFileDialogFilterIndex == other.OpenFileDialogFilterIndex;
}
}
}
5 changes: 4 additions & 1 deletion Rubberduck.Core/Settings/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class UserSettings
public UnitTestSettings UnitTestSettings { get; set; }
public IndenterSettings IndenterSettings { get; set; }
public WindowSettings WindowSettings { get; set; }
public ProjectSettings ProjectSettings { get; set; }

public UserSettings(GeneralSettings generalSettings,
HotkeySettings hotkeySettings,
Expand All @@ -24,7 +25,8 @@ public UserSettings(GeneralSettings generalSettings,
CodeInspectionSettings codeInspectionSettings,
UnitTestSettings unitTestSettings,
IndenterSettings indenterSettings,
WindowSettings windowSettings)
WindowSettings windowSettings,
ProjectSettings projectSettings)
{
GeneralSettings = generalSettings;
HotkeySettings = hotkeySettings;
Expand All @@ -34,6 +36,7 @@ public UserSettings(GeneralSettings generalSettings,
UnitTestSettings = unitTestSettings;
IndenterSettings = indenterSettings;
WindowSettings = windowSettings;
ProjectSettings = projectSettings;
}
}
}
69 changes: 65 additions & 4 deletions Rubberduck.Core/UI/CodeExplorer/Commands/ImportCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Rubberduck.Parsing.VBA;
using Rubberduck.Parsing.VBA.DeclarationCaching;
using Rubberduck.Resources;
using Rubberduck.Settings;
using Rubberduck.SettingsProvider;
using Rubberduck.VBEditor;
using Rubberduck.VBEditor.ComManagement;
using Rubberduck.VBEditor.Events;
Expand Down Expand Up @@ -38,6 +40,7 @@ public class ImportCommand : CodeExplorerCommandBase
private readonly IModuleNameFromFileExtractor _moduleNameFromFileExtractor;
private readonly IDictionary<ComponentType, List<IRequiredBinaryFilesFromFileNameExtractor>> _binaryFileExtractors;
private readonly IFileSystem _fileSystem;
private readonly IConfigurationService<ProjectSettings> _projectSettingsProvider;

protected readonly IDeclarationFinderProvider DeclarationFinderProvider;
protected readonly IMessageBox MessageBox;
Expand All @@ -52,7 +55,8 @@ public ImportCommand(
IModuleNameFromFileExtractor moduleNameFromFileExtractor,
IEnumerable<IRequiredBinaryFilesFromFileNameExtractor> binaryFileExtractors,
IFileSystem fileSystem,
IMessageBox messageBox)
IMessageBox messageBox,
IConfigurationService<ProjectSettings> projectSettingsProvider)
: base(vbeEvents)
{
_vbe = vbe;
Expand All @@ -67,6 +71,8 @@ public ImportCommand(
MessageBox = messageBox;
DeclarationFinderProvider = declarationFinderProvider;

_projectSettingsProvider = projectSettingsProvider;

AddToCanExecuteEvaluation(SpecialEvaluateCanExecute);

ComponentTypesForExtension = ComponentTypeExtensions.ComponentTypesForExtension(_vbe.Kind);
Expand Down Expand Up @@ -146,8 +152,21 @@ private IDictionary<ComponentType, List<IRequiredBinaryFilesFromFileNameExtracto
return dict;
}


private ProjectSettings _cachedProjectSettings;
internal void UpdateFilterIndex(int index)
{
_cachedProjectSettings.OpenFileDialogFilterIndex = index;
_projectSettingsProvider.Save(_cachedProjectSettings);
}

protected virtual ICollection<string> FilesToImport(object parameter)
{
if (_cachedProjectSettings == null)
{
_cachedProjectSettings = _projectSettingsProvider.Read();
}

using (var dialog = _dialogFactory.CreateOpenFileDialog())
{
dialog.AddExtension = true;
Expand All @@ -157,15 +176,33 @@ protected virtual ICollection<string> FilesToImport(object parameter)
dialog.Multiselect = true;
dialog.ShowHelp = false;
dialog.Title = DialogsTitle;
dialog.Filter =
$"{RubberduckUI.ImportCommand_OpenDialog_Filter_VBFiles} ({FilterExtension})|{FilterExtension}|" +
$"{RubberduckUI.ImportCommand_OpenDialog_Filter_AllFiles}, (*.*)|*.*";
dialog.FilterIndex = _cachedProjectSettings.OpenFileDialogFilterIndex;
var vbFilesFilter = IndividualFilter(RubberduckUI.ImportCommand_OpenDialog_Filter_VBFiles, FilterExtension);
var nonDocumentModulesFilter = IndividualFilter(Resources.CodeExplorer.CodeExplorerUI.ImportCommand_OpenDialog_Filter_NonDocumentModules, AllNonDocumentModulesExtension);
var standardModulesFilter = IndividualFilter(Resources.CodeExplorer.CodeExplorerUI.ImportCommand_OpenDialog_Filter_StandardModules, StandardModuleExtension);
var classModulesFilter = IndividualFilter(Resources.CodeExplorer.CodeExplorerUI.ImportCommand_OpenDialog_Filter_ClassModules, ClassModuleExtension);
var formModulesFilter = IndividualFilter(Resources.CodeExplorer.CodeExplorerUI.ImportCommand_OpenDialog_Filter_FormModules, FormModuleExtension);
var documentModulesFilter = IndividualFilter(Resources.CodeExplorer.CodeExplorerUI.ImportCommand_OpenDialog_Filter_DocumentModules, DocumentModuleExtension);
var allFilesFilter = IndividualFilter(RubberduckUI.ImportCommand_OpenDialog_Filter_AllFiles, "*.*");
dialog.Filter = CompositeFilter("|",
vbFilesFilter,
nonDocumentModulesFilter,
standardModulesFilter,
classModulesFilter,
formModulesFilter,
documentModulesFilter,
allFilesFilter);

if (dialog.ShowDialog() != DialogResult.OK)
{
return new List<string>();
}

if (_cachedProjectSettings.OpenFileDialogFilterIndex != dialog.FilterIndex)
{
UpdateFilterIndex(dialog.FilterIndex);
}

var fileNames = dialog.FileNames;
var fileExtensions = fileNames.Select(Path.GetExtension);
if (fileExtensions.Any(fileExt => !ImportableExtensions.Contains(fileExt)))
Expand All @@ -176,6 +213,18 @@ protected virtual ICollection<string> FilesToImport(object parameter)

return fileNames;
}

string IndividualFilter(string filterDescription, string fileExtension)
{
return fileExtension != null
? $"{filterDescription} ({fileExtension})|{fileExtension}"
: null;
}

string CompositeFilter(string separator, params string[] filters)
{
return string.Join(separator, filters.Where(filter => !string.IsNullOrEmpty(filter)));
}
}

protected virtual string DialogsTitle => RubberduckUI.ImportCommand_OpenDialog_Title;
Expand Down Expand Up @@ -562,6 +611,18 @@ protected override void OnExecute(object parameter)

private string FilterExtension => string.Join("; ", ImportableExtensions.Select(ext => $"*{ext}"));

private string AllNonDocumentModulesExtension => string.Join("; ",
ImportableExtensions.Where(ext => !ext.EndsWith("doccls"))
.Select(ext => $"*{ext}"));

private string StandardModuleExtension => "*" + ImportableExtensions.Where(ext => ext.EndsWith("bas")).FirstOrDefault();

private string ClassModuleExtension => "*" + ImportableExtensions.Where(ext => ext.EndsWith(".cls")).FirstOrDefault();

private string FormModuleExtension => "*" + ImportableExtensions.Where(ext => ext.EndsWith("frm")).FirstOrDefault();

private string DocumentModuleExtension => "*" + ImportableExtensions.Where(ext => ext.EndsWith("doccls")).FirstOrDefault();

protected IDictionary<string, ICollection<ComponentType>> ComponentTypesForExtension { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.Resources;
using Rubberduck.Settings;
using Rubberduck.SettingsProvider;
using Rubberduck.VBEditor;
using Rubberduck.VBEditor.ComManagement;
using Rubberduck.VBEditor.Events;
Expand All @@ -26,8 +28,9 @@ public ReplaceProjectContentsFromFilesCommand(
IModuleNameFromFileExtractor moduleNameFromFileExtractor,
IEnumerable<IRequiredBinaryFilesFromFileNameExtractor> binaryFileExtractors,
IFileSystem fileSystem,
IMessageBox messageBox)
:base(vbe, dialogFactory, vbeEvents, parseManager, declarationFinderProvider, projectsProvider, moduleNameFromFileExtractor, binaryFileExtractors, fileSystem, messageBox)
IMessageBox messageBox,
IConfigurationService<ProjectSettings> projectSettingsProvider)
:base(vbe, dialogFactory, vbeEvents, parseManager, declarationFinderProvider, projectsProvider, moduleNameFromFileExtractor, binaryFileExtractors, fileSystem, messageBox, projectSettingsProvider)
{}

protected override string DialogsTitle => RubberduckUI.ReplaceProjectContentsFromFilesCommand_DialogCaption;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Rubberduck.Interaction;
using Rubberduck.Parsing.VBA;
using Rubberduck.Resources;
using Rubberduck.Settings;
using Rubberduck.SettingsProvider;
using Rubberduck.VBEditor;
using Rubberduck.VBEditor.Events;
using Rubberduck.VBEditor.ComManagement;
Expand All @@ -25,8 +27,9 @@ public UpdateFromFilesCommand(
IModuleNameFromFileExtractor moduleNameFromFileExtractor,
IEnumerable<IRequiredBinaryFilesFromFileNameExtractor> binaryFileExtractors,
IFileSystem fileSystem,
IMessageBox messageBox)
: base(vbe, dialogFactory, vbeEvents, parseManager, declarationFinderProvider, projectsProvider, moduleNameFromFileExtractor, binaryFileExtractors, fileSystem, messageBox)
IMessageBox messageBox,
IConfigurationService<ProjectSettings> projectSettingsProvider)
: base(vbe, dialogFactory, vbeEvents, parseManager, declarationFinderProvider, projectsProvider, moduleNameFromFileExtractor, binaryFileExtractors, fileSystem, messageBox, projectSettingsProvider)
{}

protected override string DialogsTitle => RubberduckUI.UpdateFromFilesCommand_DialogCaption;
Expand Down
9 changes: 9 additions & 0 deletions Rubberduck.Core/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,15 @@
</ToDoGridViewColumnInfo>
</value>
</setting>
<setting name="CodeExplorer_ImportCommand_OpenFileDialogFilterIndex"
serializeAs="Xml">
<value>
<ProjectSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<OpenFileDialogFilterIndex>1</OpenFileDialogFilterIndex>
</ProjectSettings>
</value>
</setting>
</Rubberduck.Properties.Settings>
</applicationSettings>
</configuration>
Loading