mirror of
https://github.com/helix-editor/helix.git
synced 2024-11-24 18:36:18 +04:00
wip: Basic LSP lifecycle requests/notifications.
This commit is contained in:
parent
8adcf5083f
commit
f03830b047
106
Cargo.lock
generated
106
Cargo.lock
generated
@ -330,6 +330,12 @@ dependencies = [
|
|||||||
"new_debug_unreachable",
|
"new_debug_unreachable",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures"
|
||||||
|
version = "0.1.30"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4c7e4c2612746b0df8fed4ce0c69156021b704c9aefa360311c04e6e9e002eed"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-core"
|
name = "futures-core"
|
||||||
version = "0.3.6"
|
version = "0.3.6"
|
||||||
@ -357,6 +363,43 @@ dependencies = [
|
|||||||
"waker-fn",
|
"waker-fn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-macro"
|
||||||
|
version = "0.3.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f57ed14da4603b2554682e9f2ff3c65d7567b53188db96cb71538217fc64581b"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro-hack",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-task"
|
||||||
|
version = "0.3.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4dd26820a9f3637f1302da8bceba3ff33adbe53464b54ca24d4e2d4f1db30f94"
|
||||||
|
dependencies = [
|
||||||
|
"once_cell",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-util"
|
||||||
|
version = "0.3.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8a894a0acddba51a2d49a6f4263b1e64b8c579ece8af50fa86503d52cd1eea34"
|
||||||
|
dependencies = [
|
||||||
|
"futures-core",
|
||||||
|
"futures-macro",
|
||||||
|
"futures-task",
|
||||||
|
"pin-project",
|
||||||
|
"pin-utils",
|
||||||
|
"proc-macro-hack",
|
||||||
|
"proc-macro-nested",
|
||||||
|
"slab",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "getrandom"
|
name = "getrandom"
|
||||||
version = "0.1.15"
|
version = "0.1.15"
|
||||||
@ -400,9 +443,14 @@ dependencies = [
|
|||||||
name = "helix-lsp"
|
name = "helix-lsp"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"futures-util",
|
||||||
"glob",
|
"glob",
|
||||||
|
"jsonrpc-core",
|
||||||
"lsp-types",
|
"lsp-types",
|
||||||
"pathdiff",
|
"pathdiff",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"shellexpand",
|
"shellexpand",
|
||||||
"smol",
|
"smol",
|
||||||
"url",
|
"url",
|
||||||
@ -424,6 +472,7 @@ dependencies = [
|
|||||||
"clap",
|
"clap",
|
||||||
"crossterm",
|
"crossterm",
|
||||||
"helix-core",
|
"helix-core",
|
||||||
|
"helix-lsp",
|
||||||
"helix-view",
|
"helix-view",
|
||||||
"num_cpus",
|
"num_cpus",
|
||||||
"smol",
|
"smol",
|
||||||
@ -495,6 +544,19 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "jsonrpc-core"
|
||||||
|
version = "15.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0745a6379e3edc893c84ec203589790774e4247420033e71a76d3ab4687991fa"
|
||||||
|
dependencies = [
|
||||||
|
"futures",
|
||||||
|
"log",
|
||||||
|
"serde",
|
||||||
|
"serde_derive",
|
||||||
|
"serde_json",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.4.0"
|
version = "1.4.0"
|
||||||
@ -671,12 +733,38 @@ version = "2.1.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
|
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pin-project"
|
||||||
|
version = "0.4.27"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2ffbc8e94b38ea3d2d8ba92aea2983b503cd75d0888d75b86bb37970b5698e15"
|
||||||
|
dependencies = [
|
||||||
|
"pin-project-internal",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pin-project-internal"
|
||||||
|
version = "0.4.27"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "65ad2ae56b6abe3a1ee25f15ee605bacadb9a764edaba9c2bf4103800d4a1895"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pin-project-lite"
|
name = "pin-project-lite"
|
||||||
version = "0.1.10"
|
version = "0.1.10"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e555d9e657502182ac97b539fb3dae8b79cda19e3e4f8ffb5e8de4f18df93c95"
|
checksum = "e555d9e657502182ac97b539fb3dae8b79cda19e3e4f8ffb5e8de4f18df93c95"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pin-utils"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "polling"
|
name = "polling"
|
||||||
version = "2.0.1"
|
version = "2.0.1"
|
||||||
@ -690,6 +778,18 @@ dependencies = [
|
|||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro-hack"
|
||||||
|
version = "0.5.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "99c605b9a0adc77b7211c6b1f722dcb613d68d66859a44f3d485a6da332b0598"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro-nested"
|
||||||
|
version = "0.1.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "eba180dafb9038b050a4c280019bbedf9f2467b61e5d892dcad585bb57aadc5a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.24"
|
version = "1.0.24"
|
||||||
@ -848,6 +948,12 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "slab"
|
||||||
|
version = "0.4.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "smallvec"
|
name = "smallvec"
|
||||||
version = "1.4.2"
|
version = "1.4.2"
|
||||||
|
@ -9,7 +9,12 @@ edition = "2018"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
lsp-types = { version = "0.82", features = ["proposed"] }
|
lsp-types = { version = "0.82", features = ["proposed"] }
|
||||||
smol = "1.2"
|
smol = "1.2"
|
||||||
url = "2.1.1"
|
url = "2"
|
||||||
pathdiff = "0.2.0"
|
pathdiff = "0.2.0"
|
||||||
shellexpand = "2.0.0"
|
shellexpand = "2.0.0"
|
||||||
glob = "0.3.0"
|
glob = "0.3"
|
||||||
|
anyhow = "1"
|
||||||
|
serde_json = "1.0"
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
jsonrpc-core = "15.1"
|
||||||
|
futures-util = "0.3"
|
||||||
|
@ -1,19 +1,44 @@
|
|||||||
use std::{
|
use std::collections::HashMap;
|
||||||
collections::HashMap,
|
|
||||||
process::{ChildStderr, ChildStdin, ChildStdout, Command, Stdio},
|
|
||||||
};
|
|
||||||
|
|
||||||
|
use jsonrpc_core as jsonrpc;
|
||||||
|
use lsp_types as lsp;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
use smol::channel::{Receiver, Sender};
|
||||||
use smol::io::{BufReader, BufWriter};
|
use smol::io::{BufReader, BufWriter};
|
||||||
use smol::prelude::*;
|
use smol::prelude::*;
|
||||||
use smol::Unblock;
|
use smol::process::{Child, ChildStderr, ChildStdin, ChildStdout, Command, Stdio};
|
||||||
|
use smol::Executor;
|
||||||
|
|
||||||
struct Client {
|
use futures_util::{select, FutureExt};
|
||||||
// process: Command,
|
|
||||||
reader: BufReader<Unblock<ChildStdout>>,
|
/// A type representing all possible values sent from the server to the client.
|
||||||
|
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
enum Message {
|
||||||
|
/// A regular JSON-RPC request output (single response).
|
||||||
|
Output(jsonrpc::Output),
|
||||||
|
/// A notification.
|
||||||
|
Notification(jsonrpc::Notification),
|
||||||
|
/// A JSON-RPC request
|
||||||
|
Call(jsonrpc::Call),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Client {
|
||||||
|
process: Child,
|
||||||
|
stderr: BufReader<ChildStderr>,
|
||||||
|
outgoing: Sender<Payload>,
|
||||||
|
|
||||||
|
pub request_counter: u64,
|
||||||
|
|
||||||
|
capabilities: Option<lsp::ServerCapabilities>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
fn start(cmd: &str, args: &[String]) -> Self {
|
pub fn start(ex: &Executor, cmd: &str, args: &[String]) -> Self {
|
||||||
let mut process = Command::new(cmd)
|
let mut process = Command::new(cmd)
|
||||||
.args(args)
|
.args(args)
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
@ -21,51 +46,332 @@ fn start(cmd: &str, args: &[String]) -> Self {
|
|||||||
.stderr(Stdio::piped())
|
.stderr(Stdio::piped())
|
||||||
.spawn()
|
.spawn()
|
||||||
.expect("Failed to start language server");
|
.expect("Failed to start language server");
|
||||||
// TODO: impl drop that kills the process
|
// smol makes sure the process is reaped on drop, but using kill_on_drop(true) maybe?
|
||||||
|
|
||||||
// TODO: do we need bufreader/writer here? or do we use async wrappers on unblock?
|
// TODO: do we need bufreader/writer here? or do we use async wrappers on unblock?
|
||||||
let writer = BufWriter::new(Unblock::new(
|
let writer = BufWriter::new(process.stdin.take().expect("Failed to open stdin"));
|
||||||
process.stdin.take().expect("Failed to open stdin"),
|
let reader = BufReader::new(process.stdout.take().expect("Failed to open stdout"));
|
||||||
));
|
let stderr = BufReader::new(process.stderr.take().expect("Failed to open stderr"));
|
||||||
let reader = BufReader::new(Unblock::new(
|
|
||||||
process.stdout.take().expect("Failed to open stdout"),
|
|
||||||
));
|
|
||||||
let stderr = BufReader::new(Unblock::new(
|
|
||||||
process.stderr.take().expect("Failed to open stderr"),
|
|
||||||
));
|
|
||||||
|
|
||||||
Client { reader }
|
let (incoming, outgoing) = Transport::start(ex, reader, writer);
|
||||||
|
|
||||||
|
Client {
|
||||||
|
process,
|
||||||
|
stderr,
|
||||||
|
outgoing,
|
||||||
|
|
||||||
|
request_counter: 0,
|
||||||
|
|
||||||
|
capabilities: None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn receiver(&mut self) -> Result<(), std::io::Error> {
|
fn next_request_id(&mut self) -> jsonrpc::Id {
|
||||||
let mut headers: HashMap<String, String> = HashMap::default();
|
let id = jsonrpc::Id::Num(self.request_counter);
|
||||||
loop {
|
self.request_counter += 1;
|
||||||
// read headers
|
id
|
||||||
loop {
|
}
|
||||||
let mut header = String::new();
|
|
||||||
// detect pipe closed if 0
|
|
||||||
self.reader.read_line(&mut header).await?;
|
|
||||||
let header = header.trim();
|
|
||||||
|
|
||||||
if header.is_empty() {
|
fn to_params(value: Value) -> anyhow::Result<jsonrpc::Params> {
|
||||||
break;
|
use jsonrpc::Params;
|
||||||
}
|
|
||||||
|
|
||||||
let parts: Vec<&str> = header.split(": ").collect();
|
let params = match value {
|
||||||
if parts.len() != 2 {
|
Value::Null => Params::None,
|
||||||
// return Err(Error::new(ErrorKind::Other, "Failed to parse header"));
|
Value::Bool(_) | Value::Number(_) | Value::String(_) => Params::Array(vec![value]),
|
||||||
panic!()
|
Value::Array(vec) => Params::Array(vec),
|
||||||
}
|
Value::Object(map) => Params::Map(map),
|
||||||
headers.insert(parts[0].to_string(), parts[1].to_string());
|
};
|
||||||
}
|
|
||||||
|
|
||||||
// find content-length
|
Ok(params)
|
||||||
|
}
|
||||||
|
|
||||||
// read data
|
pub async fn request<R: lsp::request::Request>(
|
||||||
// decode via serde_json decoding into jsonrpc_core Output
|
&mut self,
|
||||||
break;
|
params: R::Params,
|
||||||
}
|
) -> anyhow::Result<R::Result>
|
||||||
|
where
|
||||||
|
R::Params: serde::Serialize,
|
||||||
|
R::Result: core::fmt::Debug, // TODO: temporary
|
||||||
|
{
|
||||||
|
let params = serde_json::to_value(params)?;
|
||||||
|
|
||||||
|
let request = jsonrpc::MethodCall {
|
||||||
|
jsonrpc: Some(jsonrpc::Version::V2),
|
||||||
|
id: self.next_request_id(),
|
||||||
|
method: R::METHOD.to_string(),
|
||||||
|
params: Self::to_params(params)?,
|
||||||
|
};
|
||||||
|
|
||||||
|
let (tx, rx) = smol::channel::bounded::<anyhow::Result<Value>>(1);
|
||||||
|
|
||||||
|
self.outgoing
|
||||||
|
.send(Payload::Request {
|
||||||
|
chan: tx,
|
||||||
|
value: request,
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let response = rx.recv().await??;
|
||||||
|
|
||||||
|
let response = serde_json::from_value(response)?;
|
||||||
|
|
||||||
|
// TODO: we should pass request to a sender thread via a channel
|
||||||
|
// so it can't be interleaved
|
||||||
|
|
||||||
|
// TODO: responses can be out of order, we need to register a single shot response channel
|
||||||
|
|
||||||
|
Ok(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn notify<R: lsp::notification::Notification>(
|
||||||
|
&mut self,
|
||||||
|
params: R::Params,
|
||||||
|
) -> anyhow::Result<()>
|
||||||
|
where
|
||||||
|
R::Params: serde::Serialize,
|
||||||
|
{
|
||||||
|
let params = serde_json::to_value(params)?;
|
||||||
|
|
||||||
|
let notification = jsonrpc::Notification {
|
||||||
|
jsonrpc: Some(jsonrpc::Version::V2),
|
||||||
|
method: R::METHOD.to_string(),
|
||||||
|
params: Self::to_params(params)?,
|
||||||
|
};
|
||||||
|
|
||||||
|
self.outgoing
|
||||||
|
.send(Payload::Notification(notification))
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------------------------
|
||||||
|
// General messages
|
||||||
|
// -------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
pub async fn initialize(&mut self) -> anyhow::Result<()> {
|
||||||
|
// TODO: delay any requests that are triggered prior to initialize
|
||||||
|
|
||||||
|
#[allow(deprecated)]
|
||||||
|
let params = lsp::InitializeParams {
|
||||||
|
process_id: Some(u64::from(std::process::id())),
|
||||||
|
root_path: None,
|
||||||
|
// root_uri: Some(lsp_types::Url::parse("file://localhost/")?),
|
||||||
|
root_uri: None, // set to project root in the future
|
||||||
|
initialization_options: None,
|
||||||
|
capabilities: lsp::ClientCapabilities::default(),
|
||||||
|
trace: None,
|
||||||
|
workspace_folders: None,
|
||||||
|
client_info: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let response = self.request::<lsp::request::Initialize>(params).await?;
|
||||||
|
self.capabilities = Some(response.capabilities);
|
||||||
|
|
||||||
|
// next up, notify<initialized>
|
||||||
|
self.notify::<lsp::notification::Initialized>(lsp::InitializedParams {})
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn shutdown(&mut self) -> anyhow::Result<()> {
|
||||||
|
self.request::<lsp::request::Shutdown>(()).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn exit(&mut self) -> anyhow::Result<()> {
|
||||||
|
self.notify::<lsp::notification::Exit>(()).await
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------------------------
|
||||||
|
// Text document
|
||||||
|
// -------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
pub async fn text_document_did_open(&mut self) -> anyhow::Result<()> {
|
||||||
|
self.notify::<lsp::notification::DidOpenTextDocument>(lsp::DidOpenTextDocumentParams {
|
||||||
|
text_document: lsp::TextDocumentItem {
|
||||||
|
uri: lsp::Url::parse(".")?,
|
||||||
|
language_id: "rust".to_string(),
|
||||||
|
version: 0,
|
||||||
|
text: "".to_string(),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn text_document_did_change(&mut self) -> anyhow::Result<()> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
// will_save / will_save_wait_until
|
||||||
|
|
||||||
|
pub async fn text_document_did_save(&mut self) -> anyhow::Result<()> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn text_document_did_close(&mut self) -> anyhow::Result<()> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Payload {
|
||||||
|
Request {
|
||||||
|
chan: Sender<anyhow::Result<Value>>,
|
||||||
|
value: jsonrpc::MethodCall,
|
||||||
|
},
|
||||||
|
Notification(jsonrpc::Notification),
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Transport {
|
||||||
|
incoming: Sender<Message>,
|
||||||
|
outgoing: Receiver<Payload>,
|
||||||
|
|
||||||
|
pending_requests: HashMap<jsonrpc::Id, Sender<anyhow::Result<Value>>>,
|
||||||
|
headers: HashMap<String, String>,
|
||||||
|
|
||||||
|
writer: BufWriter<ChildStdin>,
|
||||||
|
reader: BufReader<ChildStdout>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Transport {
|
||||||
|
pub fn start(
|
||||||
|
ex: &Executor,
|
||||||
|
reader: BufReader<ChildStdout>,
|
||||||
|
writer: BufWriter<ChildStdin>,
|
||||||
|
) -> (Receiver<Message>, Sender<Payload>) {
|
||||||
|
let (incoming, rx) = smol::channel::unbounded();
|
||||||
|
let (tx, outgoing) = smol::channel::unbounded();
|
||||||
|
|
||||||
|
let transport = Self {
|
||||||
|
reader,
|
||||||
|
writer,
|
||||||
|
incoming,
|
||||||
|
outgoing,
|
||||||
|
pending_requests: Default::default(),
|
||||||
|
headers: Default::default(),
|
||||||
|
};
|
||||||
|
|
||||||
|
ex.spawn(transport.duplex()).detach();
|
||||||
|
|
||||||
|
(rx, tx)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn recv(
|
||||||
|
reader: &mut (impl AsyncBufRead + Unpin),
|
||||||
|
headers: &mut HashMap<String, String>,
|
||||||
|
) -> Result<Message, std::io::Error> {
|
||||||
|
// read headers
|
||||||
|
loop {
|
||||||
|
let mut header = String::new();
|
||||||
|
// detect pipe closed if 0
|
||||||
|
reader.read_line(&mut header).await?;
|
||||||
|
let header = header.trim();
|
||||||
|
|
||||||
|
if header.is_empty() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let parts: Vec<&str> = header.split(": ").collect();
|
||||||
|
if parts.len() != 2 {
|
||||||
|
// return Err(Error::new(ErrorKind::Other, "Failed to parse header"));
|
||||||
|
panic!()
|
||||||
|
}
|
||||||
|
headers.insert(parts[0].to_string(), parts[1].to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
// find content-length
|
||||||
|
let content_length = headers.get("Content-Length").unwrap().parse().unwrap();
|
||||||
|
|
||||||
|
let mut content = vec![0; content_length];
|
||||||
|
reader.read_exact(&mut content).await?;
|
||||||
|
let msg = String::from_utf8(content).unwrap();
|
||||||
|
|
||||||
|
// read data
|
||||||
|
|
||||||
|
// try parsing as output (server response) or call (server request)
|
||||||
|
let output: serde_json::Result<Message> = serde_json::from_str(&msg);
|
||||||
|
|
||||||
|
Ok(output?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn send_payload(&mut self, payload: Payload) -> anyhow::Result<()> {
|
||||||
|
match payload {
|
||||||
|
Payload::Request { chan, value } => {
|
||||||
|
self.pending_requests.insert(value.id.clone(), chan);
|
||||||
|
|
||||||
|
let json = serde_json::to_string(&value)?;
|
||||||
|
self.send(json).await
|
||||||
|
}
|
||||||
|
Payload::Notification(value) => {
|
||||||
|
let json = serde_json::to_string(&value)?;
|
||||||
|
self.send(json).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn send(&mut self, request: String) -> anyhow::Result<()> {
|
||||||
|
println!("-> {}", request);
|
||||||
|
|
||||||
|
// send the headers
|
||||||
|
self.writer
|
||||||
|
.write_all(format!("Content-Length: {}\r\n\r\n", request.len()).as_bytes())
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
// send the body
|
||||||
|
self.writer.write_all(request.as_bytes()).await?;
|
||||||
|
|
||||||
|
self.writer.flush().await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn recv_response(&mut self, output: jsonrpc::Output) -> anyhow::Result<()> {
|
||||||
|
match output {
|
||||||
|
jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => {
|
||||||
|
println!("<- {}", result);
|
||||||
|
|
||||||
|
let tx = self
|
||||||
|
.pending_requests
|
||||||
|
.remove(&id)
|
||||||
|
.expect("pending_request with id not found!");
|
||||||
|
tx.send(Ok(result)).await?;
|
||||||
|
}
|
||||||
|
jsonrpc::Output::Failure(_) => panic!("recv fail"),
|
||||||
|
msg => unimplemented!("{:?}", msg),
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn duplex(mut self) {
|
||||||
|
loop {
|
||||||
|
select! {
|
||||||
|
// client -> server
|
||||||
|
msg = self.outgoing.next().fuse() => {
|
||||||
|
if msg.is_none() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let msg = msg.unwrap();
|
||||||
|
|
||||||
|
self.send_payload(msg).await.unwrap();
|
||||||
|
}
|
||||||
|
// server <- client
|
||||||
|
msg = Self::recv(&mut self.reader, &mut self.headers).fuse() => {
|
||||||
|
if msg.is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let msg = msg.unwrap();
|
||||||
|
|
||||||
|
match msg {
|
||||||
|
Message::Output(output) => self.recv_response(output).await.unwrap(),
|
||||||
|
Message::Notification(_) => {
|
||||||
|
// dispatch
|
||||||
|
}
|
||||||
|
Message::Call(_) => {
|
||||||
|
// dispatch
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ path = "src/main.rs"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
helix-core = { path = "../helix-core" }
|
helix-core = { path = "../helix-core" }
|
||||||
helix-view = { path = "../helix-view", features = ["term"]}
|
helix-view = { path = "../helix-view", features = ["term"]}
|
||||||
|
helix-lsp = { path = "../helix-lsp"}
|
||||||
|
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
|
|
||||||
|
@ -26,9 +26,14 @@ fn main() -> Result<(), Error> {
|
|||||||
std::thread::spawn(move || smol::block_on(EX.run(smol::future::pending::<()>())));
|
std::thread::spawn(move || smol::block_on(EX.run(smol::future::pending::<()>())));
|
||||||
}
|
}
|
||||||
|
|
||||||
smol::block_on(EX.run(async {
|
let mut lsp = helix_lsp::Client::start(&EX, "rust-analyzer", &[]);
|
||||||
Application::new(args).unwrap().run().await;
|
|
||||||
}));
|
smol::block_on(async {
|
||||||
|
let res = lsp.initialize().await;
|
||||||
|
// Application::new(args).unwrap().run().await;
|
||||||
|
|
||||||
|
loop {}
|
||||||
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user