Skip to content

Commit 5ec5b5f

Browse files
Extract logic
1 parent fe73cc5 commit 5ec5b5f

File tree

4 files changed

+41
-23
lines changed

4 files changed

+41
-23
lines changed

CodeConverter/CSharp/CommonConversions.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public SyntaxToken ConvertIdentifier(SyntaxToken id, bool isAttribute = false, S
283283
text = propertyFieldSymbol.AssociatedSymbol.Name;
284284
} else if (normalizedText.EndsWith("Event", StringComparison.OrdinalIgnoreCase) && idSymbol is IFieldSymbol eventFieldSymbol && eventFieldSymbol.AssociatedSymbol?.IsKind(SymbolKind.Event) == true) {
285285
text = eventFieldSymbol.AssociatedSymbol.Name;
286-
} else if (MustInlinePropertyWithEventsAccess(id.Parent, idSymbol)) {
286+
} else if (WinformsConversions.MustInlinePropertyWithEventsAccess(id.Parent, idSymbol)) {
287287
// For C# Winforms designer, we need to use direct field access - see other usage of MustInlinePropertyWithEventsAccess
288288
text = "_" + text;
289289
}
@@ -317,20 +317,6 @@ private static string WithDeclarationCasing(SyntaxToken id, ISymbol idSymbol, st
317317
return text;
318318
}
319319

320-
/// <remarks>
321-
/// Co-ordinates inlining property events, see <see cref="MethodBodyExecutableStatementVisitor.GetPostAssignmentStatements"/>
322-
/// Also see usages of IsDesignerGeneratedTypeWithInitializeComponent
323-
/// </remarks>
324-
public static bool MustInlinePropertyWithEventsAccess(SyntaxNode anyNodePossiblyWithinMethod, ISymbol potentialPropertySymbol)
325-
{
326-
return InMethodCalledInitializeComponent(anyNodePossiblyWithinMethod) && potentialPropertySymbol is IPropertySymbol prop && prop.IsWithEvents;
327-
}
328-
329-
public static bool InMethodCalledInitializeComponent(SyntaxNode anyNodePossiblyWithinMethod)
330-
{
331-
return anyNodePossiblyWithinMethod.GetAncestor<VBSyntax.MethodBlockSyntax>()?.SubOrFunctionStatement.Identifier.Text == "InitializeComponent";
332-
}
333-
334320
public static SyntaxToken CsEscapedIdentifier(string text)
335321
{
336322
if (SyntaxFacts.GetKeywordKind(text) != CSSyntaxKind.None) text = "@" + text;

CodeConverter/CSharp/ExpressionNodeVisitor.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,9 @@ public override async Task<CSharpSyntaxNode> VisitLiteralExpression(VBasic.Synta
276276

277277
var val = node.Token.Value;
278278
var text = node.Token.Text;
279-
if (node.Parent is VBSyntax.AssignmentStatementSyntax assignment && CommonConversions.InMethodCalledInitializeComponent(node.Parent) &&
280-
assignment.Left is VBSyntax.MemberAccessExpressionSyntax maes && !(maes.Expression is VBSyntax.MeExpressionSyntax) &&
281-
maes.Name.ToString() == "Name" &&
282-
val is string valStr) {
283-
// Update name so field is regenerated correctly by winforms designer
284-
val = "_" + valStr;
285-
text = "\"_" + valStr + "\"";
279+
if (WinformsConversions.ShouldPrefixAssignedNameWithUnderscore(node.Parent as VBSyntax.AssignmentStatementSyntax) && val is string valStr) {
280+
val = "_" + valStr;
281+
text = "\"_" + valStr + "\"";
286282
}
287283

288284
return CommonConversions.Literal(val, text, convertedType);

CodeConverter/CSharp/MethodsWithHandles.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public IEnumerable<MemberDeclarationSyntax> GetDeclarationsForFieldBackedPropert
4747
/// </summary>
4848
public SyntaxList<StatementSyntax> GetPostAssignmentStatements(Microsoft.CodeAnalysis.VisualBasic.Syntax.AssignmentStatementSyntax node, ISymbol potentialPropertySymbol)
4949
{
50-
if (CommonConversions.MustInlinePropertyWithEventsAccess(node, potentialPropertySymbol))
50+
if (WinformsConversions.MustInlinePropertyWithEventsAccess(node, potentialPropertySymbol))
5151
{
5252
var fieldName = SyntaxFactory.IdentifierName("_" + potentialPropertySymbol.Name);
5353
var handledMethods = _handledMethodsFromPropertyWithEventName[potentialPropertySymbol.Name].ToArray();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Linq;
2+
using ICSharpCode.CodeConverter.Util;
3+
using Microsoft.CodeAnalysis;
4+
using VBSyntax = Microsoft.CodeAnalysis.VisualBasic.Syntax;
5+
6+
namespace ICSharpCode.CodeConverter.CSharp
7+
{
8+
internal static class WinformsConversions
9+
{
10+
/// <remarks>
11+
/// Co-ordinates inlining property events, see <see cref="MethodBodyExecutableStatementVisitor.GetPostAssignmentStatements"/>
12+
/// Also see usages of IsDesignerGeneratedTypeWithInitializeComponent
13+
/// </remarks>
14+
public static bool MustInlinePropertyWithEventsAccess(SyntaxNode anyNodePossiblyWithinMethod, ISymbol potentialPropertySymbol)
15+
{
16+
return InMethodCalledInitializeComponent(anyNodePossiblyWithinMethod) && potentialPropertySymbol is IPropertySymbol prop && prop.IsWithEvents;
17+
}
18+
19+
public static bool InMethodCalledInitializeComponent(SyntaxNode anyNodePossiblyWithinMethod)
20+
{
21+
return anyNodePossiblyWithinMethod.GetAncestor<VBSyntax.MethodBlockSyntax>()?.SubOrFunctionStatement.Identifier.Text == "InitializeComponent";
22+
}
23+
24+
/// <summary>
25+
/// We replace a field with a property to handle event subscription, so need to update the name so the winforms designer regenerates the file correctly in future
26+
/// </summary>
27+
/// <returns></returns>
28+
public static bool ShouldPrefixAssignedNameWithUnderscore(VBSyntax.StatementSyntax statementOrNull)
29+
{
30+
return statementOrNull is VBSyntax.AssignmentStatementSyntax assignment && InMethodCalledInitializeComponent(assignment) &&
31+
assignment.Left is VBSyntax.MemberAccessExpressionSyntax maes &&
32+
!(maes.Expression is VBSyntax.MeExpressionSyntax) &&
33+
maes.Name.ToString() == "Name";
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)