diff --git a/crates/rune/src/languageserver/completion.rs b/crates/rune/src/languageserver/completion.rs index 695356e9a..53cfa8229 100644 --- a/crates/rune/src/languageserver/completion.rs +++ b/crates/rune/src/languageserver/completion.rs @@ -1,3 +1,5 @@ +use std::borrow::ToOwned; + use anyhow::Result; use lsp::CompletionItem; use lsp::CompletionItemKind; @@ -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({ @@ -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| { @@ -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), @@ -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() })?; } @@ -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), @@ -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() })?; } diff --git a/crates/rune/src/languageserver/envelope.rs b/crates/rune/src/languageserver/envelope.rs index cfc0e28cd..1ba2e95e3 100644 --- a/crates/rune/src/languageserver/envelope.rs +++ b/crates/rune/src/languageserver/envelope.rs @@ -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"); - } -} diff --git a/crates/rune/src/languageserver/mod.rs b/crates/rune/src/languageserver/mod.rs index 13afe8e89..6323fad53 100644 --- a/crates/rune/src/languageserver/mod.rs +++ b/crates/rune/src/languageserver/mod.rs @@ -2,6 +2,9 @@ #![allow(clippy::too_many_arguments)] +#[cfg(test)] +mod tests; + mod completion; mod connection; pub mod envelope; diff --git a/crates/rune/src/languageserver/tests.rs b/crates/rune/src/languageserver/tests.rs new file mode 100644 index 000000000..da59a907a --- /dev/null +++ b/crates/rune/src/languageserver/tests.rs @@ -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"); +}