Skip to content

Commit 90fb494

Browse files
Rationalize FileKinds methods
- Add TryGetFileKindFromPath - Rename FilePathToRazorFileKind to GetFileKindFromPath and implement using TryGetFileKindFromPath. - Remove ComponentFilePathToRazorFileKind
1 parent 610c5a8 commit 90fb494

File tree

15 files changed

+59
-58
lines changed

15 files changed

+59
-58
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/FileKinds.cs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ namespace Microsoft.AspNetCore.Razor.Language;
99

1010
public static class FileKinds
1111
{
12+
private const string ComponentFileExtension = ".razor";
13+
private const string LegacyFileExtension = ".cshtml";
14+
1215
/// <summary>
1316
/// Returns <see langword="true"/> if the specified value represents a component or component import.
1417
/// </summary>
@@ -19,43 +22,54 @@ public static bool IsComponent(this RazorFileKind fileKind)
1922
/// Returns <see langword="true"/> if the specified value represents a component import.
2023
/// </summary>
2124
public static bool IsComponentImport(this RazorFileKind fileKind)
22-
=> fileKind == RazorFileKind.ComponentImport;
25+
=> fileKind is RazorFileKind.ComponentImport;
2326

2427
/// <summary>
2528
/// Returns <see langword="true"/> if the specified value represents a legacy file kind.
2629
/// </summary>
27-
internal static bool IsLegacy(this RazorFileKind fileKind)
28-
=> fileKind == RazorFileKind.Legacy;
30+
public static bool IsLegacy(this RazorFileKind fileKind)
31+
=> fileKind is RazorFileKind.Legacy;
2932

30-
public static RazorFileKind ComponentFilePathToRazorFileKind(string filePath)
33+
/// <summary>
34+
/// Compares the given file path to known Razor file extensions and names to determine the <see cref="RazorFileKind"/>.
35+
/// Returns <see langword="true"/> if a file kind can be determined; otherwise, <see langword="false"/>.
36+
/// </summary>
37+
public static bool TryGetFileKindFromPath(string filePath, out RazorFileKind fileKind)
3138
{
3239
ArgHelper.ThrowIfNull(filePath);
3340

34-
if (string.Equals(ComponentMetadata.ImportsFileName, Path.GetFileName(filePath), StringComparison.Ordinal))
35-
{
36-
return RazorFileKind.ComponentImport;
37-
}
38-
else
41+
var fileName = Path.GetFileName(filePath);
42+
43+
if (string.Equals(ComponentMetadata.ImportsFileName, fileName, StringComparison.Ordinal))
3944
{
40-
return RazorFileKind.Component;
45+
fileKind = RazorFileKind.ComponentImport;
46+
return true;
4147
}
42-
}
4348

44-
public static RazorFileKind FilePathToRazorFileKind(string filePath)
45-
{
46-
ArgHelper.ThrowIfNull(filePath);
49+
var extension = Path.GetExtension(filePath);
4750

48-
if (string.Equals(ComponentMetadata.ImportsFileName, Path.GetFileName(filePath), StringComparison.Ordinal))
49-
{
50-
return RazorFileKind.ComponentImport;
51-
}
52-
else if (string.Equals(".razor", Path.GetExtension(filePath), StringComparison.OrdinalIgnoreCase))
51+
if (string.Equals(ComponentFileExtension, extension, StringComparison.OrdinalIgnoreCase))
5352
{
54-
return RazorFileKind.Component;
53+
fileKind = RazorFileKind.Component;
54+
return true;
5555
}
56-
else
56+
57+
if (string.Equals(LegacyFileExtension, extension, StringComparison.OrdinalIgnoreCase))
5758
{
58-
return RazorFileKind.Legacy;
59+
fileKind = RazorFileKind.Legacy;
60+
return true;
5961
}
62+
63+
fileKind = default;
64+
return false;
6065
}
66+
67+
/// <summary>
68+
/// Compares the given file path to known Razor file extensions and names to determine the <see cref="RazorFileKind"/>.
69+
/// If a file kind can't be determined, the result is <see cref="RazorFileKind.Legacy"/>.
70+
/// </summary>
71+
public static RazorFileKind GetFileKindFromPath(string filePath)
72+
=> TryGetFileKindFromPath(filePath, out var fileKind)
73+
? fileKind
74+
: RazorFileKind.Legacy;
6175
}

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/NotFoundProjectItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal class NotFoundProjectItem(string path, RazorFileKind? fileKind) : Razor
2222

2323
/// <inheritdoc />
2424
public override RazorFileKind FileKind { get; }
25-
= fileKind ?? FileKinds.FilePathToRazorFileKind(path);
25+
= fileKind ?? FileKinds.GetFileKindFromPath(path);
2626

