Skip to content

[WIP] Implement LSP textDocument/definition #18679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/copilot-setup-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ jobs:
- name: Restore the compiler service solution
env:
CI: false
run: dotnet build ./FSharp.Compiler.Service.sln --verbosity quiet
run: ./build.sh -c Release --verbosity quiet
- name: Restore the language server solution
env:
CI: false
run: dotnet build ./VSFSharpExtension.sln --verbosity quiet
run: dotnet build ./LSPSolutionSlim.sln -c Release --verbosity quiet
- name: Restore dotnet tools
env:
CI: false
Expand Down
117 changes: 117 additions & 0 deletions LSPSolutionSlim.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{827E0CD3-B72D-47B6-A68D-7590B98EB39B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Compiler", "Compiler", "{87D01E8D-2580-81A2-8FDB-46C6008C302D}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FSharp.Compiler.Service", "src\Compiler\FSharp.Compiler.Service.fsproj", "{876C64E7-1131-48AD-BD7B-CB3E17CB0E93}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FSharp.DependencyManager.Nuget", "src\FSharp.DependencyManager.Nuget\FSharp.DependencyManager.Nuget.fsproj", "{BD632E2B-6A64-458A-B236-37D5B35F3ACD}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FSharp.Core", "src\FSharp.Core\FSharp.Core.fsproj", "{350BB272-96EF-462C-BDF7-1E711FB1C684}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FSharp.VisualStudio.Extension", "src\FSharp.VisualStudio.Extension\FSharp.VisualStudio.Extension.csproj", "{F78BF321-03DE-41B0-BBDA-700A49DDD541}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FSharp.Compiler.LanguageServer", "src\FSharp.Compiler.LanguageServer\FSharp.Compiler.LanguageServer.fsproj", "{72C85CB8-674E-40B8-A972-1710E503DDCD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.CommonLanguageServerProtocol.Framework.Proxy", "src\Microsoft.CommonLanguageServerProtocol.Framework.Proxy\Microsoft.CommonLanguageServerProtocol.Framework.Proxy.csproj", "{71F0B4AC-786A-498E-88C7-2B922025F192}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{876C64E7-1131-48AD-BD7B-CB3E17CB0E93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{876C64E7-1131-48AD-BD7B-CB3E17CB0E93}.Debug|Any CPU.Build.0 = Debug|Any CPU
{876C64E7-1131-48AD-BD7B-CB3E17CB0E93}.Debug|x64.ActiveCfg = Debug|Any CPU
{876C64E7-1131-48AD-BD7B-CB3E17CB0E93}.Debug|x64.Build.0 = Debug|Any CPU
{876C64E7-1131-48AD-BD7B-CB3E17CB0E93}.Debug|x86.ActiveCfg = Debug|Any CPU
{876C64E7-1131-48AD-BD7B-CB3E17CB0E93}.Debug|x86.Build.0 = Debug|Any CPU
{876C64E7-1131-48AD-BD7B-CB3E17CB0E93}.Release|Any CPU.ActiveCfg = Release|Any CPU
{876C64E7-1131-48AD-BD7B-CB3E17CB0E93}.Release|Any CPU.Build.0 = Release|Any CPU
{876C64E7-1131-48AD-BD7B-CB3E17CB0E93}.Release|x64.ActiveCfg = Release|Any CPU
{876C64E7-1131-48AD-BD7B-CB3E17CB0E93}.Release|x64.Build.0 = Release|Any CPU
{876C64E7-1131-48AD-BD7B-CB3E17CB0E93}.Release|x86.ActiveCfg = Release|Any CPU
{876C64E7-1131-48AD-BD7B-CB3E17CB0E93}.Release|x86.Build.0 = Release|Any CPU
{BD632E2B-6A64-458A-B236-37D5B35F3ACD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BD632E2B-6A64-458A-B236-37D5B35F3ACD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD632E2B-6A64-458A-B236-37D5B35F3ACD}.Debug|x64.ActiveCfg = Debug|Any CPU
{BD632E2B-6A64-458A-B236-37D5B35F3ACD}.Debug|x64.Build.0 = Debug|Any CPU
{BD632E2B-6A64-458A-B236-37D5B35F3ACD}.Debug|x86.ActiveCfg = Debug|Any CPU
{BD632E2B-6A64-458A-B236-37D5B35F3ACD}.Debug|x86.Build.0 = Debug|Any CPU
{BD632E2B-6A64-458A-B236-37D5B35F3ACD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD632E2B-6A64-458A-B236-37D5B35F3ACD}.Release|Any CPU.Build.0 = Release|Any CPU
{BD632E2B-6A64-458A-B236-37D5B35F3ACD}.Release|x64.ActiveCfg = Release|Any CPU
{BD632E2B-6A64-458A-B236-37D5B35F3ACD}.Release|x64.Build.0 = Release|Any CPU
{BD632E2B-6A64-458A-B236-37D5B35F3ACD}.Release|x86.ActiveCfg = Release|Any CPU
{BD632E2B-6A64-458A-B236-37D5B35F3ACD}.Release|x86.Build.0 = Release|Any CPU
{350BB272-96EF-462C-BDF7-1E711FB1C684}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{350BB272-96EF-462C-BDF7-1E711FB1C684}.Debug|Any CPU.Build.0 = Debug|Any CPU
{350BB272-96EF-462C-BDF7-1E711FB1C684}.Debug|x64.ActiveCfg = Debug|Any CPU
{350BB272-96EF-462C-BDF7-1E711FB1C684}.Debug|x64.Build.0 = Debug|Any CPU
{350BB272-96EF-462C-BDF7-1E711FB1C684}.Debug|x86.ActiveCfg = Debug|Any CPU
{350BB272-96EF-462C-BDF7-1E711FB1C684}.Debug|x86.Build.0 = Debug|Any CPU
{350BB272-96EF-462C-BDF7-1E711FB1C684}.Release|Any CPU.ActiveCfg = Release|Any CPU
{350BB272-96EF-462C-BDF7-1E711FB1C684}.Release|Any CPU.Build.0 = Release|Any CPU
{350BB272-96EF-462C-BDF7-1E711FB1C684}.Release|x64.ActiveCfg = Release|Any CPU
{350BB272-96EF-462C-BDF7-1E711FB1C684}.Release|x64.Build.0 = Release|Any CPU
{350BB272-96EF-462C-BDF7-1E711FB1C684}.Release|x86.ActiveCfg = Release|Any CPU
{350BB272-96EF-462C-BDF7-1E711FB1C684}.Release|x86.Build.0 = Release|Any CPU
{F78BF321-03DE-41B0-BBDA-700A49DDD541}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F78BF321-03DE-41B0-BBDA-700A49DDD541}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F78BF321-03DE-41B0-BBDA-700A49DDD541}.Debug|x64.ActiveCfg = Debug|Any CPU
{F78BF321-03DE-41B0-BBDA-700A49DDD541}.Debug|x64.Build.0 = Debug|Any CPU
{F78BF321-03DE-41B0-BBDA-700A49DDD541}.Debug|x86.ActiveCfg = Debug|Any CPU
{F78BF321-03DE-41B0-BBDA-700A49DDD541}.Debug|x86.Build.0 = Debug|Any CPU
{F78BF321-03DE-41B0-BBDA-700A49DDD541}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F78BF321-03DE-41B0-BBDA-700A49DDD541}.Release|Any CPU.Build.0 = Release|Any CPU
{F78BF321-03DE-41B0-BBDA-700A49DDD541}.Release|x64.ActiveCfg = Release|Any CPU
{F78BF321-03DE-41B0-BBDA-700A49DDD541}.Release|x64.Build.0 = Release|Any CPU
{F78BF321-03DE-41B0-BBDA-700A49DDD541}.Release|x86.ActiveCfg = Release|Any CPU
{F78BF321-03DE-41B0-BBDA-700A49DDD541}.Release|x86.Build.0 = Release|Any CPU
{72C85CB8-674E-40B8-A972-1710E503DDCD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{72C85CB8-674E-40B8-A972-1710E503DDCD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{72C85CB8-674E-40B8-A972-1710E503DDCD}.Debug|x64.ActiveCfg = Debug|Any CPU
{72C85CB8-674E-40B8-A972-1710E503DDCD}.Debug|x64.Build.0 = Debug|Any CPU
{72C85CB8-674E-40B8-A972-1710E503DDCD}.Debug|x86.ActiveCfg = Debug|Any CPU
{72C85CB8-674E-40B8-A972-1710E503DDCD}.Debug|x86.Build.0 = Debug|Any CPU
{72C85CB8-674E-40B8-A972-1710E503DDCD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{72C85CB8-674E-40B8-A972-1710E503DDCD}.Release|Any CPU.Build.0 = Release|Any CPU
{72C85CB8-674E-40B8-A972-1710E503DDCD}.Release|x64.ActiveCfg = Release|Any CPU
{72C85CB8-674E-40B8-A972-1710E503DDCD}.Release|x64.Build.0 = Release|Any CPU
{72C85CB8-674E-40B8-A972-1710E503DDCD}.Release|x86.ActiveCfg = Release|Any CPU
{72C85CB8-674E-40B8-A972-1710E503DDCD}.Release|x86.Build.0 = Release|Any CPU
{71F0B4AC-786A-498E-88C7-2B922025F192}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{71F0B4AC-786A-498E-88C7-2B922025F192}.Debug|Any CPU.Build.0 = Debug|Any CPU
{71F0B4AC-786A-498E-88C7-2B922025F192}.Debug|x64.ActiveCfg = Debug|Any CPU
{71F0B4AC-786A-498E-88C7-2B922025F192}.Debug|x64.Build.0 = Debug|Any CPU
{71F0B4AC-786A-498E-88C7-2B922025F192}.Debug|x86.ActiveCfg = Debug|Any CPU
{71F0B4AC-786A-498E-88C7-2B922025F192}.Debug|x86.Build.0 = Debug|Any CPU
{71F0B4AC-786A-498E-88C7-2B922025F192}.Release|Any CPU.ActiveCfg = Release|Any CPU
{71F0B4AC-786A-498E-88C7-2B922025F192}.Release|Any CPU.Build.0 = Release|Any CPU
{71F0B4AC-786A-498E-88C7-2B922025F192}.Release|x64.ActiveCfg = Release|Any CPU
{71F0B4AC-786A-498E-88C7-2B922025F192}.Release|x64.Build.0 = Release|Any CPU
{71F0B4AC-786A-498E-88C7-2B922025F192}.Release|x86.ActiveCfg = Release|Any CPU
{71F0B4AC-786A-498E-88C7-2B922025F192}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{87D01E8D-2580-81A2-8FDB-46C6008C302D} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{876C64E7-1131-48AD-BD7B-CB3E17CB0E93} = {87D01E8D-2580-81A2-8FDB-46C6008C302D}
{BD632E2B-6A64-458A-B236-37D5B35F3ACD} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{350BB272-96EF-462C-BDF7-1E711FB1C684} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{F78BF321-03DE-41B0-BBDA-700A49DDD541} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{72C85CB8-674E-40B8-A972-1710E503DDCD} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{71F0B4AC-786A-498E-88C7-2B922025F192} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type CapabilitiesManager(config: FSharpLanguageServerConfig, scOverrides: IServe
)),
//CompletionProvider = CompletionOptions(TriggerCharacters = [| "."; " " |], ResolveProvider = true, WorkDoneProgress = true),
//HoverProvider = SumType<bool, HoverOptions>(HoverOptions(WorkDoneProgress = true))
DefinitionProvider = SumType<bool, DefinitionOptions>(true),
SemanticTokensOptions =
addIf
config.EnabledFeatures.SemanticHighlighting
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace FSharp.Compiler.LanguageServer.Handlers

open System
open Microsoft.CommonLanguageServerProtocol.Framework
open Microsoft.VisualStudio.LanguageServer.Protocol
open Microsoft.VisualStudio.FSharp.Editor.CancellableTasks
Expand All @@ -9,6 +10,7 @@ open System.Threading.Tasks
open System.Threading
open System.Collections.Generic
open Microsoft.VisualStudio.FSharp.Editor
open FSharp.Compiler.EditorServices

#nowarn "57"

Expand Down Expand Up @@ -57,3 +59,66 @@ type LanguageFeaturesHandler() =
return SemanticTokens(Data = tokens)
}
|> CancellableTask.start cancellationToken

interface IRequestHandler<TextDocumentPositionParams, SumType<Location[], Location>, FSharpRequestContext> with
[<LanguageServerEndpoint(Methods.TextDocumentDefinitionName, LanguageServerConstants.DefaultLanguageName)>]
member _.HandleRequestAsync
(request: TextDocumentPositionParams, context: FSharpRequestContext, cancellationToken: CancellationToken)
=
cancellableTask {
let file = request.TextDocument.Uri
let line = int request.Position.Line
let col = int request.Position.Character

let! _, checkResults = context.Workspace.Query.GetParseAndCheckResultsForFile file

match checkResults with
| Some checkResults ->
let! sourceText = context.Workspace.Query.GetSource file

match sourceText with
| Some sourceText ->
try
// Convert LSP position to F# compiler position
let lineText = sourceText.GetLineString(line)
let fcsLine = FSharp.Compiler.Text.Line.fromZ line

// Try to find the identifier at the position using QuickParse
let qualifyingIdents, partialIdent = QuickParse.GetPartialLongName(lineText, col)
let fullIdent = qualifyingIdents @ [ partialIdent ]

if not (List.isEmpty fullIdent) && not (String.IsNullOrEmpty partialIdent) then
// Get declaration location using F# Compiler Service
let declarationResult =
checkResults.GetDeclarationLocation(fcsLine, col, lineText, fullIdent, false)

match declarationResult with
| FindDeclResult.DeclFound targetRange ->
let location =
Microsoft.VisualStudio.LanguageServer.Protocol.Location(
Uri = System.Uri(targetRange.FileName),
Range =
Microsoft.VisualStudio.LanguageServer.Protocol.Range(
Start =
Microsoft.VisualStudio.LanguageServer.Protocol.Position(
Line = targetRange.StartLine - 1,
Character = targetRange.StartColumn
),
End =
Microsoft.VisualStudio.LanguageServer.Protocol.Position(
Line = targetRange.EndLine - 1,
Character = targetRange.EndColumn
)
)
)

return SumType<Location[], Location>([| location |])
| _ -> return SumType<Location[], Location>([||])
else
return SumType<Location[], Location>([||])
with _ ->
return SumType<Location[], Location>([||])
| None -> return SumType<Location[], Location>([||])
| _ -> return SumType<Location[], Location>([||])
}
|> CancellableTask.start cancellationToken
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>12</LangVersion>
<NeutralLanguage>en-US</NeutralLanguage>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
```mermaid

graph LR

1[ProjectWithoutFiles /test1.fsproj 🡒 /test1.dll] --> 0[ProjectConfig /test1.fsproj 🡒 /test1.dll]
3[ProjectSnapshot /test1.fsproj 🡒 /test1.dll] --> 1[ProjectWithoutFiles /test1.fsproj 🡒 /test1.dll]
3[ProjectSnapshot /test1.fsproj 🡒 /test1.dll] --> 2[File /test1.fs]
5[ProjectConfig /test2.fsproj 🡒 /test2.dll] --> 4[Reference on disk /...]
6[ProjectWithoutFiles /test2.fsproj 🡒 /test2.dll] --> 5[ProjectConfig /test2.fsproj 🡒 /test2.dll]
6[ProjectWithoutFiles /test2.fsproj 🡒 /test2.dll] --> 3[ProjectSnapshot /test1.fsproj 🡒 /test1.dll]
8[ProjectSnapshot /test2.fsproj 🡒 /test2.dll] --> 6[ProjectWithoutFiles /test2.fsproj 🡒 /test2.dll]
8[ProjectSnapshot /test2.fsproj 🡒 /test2.dll] --> 7[File /test2.fs]

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```mermaid

graph LR

1[ProjectWithoutFiles /test.fsproj 🡒 /test.dll] --> 0[ProjectConfig /test.fsproj 🡒 /test.dll]
3[ProjectSnapshot /test.fsproj 🡒 /test.dll] --> 1[ProjectWithoutFiles /test.fsproj 🡒 /test.dll]
3[ProjectSnapshot /test.fsproj 🡒 /test.dll] --> 2[File /test.fs]

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```mermaid

graph LR

1[ProjectWithoutFiles FSharp-Test-Project-b5a7d97b-/test.fsproj 🡒 FSharp-Test-Project-b5a7d97b-/test.dll] --> 0[ProjectConfig FSharp-Test-Project-b5a7d97b-/test.fsproj 🡒 FSharp-Test-Project-b5a7d97b-/test.dll]
3[ProjectSnapshot FSharp-Test-Project-b5a7d97b-/test.fsproj 🡒 FSharp-Test-Project-b5a7d97b-/test.dll] --> 1[ProjectWithoutFiles FSharp-Test-Project-b5a7d97b-/test.fsproj 🡒 FSharp-Test-Project-b5a7d97b-/test.dll]
3[ProjectSnapshot FSharp-Test-Project-b5a7d97b-/test.fsproj 🡒 FSharp-Test-Project-b5a7d97b-/test.dll] --> 2[File /test.fs]

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```mermaid

graph LR

1[ProjectWithoutFiles FSharp-Test-Project-6bab5035-/test.fsproj 🡒 FSharp-Test-Project-6bab5035-/test.dll] --> 0[ProjectConfig FSharp-Test-Project-6bab5035-/test.fsproj 🡒 FSharp-Test-Project-6bab5035-/test.dll]
3[ProjectSnapshot FSharp-Test-Project-6bab5035-/test.fsproj 🡒 FSharp-Test-Project-6bab5035-/test.dll] --> 1[ProjectWithoutFiles FSharp-Test-Project-6bab5035-/test.fsproj 🡒 FSharp-Test-Project-6bab5035-/test.dll]
3[ProjectSnapshot FSharp-Test-Project-6bab5035-/test.fsproj 🡒 FSharp-Test-Project-6bab5035-/test.dll] --> 2[File c16d9b38/9f9bc6ed.fs]

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
```mermaid

graph LR

1[ProjectWithoutFiles /test1.fsproj 🡒 /test1.dll] --> 0[ProjectConfig /test1.fsproj 🡒 /test1.dll]
3[ProjectSnapshot /test1.fsproj 🡒 /test1.dll] --> 1[ProjectWithoutFiles /test1.fsproj 🡒 /test1.dll]
3[ProjectSnapshot /test1.fsproj 🡒 /test1.dll] --> 2[File /test1.fs]
4[ProjectConfig /test2.fsproj 🡒 /test2.dll] --> 8[Reference on disk /...]
5[ProjectWithoutFiles /test2.fsproj 🡒 /test2.dll] --> 4[ProjectConfig /test2.fsproj 🡒 /test2.dll]
5[ProjectWithoutFiles /test2.fsproj 🡒 /test2.dll] --> 3[ProjectSnapshot /test1.fsproj 🡒 /test1.dll]
7[ProjectSnapshot /test2.fsproj 🡒 /test2.dll] --> 5[ProjectWithoutFiles /test2.fsproj 🡒 /test2.dll]
7[ProjectSnapshot /test2.fsproj 🡒 /test2.dll] --> 6[File /test2.fs]

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```mermaid

graph LR

1[ProjectWithoutFiles /test.fsproj 🡒 /test.dll] --> 0[ProjectConfig /test.fsproj 🡒 /test.dll]
3[ProjectSnapshot /test.fsproj 🡒 /test.dll] --> 1[ProjectWithoutFiles /test.fsproj 🡒 /test.dll]
3[ProjectSnapshot /test.fsproj 🡒 /test.dll] --> 2[File /test.fs]

```
Loading
Loading