Skip to content

Commit 3bb328a

Browse files
Output skipped text
1 parent 1eb7911 commit 3bb328a

8 files changed

Lines changed: 1380 additions & 105 deletions

File tree

CodeConverter/Util/SyntaxNodeExtensions.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

Tests/CSharp/TriviaTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,57 @@ public static void Main(string[] args)
9898
#endregion",
9999
hasLineCommentConversionIssue: true);//Auto-test code doesn't know to avoid adding comment on same line as region
100100
}
101+
102+
[Fact]
103+
public async Task Issue15_IfTrueAsync()
104+
{
105+
await TestConversionVisualBasicToCSharpAsync(
106+
@"Public Class AClass
107+
#If TRUE
108+
Private Sub AMethod()
109+
End Sub
110+
#End If
111+
End Class",
112+
@"
113+
public partial class AClass
114+
{
115+
/* TODO ERROR: Skipped IfDirectiveTrivia
116+
#If TRUE
117+
*/
118+
private void AMethod()
119+
{
120+
}
121+
/* TODO ERROR: Skipped EndIfDirectiveTrivia
122+
#End If
123+
*/
124+
}
125+
");
126+
}
127+
128+
[Fact]
129+
public async Task Issue15_IfFalseAsync()
130+
{
131+
await TestConversionVisualBasicToCSharpAsync(
132+
@"Public Class AClass
133+
#If FALSE
134+
Private Sub AMethod()
135+
End Sub
136+
#End If
137+
End Class",
138+
@"
139+
public partial class AClass
140+
{
141+
/* TODO ERROR: Skipped IfDirectiveTrivia
142+
#If TRUE
143+
*/
144+
private void AMethod()
145+
{
146+
}
147+
/* TODO ERROR: Skipped EndIfDirectiveTrivia
148+
#End If
149+
*/
150+
}
151+
");
152+
}
101153
}
102154
}

0 commit comments

Comments
 (0)