From 0e15394e43d761ff1572ec80e2138ad26054f0e0 Mon Sep 17 00:00:00 2001 From: Dominik Nakamura Date: Sat, 16 Dec 2023 02:03:27 +0900 Subject: [PATCH] refactor(lsp): Use the same pattern for similar functions Adjust a few internal functions with similar patterns to behave the same way. --- crates/stef-lsp/src/handlers/mod.rs | 87 +++++++++++++++-------------- crates/stef-lsp/src/main.rs | 31 ++++++++-- 2 files changed, 69 insertions(+), 49 deletions(-) diff --git a/crates/stef-lsp/src/handlers/mod.rs b/crates/stef-lsp/src/handlers/mod.rs index 9de3b64..19bc739 100644 --- a/crates/stef-lsp/src/handlers/mod.rs +++ b/crates/stef-lsp/src/handlers/mod.rs @@ -180,29 +180,26 @@ pub fn hover(state: &mut GlobalState<'_>, params: HoverParams) -> Result Result> { debug!(uri = as_display!(params.text_document.uri); "requested document symbols"); - if let Some((schema, index)) = state.files.get(¶ms.text_document.uri).and_then(|file| { - file.borrow_schema() - .as_ref() - .ok() - .zip(Some(file.borrow_index())) - }) { - return Ok(Some(document_symbols::visit_schema(index, schema)?.into())); - } - - Ok(None) + Ok( + if let Some((schema, index)) = state.files.get(¶ms.text_document.uri).and_then(|file| { + file.borrow_schema() + .as_ref() + .ok() + .zip(Some(file.borrow_index())) + }) { + Some(document_symbols::visit_schema(index, schema)?.into()) + } else { + None + }, + ) } pub fn semantic_tokens_full( @@ -229,22 +228,24 @@ pub fn semantic_tokens_full( ) -> Result> { debug!(uri = as_display!(params.text_document.uri); "requested semantic tokens"); - if let Some((schema, index)) = state.files.get(¶ms.text_document.uri).and_then(|file| { - file.borrow_schema() - .as_ref() - .ok() - .zip(Some(file.borrow_index())) - }) { - return Ok(Some( - SemanticTokens { - result_id: None, - data: semantic_tokens::Visitor::new(index).visit_schema(schema)?, - } - .into(), - )); - } - - Ok(None) + Ok( + if let Some((schema, index)) = state.files.get(¶ms.text_document.uri).and_then(|file| { + file.borrow_schema() + .as_ref() + .ok() + .zip(Some(file.borrow_index())) + }) { + Some( + SemanticTokens { + result_id: None, + data: semantic_tokens::Visitor::new(index).visit_schema(schema)?, + } + .into(), + ) + } else { + None + }, + ) } pub fn did_change_configuration( diff --git a/crates/stef-lsp/src/main.rs b/crates/stef-lsp/src/main.rs index 666457b..b0abc56 100644 --- a/crates/stef-lsp/src/main.rs +++ b/crates/stef-lsp/src/main.rs @@ -127,21 +127,22 @@ fn main_loop(conn: &Connection, mut state: GlobalState<'_>) -> Result<()> { } lsp_server::Message::Notification(notif) => match notif.method.as_str() { Initialized::METHOD => { - handlers::initialized(&mut state, cast_notify::(notif)?); + handle_notify::(&mut state, notif, handlers::initialized); } DidOpenTextDocument::METHOD => { - handlers::did_open(&mut state, cast_notify::(notif)?); + handle_notify::(&mut state, notif, handlers::did_open); } DidChangeTextDocument::METHOD => { - handlers::did_change(&mut state, cast_notify::(notif)?); + handle_notify::(&mut state, notif, handlers::did_change); } DidCloseTextDocument::METHOD => { - handlers::did_close(&mut state, cast_notify::(notif)?); + handle_notify::(&mut state, notif, handlers::did_close); } DidChangeConfiguration::METHOD => { - handlers::did_change_configuration( + handle_notify::( &mut state, - cast_notify::(notif)?, + notif, + handlers::did_change_configuration, ); } _ => debug!(notification = as_debug!(notif); "got unknown notification"), @@ -206,6 +207,24 @@ where } } +fn handle_notify( + state: &mut GlobalState<'_>, + notif: Notification, + handler: fn(&mut GlobalState<'_>, T::Params), +) where + T: LspNotification, +{ + let params = match cast_notify::(notif) { + Ok(notif) => notif, + Err(e) => { + error!(error = as_debug!(e); "invalid notification parameters"); + return; + } + }; + + handler(state, params); +} + fn cast_notify(notif: Notification) -> Result where R: LspNotification,