Implement Language Server Protocol (LSP) for IDE Integration
Labels: arch-review, ide-support, lsp, critical
Priority: P0
Severity: CRITICAL
Epic: Architectural Improvements Q2 2026
Problem Summary
The compiler has no Language Server Protocol implementation, preventing integration with modern editors (VS Code, Neovim, Emacs, etc.). This severely limits the language's adoption potential and developer experience.
Current State
- No LSP-related code in codebase
- No language server executable
- Only basic VS Code configuration
- No incremental compilation (required for LSP)
- No real-time diagnostics
- No IDE features (autocomplete, go-to-definition, etc.)
Impact
Adoption Barrier
- Developers expect IDE features from modern languages
- Competing languages (Rust, TypeScript, Swift) all have excellent LSP support
- No Fifth language support for popular editors
- Makes language feel "unfinished" or "hobby project"
Development Velocity
- Contributors cannot efficiently work on Fifth code
- No tooling to support language feature development
- Testing requires full compilation cycles
- Debugging is manual and time-consuming
Feature Gap
Cannot implement standard IDE features: - Hover information (type info, documentation) - Signature help (function parameter hints) - Code completion (context-aware suggestions) - Go to definition/implementation - Find all references - Rename symbol - Code actions/quick fixes - Semantic tokens (syntax highlighting) - Document/workspace symbols - Document formatting
Requirements
Core LSP Server
- Implement LSP server as separate executable
- Support stdio communication protocol
- Handle standard LSP lifecycle (initialize, initialized, shutdown)
- Implement core capabilities negotiation
Document Management
- Track open documents in workspace
- Synchronize document changes (didOpen, didChange, didClose)
- Parse documents incrementally
- Maintain AST cache per document
Essential Features (MVP)
- Diagnostics (textDocument/publishDiagnostics)
- Real-time syntax errors
- Real-time semantic errors
-
Error recovery support
-
Hover (textDocument/hover)
- Type information
- Function signatures
-
Symbol documentation
-
Completion (textDocument/completion)
- Context-aware suggestions
- Keyword completion
- Symbol completion
-
Function completion with signatures
-
Go to Definition (textDocument/definition)
- Navigate to symbol definition
- Support cross-file navigation
Advanced Features (Post-MVP)
- Find References (textDocument/references)
- Rename Symbol (textDocument/rename)
- Document Symbols (textDocument/documentSymbol)
- Code Actions (textDocument/codeAction)
- Semantic Tokens (textDocument/semanticTokens)
- Signature Help (textDocument/signatureHelp)
Architecture
Project Structure
src/language-server/
├── FifthLanguageServer.csproj
├── Program.cs # Entry point
├── LanguageServer.cs # Main server class
├── Handlers/
│ ├── TextDocumentHandler.cs # Document sync
│ ├── DiagnosticHandler.cs # Error checking
│ ├── CompletionHandler.cs # Code completion
│ ├── HoverHandler.cs # Hover info
│ └── DefinitionHandler.cs # Go to definition
├── Services/
│ ├── WorkspaceService.cs # Workspace management
│ ├── DocumentService.cs # Document tracking
│ ├── ParsingService.cs # Incremental parsing
│ ├── DiagnosticService.cs # Error collection
│ ├── CompletionService.cs # Completion logic
│ └── SymbolService.cs # Symbol queries
└── Protocol/
└── LSPTypes.cs # LSP protocol types
Key Components
WorkspaceService: - Manages open documents - Tracks project structure - Handles workspace-wide operations
DocumentService: - Synchronizes document state - Manages document versions - Caches parsed ASTs
ParsingService: - Incremental parsing with error recovery - AST caching and invalidation - Background parsing for diagnostics
SymbolService: - Symbol resolution using enhanced symbol table - Cross-file symbol queries - Supports "find references" and "go to definition"
Implementation Plan
Phase 1: Infrastructure (Weeks 1-4)
- Create language-server project
- Add OmniSharp LSP library dependency
- Implement basic server lifecycle
- Set up stdio communication
- Add VS Code extension configuration
Phase 2: Document Synchronization (Weeks 5-8)
- Implement document tracking
- Handle didOpen/didChange/didClose
- Set up incremental parsing
- Add document AST caching
Phase 3: Core Features (Weeks 9-12)
- Diagnostics:
- Real-time syntax error reporting
- Semantic error reporting
-
Error recovery integration
-
Hover:
- Type information display
- Function signature display
-
Symbol documentation
-
Completion:
- Keyword completion
- Symbol completion
- Context-aware filtering
Phase 4: Navigation Features (Weeks 13-16)
- Go to Definition:
- Symbol resolution
- Cross-file navigation
-
Handle multiple definitions
-
Find References:
- Build reference index
- Query all usages
- Workspace-wide search
Phase 5: Polish & Testing (Weeks 17-20)
- Performance optimization
- Integration testing
- VS Code extension
- Documentation and examples
Acceptance Criteria
- [ ] Language server starts and responds to LSP messages
- [ ] Real-time diagnostics work in VS Code
- [ ] Hover shows type information
- [ ] Code completion provides context-aware suggestions
- [ ] Go to definition navigates to symbol
- [ ] Works with multiple open files
- [ ] Performance: <100ms response time for most operations
- [ ] VS Code extension published (optional)
- [ ] Documentation for setup and usage
Technical Requirements
Dependencies
<ItemGroup>
<PackageReference Include="OmniSharp.Extensions.LanguageServer" Version="0.19.x" />
<ProjectReference Include="..\compiler\compiler.csproj" />
<ProjectReference Include="..\parser\parser.csproj" />
</ItemGroup>
VS Code Extension
{
"name": "fifth-language",
"displayName": "Fifth Language Support",
"description": "Language support for Fifth",
"version": "0.1.0",
"engines": { "vscode": "^1.75.0" },
"activationEvents": ["onLanguage:fifth"],
"contributes": {
"languages": [{
"id": "fifth",
"extensions": [".5th"],
"configuration": "./language-configuration.json"
}],
"grammars": [{
"language": "fifth",
"scopeName": "source.fifth",
"path": "./syntaxes/fifth.tmLanguage.json"
}]
}
}
Example Implementation
Hover Handler:
public class HoverHandler : IRequestHandler<HoverParams, Hover>
{
private readonly DocumentService _documentService;
private readonly SymbolService _symbolService;
public async Task<Hover> Handle(HoverParams request, CancellationToken token)
{
var document = _documentService.GetDocument(request.TextDocument.Uri);
var position = request.Position;
// Get AST for document (from cache or parse)
var (ast, _) = await document.GetASTAsync(resilient: true);
// Find symbol at position
var symbol = _symbolService.GetSymbolAt(ast, position);
if (symbol == null)
return null;
// Build hover content
var content = new MarkedStringsOrMarkupContent(
new MarkupContent
{
Kind = MarkupKind.Markdown,
Value = FormatSymbolInfo(symbol)
});
return new Hover
{
Contents = content,
Range = symbol.Range
};
}
}
References
- Architectural Review:
docs/architectural-review-2025.md- Finding #2 - LSP Specification: https://microsoft.github.io/language-server-protocol/
- OmniSharp LSP library: https://github.com/OmniSharp/csharp-language-server-protocol
- Rust-analyzer (reference impl): https://github.com/rust-lang/rust-analyzer
- Related Issues: Requires #ISSUE-001 (Error Recovery), #ISSUE-003 (Incremental Compilation), #ISSUE-006 (Symbol Table)
Estimated Effort
20 weeks (5 months) - Weeks 1-4: Infrastructure and setup - Weeks 5-8: Document synchronization - Weeks 9-12: Core features (diagnostics, hover, completion) - Weeks 13-16: Navigation features - Weeks 17-20: Polish, testing, and documentation
Dependencies
- Issue #001: Error Recovery (CRITICAL - must complete first)
- Issue #003: Incremental Compilation (for performance)
- Issue #006: Enhanced Symbol Table (for navigation features)
Success Metrics
- Language server handles 1000+ document operations without restart
- Response time <100ms for 90% of operations
- Diagnostics appear within 500ms of typing
- Zero crashes in normal usage
- Positive user feedback on IDE experience