forked from dotnet/roslynator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddSummaryToDocumentationCommentRefactoring.cs
65 lines (49 loc) · 2.08 KB
/
AddSummaryToDocumentationCommentRefactoring.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) Josef Pihrt and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace Roslynator.CSharp.Refactorings;
internal static class AddSummaryToDocumentationCommentRefactoring
{
public static async Task<Document> RefactorAsync(
Document document,
DocumentationCommentTriviaSyntax documentationComment,
CancellationToken cancellationToken)
{
SyntaxList<XmlNodeSyntax> content = documentationComment.Content;
SourceText sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
TextLine line = sourceText.Lines[documentationComment.GetFullSpanStartLine(cancellationToken)];
string indent = StringUtility.GetLeadingWhitespaceExceptNewLine(line.ToString());
TextChange textChange;
if (content.Count == 1
&& content[0].IsKind(SyntaxKind.XmlText))
{
string text = content[0].ToString().Trim();
string newText = CreateSummaryElement(indent, text);
textChange = new TextChange(documentationComment.FullSpan, newText);
}
else
{
string newText = CreateSummaryElement(indent);
textChange = new TextChange(new TextSpan(documentationComment.FullSpan.Start, 0), newText);
}
return await document.WithTextChangeAsync(textChange, cancellationToken).ConfigureAwait(false);
}
private static string CreateSummaryElement(string indent, string text = null)
{
var sb = new StringBuilder();
sb.AppendLine("/// <summary>");
sb.Append(indent);
sb.Append("/// ");
sb.AppendLine(text);
sb.Append(indent);
sb.AppendLine("/// </summary>");
if (text is null)
sb.Append(indent);
return sb.ToString();
}
}