3
3
using System . Collections . ObjectModel ;
4
4
using System . Linq ;
5
5
using System . Windows . Input ;
6
- using System . Windows . Threading ;
7
6
using Microsoft . Vbe . Interop ;
8
7
using Rubberduck . Navigation . Folders ;
8
+ using Rubberduck . Parsing . Annotations ;
9
9
using Rubberduck . Parsing . Symbols ;
10
10
using Rubberduck . Parsing . VBA ;
11
11
using Rubberduck . UI ;
12
12
using Rubberduck . UI . CodeExplorer . Commands ;
13
13
using Rubberduck . UI . Command ;
14
+ using Rubberduck . UI . Command . MenuItems ;
15
+ using Rubberduck . VBEditor ;
14
16
15
17
// ReSharper disable CanBeReplacedWithTryCastAndCheckForNull
16
18
@@ -20,12 +22,9 @@ public sealed class CodeExplorerViewModel : ViewModelBase, IDisposable
20
22
{
21
23
private readonly FolderHelper _folderHelper ;
22
24
private readonly RubberduckParserState _state ;
23
- private readonly Dispatcher _dispatcher ;
24
25
25
26
public CodeExplorerViewModel ( FolderHelper folderHelper , RubberduckParserState state , List < ICommand > commands )
26
27
{
27
- _dispatcher = Dispatcher . CurrentDispatcher ;
28
-
29
28
_folderHelper = folderHelper ;
30
29
_state = state ;
31
30
_state . StateChanged += ParserState_StateChanged ;
@@ -217,11 +216,6 @@ public string Description
217
216
return ( ( CodeExplorerCustomFolderViewModel ) SelectedItem ) . FolderAttribute ;
218
217
}
219
218
220
- if ( SelectedItem is CodeExplorerErrorNodeViewModel )
221
- {
222
- return ( ( CodeExplorerErrorNodeViewModel ) SelectedItem ) . Name ;
223
- }
224
-
225
219
return string . Empty ;
226
220
}
227
221
}
@@ -267,9 +261,6 @@ private void ParserState_StateChanged(object sender, EventArgs e)
267
261
return ;
268
262
}
269
263
270
-
271
- var components = userDeclarations . SelectMany ( s => s . Key . VBComponents . Cast < VBComponent > ( ) ) . FirstOrDefault ( s => s . Name == "asdf" ) ;
272
-
273
264
var newProjects = userDeclarations . Select ( grouping =>
274
265
new CodeExplorerProjectViewModel ( _folderHelper ,
275
266
grouping . SingleOrDefault ( declaration => declaration . DeclarationType == DeclarationType . Project ) ,
@@ -322,36 +313,60 @@ private void ParserState_ModuleStateChanged(object sender, Parsing.ParseProgress
322
313
}
323
314
324
315
var componentProject = e . Component . Collection . Parent ;
325
- var node = Projects . OfType < CodeExplorerProjectViewModel > ( )
316
+ var projectNode = Projects . OfType < CodeExplorerProjectViewModel > ( )
326
317
. FirstOrDefault ( p => p . Declaration . Project == componentProject ) ;
327
318
328
- if ( node == null )
319
+ if ( projectNode == null )
329
320
{
330
321
return ;
331
322
}
332
323
333
- var folderNode = node . Items . First ( f => f is CodeExplorerCustomFolderViewModel && f . Name == node . Name ) ;
324
+ SetErrorState ( projectNode , e . Component ) ;
334
325
335
- AddErrorNode addNode = AddComponentErrorNode ;
336
- _dispatcher . BeginInvoke ( addNode , node , folderNode , e . Component . Name ) ;
337
- }
326
+ if ( _errorStateSet ) { return ; }
338
327
339
- private delegate void AddErrorNode ( CodeExplorerItemViewModel projectNode , CodeExplorerItemViewModel folderNode , string componentName ) ;
340
- private void AddComponentErrorNode ( CodeExplorerItemViewModel projectNode , CodeExplorerItemViewModel folderNode ,
341
- string componentName )
342
- {
343
- Projects . Remove ( projectNode ) ;
344
- RemoveFailingComponent ( projectNode , componentName ) ;
328
+ // at this point, we know the node is newly added--we have to add a new node, not just change the icon of the old one.
345
329
346
- folderNode . AddChild ( new CodeExplorerErrorNodeViewModel ( folderNode , componentName ) ) ;
347
- Projects . Add ( projectNode ) ;
330
+ var folderNode = projectNode . Items . FirstOrDefault ( f => f is CodeExplorerCustomFolderViewModel && f . Name == componentProject . Name ) ;
348
331
349
- if ( SortByName )
332
+ UiDispatcher . Invoke ( ( ) =>
350
333
{
351
- // this setter does not ensure the values are the same
352
- // it also sorts the projects by name
334
+ if ( folderNode == null )
335
+ {
336
+ folderNode = new CodeExplorerCustomFolderViewModel ( projectNode , componentProject . Name ,
337
+ componentProject . Name ) ;
338
+ projectNode . AddChild ( folderNode ) ;
339
+ }
340
+
341
+ var declaration = CreateDeclaration ( e . Component ) ;
342
+ var newNode = new CodeExplorerComponentViewModel ( folderNode , declaration , new List < Declaration > ( ) )
343
+ {
344
+ IsErrorState = true
345
+ } ;
346
+
347
+ folderNode . AddChild ( newNode ) ;
348
+
349
+ // Force a refresh. OnPropertyChanged("Projects") didn't work.
353
350
Projects = Projects ;
351
+ } ) ;
352
+ }
353
+
354
+ private Declaration CreateDeclaration ( VBComponent component )
355
+ {
356
+ var projectDeclaration =
357
+ _state . AllUserDeclarations . FirstOrDefault ( item =>
358
+ item . DeclarationType == DeclarationType . Project &&
359
+ item . Project . VBComponents . Cast < VBComponent > ( ) . Contains ( component ) ) ;
360
+
361
+ if ( component . Type == vbext_ComponentType . vbext_ct_StdModule )
362
+ {
363
+ return new ProceduralModuleDeclaration (
364
+ new QualifiedMemberName ( new QualifiedModuleName ( component ) , component . Name ) , projectDeclaration ,
365
+ component . Name , false , new List < IAnnotation > ( ) , null ) ;
354
366
}
367
+
368
+ return new ClassModuleDeclaration ( new QualifiedMemberName ( new QualifiedModuleName ( component ) , component . Name ) ,
369
+ projectDeclaration , component . Name , false , new List < IAnnotation > ( ) , null ) ;
355
370
}
356
371
357
372
private void ReorderChildNodes ( IEnumerable < CodeExplorerItemViewModel > nodes )
@@ -363,30 +378,30 @@ private void ReorderChildNodes(IEnumerable<CodeExplorerItemViewModel> nodes)
363
378
}
364
379
}
365
380
366
- private bool _removedNode ;
367
- private void RemoveFailingComponent ( CodeExplorerItemViewModel itemNode , string componentName )
381
+ private bool _errorStateSet ;
382
+ private void SetErrorState ( CodeExplorerItemViewModel itemNode , VBComponent component )
368
383
{
384
+ _errorStateSet = false ;
385
+
369
386
foreach ( var node in itemNode . Items )
370
387
{
371
388
if ( node is CodeExplorerCustomFolderViewModel )
372
389
{
373
- RemoveFailingComponent ( node , componentName ) ;
390
+ SetErrorState ( node , component ) ;
374
391
}
375
392
376
- if ( _removedNode )
393
+ if ( _errorStateSet )
377
394
{
378
395
return ;
379
396
}
380
397
381
398
if ( node is CodeExplorerComponentViewModel )
382
399
{
383
- var component = ( CodeExplorerComponentViewModel ) node ;
384
- if ( component . Name == componentName )
400
+ var componentNode = ( CodeExplorerComponentViewModel ) node ;
401
+ if ( componentNode . GetSelectedDeclaration ( ) . QualifiedName . QualifiedModuleName . Component == component )
385
402
{
386
- itemNode . Items . Remove ( node ) ;
387
- _removedNode = true ;
388
-
389
- return ;
403
+ componentNode . IsErrorState = true ;
404
+ _errorStateSet = true ;
390
405
}
391
406
}
392
407
}
0 commit comments