2727
/// <inheritdoc />
2828
public override bool Exists => false;

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/RazorProjectEngineExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private static RazorCodeDocument CreateCodeDocumentCore(
6565
IReadOnlyList<TagHelperDescriptor>? tagHelpers = null)
6666
{
6767
var fileKindValue = fileKind ?? (source.FilePath is string filePath
68-
? FileKinds.FilePathToRazorFileKind(filePath)
68+
? FileKinds.GetFileKindFromPath(filePath)
6969
: DefaultFileKind);
7070

7171
return projectEngine.CreateCodeDocument(source, fileKindValue, importSources, tagHelpers, cssScope: null);
@@ -126,7 +126,7 @@ private static RazorCodeDocument CreateDesignTimeCodeDocumentCore(
126126
IReadOnlyList<TagHelperDescriptor>? tagHelpers = null)
127127
{
128128
var fileKindValue = fileKind ?? (source.FilePath is string filePath
129-
? FileKinds.FilePathToRazorFileKind(filePath)
129+
? FileKinds.GetFileKindFromPath(filePath)
130130
: DefaultFileKind);
131131

132132
return projectEngine.CreateDesignTimeCodeDocument(source, fileKindValue, importSources, tagHelpers);

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/RazorProjectItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public abstract class RazorProjectItem
4848
public virtual RazorFileKind FileKind
4949
=> FilePath == null
5050
? RazorFileKind.None
51-
: FileKinds.FilePathToRazorFileKind(FilePath);
51+
: FileKinds.GetFileKindFromPath(FilePath);
5252

5353
/// <summary>
5454
/// Gets the file contents as readonly <see cref="Stream"/>.

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/SourceGenerators/RazorSourceGenerator.RazorProviders.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private static (SourceGeneratorProjectItem?, Diagnostic?) ComputeProjectItems((A
9696
.Replace(Path.DirectorySeparatorChar, '/')
9797
.Replace("//", "/"),
9898
relativePhysicalPath: relativePath,
99-
fileKind: FileKinds.FilePathToRazorFileKind(additionalText.Path),
99+
fileKind: FileKinds.GetFileKindFromPath(additionalText.Path),
100100
additionalText: additionalText,
101101
cssScope: cssScope);
102102
return (projectItem, null);

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RemoteProjectItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public RemoteProjectItem(string filePath, string physicalPath, RazorFileKind? fi
1313
{
1414
FilePath = filePath;
1515
PhysicalPath = physicalPath;
16-
FileKind = fileKind ?? FileKinds.FilePathToRazorFileKind(FilePath);
16+
FileKind = fileKind ?? FileKinds.GetFileKindFromPath(FilePath);
1717
RelativePhysicalPath = FilePath.StartsWith('/')
1818
? FilePath[1..]
1919
: FilePath;

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Hover/HoverFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ internal static class HoverFactory
225225
CancellationToken cancellationToken)
226226
{
227227
// Filter out attribute descriptors since we're creating an element hover
228-
var keepAttributeInfo = FileKinds.FilePathToRazorFileKind(documentFilePath) == RazorFileKind.Legacy;
228+
var keepAttributeInfo = FileKinds.GetFileKindFromPath(documentFilePath) == RazorFileKind.Legacy;
229229
var descriptionInfos = descriptors
230230
.Where(d => keepAttributeInfo || !d.IsAttributeDescriptor())
231231
.SelectAsArray(BoundElementDescriptionInfo.From);

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/HostDocument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public HostDocument(string filePath, string targetPath, RazorFileKind? fileKind
1515
{
1616
FilePath = filePath;
1717
TargetPath = targetPath;
18-
FileKind = fileKind ?? FileKinds.FilePathToRazorFileKind(filePath);
18+
FileKind = fileKind ?? FileKinds.GetFileKindFromPath(filePath);
1919
}
2020
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Utilities/RazorProjectInfoFactory.cs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ internal static ImmutableArray<DocumentSnapshotHandle> GetDocuments(Project proj
150150
foreach (var document in project.AdditionalDocuments)
151151
{
152152
if (document.FilePath is { } filePath &&
153-
TryGetFileKind(filePath, out var kind))
153+
FileKinds.TryGetFileKindFromPath(filePath, out var kind))
154154
{
155155
documents.Add(new DocumentSnapshotHandle(filePath, GetTargetPath(filePath, normalizedProjectPath), kind));
156156
}
@@ -165,7 +165,7 @@ internal static ImmutableArray<DocumentSnapshotHandle> GetDocuments(Project proj
165165
foreach (var document in project.Documents)
166166
{
167167
if (TryGetRazorFileName(document.FilePath, out var razorFilePath) &&
168-
TryGetFileKind(razorFilePath, out var kind))
168+
FileKinds.TryGetFileKindFromPath(razorFilePath, out var kind))
169169
{
170170
documents.Add(new DocumentSnapshotHandle(razorFilePath, GetTargetPath(razorFilePath, normalizedProjectPath), kind));
171171
}
@@ -190,25 +190,6 @@ private static string GetTargetPath(string documentFilePath, string normalizedPr
190190
return normalizedTargetFilePath;
191191
}
192192

193-
private static bool TryGetFileKind(string filePath, out RazorFileKind fileKind)
194-
{
195-
var extension = Path.GetExtension(filePath);
196-
197-
if (extension.Equals(".cshtml", s_stringComparison))
198-
{
199-
fileKind = RazorFileKind.Legacy;
200-
return true;
201-
}
202-
else if (extension.Equals(".razor", s_stringComparison))
203-
{
204-
fileKind = FileKinds.ComponentFilePathToRazorFileKind(filePath);
205-
return true;
206-
}
207-
208-
fileKind = default;
209-
return false;
210-
}
211-
212193
private static bool TryGetRazorFileName(string? filePath, [NotNullWhen(true)] out string? razorFilePath)
213194
{
214195
if (filePath is null)

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/RemoteDocumentSnapshot.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public RemoteDocumentSnapshot(TextDocument textDocument, RemoteProjectSnapshot p
3131
ProjectSnapshot = projectSnapshot;
3232
}
3333

34-
public RazorFileKind FileKind => FileKinds.FilePathToRazorFileKind(FilePath);
34+
public RazorFileKind FileKind => FileKinds.GetFileKindFromPath(FilePath);
3535
public string FilePath => TextDocument.FilePath.AssumeNotNull();
3636
public string TargetPath => TextDocument.FilePath.AssumeNotNull();
3737

0 commit comments

Comments
 (0)