1
1
using System . Collections . Generic ;
2
2
using System . IO ;
3
3
using System . Linq ;
4
+ using Rubberduck . Interaction ;
4
5
using Rubberduck . Parsing . VBA ;
5
6
using Rubberduck . Parsing . VBA . DeclarationCaching ;
6
7
using Rubberduck . Parsing . VBA . Extensions ;
8
+ using Rubberduck . Resources ;
7
9
using Rubberduck . VBEditor ;
8
10
using Rubberduck . VBEditor . Events ;
9
11
using Rubberduck . VBEditor . ComManagement ;
@@ -27,14 +29,17 @@ public UpdateFromFilesCommand(
27
29
IParseManager parseManager ,
28
30
IDeclarationFinderProvider declarationFinderProvider ,
29
31
IProjectsProvider projectsProvider ,
30
- IModuleNameFromFileExtractor moduleNameFromFileExtractor )
31
- : base ( vbe , dialogFactory , vbeEvents , parseManager )
32
+ IModuleNameFromFileExtractor moduleNameFromFileExtractor ,
33
+ IMessageBox messageBox )
34
+ : base ( vbe , dialogFactory , vbeEvents , parseManager , messageBox )
32
35
{
33
36
_projectsProvider = projectsProvider ;
34
37
_declarationFinderProvider = declarationFinderProvider ;
35
38
_moduleNameFromFileExtractor = moduleNameFromFileExtractor ;
36
39
}
37
40
41
+ protected override string DialogsTitle => RubberduckUI . UpdateFromFilesCommand_DialogCaption ;
42
+
38
43
protected override void ImportFiles ( ICollection < string > filesToImport , IVBProject targetProject )
39
44
{
40
45
var finder = _declarationFinderProvider . DeclarationFinder ;
@@ -56,15 +61,15 @@ protected override void ImportFiles(ICollection<string> filesToImport, IVBProjec
56
61
57
62
if ( ! ValuesAreUnique ( moduleNames ) )
58
63
{
59
- //TODO: report this to the user.
64
+ NotifyUserAboutAbortDueToDuplicateComponent ( moduleNames ) ;
60
65
return ;
61
66
}
62
67
63
- var modules = Modules ( moduleNames , targetProject . ProjectId , finder ) ;
68
+ var modulesToRemoveBeforeImport = Modules ( moduleNames , targetProject . ProjectId , finder ) ;
64
69
65
- if ( ! modules . All ( kvp => HasMatchingFileExtension ( kvp . Key , kvp . Value ) ) )
70
+ if ( ! modulesToRemoveBeforeImport . All ( kvp => HasMatchingFileExtension ( kvp . Key , kvp . Value ) ) )
66
71
{
67
- //TODO: report this to the user.
72
+ NotifyUserAboutAbortDueToNonMatchingFileExtension ( modulesToRemoveBeforeImport ) ;
68
73
return ;
69
74
}
70
75
@@ -75,37 +80,37 @@ protected override void ImportFiles(ICollection<string> filesToImport, IVBProjec
75
80
&& componentType == ComponentType . Document )
76
81
. ToHashSet ( ) ;
77
82
78
- //We can only insert inte existing documents.
79
- if ( ! documentFiles . All ( filename => modules . ContainsKey ( filename ) ) )
83
+ //We can only insert into existing documents.
84
+ if ( ! documentFiles . All ( filename => modulesToRemoveBeforeImport . ContainsKey ( filename ) ) )
80
85
{
81
- //TODO: report this to the user.
86
+ NotifyUserAboutAbortDueToNonExistingDocument ( documentFiles , moduleNames , modulesToRemoveBeforeImport ) ;
82
87
return ;
83
88
}
84
89
85
90
//We must not remove document modules.
86
91
foreach ( var filename in documentFiles )
87
92
{
88
- modules . Remove ( filename ) ;
93
+ modulesToRemoveBeforeImport . Remove ( filename ) ;
89
94
}
90
95
91
96
//We import the standalone code behind by replacing the code in an existing form.
92
97
//So, the form has to exist already.
93
- if ( ! formFilesWithoutBinaries . All ( filename => modules . ContainsKey ( filename ) ) )
98
+ if ( ! formFilesWithoutBinaries . All ( filename => modulesToRemoveBeforeImport . ContainsKey ( filename ) ) )
94
99
{
95
- //TODO: report this to the user.
100
+ NotifyUserAboutAbortDueToNonExistingUserForm ( documentFiles , moduleNames , modulesToRemoveBeforeImport ) ;
96
101
return ;
97
102
}
98
103
99
104
foreach ( var filename in formFilesWithoutBinaries )
100
105
{
101
- modules . Remove ( filename ) ;
106
+ modulesToRemoveBeforeImport . Remove ( filename ) ;
102
107
}
103
108
104
109
using ( var components = targetProject . VBComponents )
105
110
{
106
111
foreach ( var filename in filesToImport )
107
112
{
108
- if ( modules . TryGetValue ( filename , out var module ) )
113
+ if ( modulesToRemoveBeforeImport . TryGetValue ( filename , out var module ) )
109
114
{
110
115
var component = _projectsProvider . Component ( module ) ;
111
116
components . Remove ( component ) ;
@@ -172,6 +177,16 @@ private bool ValuesAreUnique(Dictionary<string, string> moduleNames)
172
177
. All ( moduleNameGroup => moduleNameGroup . Count ( ) == 1 ) ;
173
178
}
174
179
180
+ private void NotifyUserAboutAbortDueToDuplicateComponent ( IDictionary < string , string > moduleNames )
181
+ {
182
+ var firstDuplicateModuleName = moduleNames
183
+ . GroupBy ( kvp => kvp . Value )
184
+ . First ( moduleNameGroup => moduleNameGroup . Count ( ) > 1 )
185
+ . Key ;
186
+ var message = string . Format ( RubberduckUI . UpdateFromFilesCommand_DuplicateModule , firstDuplicateModuleName ) ;
187
+ MessageBox . NotifyWarn ( message , DialogsTitle ) ;
188
+ }
189
+
175
190
private ICollection < string > FormFilesWithoutBinaries ( IDictionary < string , string > moduleNames , ICollection < string > formBinaryModuleNames )
176
191
{
177
192
return moduleNames
@@ -204,5 +219,37 @@ private bool HasMatchingFileExtension(string filename, QualifiedModuleName modul
204
219
&& ComponentTypeForExtension . TryGetValue ( fileExtension , out var componentType )
205
220
&& module . ComponentType . Equals ( componentType ) ;
206
221
}
222
+
223
+ private void NotifyUserAboutAbortDueToNonMatchingFileExtension ( IDictionary < string , QualifiedModuleName > modules )
224
+ {
225
+ var ( firstNonMatchingFileName , firstNonMatchingModule ) = modules . First ( kvp => ! HasMatchingFileExtension ( kvp . Key , kvp . Value ) ) ;
226
+ var message = string . Format (
227
+ RubberduckUI . UpdateFromFilesCommand_DifferentComponentType ,
228
+ firstNonMatchingModule . ComponentName ,
229
+ firstNonMatchingFileName ) ;
230
+ MessageBox . NotifyWarn ( message , DialogsTitle ) ;
231
+ }
232
+
233
+ private void NotifyUserAboutAbortDueToNonExistingDocument ( ICollection < string > documentFiles , IDictionary < string , string > moduleNames , IDictionary < string , QualifiedModuleName > existingModules )
234
+ {
235
+ var firstNonExistingDocumentFilename = documentFiles . First ( filename => ! existingModules . ContainsKey ( filename ) ) ;
236
+ var firstNonExistingDocumentModuleName = moduleNames [ firstNonExistingDocumentFilename ] ;
237
+ var message = string . Format (
238
+ RubberduckUI . UpdateFromFilesCommand_DocumentDoesNotExist ,
239
+ firstNonExistingDocumentModuleName ,
240
+ firstNonExistingDocumentFilename ) ;
241
+ MessageBox . NotifyWarn ( message , DialogsTitle ) ;
242
+ }
243
+
244
+ private void NotifyUserAboutAbortDueToNonExistingUserForm ( ICollection < string > userFormFiles , IDictionary < string , string > moduleNames , IDictionary < string , QualifiedModuleName > existingModules )
245
+ {
246
+ var firstNonExistingUserFormFilename = userFormFiles . First ( filename => ! existingModules . ContainsKey ( filename ) ) ;
247
+ var firstNonExistingUserFormModuleName = moduleNames [ firstNonExistingUserFormFilename ] ;
248
+ var message = string . Format (
249
+ RubberduckUI . UpdateFromFilesCommand_UserFormDoesNotExist ,
250
+ firstNonExistingUserFormModuleName ,
251
+ firstNonExistingUserFormFilename ) ;
252
+ MessageBox . NotifyWarn ( message , DialogsTitle ) ;
253
+ }
207
254
}
208
255
}
0 commit comments