Skip to content

Commit ba0ceeb

Browse files
Copilotdavidwengier
andcommitted
Simplify code per review: use IsRazorFilePath, pattern matching, remove DocumentFilePaths checks, fix string literal handling to run before C# GTD
Co-authored-by: davidwengier <[email protected]>
1 parent afde1e3 commit ba0ceeb

File tree

2 files changed

+25
-44
lines changed

2 files changed

+25
-44
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/GoToDefinition/AbstractDefinitionService.cs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
using System;
55
using System.Diagnostics;
66
using System.IO;
7-
using System.Linq;
87
using System.Threading;
98
using System.Threading.Tasks;
10-
using Microsoft.AspNetCore.Razor;
119
using Microsoft.AspNetCore.Razor.Language;
1210
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
1311
using Microsoft.CodeAnalysis.Razor.Logging;
@@ -131,11 +129,15 @@ private async Task<LspRange> GetNavigateRangeAsync(IDocumentSnapshot documentSna
131129
var literalText = token.ValueText;
132130
_logger.LogDebug($"Found string literal: {literalText}");
133131

134-
// Try to resolve the file path
135-
if (TryResolveFilePath(documentSnapshot, literalText, out var resolvedPath))
132+
// Only process if it looks like a Razor file path
133+
if (literalText.IsRazorFilePath())
136134
{
137-
_logger.LogDebug($"Resolved file path: {resolvedPath}");
138-
return [LspFactory.CreateLocation(resolvedPath, LspFactory.DefaultRange)];
135+
// Try to resolve the file path
136+
if (TryResolveFilePath(documentSnapshot, literalText, out var resolvedPath))
137+
{
138+
_logger.LogDebug($"Resolved file path: {resolvedPath}");
139+
return [LspFactory.CreateLocation(resolvedPath, LspFactory.DefaultRange)];
140+
}
139141
}
140142
}
141143

@@ -151,18 +153,10 @@ private bool TryResolveFilePath(IDocumentSnapshot documentSnapshot, string fileP
151153
return false;
152154
}
153155

154-
// Check if the file extension is .cshtml or .razor
155-
var extension = Path.GetExtension(filePath);
156-
if (!extension.Equals(".cshtml", StringComparison.OrdinalIgnoreCase) &&
157-
!extension.Equals(".razor", StringComparison.OrdinalIgnoreCase))
158-
{
159-
return false;
160-
}
161-
162156
var project = documentSnapshot.Project;
163157

164158
// Handle tilde paths (~/ or ~\) - these are relative to the project root
165-
if (filePath.StartsWith("~/") || filePath.StartsWith("~\\"))
159+
if (filePath is ['~', '/' or '\\', ..])
166160
{
167161
var projectDirectory = Path.GetDirectoryName(project.FilePath);
168162
if (projectDirectory is null)
@@ -174,14 +168,6 @@ private bool TryResolveFilePath(IDocumentSnapshot documentSnapshot, string fileP
174168
var relativePath = filePath.Substring(2).Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar);
175169
var candidatePath = Path.GetFullPath(Path.Combine(projectDirectory, relativePath));
176170

177-
// Check using path comparison since the project might not have the document loaded yet
178-
var matchingPath = project.DocumentFilePaths.FirstOrDefault(d => PathUtilities.OSSpecificPathComparer.Equals(d, candidatePath));
179-
if (matchingPath is not null)
180-
{
181-
resolvedPath = matchingPath;
182-
return true;
183-
}
184-
185171
if (project.ContainsDocument(candidatePath))
186172
{
187173
resolvedPath = candidatePath;
@@ -196,14 +182,6 @@ private bool TryResolveFilePath(IDocumentSnapshot documentSnapshot, string fileP
196182
var normalizedPath = filePath.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar);
197183
var candidatePath = Path.GetFullPath(Path.Combine(currentDocumentDirectory, normalizedPath));
198184

199-
// Check using path comparison since the project might not have the document loaded yet
200-
var matchingPath = project.DocumentFilePaths.FirstOrDefault(d => PathUtilities.OSSpecificPathComparer.Equals(d, candidatePath));
201-
if (matchingPath is not null)
202-
{
203-
resolvedPath = matchingPath;
204-
return true;
205-
}
206-
207185
if (project.ContainsDocument(candidatePath))
208186
{
209187
resolvedPath = candidatePath;

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/GoToDefinition/RemoteGoToDefinitionService.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ protected override IRemoteGoToDefinitionService CreateService(in ServiceArgs arg
7171
return Results(componentLocations);
7272
}
7373

74+
// Check if we're in a string literal with a file path (before calling C# which would navigate to String class)
75+
if (positionInfo.LanguageKind is RazorLanguageKind.CSharp)
76+
{
77+
var stringLiteralLocations = await _definitionService.TryGetDefinitionFromStringLiteralAsync(
78+
context.Snapshot,
79+
positionInfo.Position.ToLinePosition(),
80+
cancellationToken)
81+
.ConfigureAwait(false);
82+
83+
if (stringLiteralLocations is { Length: > 0 })
84+
{
85+
return Results(stringLiteralLocations);
86+
}
87+
}
88+
7489
if (positionInfo.LanguageKind is RazorLanguageKind.Html or RazorLanguageKind.Razor)
7590
{
7691
// If it isn't a Razor construct, and it isn't C#, let the server know to delegate to HTML.
@@ -93,19 +108,7 @@ protected override IRemoteGoToDefinitionService CreateService(in ServiceArgs arg
93108

94109
if (locations is null and not [])
95110
{
96-
// C# didn't return anything, check if we're in a string literal with a file path
97-
var stringLiteralLocations = await _definitionService.TryGetDefinitionFromStringLiteralAsync(
98-
context.Snapshot,
99-
positionInfo.Position.ToLinePosition(),
100-
cancellationToken)
101-
.ConfigureAwait(false);
102-
103-
if (stringLiteralLocations is { Length: > 0 })
104-
{
105-
return Results(stringLiteralLocations);
106-
}
107-
108-
// Nothing found, so we're done.
111+
// C# didn't return anything, so we're done.
109112
return NoFurtherHandling;
110113
}
111114

0 commit comments

Comments
 (0)