Skip to content

Commit

Permalink
rune: Remove unwraps in languageserver
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Nov 5, 2024
1 parent 471a60c commit 9f15557
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
20 changes: 17 additions & 3 deletions crates/rune/src/languageserver/completion.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::ToOwned;

use anyhow::Result;
use lsp::CompletionItem;
use lsp::CompletionItemKind;
Expand Down Expand Up @@ -36,6 +38,10 @@ pub(super) fn complete_for_unit(
continue;
}

let Some(last) = function.path.base_name() else {
continue;
};

let args = match &function.args {
DebugArgs::EmptyArgs => None,
DebugArgs::TupleArgs(n) => Some({
Expand Down Expand Up @@ -64,7 +70,7 @@ pub(super) fn complete_for_unit(
let detail = args.map(|a| format!("({a:}) -> ?"));

results.try_push(CompletionItem {
label: format!("{}", function.path.last().unwrap()),
label: last.to_owned(),
kind: Some(CompletionItemKind::FUNCTION),
detail: detail.clone(),
documentation: docs.map(|d| {
Expand Down Expand Up @@ -126,6 +132,10 @@ pub(super) fn complete_native_instance_data(

let detail = return_type.map(|r| format!("({args}) -> {r}"));

let Ok(data) = serde_json::to_value(meta.hash.into_inner()) else {
continue;
};

results.try_push(CompletionItem {
label: n.try_to_string()?.into_std(),
kind: Some(kind),
Expand All @@ -148,7 +158,7 @@ pub(super) fn complete_native_instance_data(
detail: None,
description: Some(prefix.try_to_string()?.into_std()),
}),
data: Some(serde_json::to_value(meta.hash.into_inner() as i64).unwrap()),
data: Some(data),
..Default::default()
})?;
}
Expand Down Expand Up @@ -187,6 +197,10 @@ pub(super) fn complete_native_loose_data(

let detail = return_type.map(|r| format!("({args}) -> {r}"));

let Ok(data) = serde_json::to_value(meta.hash.into_inner()) else {
continue;
};

results.try_push(CompletionItem {
label: func_name.try_clone()?.into_std(),
kind: Some(kind),
Expand All @@ -205,7 +219,7 @@ pub(super) fn complete_native_loose_data(
},
new_text: func_name.into_std(),
})),
data: Some(serde_json::to_value(meta.hash.into_inner() as i64).unwrap()),
data: Some(data),
..Default::default()
})?;
}
Expand Down
16 changes: 0 additions & 16 deletions crates/rune/src/languageserver/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,3 @@ impl<'a> serde::Deserialize<'a> for V2 {
}
}
}

#[cfg(test)]
mod tests {
use super::Code;

#[test]
fn test_code() {
let code: Code = serde_json::from_str("-1").unwrap();
assert_eq!(code, Code::Unknown(-1));
assert_eq!(serde_json::to_string(&code).unwrap(), "-1");

let code: Code = serde_json::from_str("-32601").unwrap();
assert_eq!(code, Code::MethodNotFound);
assert_eq!(serde_json::to_string(&code).unwrap(), "-32601");
}
}
3 changes: 3 additions & 0 deletions crates/rune/src/languageserver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#![allow(clippy::too_many_arguments)]

#[cfg(test)]
mod tests;

mod completion;
mod connection;
pub mod envelope;
Expand Down
12 changes: 12 additions & 0 deletions crates/rune/src/languageserver/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use super::Code;

#[test]
fn test_code() {
let code: Code = serde_json::from_str("-1").unwrap();
assert_eq!(code, Code::Unknown(-1));
assert_eq!(serde_json::to_string(&code).unwrap(), "-1");

let code: Code = serde_json::from_str("-32601").unwrap();
assert_eq!(code, Code::MethodNotFound);
assert_eq!(serde_json::to_string(&code).unwrap(), "-32601");
}

0 comments on commit 9f15557

Please sign in to comment.