33using System . Collections . ObjectModel ;
44using System . Linq ;
55using System . Windows . Input ;
6- using System . Windows . Threading ;
76using Microsoft . Vbe . Interop ;
87using Rubberduck . Navigation . Folders ;
8+ using Rubberduck . Parsing . Annotations ;
99using Rubberduck . Parsing . Symbols ;
1010using Rubberduck . Parsing . VBA ;
1111using Rubberduck . UI ;
1212using Rubberduck . UI . CodeExplorer . Commands ;
1313using Rubberduck . UI . Command ;
14+ using Rubberduck . UI . Command . MenuItems ;
15+ using Rubberduck . VBEditor ;
1416
1517// ReSharper disable CanBeReplacedWithTryCastAndCheckForNull
1618
@@ -20,12 +22,9 @@ public sealed class CodeExplorerViewModel : ViewModelBase, IDisposable
2022 {
2123 private readonly FolderHelper _folderHelper ;
2224 private readonly RubberduckParserState _state ;
23- private readonly Dispatcher _dispatcher ;
2425
2526 public CodeExplorerViewModel ( FolderHelper folderHelper , RubberduckParserState state , List < ICommand > commands )
2627 {
27- _dispatcher = Dispatcher . CurrentDispatcher ;
28-
2928 _folderHelper = folderHelper ;
3029 _state = state ;
3130 _state . StateChanged += ParserState_StateChanged ;
@@ -217,11 +216,6 @@ public string Description
217216 return ( ( CodeExplorerCustomFolderViewModel ) SelectedItem ) . FolderAttribute ;
218217 }
219218
220- if ( SelectedItem is CodeExplorerErrorNodeViewModel )
221- {
222- return ( ( CodeExplorerErrorNodeViewModel ) SelectedItem ) . Name ;
223- }
224-
225219 return string . Empty ;
226220 }
227221 }
@@ -267,9 +261,6 @@ private void ParserState_StateChanged(object sender, EventArgs e)
267261 return ;
268262 }
269263
270-
271- var components = userDeclarations . SelectMany ( s => s . Key . VBComponents . Cast < VBComponent > ( ) ) . FirstOrDefault ( s => s . Name == "asdf" ) ;
272-
273264 var newProjects = userDeclarations . Select ( grouping =>
274265 new CodeExplorerProjectViewModel ( _folderHelper ,
275266 grouping . SingleOrDefault ( declaration => declaration . DeclarationType == DeclarationType . Project ) ,
@@ -322,36 +313,60 @@ private void ParserState_ModuleStateChanged(object sender, Parsing.ParseProgress
322313 }
323314
324315 var componentProject = e . Component . Collection . Parent ;
325- var node = Projects . OfType < CodeExplorerProjectViewModel > ( )
316+ var projectNode = Projects . OfType < CodeExplorerProjectViewModel > ( )
326317 . FirstOrDefault ( p => p . Declaration . Project == componentProject ) ;
327318
328- if ( node == null )
319+ if ( projectNode == null )
329320 {
330321 return ;
331322 }
332323
333- var folderNode = node . Items . First ( f => f is CodeExplorerCustomFolderViewModel && f . Name == node . Name ) ;
324+ SetErrorState ( projectNode , e . Component ) ;
334325
335- AddErrorNode addNode = AddComponentErrorNode ;
336- _dispatcher . BeginInvoke ( addNode , node , folderNode , e . Component . Name ) ;
337- }
326+ if ( _errorStateSet ) { return ; }
338327
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.
345329
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 ) ;
348331
349- if ( SortByName )
332+ UiDispatcher . Invoke ( ( ) =>
350333 {
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.
353350 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 ) ;
354366 }
367+
368+ return new ClassModuleDeclaration ( new QualifiedMemberName ( new QualifiedModuleName ( component ) , component . Name ) ,
369+ projectDeclaration , component . Name , false , new List < IAnnotation > ( ) , null ) ;
355370 }
356371
357372 private void ReorderChildNodes ( IEnumerable < CodeExplorerItemViewModel > nodes )
@@ -363,30 +378,30 @@ private void ReorderChildNodes(IEnumerable<CodeExplorerItemViewModel> nodes)
363378 }
364379 }
365380
366- private bool _removedNode ;
367- private void RemoveFailingComponent ( CodeExplorerItemViewModel itemNode , string componentName )
381+ private bool _errorStateSet ;
382+ private void SetErrorState ( CodeExplorerItemViewModel itemNode , VBComponent component )
368383 {
384+ _errorStateSet = false ;
385+
369386 foreach ( var node in itemNode . Items )
370387 {
371388 if ( node is CodeExplorerCustomFolderViewModel )
372389 {
373- RemoveFailingComponent ( node , componentName ) ;
390+ SetErrorState ( node , component ) ;
374391 }
375392
376- if ( _removedNode )
393+ if ( _errorStateSet )
377394 {
378395 return ;
379396 }
380397
381398 if ( node is CodeExplorerComponentViewModel )
382399 {
383- var component = ( CodeExplorerComponentViewModel ) node ;
384- if ( component . Name == componentName )
400+ var componentNode = ( CodeExplorerComponentViewModel ) node ;
401+ if ( componentNode . GetSelectedDeclaration ( ) . QualifiedName . QualifiedModuleName . Component == component )
385402 {
386- itemNode . Items . Remove ( node ) ;
387- _removedNode = true ;
388-
389- return ;
403+ componentNode . IsErrorState = true ;
404+ _errorStateSet = true ;
390405 }
391406 }
392407 }
0 commit comments