11using System ;
22using System . Collections . Generic ;
3+ using System . Collections . Immutable ;
34using System . Linq ;
45using ICSharpCode . CodeConverter . Util ;
56using Microsoft . CodeAnalysis ;
@@ -15,47 +16,47 @@ internal class MethodsWithHandles
1516 {
1617 private readonly List < MethodWithHandles > _methodWithHandleses ;
1718 private readonly ILookup < string , MethodWithHandles > _handledMethodsFromPropertyWithEventName ;
19+ private readonly ImmutableHashSet < string > _containerFieldsConvertedToProperties ;
1820
1921
20- public MethodsWithHandles ( List < MethodWithHandles > methodWithHandleses , ILookup < string , MethodWithHandles > handledMethodsFromPropertyWithEventName )
22+ private MethodsWithHandles ( List < MethodWithHandles > methodWithHandleses ,
23+ ILookup < string , MethodWithHandles > handledMethodsFromPropertyWithEventName ,
24+ ImmutableHashSet < string > containerFieldsConvertedToProperties )
2125 {
2226 _methodWithHandleses = methodWithHandleses ;
2327 _handledMethodsFromPropertyWithEventName = handledMethodsFromPropertyWithEventName ;
28+ _containerFieldsConvertedToProperties = containerFieldsConvertedToProperties ;
2429 }
2530
2631 public static MethodsWithHandles Create ( List < MethodWithHandles > methodWithHandleses )
2732 {
2833 var handledMethodsFromPropertyWithEventName = methodWithHandleses
29- . SelectMany ( m => m . HandledPropertyEventCSharpIds . Select ( h => ( EventPropertyName : h . Item1 . Text , MethodWithHandles : m ) ) )
34+ . SelectMany ( m => m . HandledPropertyEventCSharpIds . Select ( h => ( EventPropertyName : h . EventContainerName . Text , MethodWithHandles : m ) ) )
3035 . ToLookup ( m => m . EventPropertyName , m => m . MethodWithHandles ) ;
31- return new MethodsWithHandles ( methodWithHandleses , handledMethodsFromPropertyWithEventName ) ;
36+ var containerFieldsConvertedToProperties = methodWithHandleses
37+ . SelectMany ( m => m . HandledPropertyEventCSharpIds , ( _ , handled ) => handled . EventContainerName . Text )
38+ . ToImmutableHashSet ( StringComparer . OrdinalIgnoreCase ) ;
39+ return new MethodsWithHandles ( methodWithHandleses , handledMethodsFromPropertyWithEventName , containerFieldsConvertedToProperties ) ;
3240 }
3341
34- public bool Any ( )
35- {
36- return _methodWithHandleses . Any ( ) ;
37- }
42+ public bool Any ( ) => _methodWithHandleses . Any ( ) ;
43+ public bool AnyForPropertyName ( string propertyIdentifierText ) => _containerFieldsConvertedToProperties . Contains ( propertyIdentifierText ) ;
3844
3945 public IEnumerable < MemberDeclarationSyntax > GetDeclarationsForFieldBackedProperty ( VariableDeclarationSyntax fieldDecl , SyntaxTokenList convertedModifiers , SyntaxList < AttributeListSyntax > attributes )
4046 {
4147 return MethodWithHandles . GetDeclarationsForFieldBackedProperty ( fieldDecl , convertedModifiers , attributes ,
4248 _methodWithHandleses ) ;
4349 }
4450
45-
46- /// <summary>
47- /// Make winforms designer work: https://github.com/icsharpcode/CodeConverter/issues/321
48- /// </summary>
49- public SyntaxList < StatementSyntax > GetPostAssignmentStatements ( Microsoft . CodeAnalysis . VisualBasic . Syntax . AssignmentStatementSyntax node , ISymbol potentialPropertySymbol )
51+ public SyntaxList < StatementSyntax > GetPostAssignmentStatements ( ISymbol potentialPropertySymbol )
5052 {
51- if ( WinformsConversions . MustInlinePropertyWithEventsAccess ( node , potentialPropertySymbol ) )
53+ var fieldName = SyntaxFactory . IdentifierName ( "_" + potentialPropertySymbol . Name ) ;
54+ var handledMethods = _handledMethodsFromPropertyWithEventName [ potentialPropertySymbol . Name ] . ToArray ( ) ;
55+ if ( handledMethods . Any ( ) )
5256 {
53- var fieldName = SyntaxFactory . IdentifierName ( "_" + potentialPropertySymbol . Name ) ;
54- var handledMethods = _handledMethodsFromPropertyWithEventName [ potentialPropertySymbol . Name ] . ToArray ( ) ;
55- if ( handledMethods . Any ( ) )
57+ var postAssignmentStatements = handledMethods . SelectMany ( h =>
58+ h . GetPostInitializationStatements ( potentialPropertySymbol . Name , fieldName ) ) ;
5659 {
57- var postAssignmentStatements = handledMethods . SelectMany ( h =>
58- h . GetPostInitializationStatements ( potentialPropertySymbol . Name , fieldName ) ) ;
5960 return SyntaxFactory . List ( postAssignmentStatements ) ;
6061 }
6162 }
0 commit comments