Skip to content

Commit

Permalink
properly handle exit of lsp server
Browse files Browse the repository at this point in the history
  • Loading branch information
oysandvik94 committed Sep 3, 2024
1 parent 335ccba commit bd46a3d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 10 additions & 5 deletions crates/son_of_anton/src/lsp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod initialize;

use std::io::{Stdout, Write};
use std::process::exit;

use anyhow::anyhow;
use anyhow::Result;
Expand Down Expand Up @@ -39,6 +40,14 @@ impl SonOfAnton {
let _ = deserialize_request::<InitializedParams>(&lsp_request)?;
event!(Level::INFO, "Server was initialized");
}
"shutdown" => {
event!(Level::INFO, "Shutting down server by request of the client");
self.send_response(lsp_request, serde_json::Value::Null)?;
}
"exit" => {
event!(Level::INFO, "Exiting after confirmation by client");
exit(0)
}
unknown_method => {
event!(
Level::DEBUG,
Expand All @@ -61,11 +70,7 @@ impl SonOfAnton {
result: Some(resp),
};
let resp = rpc::encode_response(resp)?;
event!(
Level::DEBUG,
"Sending initialize response to server: {:?}",
resp
);
event!(Level::DEBUG, "Sending response to server: {:?}", resp);
self.writer.write_all(resp.as_bytes())?;
self.writer.flush()?;
Ok(())
Expand Down
2 changes: 0 additions & 2 deletions crates/son_of_anton/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ pub struct Response<T> {

pub fn encode_response<T: Serialize>(message: Response<T>) -> Result<String> {
let content = serde_json::to_string(&message).context("Could not serialize message")?;
event!(Level::DEBUG, "Responding with {}", content);

let resp = format!("Content-Length: {}\r\n\r\n{}", content.len(), content);
Ok(resp)
}
Expand Down

0 comments on commit bd46a3d

Please sign in to comment.