From 398b756aeede055c55b327b8bf83ae774b7ff260 Mon Sep 17 00:00:00 2001 From: Auca Coyan Date: Fri, 15 Dec 2023 14:39:19 -0300 Subject: [PATCH] :sparkles: Make `hover` equal to `help` command (#11320) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description Hi! A few days ago I changed the hover from `--ide-lsp` to match `help` #11284, now this PR is doing the same but for the new `--lsp` server I also did some tiny fixes to syntax, with some clippy `pedantic` lints # User-Facing Changes ![image](https://github.com/nushell/nushell/assets/30557287/0e167dc8-777a-4961-8746-aa29f18eccfa) # Tests + Formatting ✅ ran `toolkit check pr` --- crates/nu-lsp/src/lib.rs | 125 +++++++++++++++++------------- crates/nu-lsp/src/notification.rs | 6 +- 2 files changed, 72 insertions(+), 59 deletions(-) diff --git a/crates/nu-lsp/src/lib.rs b/crates/nu-lsp/src/lib.rs index e7bb813e2c..288549d34c 100644 --- a/crates/nu-lsp/src/lib.rs +++ b/crates/nu-lsp/src/lib.rs @@ -312,7 +312,7 @@ impl LanguageServer { } } } - _ => {} + Id::Value(_) => {} } None } @@ -347,11 +347,22 @@ 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 = String::new(); + + // First description + description.push_str(&format!("{}\n", decl.usage().replace('\r', ""))); + + // Additional description + if !decl.extra_usage().is_empty() { + description.push_str(&format!("\n{}\n", decl.extra_usage())); + } + + // Usage + description.push_str("### Usage \n```\n"); let signature = decl.signature(); description.push_str(&format!(" {}", signature.name)); if !signature.named.is_empty() { - description.push_str(" {flags}") + description.push_str(" {flags}"); } for required_arg in &signature.required_positional { description.push_str(&format!(" <{}>", required_arg.name)); @@ -363,6 +374,39 @@ impl LanguageServer { description.push_str(&format!(" <...{}>", arg.name)); } description.push_str("\n```\n"); + + // Flags + if !signature.named.is_empty() { + description.push_str("\n### Flags\n\n"); + let mut first = true; + for named in &signature.named { + if first { + first = false; + } else { + description.push('\n'); + } + description.push_str(" "); + if let Some(short_flag) = &named.short { + description.push_str(&format!("`-{short_flag}`")); + } + if !named.long.is_empty() { + if named.short.is_some() { + description.push_str(", "); + } + description.push_str(&format!("`--{}`", named.long)); + } + if let Some(arg) = &named.arg { + description.push_str(&format!(" `<{}>`", arg.to_type())); + } + if !named.desc.is_empty() { + description.push_str(&format!(" - {}", named.desc)); + } + description.push('\n'); + } + description.push('\n'); + } + + // Parameters if !signature.required_positional.is_empty() || !signature.optional_positional.is_empty() || signature.rest_positional.is_some() @@ -370,10 +414,10 @@ impl LanguageServer { description.push_str("\n### Parameters\n\n"); let mut first = true; for required_arg in &signature.required_positional { - if !first { - description.push('\n'); - } else { + if first { first = false; + } else { + description.push('\n'); } description.push_str(&format!( " `{}: {}`", @@ -386,10 +430,10 @@ impl LanguageServer { description.push('\n'); } for optional_arg in &signature.optional_positional { - if !first { - description.push('\n'); - } else { + if first { first = false; + } else { + description.push('\n'); } description.push_str(&format!( " `{}: {}`", @@ -417,36 +461,10 @@ impl LanguageServer { } description.push('\n'); } - if !signature.named.is_empty() { - description.push_str("\n### Flags\n\n"); - let mut first = true; - for named in &signature.named { - if !first { - description.push('\n'); - } else { - first = false; - } - description.push_str(" "); - if let Some(short_flag) = &named.short { - description.push_str(&format!("`-{}`", short_flag)); - } - if !named.long.is_empty() { - if named.short.is_some() { - description.push_str(", ") - } - description.push_str(&format!("`--{}`", named.long)); - } - if let Some(arg) = &named.arg { - description.push_str(&format!(" `<{}>`", arg.to_type())) - } - if !named.desc.is_empty() { - description.push_str(&format!(" - {}", named.desc)); - } - } - description.push('\n'); - } + + // Input/output types if !signature.input_output_types.is_empty() { - description.push_str("\n### Input/output\n"); + description.push_str("\n### Input/output types\n"); description.push_str("\n```\n"); for input_output in &signature.input_output_types { description @@ -454,14 +472,8 @@ impl LanguageServer { } description.push_str("\n```\n"); } - description.push_str(&format!( - "### Usage\n {}\n", - decl.usage().replace('\r', "") - )); - if !decl.extra_usage().is_empty() { - description - .push_str(&format!("\n### Extra usage:\n {}\n", decl.extra_usage())); - } + + // Examples if !decl.examples().is_empty() { description.push_str("### Example(s)\n"); for example in decl.examples() { @@ -578,8 +590,9 @@ mod tests { }, request::{Completion, GotoDefinition, HoverRequest, Initialize, Request, Shutdown}, CompletionParams, DidChangeTextDocumentParams, DidOpenTextDocumentParams, - GotoDefinitionParams, InitializeParams, InitializedParams, TextDocumentContentChangeEvent, - TextDocumentIdentifier, TextDocumentItem, TextDocumentPositionParams, Url, + GotoDefinitionParams, InitializeParams, InitializedParams, PartialResultParams, + TextDocumentContentChangeEvent, TextDocumentIdentifier, TextDocumentItem, + TextDocumentPositionParams, Url, WorkDoneProgressParams, }; use nu_test_support::fs::{fixtures, root}; use std::sync::mpsc::Receiver; @@ -671,8 +684,8 @@ mod tests { character: 0, }, }, - work_done_progress_params: Default::default(), - partial_result_params: Default::default(), + work_done_progress_params: WorkDoneProgressParams::default(), + partial_result_params: PartialResultParams::default(), }) .unwrap(), })) @@ -777,8 +790,8 @@ mod tests { text_document: TextDocumentIdentifier { uri }, position: lsp_types::Position { line, character }, }, - work_done_progress_params: Default::default(), - partial_result_params: Default::default(), + work_done_progress_params: WorkDoneProgressParams::default(), + partial_result_params: PartialResultParams::default(), }) .unwrap(), })) @@ -894,7 +907,7 @@ mod tests { text_document: TextDocumentIdentifier { uri }, position: lsp_types::Position { line, character }, }, - work_done_progress_params: Default::default(), + work_done_progress_params: WorkDoneProgressParams::default(), }) .unwrap(), })) @@ -957,7 +970,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": "Renders some greeting message\n### Usage \n```\n hello {flags}\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n\n" } }) ); @@ -974,8 +987,8 @@ mod tests { text_document: TextDocumentIdentifier { uri }, position: lsp_types::Position { line, character }, }, - work_done_progress_params: Default::default(), - partial_result_params: Default::default(), + work_done_progress_params: WorkDoneProgressParams::default(), + partial_result_params: PartialResultParams::default(), context: None, }) .unwrap(), diff --git a/crates/nu-lsp/src/notification.rs b/crates/nu-lsp/src/notification.rs index cd74716cad..085baebcbd 100644 --- a/crates/nu-lsp/src/notification.rs +++ b/crates/nu-lsp/src/notification.rs @@ -121,7 +121,7 @@ mod tests { 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" + "value": "Create a variable and give it a value.\n\nThis command is a parser keyword. For details, check:\n https://www.nushell.sh/book/thinking_in_nu.html\n### Usage \n```\n let {flags} \n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n\n\n### Parameters\n\n `var_name: any` - Variable name.\n\n `initial_value: any` - Equals sign followed by value.\n\n\n### Input/output types\n\n```\n any | nothing\n\n```\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" } }) ); @@ -162,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": "Renders some updated greeting message\n### Usage \n```\n hello {flags}\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n\n" } }) ); @@ -207,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": "Renders some updated greeting message\n### Usage \n```\n hello {flags}\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n\n" } }) );