Skip to content

Commit

Permalink
(lsp) rewrite LSP service using traits
Browse files Browse the repository at this point in the history
A dashmap of closures was silly. This is much cleaner
  • Loading branch information
keithamus committed Dec 18, 2024
1 parent c7fab07 commit 70fc565
Show file tree
Hide file tree
Showing 9 changed files with 1,163 additions and 322 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ crossbeam-channel = { version = "0.5.13" }
dashmap = { version = "6.1.0" }

# Logging
log = { version = "0.4.22" }
tracing = { version = "0.1.41" }
tracing-subscriber = { version = "0.3.19" }
console = { version = "0.15.8" }
Expand Down
4 changes: 2 additions & 2 deletions crates/hdx/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bumpalo::Bump;
use clap::{crate_version, Parser, Subcommand};
use hdx_ast::css::StyleSheet;
use hdx_lsp::server_with_handlers;
use hdx_lsp::{LSPService, Server};
use hdx_parser::{CursorStream, ToCursors};
use miette::{GraphicalReportHandler, GraphicalTheme, NamedSource};
use std::io;
Expand Down Expand Up @@ -141,7 +141,7 @@ fn main() {
}
}
Commands::Lsp {} => {
let server = server_with_handlers(crate_version!());
let server = Server::new(LSPService::new(crate_version!()));
let stderr_log = fmt::layer().with_writer(io::stderr).with_filter(LevelFilter::TRACE);
registry().with(stderr_log).with(server.tracer()).init();
let thread = server.listen_stdio().unwrap();
Expand Down
145 changes: 0 additions & 145 deletions crates/hdx_lsp/src/handlers.rs

This file was deleted.

6 changes: 6 additions & 0 deletions crates/hdx_lsp/src/jsonrpc/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ pub enum Id {
String(String),
}

impl Default for Id {
fn default() -> Self {
Self::Number(0)
}
}

impl From<&str> for Id {
fn from(value: &str) -> Self {
Self::String(value.into())
Expand Down
49 changes: 47 additions & 2 deletions crates/hdx_lsp/src/jsonrpc/message.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use httparse::{parse_headers, EMPTY_HEADER};
use serde::{Deserialize, Serialize};
use serde_json::to_string;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::{from_value, to_string, to_value, Error, Value};
use std::io;

use crate::{Notification, Request, Response};

use super::{ErrorCode, Id};

/// JSON RPC Message
/// This represents a single message coming in or going out, that is
/// compliant with the [JSON-RPC 2.0 spec](https://www.jsonrpc.org/specification).
Expand All @@ -30,6 +32,49 @@ impl Message {
false
}
}

#[doc(hidden)]
#[inline]
pub fn is_initialize_request(&self) -> bool {
if let Message::Request(request) = self {
matches!(request.method.as_str(), "initialize")
} else {
false
}
}

pub fn method(&self) -> Option<&str> {
match self {
Message::Request(Request { method, .. }) => Some(method),
Message::Notification(Notification { method, .. }) => Some(method),
_ => None,
}
}

pub fn id(&self) -> Option<Id> {
match self {
Message::Request(Request { id, .. }) => Some(id.clone()),
Message::Notification(_) => None,
Message::Response(Response::Ok(id, _)) => Some(id.clone()),
Message::Response(Response::Err(id, _, _, _)) => Some(id.clone()),
}
}

pub fn from_value<T: DeserializeOwned>(&self) -> Result<T, Error> {
match self {
Message::Request(Request { params, .. }) => from_value(params.clone()),
Message::Notification(Notification { params, .. }) => from_value(params.clone()),
Message::Response(Response::Ok(_, params)) => from_value(params.clone()),
Message::Response(Response::Err(_, _, _, params)) => from_value(params.clone()),
}
}

pub fn from_result(id: Id, result: Result<impl Serialize, ErrorCode>) -> Message {
match result {
Err(code) => Message::Response(Response::Err(id, code, "".into(), Value::Null)),
Ok(value) => Message::Response(Response::Ok(id, to_value(value).unwrap())),
}
}
}

impl Message {
Expand Down
6 changes: 3 additions & 3 deletions crates/hdx_lsp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod handlers;
mod jsonrpc;
mod server;
mod service;

#[doc(inline)]
pub use jsonrpc::*;
#[doc(inline)]
pub use server::*;

pub use handlers::*;
#[doc(inline)]
pub use service::*;
Loading

0 comments on commit 70fc565

Please sign in to comment.