11using  System . Linq ; 
22using  System . Runtime . InteropServices ; 
3- using  System . Windows . Forms ; 
3+ using  Rubberduck . Parsing . Grammar ; 
44using  Rubberduck . Parsing . Symbols ; 
55using  Rubberduck . Parsing . VBA ; 
66using  Rubberduck . Refactorings . Rename ; 
77using  Rubberduck . UI . Refactorings . Rename ; 
8+ using  Rubberduck . VBEditor ; 
89using  Rubberduck . VBEditor . SafeComWrappers . Abstract ; 
910
1011namespace  Rubberduck . UI . Command . Refactorings 
1112{ 
1213    [ ComVisible ( false ) ] 
1314    public  class  FormDesignerRefactorRenameCommand  :  RefactorCommandBase 
1415    { 
15-         private  readonly  IVBE  _vbe ; 
1616        private  readonly  RubberduckParserState  _state ; 
1717        private  readonly  IMessageBox  _messageBox ; 
1818
1919        public  FormDesignerRefactorRenameCommand ( IVBE  vbe ,  RubberduckParserState  state ,  IMessageBox  messageBox )  
2020            :  base  ( vbe ) 
2121        { 
22-             _vbe  =  vbe ; 
2322            _state  =  state ; 
2423            _messageBox  =  messageBox ; 
2524        } 
2625
2726        protected  override  bool  EvaluateCanExecute ( object  parameter ) 
2827        { 
29-             return  _state . Status  ==  ParserState . Ready ; 
28+             return  _state . Status  ==  ParserState . Ready   &&   GetTarget ( )   !=   null ; 
3029        } 
3130
3231        protected  override  void  OnExecute ( object  parameter ) 
@@ -45,35 +44,37 @@ protected override void OnExecute(object parameter)
4544            } 
4645        } 
4746
48-         private  Declaration  GetTarget ( ) 
47+         private  Declaration  GetTarget ( QualifiedModuleName ?   qualifiedModuleName   =   null ) 
4948        { 
50-             var  project  =  _vbe . ActiveVBProject ; 
51-             var  component  =  _vbe . SelectedVBComponent ; 
49+             ( var  projectId ,  var  component )  =  qualifiedModuleName . HasValue  
50+                 ?  ( qualifiedModuleName . Value . ProjectId ,  qualifiedModuleName . Value . Component ) 
51+                 :  ( Vbe . ActiveVBProject . ProjectId ,  Vbe . SelectedVBComponent ) ; 
52+                         
53+             if  ( component ? . HasDesigner  ??  false ) 
5254            { 
53-                 if  ( Vbe . SelectedVBComponent   !=   null   &&   Vbe . SelectedVBComponent . HasDesigner ) 
55+                 if  ( qualifiedModuleName . HasValue ) 
5456                { 
55-                     var  designer  =  ( ( dynamic ) component . Target ) . Designer ; 
56- 
57-                     if  ( designer . selected . count  ==  1 ) 
58-                     { 
59-                         var  control  =  designer . selected . item ( 0 ) ; 
60-                         var  result  =  _state . AllUserDeclarations 
61-                             . FirstOrDefault ( item =>  item . DeclarationType  ==  DeclarationType . Control 
62-                                                     &&  project . HelpFile  ==  item . ProjectId 
63-                                                     &&  item . ComponentName  ==  component . Name 
64-                                                     &&  item . IdentifierName  ==  control . Name ) ; 
65- 
66-                         Marshal . ReleaseComObject ( control ) ; 
67-                         Marshal . ReleaseComObject ( designer ) ; 
68-                         return  result ; 
69-                     }  else  { 
70-                         var  message  =  string . Format ( RubberduckUI . RenameDialog_AmbiguousSelection ) ; 
71-                         _messageBox . Show ( message ,  RubberduckUI . RenameDialog_Caption ,  MessageBoxButtons . OK , 
72-                             MessageBoxIcon . Exclamation ) ; 
73-                     } 
57+                     return  _state . DeclarationFinder 
58+                         . MatchName ( qualifiedModuleName . Value . Name ) 
59+                         . SingleOrDefault ( m =>  m . ProjectId  ==  projectId 
60+                             &&  m . DeclarationType . HasFlag ( qualifiedModuleName . Value . ComponentType ) 
61+                             &&  m . ComponentName  ==  component . Name ) ; 
7462                } 
75-             } 
76- 
63+                 
64+                 var  selectedCount  =  component . SelectedControls . Count ; 
65+                 if  ( selectedCount  >  1 )  {  return  null ;  } 
66+                 
67+                 // Cannot use DeclarationType.UserForm, parser only assigns UserForms the ClassModule flag 
68+                 ( var  selectedType ,  var  selectedName )  =  selectedCount  ==  0 
69+                     ?  ( DeclarationType . ClassModule ,  component . Name ) 
70+                     :  ( DeclarationType . Control ,  component . SelectedControls [ 0 ] . Name ) ; 
71+                 
72+                 return  _state . DeclarationFinder 
73+                     . MatchName ( selectedName ) 
74+                     . SingleOrDefault ( m =>  m . ProjectId  ==  projectId 
75+                         &&  m . DeclarationType . HasFlag ( selectedType ) 
76+                         &&  m . ComponentName  ==  component . Name ) ; 
77+              } 
7778            return  null ; 
7879        } 
7980    } 
0 commit comments