1 个不稳定版本
| 0.20.0 | 2024年4月24日 | 
|---|
#1318 in 异步
210KB
 3K  SLoC
tower-lsp
Language Server Protocol实现,基于Tower。
Tower是一个用于在Rust中实现异步服务的简单且可组合的框架。Tower的核心是Service特质,它为定义请求/响应客户端和服务器提供了必要的抽象。使用Service特质实现的协议示例包括用于HTTP的hyper和用于gRPC的tonic。
此库(tower-lsp)提供了一个简单的Language Server Protocol(LSP)实现,使编写自己的语言服务器变得容易。它由三部分组成
- LanguageServer特质,它定义了你的语言服务器的行为。
- 异步的LspService代理,它封装你的语言服务器实现并定义了协议的行为。
- 一个Server,它启动LspService并在stdio或TCP上处理请求和响应。
示例
use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer, LspService, Server};
#[derive(Debug)]
struct Backend {
    client: Client,
}
#[tower_lsp::async_trait]
impl LanguageServer for Backend {
    async fn initialize(&self, _: InitializeParams) -> Result<InitializeResult> {
        Ok(InitializeResult::default())
    }
    async fn initialized(&self, _: InitializedParams) {
        self.client
            .log_message(MessageType::INFO, "server initialized!")
            .await;
    }
    async fn shutdown(&self) -> Result<()> {
        Ok(())
    }
}
#[tokio::main]
async fn main() {
    let stdin = tokio::io::stdin();
    let stdout = tokio::io::stdout();
    let (service, socket) = LspService::new(|client| Backend { client });
    Server::new(stdin, stdout, socket).serve(service).await;
}
使用除了tokio之外的其他运行时
默认情况下,tower-lsp配置为与tokio一起使用。
要使用其他运行时与tower-lsp一起使用,需要禁用default-features并启用runtime-agnostic功能
[dependencies.tower-lsp]
version = "*"
default-features = false
features = ["runtime-agnostic"]
使用提议的功能
你可以通过启用proposed Cargo crate功能来使用LSP规范版本3.18中的提议功能。请注意,对于proposed功能没有semver保证,因此在proposed功能中,任何类型的版本之间可能会有破坏性更改。
生态系统
- tower-lsp-boilerplate - 有用的GitHub项目模板,使编写新的语言服务器变得容易。
许可证
tower-lsp是免费且开源的软件,根据你的选择,在MIT或Apache 2.0许可证的条款下分发。
除非您明确声明,否则您根据Apache-2.0许可证定义的,有意提交以包含在工作中的任何贡献,将双重许可,如上所述,没有任何附加条款或条件。
依赖项
~5–12MB
~166K SLoC