|
4 | 4 | using Microsoft.CodeAnalysis.CSharp; |
5 | 5 | using Microsoft.CodeAnalysis.CSharp.Syntax; |
6 | 6 | using Microsoft.CodeAnalysis.Editing; |
7 | | -using Microsoft.CodeAnalysis.Simplification; |
8 | 7 | using static Microsoft.CodeAnalysis.VisualBasic.VisualBasicExtensions; |
9 | 8 | using ICSharpCode.CodeConverter.Util.FromRoslyn; |
10 | 9 | using ISymbolExtensions = ICSharpCode.CodeConverter.Util.ISymbolExtensions; |
@@ -733,16 +732,34 @@ private static ExpressionSyntax BuildCharExpressionFromVbSyntax(VBSyntax.Express |
733 | 732 | // For char literal expressions (e.g. "^"c), use the constant value directly |
734 | 733 | if (defaultValueNode.IsKind(VBasic.SyntaxKind.CharacterLiteralExpression)) { |
735 | 734 | var constant = semanticModel.GetConstantValue(defaultValueNode); |
736 | | - if (constant.HasValue && constant.Value is char c) { |
| 735 | + if (constant.HasValue && constant.Value is char c) |
737 | 736 | return CS.SyntaxFactory.LiteralExpression(CS.SyntaxKind.CharacterLiteralExpression, CS.SyntaxFactory.Literal(c)); |
738 | | - } |
739 | 737 | } |
740 | | - // For named constant references (e.g. DlM or Module.DlM), build a member access expression. |
741 | | - // Strip VB's "Global." prefix (VB global namespace qualifier, has no C# identifier equivalent). |
742 | | - // Annotate for simplification so Roslyn reduces e.g. TestModule.DlM → DlM within TestModule. |
743 | | - var parts = defaultValueNode.ToString().Trim().Split('.'); |
744 | | - if (parts.Length > 1 && parts[0] == "Global") parts = parts.Skip(1).ToArray(); |
745 | | - return ValidSyntaxFactory.MemberAccess(parts).WithAdditionalAnnotations(Simplifier.Annotation); |
| 738 | + return VbNameExprToCsExpr(defaultValueNode); |
| 739 | + } |
| 740 | + |
| 741 | + private static ExpressionSyntax VbNameExprToCsExpr(VBSyntax.ExpressionSyntax vbExpr) |
| 742 | + { |
| 743 | + switch (vbExpr) { |
| 744 | + case VBSyntax.IdentifierNameSyntax identifier: |
| 745 | + return CS.SyntaxFactory.IdentifierName(identifier.Identifier.Text); |
| 746 | + case VBSyntax.MemberAccessExpressionSyntax memberAccess: |
| 747 | + // Skip VB's "Global." qualifier — it's VB's global namespace prefix, has no direct C# identifier equivalent |
| 748 | + if (memberAccess.Expression.IsKind(VBasic.SyntaxKind.GlobalName)) |
| 749 | + return CS.SyntaxFactory.IdentifierName(memberAccess.Name.Identifier.Text); |
| 750 | + return CS.SyntaxFactory.MemberAccessExpression(CS.SyntaxKind.SimpleMemberAccessExpression, |
| 751 | + VbNameExprToCsExpr(memberAccess.Expression), |
| 752 | + CS.SyntaxFactory.IdentifierName(memberAccess.Name.Identifier.Text)); |
| 753 | + case VBSyntax.QualifiedNameSyntax qualifiedName: |
| 754 | + // Qualified names (e.g. Global.TestModule) appear in name/type context within default values |
| 755 | + if (qualifiedName.Left.IsKind(VBasic.SyntaxKind.GlobalName)) |
| 756 | + return CS.SyntaxFactory.IdentifierName(qualifiedName.Right.Identifier.Text); |
| 757 | + return CS.SyntaxFactory.MemberAccessExpression(CS.SyntaxKind.SimpleMemberAccessExpression, |
| 758 | + VbNameExprToCsExpr(qualifiedName.Left), |
| 759 | + CS.SyntaxFactory.IdentifierName(qualifiedName.Right.Identifier.Text)); |
| 760 | + default: |
| 761 | + throw new NotSupportedException($"Unexpected VB expression in char default value: {vbExpr}"); |
| 762 | + } |
746 | 763 | } |
747 | 764 |
|
748 | 765 | private static bool IsThisResumeLayoutInvocation(StatementSyntax s) |
|
0 commit comments