Skip to content

Commit 1f6c929

Browse files
authored
Merge pull request #2198 from dfyx/implement-1143
Implement #1143: custom culture/locale for documentation texts
2 parents 6661c25 + 6957625 commit 1f6c929

22 files changed

Lines changed: 1774 additions & 132 deletions

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1642SA1643CodeFixProvider.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ namespace StyleCop.Analyzers.DocumentationRules
66
using System;
77
using System.Collections.Immutable;
88
using System.Composition;
9+
using System.Globalization;
910
using System.Linq;
1011
using System.Text.RegularExpressions;
12+
using System.Threading;
1113
using System.Threading.Tasks;
1214
using Helpers;
1315
using Microsoft.CodeAnalysis;
@@ -57,7 +59,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
5759
context.RegisterCodeFix(
5860
CodeAction.Create(
5961
DocumentationResources.SA1642SA1643CodeFix,
60-
cancellationToken => GetTransformedDocumentAsync(context.Document, root, xmlElementSyntax),
62+
cancellationToken => GetTransformedDocumentAsync(context.Document, root, xmlElementSyntax, cancellationToken),
6163
nameof(SA1642SA1643CodeFixProvider)),
6264
diagnostic);
6365
}
@@ -74,11 +76,14 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
7476
}
7577
}
7678

77-
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, XmlElementSyntax node)
79+
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, XmlElementSyntax node, CancellationToken cancellationToken)
7880
{
7981
var typeDeclaration = node.FirstAncestorOrSelf<BaseTypeDeclarationSyntax>();
8082
var declarationSyntax = node.FirstAncestorOrSelf<BaseMethodDeclarationSyntax>();
8183
bool isStruct = typeDeclaration.IsKind(SyntaxKind.StructDeclaration);
84+
var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(cancellationToken);
85+
var culture = new CultureInfo(settings.DocumentationRules.DocumentationCulture);
86+
var resourceManager = DocumentationResources.ResourceManager;
8287

8388
TypeParameterListSyntax typeParameterList;
8489
ClassDeclarationSyntax classDeclaration = typeDeclaration as ClassDeclarationSyntax;
@@ -94,35 +99,29 @@ private static Task<Document> GetTransformedDocumentAsync(Document document, Syn
9499
ImmutableArray<string> standardText;
95100
if (declarationSyntax is ConstructorDeclarationSyntax)
96101
{
102+
var typeKindText = resourceManager.GetString(isStruct ? nameof(DocumentationResources.TypeTextStruct) : nameof(DocumentationResources.TypeTextClass), culture);
97103
if (declarationSyntax.Modifiers.Any(SyntaxKind.StaticKeyword))
98104
{
99-
if (isStruct)
100-
{
101-
standardText = ImmutableArray.Create(SA1642ConstructorSummaryDocumentationMustBeginWithStandardText.StaticConstructorStandardText, " struct.");
102-
}
103-
else
104-
{
105-
standardText = ImmutableArray.Create(SA1642ConstructorSummaryDocumentationMustBeginWithStandardText.StaticConstructorStandardText, " class.");
106-
}
105+
standardText = ImmutableArray.Create(
106+
string.Format(resourceManager.GetString(nameof(DocumentationResources.StaticConstructorStandardTextFirstPart), culture), typeKindText),
107+
string.Format(resourceManager.GetString(nameof(DocumentationResources.StaticConstructorStandardTextSecondPart), culture), typeKindText));
107108
}
108109
else
109110
{
110111
// Prefer to insert the "non-private" wording for all constructors, even though both are considered
111112
// acceptable for private constructors by the diagnostic.
112113
// https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/413
113-
if (isStruct)
114-
{
115-
standardText = ImmutableArray.Create(SA1642ConstructorSummaryDocumentationMustBeginWithStandardText.NonPrivateConstructorStandardText, " struct.");
116-
}
117-
else
118-
{
119-
standardText = ImmutableArray.Create(SA1642ConstructorSummaryDocumentationMustBeginWithStandardText.NonPrivateConstructorStandardText, " class.");
120-
}
114+
standardText = ImmutableArray.Create(
115+
string.Format(resourceManager.GetString(nameof(DocumentationResources.NonPrivateConstructorStandardTextFirstPart), culture), typeKindText),
116+
string.Format(resourceManager.GetString(nameof(DocumentationResources.NonPrivateConstructorStandardTextSecondPart), culture), typeKindText));
121117
}
122118
}
123119
else if (declarationSyntax is DestructorDeclarationSyntax)
124120
{
125-
standardText = SA1643DestructorSummaryDocumentationMustBeginWithStandardText.DestructorStandardText;
121+
standardText =
122+
ImmutableArray.Create(
123+
resourceManager.GetString(nameof(DocumentationResources.DestructorStandardTextFirstPart), culture),
124+
resourceManager.GetString(nameof(DocumentationResources.DestructorStandardTextSecondPart), culture));
126125
}
127126
else
128127
{

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/StyleCop.Analyzers.nuspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
<!-- Binaries and symbols -->
2121
<file src="bin\$Configuration$\StyleCop.Analyzers.dll" target="analyzers\dotnet\cs" />
22+
<file src="bin\$Configuration$\**\StyleCop.Analyzers.resources.dll" target="analyzers\dotnet\cs" />
2223
<file src="bin\$Configuration$\StyleCop.Analyzers.pdb" target="analyzers\dotnet\cs" />
2324
<file src="bin\$Configuration$\StyleCop.Analyzers.CodeFixes.dll" target="analyzers\dotnet\cs" />
25+
<file src="bin\$Configuration$\**\StyleCop.Analyzers.CodeFixes.resources.dll" target="analyzers\dotnet\cs" />
2426
<file src="bin\$Configuration$\StyleCop.Analyzers.CodeFixes.pdb" target="analyzers\dotnet\cs" />
2527
<file src="..\..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll" target="analyzers\dotnet\cs" />
2628

0 commit comments

Comments
 (0)