Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion crates/squawk_server/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use anyhow::Result;
use log::{error, info};
use lsp_server::{Connection, Message, Response};
use lsp_types::request::Request as LspRequest;
use lsp_types::{notification::Notification as LspNotification, request::Request as LspRequest};

use crate::system::System;

Expand Down Expand Up @@ -76,3 +76,60 @@ impl<'a> RequestDispatcher<'a> {
}
}
}

pub(crate) struct NotificationDispatcher<'a> {
connection: &'a Connection,
notif: Option<lsp_server::Notification>,
system: &'a mut dyn System,
}

impl<'a> NotificationDispatcher<'a> {
pub(crate) fn new(
connection: &'a Connection,
notif: lsp_server::Notification,
system: &'a mut dyn System,
) -> Self {
Self {
connection,
notif: Some(notif),
system,
}
}

fn parse<N>(&mut self) -> Option<N::Params>
where
N: LspNotification,
{
let notif = self
.notif
.take_if(|notif| notif.method.as_str() == N::METHOD)?;

match notif.extract(N::METHOD) {
Ok(params) => Some(params),
Err(err) => {
error!("Failed to parse notification params: {err}");
None
}
}
}

pub(crate) fn on<N>(
mut self,
handler: fn(&Connection, N::Params, &mut dyn System) -> Result<()>,
) -> Result<Self>
where
N: LspNotification,
{
if let Some(params) = self.parse::<N>() {
handler(self.connection, params, self.system)?;
}

Ok(self)
}

pub(crate) fn finish(self) {
if let Some(notif) = self.notif {
info!("Ignoring unhandled notification: {}", notif.method);
}
}
}
15 changes: 6 additions & 9 deletions crates/squawk_server/src/handlers/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ fn publish_diagnostics(

pub(crate) fn handle_did_open(
connection: &Connection,
notif: lsp_server::Notification,
system: &mut impl System,
params: DidOpenTextDocumentParams,
system: &mut dyn System,
) -> Result<()> {
let params: DidOpenTextDocumentParams = serde_json::from_value(notif.params)?;
let uri = params.text_document.uri;
let content = params.text_document.text;
let version = params.text_document.version;
Expand All @@ -55,10 +54,9 @@ pub(crate) fn handle_did_open(

pub(crate) fn handle_did_change(
connection: &Connection,
notif: lsp_server::Notification,
system: &mut impl System,
params: DidChangeTextDocumentParams,
system: &mut dyn System,
) -> Result<()> {
let params: DidChangeTextDocumentParams = serde_json::from_value(notif.params)?;
let uri = params.text_document.uri;
let version = params.text_document.version;

Expand All @@ -85,10 +83,9 @@ pub(crate) fn handle_did_change(

pub(crate) fn handle_did_close(
connection: &Connection,
notif: lsp_server::Notification,
system: &mut impl System,
params: DidCloseTextDocumentParams,
system: &mut dyn System,
) -> Result<()> {
let params: DidCloseTextDocumentParams = serde_json::from_value(notif.params)?;
let uri = params.text_document.uri;

system.remove(&uri);
Expand Down
26 changes: 8 additions & 18 deletions crates/squawk_server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ use lsp_types::{
FoldingRangeProviderCapability, HoverProviderCapability, InitializeParams, OneOf,
SelectionRangeProviderCapability, ServerCapabilities, TextDocumentSyncCapability,
TextDocumentSyncKind, WorkDoneProgressOptions,
notification::{
DidChangeTextDocument, DidCloseTextDocument, DidOpenTextDocument, Notification as _,
},
notification::{DidChangeTextDocument, DidCloseTextDocument, DidOpenTextDocument},
request::{
CodeActionRequest, Completion, DocumentSymbolRequest, FoldingRangeRequest, GotoDefinition,
HoverRequest, InlayHintRequest, References, SelectionRangeRequest,
},
};

use crate::dispatch::RequestDispatcher;
use crate::dispatch::{NotificationDispatcher, RequestDispatcher};
use crate::handlers::{
SyntaxTreeRequest, TokensRequest, handle_code_action, handle_completion, handle_did_change,
handle_did_close, handle_did_open, handle_document_symbol, handle_folding_range,
Expand Down Expand Up @@ -114,20 +112,12 @@ fn main_loop(connection: Connection, params: serde_json::Value) -> Result<()> {
}
Message::Notification(notif) => {
info!("Received notification: method={}", notif.method);
match notif.method.as_ref() {
DidOpenTextDocument::METHOD => {
handle_did_open(&connection, notif, &mut system)?;
}
DidChangeTextDocument::METHOD => {
handle_did_change(&connection, notif, &mut system)?;
}
DidCloseTextDocument::METHOD => {
handle_did_close(&connection, notif, &mut system)?;
}
_ => {
info!("Ignoring unhandled notification: {}", notif.method);
}
}

NotificationDispatcher::new(&connection, notif, &mut system)
.on::<DidOpenTextDocument>(handle_did_open)?
.on::<DidChangeTextDocument>(handle_did_change)?
.on::<DidCloseTextDocument>(handle_did_close)?
.finish();
}
}
}
Expand Down
Loading