Skip to content

Commit

Permalink
(lsp) get tracing working
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Dec 19, 2024
1 parent 36f9103 commit 77dea83
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
17 changes: 10 additions & 7 deletions crates/hdx_lsp/src/jsonrpc/tracing_layer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::collections::BTreeMap;
use std::{
collections::BTreeMap,
sync::{Arc, RwLock},
};

use crossbeam_channel::Sender;
use lsp_types::{
Expand All @@ -13,13 +16,13 @@ use tracing_subscriber::{layer::Context, Layer};
use super::{Message, Notification};

pub struct TracingLayer {
level: LevelFilter,
pub level: Arc<RwLock<LevelFilter>>,
sender: Sender<Message>,
}

impl TracingLayer {
pub fn new(sender: Sender<Message>) -> Self {
Self { level: LevelFilter::OFF, sender }
pub fn new(level: Arc<RwLock<LevelFilter>>, sender: Sender<Message>) -> Self {
Self { level, sender }
}
}
// lsp_types::notification::LogTrace
Expand Down Expand Up @@ -69,7 +72,7 @@ where
S: Subscriber,
{
fn on_event(&self, event: &Event, _ctx: Context<'_, S>) {
if event.metadata().level() > &self.level {
if event.metadata().level() > &(*self.level.read().unwrap()) {
return;
}
let mut fields = MessageVisitor::default();
Expand All @@ -91,7 +94,7 @@ where
})
.unwrap_or_default(),
});
self.sender.send(message).ok();
self.sender.try_send(message).ok();
return;
}
};
Expand All @@ -105,6 +108,6 @@ where
.unwrap_or_default(),
});

self.sender.send(message).ok();
self.sender.try_send(message).ok();
}
}
45 changes: 30 additions & 15 deletions crates/hdx_lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
//!
use crossbeam_channel::{bounded, Receiver, Sender};
use lsp_types::SetTraceParams;
use serde_json::from_value;
use std::{
io,
sync::{Arc, RwLock},
thread::{Builder, JoinHandle},
};
use tracing::{trace, warn};
use tracing::{level_filters::LevelFilter, trace, warn};

use crate::TracingLayer;
use crate::{Notification, TracingLayer};

use super::Message;

Expand All @@ -39,6 +42,7 @@ pub struct Server {
read_sender: Sender<Message>,
request_handler: JoinHandle<Result<(), io::Error>>,
read_receiver: Receiver<Message>,
trace_level: Arc<RwLock<LevelFilter>>,
}

impl Server {
Expand All @@ -48,13 +52,29 @@ impl Server {

let handler_receiver = read_receiver.clone();
let handler_sender = write_sender.clone();
let trace_level = Arc::new(RwLock::new(LevelFilter::OFF));
let level_set = trace_level.clone();
let request_handler = Builder::new()
.name("LspMessageHandler".into())
.spawn(move || {
while let Ok(message) = handler_receiver.recv() {
trace!("LspMessageHandler -> {:#?}", &message);
if message.is_exit_notification() {
break;
if let Message::Notification(Notification { method, params }) = &message {
if method == "exit" {
break;
}
if method == "$/setTrace" {
let level = from_value::<SetTraceParams>(params.clone())
.map(|p| match p.value {
lsp_types::TraceValue::Off => LevelFilter::OFF,
lsp_types::TraceValue::Messages => LevelFilter::WARN,
lsp_types::TraceValue::Verbose => LevelFilter::TRACE,
})
.unwrap_or(LevelFilter::OFF);
trace!("Changing level to {:?}", level);
let mut level_set = level_set.write().unwrap();
*level_set = level;
}
}
let response = handler.handle(message);
if let Some(response) = response {
Expand All @@ -68,7 +88,11 @@ impl Server {
Ok(())
})
.expect("Failed to create Reader");
Server { write_sender, write_receiver, read_sender, read_receiver, request_handler }
Server { write_sender, write_receiver, read_sender, read_receiver, request_handler, trace_level }
}

pub fn tracer(&self) -> TracingLayer {
TracingLayer::new(self.trace_level.clone(), self.write_sender.clone())
}

pub fn listen_stdio(&self) -> Result<ThreadConnection, io::Error> {
Expand All @@ -94,10 +118,6 @@ impl Server {
Ok(ThreadConnection { sender: reader, receiver: writer })
}

pub fn tracer(&self) -> TracingLayer {
TracingLayer::new(self.write_sender.clone())
}

#[cfg(test)]
pub fn raw_channels(&self) -> (Sender<Message>, Receiver<Message>) {
(self.read_sender.clone(), self.write_receiver.clone())
Expand Down Expand Up @@ -163,12 +183,7 @@ mod tests {
.unwrap();
assert_eq!(
receiver.recv(),
Ok(Message::Response(Response::Err(
1.into(),
ErrorCode::MethodNotFound,
"".into(),
Value::Null
)))
Ok(Message::Response(Response::Err(1.into(), ErrorCode::MethodNotFound, "".into(), Value::Null)))
);
sender.send(Message::Notification(Notification { method: "exit".into(), params: Value::Null })).unwrap();
}
Expand Down
1 change: 0 additions & 1 deletion crates/hdx_lsp/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ impl File {
let mut result: ParserReturn<'_, StyleSheet<'_>> =
Parser::new(&bump, "", Features::default()).parse_entirely::<StyleSheet>();
while let Ok(call) = read_receiver.recv() {
trace!("String is currently {:?}", string);
match call {
FileCall::RopeChange(rope) => {
trace!("Parsing document");
Expand Down

0 comments on commit 77dea83

Please sign in to comment.