Skip to content

Commit fb11e70

Browse files
committed
Fix bugs in detecting trivia and modifiers.
1 parent 391e97b commit fb11e70

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

src/PortToTripleSlash/src/libraries/RoslynTripleSlash/TripleSlashSyntaxRewriter.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Diagnostics;
6-
using System;
77
using System.Diagnostics.CodeAnalysis;
88
using System.Linq;
9-
using System.Reflection.Emit;
109
using ApiDocsSync.PortToTripleSlash.Docs;
1110
using Microsoft.CodeAnalysis;
1211
using Microsoft.CodeAnalysis.CSharp;
1312
using Microsoft.CodeAnalysis.CSharp.Syntax;
14-
using Microsoft.CodeAnalysis.Editing;
15-
using System.Reflection.Metadata;
16-
using System.Xml;
17-
using System.Xml.Linq;
18-
using System.Collections;
19-
using System.Security.Policy;
2013

2114
/*
2215
* According to the Roslyn Quoter: https://roslynquoter.azurewebsites.net/
@@ -328,17 +321,10 @@ private bool TryGetType(SyntaxNode originalNode, [NotNullWhen(returnValue: true)
328321
return type != null;
329322
}
330323

331-
private static bool IsPublic([NotNullWhen(returnValue: true)] SyntaxNode? node)
332-
{
333-
if (node == null ||
334-
node is not MemberDeclarationSyntax baseNode ||
335-
!baseNode.Modifiers.Any(t => t.IsKind(SyntaxKind.PublicKeyword)))
336-
{
337-
return false;
338-
}
339-
340-
return true;
341-
}
324+
private static bool IsPublic([NotNullWhen(returnValue: true)] SyntaxNode? node) =>
325+
node != null &&
326+
node is MemberDeclarationSyntax baseNode &&
327+
baseNode.Modifiers.Any(t => t.IsKind(SyntaxKind.PublicKeyword));
342328

343329
public SyntaxNode Generate(SyntaxNode node, IDocsAPI api)
344330
{
@@ -358,9 +344,22 @@ public SyntaxNode Generate(SyntaxNode node, IDocsAPI api)
358344
break;
359345
}
360346

347+
if (originalTrivia.IsKind(SyntaxKind.WhitespaceTrivia))
348+
{
349+
// Avoid re-adding existing whitespace trivia, it will always be added later
350+
continue;
351+
}
352+
361353
if (!originalTrivia.HasStructure)
362354
{
355+
// Double slash comments do not have a structure but must be preserved with the original indentation
356+
// Only add indentation if the current trivia is not a new line
357+
if ((SyntaxKind)originalTrivia.RawKind != SyntaxKind.EndOfLineTrivia && indentationTrivia.HasValue)
358+
{
359+
updatedLeadingTrivia.Add(indentationTrivia.Value);
360+
}
363361
updatedLeadingTrivia.Add(originalTrivia);
362+
364363
continue;
365364
}
366365

@@ -369,6 +368,11 @@ public SyntaxNode Generate(SyntaxNode node, IDocsAPI api)
369368

370369
if (!structuredTrivia.IsKind(SyntaxKind.SingleLineDocumentationCommentTrivia))
371370
{
371+
// Unsure if there are other structured comments, but must preserve them with the original indentation
372+
if (indentationTrivia.HasValue)
373+
{
374+
updatedLeadingTrivia.Add(indentationTrivia.Value);
375+
}
372376
updatedLeadingTrivia.Add(originalTrivia);
373377
continue;
374378
}
@@ -396,7 +400,7 @@ public SyntaxNode Generate(SyntaxNode node, IDocsAPI api)
396400

397401
// The last trivia is the spacing before the actual node (usually before the visibility keyword)
398402
// must be replaced in its original location
399-
if (indentationTrivia != null)
403+
if (indentationTrivia.HasValue)
400404
{
401405
updatedLeadingTrivia.Add(indentationTrivia.Value);
402406
}

0 commit comments

Comments
 (0)