Commit 71676b1
authored
Add Go To Definition support for file paths in string literals (#12323)
- [x] Analyze existing Go To Definition flow and understand the
architecture
- [x] Add logic to detect string literals containing file paths in
RemoteGoToDefinitionService
- [x] Implement file path resolution and validation logic in
AbstractDefinitionService
- [x] Add tests in CohostGoToDefinitionEndpointTest.cs for various
scenarios
- [x] Build and validate compilation
- [x] Address PR feedback (all rounds):
- [x] Renamed method to `TryGetDefinitionFromStringLiteralAsync`
- [x] Changed log messages to debug level
- [x] Use `GetRequiredAbsoluteIndex` instead of
`sourceText.Lines.GetPosition`
- [x] Added file extension check (.cshtml/.razor) before path resolution
- [x] Removed absolute path handling
- [x] Use `ContainsDocument` instead of `TryGetDocument`
- [x] Added WorkItem attributes to all new tests
- [x] Added entry to copilot-instructions.md about
GetRequiredAbsoluteIndex
- [x] Updated tests to use local `FileName` helper
- [x] Use `IsRazorFilePath()` extension method
- [x] Use pattern matching for tilde path check
- [x] Removed `DocumentFilePaths` checks (redundant with
`ContainsDocument`)
- [x] Move string literal check before C# Go-to-Definition call
- [x] Merged main branch (test files moved to new location)
- [x] Move IsRazorFilePath check into TryResolveFilePath method for
better encapsulation
- [x] Remove tests with incorrect expectations
- [x] Merge main again and fix build errors
## Implementation Summary
Added support for navigating to Razor/CSHTML files by filename string in
Go To Definition (F12). When the cursor is in a string literal
containing a file path (e.g., `@Html.Partial("~/Views/xy.cshtml")`),
pressing F12 will navigate to that file.
### Key Features:
- Supports tilde paths (`~/...`) relative to project root
- Supports relative paths (relative to current file)
- Works with both .razor and .cshtml files
- Only processes strings with .cshtml or .razor extensions
### Technical Approach:
- Checks for string literals BEFORE calling C# Go-to-Definition
(prevents navigation to String class when file exists)
- Uses `IsRazorFilePath()` extension method for file type checking
(inside TryResolveFilePath)
- Uses pattern matching for tilde path detection
- Only modifies Workspaces and Remote layers (as per requirements)
### Test Status:
All 7 tests now pass (includes 4 new tests from main merge):
- ✅ StringLiteral_TildePath - Navigates to file using tilde path
- ✅ StringLiteral_RelativePath - Navigates to file using relative path
- ✅ StringLiteral_RazorComponent - Navigates to .razor component file
- ✅ StringLiteral_NotFileReference (4 variants) - Correctly ignores
non-file strings
Fixes #4325
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>Navigating to partial razor files by filename
string</issue_title>
> <issue_description>_This issue has been moved from [a ticket on
Developer
Community](https://developercommunity.visualstudio.com/t/Navigating-to-partial-razor-files-by-fil/1479773)._
>
> ---
> In our codebase we often break webpages down by adding components with
the following snippet:
>
> ```
> @Html.Partial("~/Views/xy.cshtml")
> ```
> It would be very useful if you could navigate to the specified .cshtml
file by putting the caret in the string, and pressing F12, as you often
do with other code sections.
>
> ---
> ### Original Comments
>
> #### Feedback Bot on 7/20/2021, 06:59 PM:
>
> Thank you for taking the time to provide your suggestion. We will do
some preliminary checks to make sure we can proceed further. We'll
provide an update once the issue has been triaged by the product team.
>
> #### Feedback Bot on 8/4/2021, 03:55 PM:
>
> <p>Your suggestion has been queued up for prioritization. Feature
suggestions are prioritized based on the value to our broader developer
community and the product roadmap. We may not be able to pursue this one
immediately, but we will continue to monitor it for community input</p>
> </issue_description>
>
> <agent_instructions>Don't make any changes to the compiler layer, or
to the Microsoft.AspNetCore.Razor.LanguageServer project. Changes should
be focused in services in either the Workspace or Remote projects. No
new services or endpoints will be needed. Comprehensive tests should go
in the existing CohostGoToDefinitionEndpointTest.cs
file.</agent_instructions>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> </comments>
>
</details>
Fixes #4325
<!-- START COPILOT CODING AGENT TIPS -->
---
✨ Let Copilot coding agent [set things up for
you](https://github.com/dotnet/razor/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.File tree
5 files changed
+236
-23
lines changed- .github
- src/Razor
- src
- Microsoft.CodeAnalysis.Razor.Workspaces/GoToDefinition
- Microsoft.CodeAnalysis.Remote.Razor/GoToDefinition
5 files changed
+236
-23
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
| |||
Lines changed: 92 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| 13 | + | |
| 14 | + | |
12 | 15 | | |
13 | 16 | | |
14 | 17 | | |
| |||
100 | 103 | | |
101 | 104 | | |
102 | 105 | | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
103 | 195 | | |
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
23 | 28 | | |
Lines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
78 | 93 | | |
79 | 94 | | |
80 | 95 | | |
| |||
0 commit comments