diff --git a/crates/nu-lsp/src/lib.rs b/crates/nu-lsp/src/lib.rs index f507dce4410a6..e7bb813e2c2d0 100644 --- a/crates/nu-lsp/src/lib.rs +++ b/crates/nu-lsp/src/lib.rs @@ -347,7 +347,7 @@ impl LanguageServer { Id::Declaration(decl_id) => { let decl = working_set.get_decl(decl_id); - let mut description = "```\n### Signature\n```\n".to_string(); + let mut description = "\n### Signature\n```\n".to_string(); let signature = decl.signature(); description.push_str(&format!(" {}", signature.name)); if !signature.named.is_empty() { @@ -371,7 +371,7 @@ impl LanguageServer { let mut first = true; for required_arg in &signature.required_positional { if !first { - description.push_str("\\\n"); + description.push('\n'); } else { first = false; } @@ -387,7 +387,7 @@ impl LanguageServer { } for optional_arg in &signature.optional_positional { if !first { - description.push_str("\\\n"); + description.push('\n'); } else { first = false; } @@ -403,7 +403,7 @@ impl LanguageServer { } if let Some(arg) = &signature.rest_positional { if !first { - description.push_str("\\\n"); + description.push('\n'); } description.push_str(&format!( " `...{}: {}`", @@ -422,7 +422,7 @@ impl LanguageServer { let mut first = true; for named in &signature.named { if !first { - description.push_str("\\\n"); + description.push('\n'); } else { first = false; } @@ -450,7 +450,7 @@ impl LanguageServer { description.push_str("\n```\n"); for input_output in &signature.input_output_types { description - .push_str(&format!(" {} | {}\n", input_output.0, input_output.1)); + .push_str(&format!(" {} | {}\n", input_output.0, input_output.1)); } description.push_str("\n```\n"); } @@ -463,10 +463,10 @@ impl LanguageServer { .push_str(&format!("\n### Extra usage:\n {}\n", decl.extra_usage())); } if !decl.examples().is_empty() { - description.push_str("### Example(s)\n```\n"); + description.push_str("### Example(s)\n"); for example in decl.examples() { description.push_str(&format!( - "```\n {}\n```\n {}\n\n", + " {}\n```\n {}\n```\n", example.description, example.example )); } @@ -957,7 +957,7 @@ mod tests { serde_json::json!({ "contents": { "kind": "markdown", - "value": "```\n### Signature\n```\n hello {flags}\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n### Usage\n Renders some greeting message\n" + "value": "\n### Signature\n```\n hello {flags}\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n### Usage\n Renders some greeting message\n" } }) ); diff --git a/crates/nu-lsp/src/notification.rs b/crates/nu-lsp/src/notification.rs index e8dce97c7df69..c6a22583749e4 100644 --- a/crates/nu-lsp/src/notification.rs +++ b/crates/nu-lsp/src/notification.rs @@ -97,6 +97,36 @@ mod tests { use crate::tests::{hover, initialize_language_server, open, update}; + #[test] + fn hover_correct_documentation_on_let() { + let (client_connection, _recv) = initialize_language_server(); + + let mut script = fixtures(); + script.push("lsp"); + script.push("hover"); + script.push("var.nu"); + let script = Url::from_file_path(script).unwrap(); + + open(&client_connection, script.clone()); + + let resp = hover(&client_connection, script.clone(), 0, 0); + let result = if let Message::Response(response) = resp { + response.result + } else { + panic!() + }; + + assert_json_eq!( + result, + serde_json::json!({ + "contents": { + "kind": "markdown", + "value": "\n### Signature\n```\n let {flags} \n```\n\n### Parameters\n\n `var_name: any` - variable name\n\n `initial_value: any` - equals sign followed by value\n\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n\n### Input/output\n\n```\n any | nothing\n\n```\n### Usage\n Create a variable and give it a value.\n\n### Extra usage:\n This command is a parser keyword. For details, check:\n https://www.nushell.sh/book/thinking_in_nu.html\n### Example(s)\n Set a variable to a value\n```\n let x = 10\n```\n Set a variable to the result of an expression\n```\n let x = 10 + 100\n```\n Set a variable based on the condition\n```\n let x = if false { -1 } else { 1 }\n```\n" + } + }) + ); + } + #[test] fn hover_on_command_after_full_content_change() { let (client_connection, _recv) = initialize_language_server(); @@ -132,7 +162,7 @@ hello"#, serde_json::json!({ "contents": { "kind": "markdown", - "value": "```\n### Signature\n```\n hello {flags}\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n### Usage\n Renders some updated greeting message\n" + "value": "\n### Signature\n```\n hello {flags}\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n### Usage\n Renders some updated greeting message\n" } }) ); @@ -177,7 +207,7 @@ hello"#, serde_json::json!({ "contents": { "kind": "markdown", - "value": "```\n### Signature\n```\n hello {flags}\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n### Usage\n Renders some updated greeting message\n" + "value": "\n### Signature\n```\n hello {flags}\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n### Usage\n Renders some updated greeting message\n" } }) );