MCPcopy Create free account
hub / github.com/Eeive/Evcode-ide / lsp_worker

Function lsp_worker

src/lsp/mod.rs:95–153  ·  view source on GitHub ↗
(
    server: &str,
    tx: mpsc::Sender<LspMessage>,
    req_rx: &mut mpsc::Receiver<String>,
)

Source from the content-addressed store, hash-verified

93}
94
95async fn lsp_worker(
96 server: &str,
97 tx: mpsc::Sender<LspMessage>,
98 req_rx: &mut mpsc::Receiver<String>,
99) -> Result<()> {
100 let mut child: Child = Command::new(server)
101 .arg("--stdio")
102 .stdin(Stdio::piped())
103 .stdout(Stdio::piped())
104 .stderr(Stdio::null())
105 .spawn()
106 .with_context(|| format!("Failed to start LSP: {server}"))?;
107
108 let stdin: ChildStdin = child.stdin.take().context("stdin unavailable")?;
109 let stdout: ChildStdout = child.stdout.take().context("stdout unavailable")?;
110
111 let init_req = json_rpc_request(1, "initialize", json!({
112 "processId": std::process::id(),
113 "rootUri": null,
114 "capabilities": {}
115 }));
116
117 let mut stdin = tokio::io::BufWriter::new(stdin);
118 send_rpc(&mut stdin, &init_req).await?;
119
120 let mut reader = BufReader::new(stdout);
121 let mut initialized = false;
122 let mut pending: Vec<String> = Vec::new();
123
124 loop {
125 tokio::select! {
126 msg = read_message(&mut reader) => {
127 match msg {
128 Ok(body) => {
129 if handle_lsp_response(&body, &tx).await && !initialized {
130 initialized = true;
131 let initialized_msg = json_rpc_notification("initialized", json!({}));
132 send_rpc(&mut stdin, &initialized_msg).await?;
133 for msg in pending.drain(..) {
134 send_rpc(&mut stdin, &msg).await?;
135 }
136 let _ = tx.send(LspMessage::Ready { server: server.to_string() }).await;
137 }
138 }
139 Err(_) => break,
140 }
141 }
142 req = req_rx.recv() => {
143 match req {
144 Some(msg) if initialized => { let _ = send_rpc(&mut stdin, &msg).await; }
145 Some(msg) => pending.push(msg),
146 None => break,
147 }
148 }
149 }
150 }
151
152 Ok(())

Callers 1

start_asyncMethod · 0.85

Calls 2

json_rpc_requestFunction · 0.85
send_rpcFunction · 0.85

Tested by

no test coverage detected