From 18d43832d99b80027c50dfeb389a764810c0d0d0 Mon Sep 17 00:00:00 2001 From: Auca Coyan Date: Fri, 8 Dec 2023 15:30:13 -0300 Subject: [PATCH] :bug: Fixes markdown formatting on LSP hover (#11253) # Description Hi! I was playing around and I fixed the formatting in the LSP hover. I _only tested in VS Code using Windows_, if anyone is capable, can you test it on nvim or linux if it works properly? I think markdown shouldn't have any problem The link of the LSP meta issue just for reference #10941 # User-Facing Changes Now the LSP hovers markdown properly ![image](https://github.com/nushell/nushell/assets/30557287/7e824331-d9b1-40dd-957f-da77a21e97a2) # Tests + Formatting # After Submitting --- crates/nu-lsp/src/lib.rs | 18 ++++++++-------- crates/nu-lsp/src/notification.rs | 34 +++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 11 deletions(-) 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" } }) );