Skip to content

Implement textDocument/completion for F# LSP server #18696

@abonie

Description

@abonie

Description

We need to implement the textDocument/completion endpoint as defined by the Language Server Protocol specification. This endpoint is responsible for providing intelligent code completions at a given cursor position in a document.

Goals

  • Support basic identifier and keyword completions.
  • Integrate with the F# compiler service to provide context-aware suggestions.
  • Return CompletionItem[] or CompletionList as appropriate.

Acceptance criteria

  • Each CompletionItem includes:
    label: the name of the symbol or keyword.
    kind: appropriate LSP CompletionItemKind (e.g., Function, Variable, Keyword).
    detail: short type signature or description.
    documentation (if available): optional markdown-formatted docstring or summary.
  • Completion respects the current scope and context (e.g., local variables, open modules, type members).
  • The server returns a CompletionList with isIncomplete = true when further typing may refine the results.
  • Unit tests added where relevant.
  • Test suite passes (copilot remember to run dotnet test with --no-restore)
  • Code is formatted with the fantomas tool

Additional info

To implement the logic for your new LSP endpoint. This involves creating a new type that implements an appropriate interface like IRequestHandler<TRequest, TResponse, FSharpRequestContext> and decorating it with the LanguageServerEndpoint attribute specifying the LSP method name. See src/FSharp.Compiler.LanguageServer/Handlers/LanguageFeaturesHandler.fs for reference.

The new handler should be registered with dependency injection container at src/FSharp.Compiler.LanguageServer/FSharpLanguageServer.fs

The capability needs to be advertised to the LSP client, which requires modifying src/FSharp.VisualStudio.Extension/FSharpLanguageServerProvider.cs

The new feature needs to be configurable (enabled or disabled by a setting). For this you'll need to change src/FSharp.Compiler.LanguageServer/FSharpLanguageServerConfig.fs and src/FSharp.VisualStudio.Extension/FSharpExtensionSettings.cs

Reference implementation

Currently F# provides completions in Visual Studio through a comprehensive completion system built on top of the F# compiler services. The system consists of several key components working together to deliver IntelliSense functionality. Similar approach should be adapted for the LSP implementation.

Core Completion Architecture

The completion system is primarily implemented through two main classes:

FSharpCompletionProvider serves as the main completion provider that integrates with Visual Studio's Roslyn-based editor infrastructure. It inherits from FSharpCompletionProviderBase and handles the core logic for determining when to trigger completions and what completions to provide.

FSharpCompletionService acts as the service factory that creates and manages completion providers, integrating them into Visual Studio's completion system.

Completion Generation Process

When completions are requested, the system follows this process in ProvideCompletionsAsyncAux:

  1. Parse and Type Check: Gets F# parse and check results for the current document
  2. Context Analysis: Determines the completion context using ParsedInput.TryGetCompletionContext
  3. Symbol Resolution: Calls GetDeclarationListInfo on the F# compiler's check results to get available symbols
  4. Sorting and Filtering: Sorts completion items by priority, resolution status, kind, and ownership

Integration with F# Compiler Services

The completion system leverages the F# compiler's FSharpChecker through the language service architecture. The FSharpLanguageService is registered with Visual Studio and provides various language features including completion.

Metadata

Metadata

Assignees

Projects

Status

New

Relationships

None yet

Development

No branches or pull requests

Issue actions