Skip to content

Commit 5689f02

Browse files
committed
Extend UpdateFromFilesCommand to handle all VB6 files appropriately
1 parent 5c7c292 commit 5689f02

File tree

12 files changed

+414
-125
lines changed

12 files changed

+414
-125
lines changed

Rubberduck.Core/UI/CodeExplorer/Commands/ImportCommand.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public class ImportCommand : CodeExplorerCommandBase
2626

2727
private readonly IVBE _vbe;
2828
private readonly IFileSystemBrowserFactory _dialogFactory;
29-
private readonly IList<string> _importableExtensions;
30-
private readonly string _filterExtensions;
3129
private readonly IParseManager _parseManager;
3230

3331
protected readonly IMessageBox MessageBox;
@@ -48,10 +46,7 @@ public ImportCommand(
4846

4947
AddToCanExecuteEvaluation(SpecialEvaluateCanExecute);
5048

51-
ComponentTypeForExtension = ComponentTypeExtensions.ComponentTypeForExtension(_vbe.Kind);
52-
53-
_importableExtensions = ComponentTypeForExtension.Keys.ToList();
54-
_filterExtensions = string.Join("; ", _importableExtensions.Select(ext => $"*{ext}"));
49+
ComponentTypesForExtension = ComponentTypeExtensions.ComponentTypesForExtension(_vbe.Kind);
5550

5651
AddToCanExecuteEvaluation(SpecialEvaluateCanExecute);
5752
AddToOnExecuteEvaluation(SpecialEvaluateCanExecute);
@@ -118,7 +113,7 @@ protected virtual ICollection<string> FilesToImport(object parameter)
118113
dialog.ShowHelp = false;
119114
dialog.Title = DialogsTitle;
120115
dialog.Filter =
121-
$"{RubberduckUI.ImportCommand_OpenDialog_Filter_VBFiles} ({_filterExtensions})|{_filterExtensions}|" +
116+
$"{RubberduckUI.ImportCommand_OpenDialog_Filter_VBFiles} ({FilterExtension})|{FilterExtension}|" +
122117
$"{RubberduckUI.ImportCommand_OpenDialog_Filter_AllFiles}, (*.*)|*.*";
123118

124119
if (dialog.ShowDialog() != DialogResult.OK)
@@ -128,7 +123,8 @@ protected virtual ICollection<string> FilesToImport(object parameter)
128123

129124
var fileNames = dialog.FileNames;
130125
var fileExtensions = fileNames.Select(Path.GetExtension);
131-
if (fileExtensions.Any(fileExt => !_importableExtensions.Contains(fileExt)))
126+
var importableExtensions = ImportableExtensions;
127+
if (fileExtensions.Any(fileExt => !importableExtensions.Contains(fileExt)))
132128
{
133129
NotifyUserAboutAbortDueToUnsupportedFileExtensions(fileNames);
134130
return new List<string>();
@@ -142,7 +138,7 @@ protected virtual ICollection<string> FilesToImport(object parameter)
142138

143139
private void NotifyUserAboutAbortDueToUnsupportedFileExtensions(IEnumerable<string> fileNames)
144140
{
145-
var firstUnsupportedFile = fileNames.First(filename => !_importableExtensions.Contains(Path.GetExtension(filename)));
141+
var firstUnsupportedFile = fileNames.First(filename => !ImportableExtensions.Contains(Path.GetExtension(filename)));
146142
var unsupportedFileName = Path.GetFileName(firstUnsupportedFile);
147143
var message = string.Format(RubberduckUI.ImportCommand_UnsupportedFileExtensions, unsupportedFileName);
148144
MessageBox.NotifyWarn(message, DialogsTitle);
@@ -165,8 +161,8 @@ protected virtual void ImportFiles(ICollection<string> filesToImport, IVBProject
165161
{
166162
var fileExtension = Path.GetExtension(filename);
167163
if (fileExtension != null
168-
&& ComponentTypeForExtension.TryGetValue(fileExtension, out var componentType)
169-
&& componentType == ComponentType.Document)
164+
&& ComponentTypesForExtension.TryGetValue(fileExtension, out var componentTypes)
165+
&& componentTypes.Contains(ComponentType.Document))
170166
{
171167
//We have to dispose the return value.
172168
using (components.ImportSourceFile(filename)) { }
@@ -204,6 +200,10 @@ protected override void OnExecute(object parameter)
204200
}
205201
}
206202

207-
protected IDictionary<string, ComponentType> ComponentTypeForExtension { get; }
203+
protected virtual ICollection<string> ImportableExtensions => ComponentTypesForExtension.Keys.ToList();
204+
205+
private string FilterExtension => string.Join("; ", ImportableExtensions.Select(ext => $"*{ext}"));
206+
207+
protected IDictionary<string, ICollection<ComponentType>> ComponentTypesForExtension { get; }
208208
}
209209
}

Rubberduck.Core/UI/CodeExplorer/Commands/ReplaceProjectContentsFromFilesCommand.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Rubberduck.Parsing.VBA;
55
using Rubberduck.Resources;
66
using Rubberduck.VBEditor.Events;
7+
using Rubberduck.VBEditor.Extensions;
78
using Rubberduck.VBEditor.SafeComWrappers;
89
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
910

@@ -29,7 +30,7 @@ protected override void ImportFiles(ICollection<string> filesToImport, IVBProjec
2930
return;
3031
}
3132

32-
RemoveReimportableComponents(targetProject);
33+
RemoveReImportableComponents(targetProject);
3334
base.ImportFiles(filesToImport, targetProject);
3435
}
3536

@@ -40,23 +41,28 @@ private bool UserConfirmsToReplaceProjectContents(IVBProject project)
4041
return MessageBox.ConfirmYesNo(message, DialogsTitle, false);
4142
}
4243

43-
private void RemoveReimportableComponents(IVBProject project)
44+
private void RemoveReImportableComponents(IVBProject project)
4445
{
45-
var reimportableComponentTypes = ComponentTypeForExtension.Values
46-
.Where(componentType => componentType != ComponentType.Document);
46+
var reImportableComponentTypes = ReImportableComponentTypes;
4747
using(var components = project.VBComponents)
4848
{
4949
foreach(var component in components)
5050
{
5151
using (component)
5252
{
53-
if (reimportableComponentTypes.Contains(component.Type))
53+
if (reImportableComponentTypes.Contains(component.Type))
5454
{
5555
components.Remove(component);
5656
}
5757
}
5858
}
5959
}
6060
}
61+
62+
//We currently do not take precautions for component types requiring a binary file to be present.
63+
private ICollection<ComponentType> ReImportableComponentTypes => ComponentTypesForExtension.Values
64+
.SelectMany(componentTypes => componentTypes)
65+
.Where(componentType => componentType != ComponentType.Document)
66+
.ToList();
6167
}
6268
}

0 commit comments

Comments
 (0)