dotnet / dotnet/fsharp

Implement `textDocument/completion` for F# LSP server

Open
#18,696 0 comments 1 reaction 1 assignee Assigned to @Copilot View on GitHub
Area-LSP Feature Request
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

## Description

We need to implement the `textDocument/completion` endpoint as defined by the [Language Server Protocol specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/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` 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.