Skip to content

Commit

Permalink
refactor(lsp): Use the same pattern for similar functions
Browse files Browse the repository at this point in the history
Adjust a few internal functions with similar patterns to behave the same
way.
  • Loading branch information
dnaka91 committed Dec 15, 2023
1 parent db0c603 commit 0e15394
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 49 deletions.
87 changes: 44 additions & 43 deletions crates/stef-lsp/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,29 +180,26 @@ pub fn hover(state: &mut GlobalState<'_>, params: HoverParams) -> Result<Option<
let uri = params.text_document_position_params.text_document.uri;
let position = params.text_document_position_params.position;

debug!(
uri = as_display!(uri);
"requested hover info",
);

if let Some((schema, index)) = state.files.get_mut(&uri).and_then(|file| {
file.borrow_schema()
.as_ref()
.ok()
.zip(Some(file.borrow_index()))
}) {
Ok(
debug!(uri = as_display!(uri); "requested hover info");

Ok(
if let Some((schema, index)) = state.files.get_mut(&uri).and_then(|file| {
file.borrow_schema()
.as_ref()
.ok()
.zip(Some(file.borrow_index()))
}) {
hover::visit_schema(index, schema, position)?.map(|(value, range)| Hover {
contents: HoverContents::Markup(MarkupContent {
kind: MarkupKind::Markdown,
value,
}),
range: Some(range),
}),
)
} else {
Ok(None)
}
})
} else {
None
},
)
}

pub fn document_symbol(
Expand All @@ -211,16 +208,18 @@ pub fn document_symbol(
) -> Result<Option<DocumentSymbolResponse>> {
debug!(uri = as_display!(params.text_document.uri); "requested document symbols");

if let Some((schema, index)) = state.files.get(&params.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(&params.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(
Expand All @@ -229,22 +228,24 @@ pub fn semantic_tokens_full(
) -> Result<Option<SemanticTokensResult>> {
debug!(uri = as_display!(params.text_document.uri); "requested semantic tokens");

if let Some((schema, index)) = state.files.get(&params.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(&params.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(
Expand Down
31 changes: 25 additions & 6 deletions crates/stef-lsp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Initialized>(notif)?);
handle_notify::<Initialized>(&mut state, notif, handlers::initialized);
}
DidOpenTextDocument::METHOD => {
handlers::did_open(&mut state, cast_notify::<DidOpenTextDocument>(notif)?);
handle_notify::<DidOpenTextDocument>(&mut state, notif, handlers::did_open);
}
DidChangeTextDocument::METHOD => {
handlers::did_change(&mut state, cast_notify::<DidChangeTextDocument>(notif)?);
handle_notify::<DidChangeTextDocument>(&mut state, notif, handlers::did_change);
}
DidCloseTextDocument::METHOD => {
handlers::did_close(&mut state, cast_notify::<DidCloseTextDocument>(notif)?);
handle_notify::<DidCloseTextDocument>(&mut state, notif, handlers::did_close);
}
DidChangeConfiguration::METHOD => {
handlers::did_change_configuration(
handle_notify::<DidChangeConfiguration>(
&mut state,
cast_notify::<DidChangeConfiguration>(notif)?,
notif,
handlers::did_change_configuration,
);
}
_ => debug!(notification = as_debug!(notif); "got unknown notification"),
Expand Down Expand Up @@ -206,6 +207,24 @@ where
}
}

fn handle_notify<T>(
state: &mut GlobalState<'_>,
notif: Notification,
handler: fn(&mut GlobalState<'_>, T::Params),
) where
T: LspNotification,
{
let params = match cast_notify::<T>(notif) {
Ok(notif) => notif,
Err(e) => {
error!(error = as_debug!(e); "invalid notification parameters");
return;
}
};

handler(state, params);
}

fn cast_notify<R>(notif: Notification) -> Result<R::Params>
where
R: LspNotification,
Expand Down

0 comments on commit 0e15394

Please sign in to comment.