@@ -24,6 +24,8 @@ namespace ICSharpCode.CodeConverter.Util
2424{
2525 internal static class SyntaxNodeExtensions
2626 {
27+ private static SyntaxTrivia _endOfLine = SyntaxFactory . EndOfLine ( Environment . NewLine ) ;
28+
2729 public static IEnumerable < SyntaxNode > GetAncestors ( this SyntaxNode node )
2830 {
2931 var current = node . Parent ;
@@ -370,7 +372,6 @@ public static IEnumerable<SyntaxTrivia> ConvertTrivia(this IReadOnlyCollection<S
370372
371373 private static IEnumerable < SyntaxTrivia > ConvertVBTrivia ( SyntaxTrivia t )
372374 {
373- var endOfLine = SyntaxFactory . EndOfLine ( Environment . NewLine ) ;
374375
375376 if ( t . IsKind ( VBSyntaxKind . CommentTrivia ) ) {
376377 yield return SyntaxFactory . SyntaxTrivia ( CSSyntaxKind . SingleLineCommentTrivia , $ "// { t . GetCommentText ( ) } ") ;
@@ -397,24 +398,38 @@ private static IEnumerable<SyntaxTrivia> ConvertVBTrivia(SyntaxTrivia t)
397398 yield break ;
398399 }
399400
400- if ( t . HasStructure && t . GetStructure ( ) is VBSyntax . RegionDirectiveTriviaSyntax rdts ) {
401+ if ( t . HasStructure ) {
402+ bool wasConverted = false ;
403+ foreach ( var converted in ConvertStructuredVBTrivia ( t ) ) {
404+ yield return converted ;
405+ wasConverted = true ;
406+ }
407+ if ( wasConverted ) yield break ;
408+ }
409+
410+ //Each of these would need its own method to recreate for C# with the right structure probably so let's just warn about them for now.
411+ var convertedKind = t . GetCSKind ( ) ;
412+ yield return convertedKind . HasValue
413+ ? SyntaxFactory . Comment ( @$ "/* TODO ERROR: Skipped { convertedKind . Value }
414+ { t . ToFullString ( ) } */")
415+ : default ( SyntaxTrivia ) ;
416+ }
417+
418+ private static IEnumerable < SyntaxTrivia > ConvertStructuredVBTrivia ( SyntaxTrivia t )
419+ {
420+ var triviaStructure = t . GetStructure ( ) ;
421+ if ( triviaStructure is VBSyntax . RegionDirectiveTriviaSyntax rdts ) {
401422 var regionDirective = SyntaxFactory . RegionDirectiveTrivia ( true ) ;
402423 var regionKeyword = regionDirective . RegionKeyword . WithTrailingTrivia ( SyntaxFactory . Space ) ;
403- var endOfDirectiveToken = regionDirective . EndOfDirectiveToken . WithLeadingTrivia ( SyntaxFactory . PreprocessingMessage ( rdts . Name . Text . Trim ( '"' ) ) ) . WithTrailingTrivia ( endOfLine ) ;
424+ var endOfDirectiveToken = regionDirective . EndOfDirectiveToken . WithLeadingTrivia ( SyntaxFactory . PreprocessingMessage ( rdts . Name . Text . Trim ( '"' ) ) ) . WithTrailingTrivia ( _endOfLine ) ;
404425 yield return SyntaxFactory . Trivia ( regionDirective . WithRegionKeyword ( regionKeyword ) . WithEndOfDirectiveToken ( endOfDirectiveToken ) ) ;
405426 yield break ;
406427 }
407428
408429 if ( t . IsKind ( VBSyntaxKind . EndRegionDirectiveTrivia ) ) {
409- yield return SyntaxFactory . Trivia ( SyntaxFactory . EndRegionDirectiveTrivia ( true ) . WithTrailingTrivia ( endOfLine ) ) ;
430+ yield return SyntaxFactory . Trivia ( SyntaxFactory . EndRegionDirectiveTrivia ( true ) . WithTrailingTrivia ( _endOfLine ) ) ;
410431 yield break ;
411432 }
412-
413- //Each of these would need its own method to recreate for C# with the right structure probably so let's just warn about them for now.
414- var convertedKind = t . GetCSKind ( ) ;
415- yield return convertedKind . HasValue
416- ? SyntaxFactory . Comment ( $ "/* TODO ERROR: Skipped { convertedKind . Value } */")
417- : default ( SyntaxTrivia ) ;
418433 }
419434
420435 public static SyntaxTokenList GetModifiers ( this CSharpSyntaxNode member )
0 commit comments