Skip to content

Supports the latest Language Server Protocol and AOT (Ahead-of-Time) compilation.

License

CppCXY/LanguageServer.Framework

Repository files navigation

Language Server Framework

License: MIT NuGet CI Code Quality

A modern, high-performance .NET framework for building Language Server Protocol (LSP) servers with full LSP 3.18 specification support.

✨ Features

  • 🎯 Full LSP 3.18 Support - Complete implementation of the latest LSP specification
  • ⚑ High Performance - Built with System.Text.Json source generation for optimal performance
  • πŸš€ AOT Ready - Full support for Ahead-of-Time (AOT) compilation
  • πŸ“¦ Zero Dependencies - No dependency on Newtonsoft.Json or other third-party libraries
  • πŸͺŸ Window & Progress - Complete support for window messages, logging, and progress reporting
  • πŸ”§ Easy to Use - Simple, intuitive API for building language servers
  • πŸ“Š Performance Metrics - Built-in performance monitoring and telemetry support
  • 🎨 Type Safe - Strongly typed protocol messages with full IntelliSense support

πŸ“¦ Installation

Install via NuGet Package Manager:

dotnet add package EmmyLua.LanguageServer.Framework

Or via Package Manager Console:

Install-Package EmmyLua.LanguageServer.Framework

πŸš€ Quick Start

Basic Language Server

using EmmyLua.LanguageServer.Framework.Server;
using EmmyLua.LanguageServer.Framework.Protocol.Message.Initialize;

// Create a language server
var server = LanguageServer.From(Console.OpenStandardInput(), Console.OpenStandardOutput());

// Handle initialization
server.OnInitialize((request, serverInfo) =>
{
    serverInfo.Name = "My Language Server";
    serverInfo.Version = "1.0.0";
    return Task.CompletedTask;
});

// Handle initialized notification
server.OnInitialized(async (request) =>
{
    await server.Client.LogInfo("Server initialized!");
});

// Run the server
await server.Run();

Adding Handlers

using EmmyLua.LanguageServer.Framework.Server.Handler;

// Create a hover handler
public class MyHoverHandler : HoverHandlerBase
{
    protected override Task<Hover?> Handle(HoverParams request, CancellationToken token)
    {
        var hover = new Hover
        {
            Contents = new MarkupContent
            {
                Kind = MarkupKind.Markdown,
                Value = "**Hello from LSP!**"
            }
        };
        return Task.FromResult<Hover?>(hover);
    }

    public override void RegisterCapability(ServerCapabilities serverCapabilities,
        ClientCapabilities clientCapabilities)
    {
        serverCapabilities.HoverProvider = true;
    }
}

// Add handler to server
server.AddHandler(new MyHoverHandler());

πŸ“š Documentation

Window Features

Display messages and collect user input:

// Show a message
await server.Client.ShowMessage(new ShowMessageParams
{
    Type = MessageType.Info,
    Message = "Operation completed successfully!"
});

// Request user action
var result = await server.Client.ShowMessageRequest(new ShowMessageRequestParams
{
    Type = MessageType.Warning,
    Message = "Do you want to continue?",
    Actions = new List<MessageActionItem>
    {
        new() { Title = "Yes" },
        new() { Title = "No" }
    }
}, cancellationToken);

// Log messages
await server.Client.LogError("An error occurred");
await server.Client.LogInfo("Processing file...");

Progress Reporting

Report progress for long-running operations:

// Using the helper class (recommended)
using var progress = await WorkDoneProgressReporter.Create(
    server.Client,
    title: "Analyzing workspace",
    cancellable: true
);

for (int i = 0; i < 100; i++)
{
    await progress.Report($"Processing file {i + 1}/100", (uint)i);
}

await progress.End("Analysis complete!");

For detailed examples, see LSP_WINDOW_PROGRESS_USAGE.md

Performance Monitoring

Enable performance metrics to monitor your server:

var options = new LanguageServerOptions
{
    EnablePerformanceTracing = true,
    PerformanceMetricsPrintInterval = TimeSpan.FromMinutes(5)
};

var server = LanguageServer.From(input, output, options);

// Get metrics at any time
var metrics = server.GetMetrics();
Console.WriteLine($"Total requests: {metrics.TotalRequestsHandled}");
Console.WriteLine($"Average duration: {metrics.AverageRequestDurationMs}ms");

🎯 Supported LSP Features

Text Document Synchronization

  • βœ…DidOpen / DidChange / DidClose / DidSave
  • βœ… WillSave / WillSaveWaitUntil

Language Features

  • βœ… Completion (with resolve support)
  • βœ… Hover
  • βœ… Signature Help
  • βœ… Definition / Declaration / Type Definition / Implementation
  • βœ… References
  • βœ… Document Highlight
  • βœ… Document Symbol
  • βœ… Code Action
  • βœ… Code Lens
  • βœ… Document Link
  • βœ… Document Color / Color Presentation
  • βœ… Document Formatting / Range Formatting / On Type Formatting
  • βœ… Rename / Prepare Rename
  • βœ… Folding Range
  • βœ… Selection Range
  • βœ… Call Hierarchy
  • βœ… Semantic Tokens
  • βœ… Inlay Hint
  • βœ… Inline Value
  • βœ… Type Hierarchy
  • βœ… Inline Completion
  • βœ… Linked Editing Range

Workspace Features

  • βœ… Workspace Symbols
  • βœ… Configuration
  • βœ… Workspace Folders
  • βœ… File Operations (Create/Rename/Delete)
  • βœ… File Watching
  • βœ… Diagnostics
  • βœ… Execute Command
  • βœ… Apply Edit

Window Features

  • βœ… ShowMessage / ShowMessageRequest
  • βœ… LogMessage
  • βœ… Work Done Progress
  • βœ… Telemetry

πŸ—οΈ Architecture

The framework is designed with extensibility and performance in mind:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     Language Server Application         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚           Handler Layer                 β”‚
β”‚  (HoverHandler, CompletionHandler...)   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚         LanguageServer Core             β”‚
β”‚   (Protocol, Routing, Lifecycle)        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚      JSON-RPC Protocol Layer            β”‚
β”‚  (JsonProtocolReader/Writer)            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚         Transport Layer (stdio)         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“– Examples

Check out the LanguageServer.Test project for a complete working example that demonstrates:

  • Server initialization
  • Multiple handlers implementation
  • Configuration management
  • Progress reporting
  • Error handling

🌟 Projects Using This Framework

🀝 Contributing

Contributions are welcome! Please read our Contributing Guide for details on how to submit pull requests, report issues, and contribute to the project.

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ”— Resources

πŸ’¬ Support


Made with ❀️ by the EmmyLua community

About

Supports the latest Language Server Protocol and AOT (Ahead-of-Time) compilation.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 5

Languages