Skip to content

Commit 2969dee

Browse files
committed
Fix FileHeaderCodeFixProvider determination of file names
1 parent 95d103c commit 2969dee

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/FileHeaderCodeFixProvider.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
6666
return SpecializedTasks.CompletedTask;
6767
}
6868

69+
private static string GetFileName(Document document)
70+
=> Path.GetFileName(document.FilePath ?? document.Name);
71+
6972
private static async Task<Document> GetTransformedDocumentAsync(Document document, CancellationToken cancellationToken)
7073
{
7174
return document.WithSyntaxRoot(await GetTransformedSyntaxRootAsync(document, cancellationToken).ConfigureAwait(false));
@@ -80,7 +83,7 @@ private static async Task<SyntaxNode> GetTransformedSyntaxRootAsync(Document doc
8083
SyntaxNode newSyntaxRoot;
8184
if (fileHeader.IsMissing)
8285
{
83-
newSyntaxRoot = AddHeader(document, root, document.Name, settings);
86+
newSyntaxRoot = AddHeader(document, root, GetFileName(document), settings);
8487
}
8588
else
8689
{
@@ -144,9 +147,9 @@ private static SyntaxNode ReplaceWellFormedMultiLineCommentHeader(Document docum
144147
// Pad line that used to be next to a /*
145148
triviaStringParts[0] = commentIndentation + interlinePadding + " " + triviaStringParts[0];
146149
StringBuilder sb = StringBuilderPool.Allocate();
147-
string fileName = Path.GetFileName(document.FilePath);
150+
string fileName = GetFileName(document);
148151
var copyrightText = GetCopyrightText(commentIndentation + interlinePadding, settings.DocumentationRules.GetCopyrightText(fileName), newLineText);
149-
var newHeader = WrapInXmlComment(commentIndentation + interlinePadding, copyrightText, document.Name, settings, newLineText);
152+
var newHeader = WrapInXmlComment(commentIndentation + interlinePadding, copyrightText, fileName, settings, newLineText);
150153

151154
sb.Append(commentIndentation);
152155
sb.Append("/*");
@@ -310,7 +313,7 @@ private static SyntaxNode ReplaceHeader(Document document, SyntaxNode root, Styl
310313
string newLineText = document.Project.Solution.Workspace.Options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp);
311314
var newLineTrivia = SyntaxFactory.EndOfLine(newLineText);
312315

313-
var newHeaderTrivia = CreateNewHeader(leadingSpaces + "//", document.Name, settings, newLineText);
316+
var newHeaderTrivia = CreateNewHeader(leadingSpaces + "//", GetFileName(document), settings, newLineText);
314317
if (!isMalformedHeader && copyrightTriviaIndex.HasValue)
315318
{
316319
// Does the copyright element have leading whitespace? If so remove it.

0 commit comments

Comments
 (0)