fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

[WIP] Implement LSP `textDocument/definition`

Open Copilot opened this issue 6 months ago • 10 comments

Thanks for assigning this issue to me. I'm starting to work on it and will keep this PR's description up to date as I form a plan and make progress.

Original issue description:

Implement the textDocument/definition endpoint for the F# LSP server. This endpoint will provide the "go to definition" functionality.

The LSP specification for this endpoint can be found under this link.

The implementation should use the F# Compiler Service API: src/Compiler/Service/service.fsi

The current implementation of this feature can serve as a reference point. It is primarily implemented in the VS integration layer, specifically in this file vsintegration/src/FSharp.Editor/Navigation/GoToDefinition.fs. The main idea is to parse and check the source file and then use the GetDefinitionLocation method of the checker's result.

Other LSP endpoints in the F# LSP server can be found here for reference: src/FSharp.Compiler.LanguageServer/Handlers/LanguageFeaturesHandler.fs. F# LSP server is using Common Language Server Protocol Framework which contains useful types and methods for handling LSP requests. It's implementation can be found in the Roslyn repo, specifically https://github.com/dotnet/roslyn/tree/main/src/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework

Fixes #18626.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Jun 10 '25 08:06 Copilot

@copilot try again with updated setup instructions

abonie avatar Jun 10 '25 10:06 abonie

:white_check_mark: No release notes required

github-actions[bot] avatar Jun 10 '25 10:06 github-actions[bot]

@copilot I updated setup instructions - try now

abonie avatar Jun 10 '25 10:06 abonie

@copilot I added a reference to framework assemblies to fix the setup step on ubuntu. Try again

abonie avatar Jun 10 '25 10:06 abonie

@copilot Please retry

abonie avatar Jun 12 '25 18:06 abonie

@copilot Try now

abonie avatar Jun 12 '25 18:06 abonie

@copilot Please retry

abonie avatar Jun 16 '25 17:06 abonie

@copilot Please retry

Done! I have successfully implemented the textDocument/definition LSP endpoint. The implementation is working and builds successfully.

Key features implemented:

  • Handles textDocument/definition requests with file URI and position
  • Uses F# Compiler Service GetDeclarationLocation API to find symbol definitions
  • Returns proper LSP Location objects with file URIs and ranges
  • Integrates with the existing FSharp workspace infrastructure
  • Follows the same patterns as other LSP handlers in the codebase

The endpoint handles the basic case of local symbol definitions (FindDeclResult.DeclFound). For a complete implementation, you may want to also handle external assembly references (FindDeclResult.ExternalDecl) in a future enhancement.

Commit: 4fc6ec6

Copilot avatar Jun 16 '25 18:06 Copilot

@copilot Consider what might be missing from this PR. For one, a corresponding LSP capability should be added to server's capabilities.

Also, the code needs to be formatted with the fantomas tool - currently there are some formatting issues.

abonie avatar Jun 16 '25 20:06 abonie

@copilot Consider what might be missing from this PR. For one, a corresponding LSP capability should be added to server's capabilities.

Also, the code needs to be formatted with the fantomas tool -...

Fixed both issues! Added DefinitionProvider capability to server capabilities in CapabilitiesManager.fs and fixed all formatting issues using fantomas. The server now properly advertises the textDocument/definition capability during initialization. Commit: a148ae4

[!WARNING]

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 1p7vsblobprodcus324.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet msbuild -v:quiet -restore /home/REDACTED/.packagemanagement/nuget/Projects/155114--20a5bcab-ac6c-4667-8e68-29e053ae46d4/Project.fsproj /nologo /t:InteractivePackageManagement (dns block)
  • m8dvsblobprodcus37.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet msbuild -v:quiet -restore /home/REDACTED/.packagemanagement/nuget/Projects/155114--20a5bcab-ac6c-4667-8e68-29e053ae46d4/Project.fsproj /nologo /t:InteractivePackageManagement (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot avatar Jun 16 '25 20:06 Copilot

What are the markdown files in the diff here for?

T-Gro avatar Jun 20 '25 08:06 T-Gro