Make hover equal to help command (#11320)

# 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`
This commit is contained in:
Auca Coyan 2023-12-15 14:39:19 -03:00 committed by GitHub
parent 533c1a89af
commit 398b756aee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 59 deletions

View File

@ -312,7 +312,7 @@ impl LanguageServer {
} }
} }
} }
_ => {} Id::Value(_) => {}
} }
None None
} }
@ -347,11 +347,22 @@ impl LanguageServer {
Id::Declaration(decl_id) => { Id::Declaration(decl_id) => {
let decl = working_set.get_decl(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(); let signature = decl.signature();
description.push_str(&format!(" {}", signature.name)); description.push_str(&format!(" {}", signature.name));
if !signature.named.is_empty() { if !signature.named.is_empty() {
description.push_str(" {flags}") description.push_str(" {flags}");
} }
for required_arg in &signature.required_positional { for required_arg in &signature.required_positional {
description.push_str(&format!(" <{}>", required_arg.name)); description.push_str(&format!(" <{}>", required_arg.name));
@ -363,6 +374,39 @@ impl LanguageServer {
description.push_str(&format!(" <...{}>", arg.name)); description.push_str(&format!(" <...{}>", arg.name));
} }
description.push_str("\n```\n"); 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() if !signature.required_positional.is_empty()
|| !signature.optional_positional.is_empty() || !signature.optional_positional.is_empty()
|| signature.rest_positional.is_some() || signature.rest_positional.is_some()
@ -370,10 +414,10 @@ impl LanguageServer {
description.push_str("\n### Parameters\n\n"); description.push_str("\n### Parameters\n\n");
let mut first = true; let mut first = true;
for required_arg in &signature.required_positional { for required_arg in &signature.required_positional {
if !first { if first {
description.push('\n');
} else {
first = false; first = false;
} else {
description.push('\n');
} }
description.push_str(&format!( description.push_str(&format!(
" `{}: {}`", " `{}: {}`",
@ -386,10 +430,10 @@ impl LanguageServer {
description.push('\n'); description.push('\n');
} }
for optional_arg in &signature.optional_positional { for optional_arg in &signature.optional_positional {
if !first { if first {
description.push('\n');
} else {
first = false; first = false;
} else {
description.push('\n');
} }
description.push_str(&format!( description.push_str(&format!(
" `{}: {}`", " `{}: {}`",
@ -417,36 +461,10 @@ impl LanguageServer {
} }
description.push('\n'); description.push('\n');
} }
if !signature.named.is_empty() {
description.push_str("\n### Flags\n\n"); // Input/output types
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');
}
if !signature.input_output_types.is_empty() { 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"); description.push_str("\n```\n");
for input_output in &signature.input_output_types { for input_output in &signature.input_output_types {
description description
@ -454,14 +472,8 @@ impl LanguageServer {
} }
description.push_str("\n```\n"); description.push_str("\n```\n");
} }
description.push_str(&format!(
"### Usage\n {}\n", // Examples
decl.usage().replace('\r', "")
));
if !decl.extra_usage().is_empty() {
description
.push_str(&format!("\n### Extra usage:\n {}\n", decl.extra_usage()));
}
if !decl.examples().is_empty() { if !decl.examples().is_empty() {
description.push_str("### Example(s)\n"); description.push_str("### Example(s)\n");
for example in decl.examples() { for example in decl.examples() {
@ -578,8 +590,9 @@ mod tests {
}, },
request::{Completion, GotoDefinition, HoverRequest, Initialize, Request, Shutdown}, request::{Completion, GotoDefinition, HoverRequest, Initialize, Request, Shutdown},
CompletionParams, DidChangeTextDocumentParams, DidOpenTextDocumentParams, CompletionParams, DidChangeTextDocumentParams, DidOpenTextDocumentParams,
GotoDefinitionParams, InitializeParams, InitializedParams, TextDocumentContentChangeEvent, GotoDefinitionParams, InitializeParams, InitializedParams, PartialResultParams,
TextDocumentIdentifier, TextDocumentItem, TextDocumentPositionParams, Url, TextDocumentContentChangeEvent, TextDocumentIdentifier, TextDocumentItem,
TextDocumentPositionParams, Url, WorkDoneProgressParams,
}; };
use nu_test_support::fs::{fixtures, root}; use nu_test_support::fs::{fixtures, root};
use std::sync::mpsc::Receiver; use std::sync::mpsc::Receiver;
@ -671,8 +684,8 @@ mod tests {
character: 0, character: 0,
}, },
}, },
work_done_progress_params: Default::default(), work_done_progress_params: WorkDoneProgressParams::default(),
partial_result_params: Default::default(), partial_result_params: PartialResultParams::default(),
}) })
.unwrap(), .unwrap(),
})) }))
@ -777,8 +790,8 @@ mod tests {
text_document: TextDocumentIdentifier { uri }, text_document: TextDocumentIdentifier { uri },
position: lsp_types::Position { line, character }, position: lsp_types::Position { line, character },
}, },
work_done_progress_params: Default::default(), work_done_progress_params: WorkDoneProgressParams::default(),
partial_result_params: Default::default(), partial_result_params: PartialResultParams::default(),
}) })
.unwrap(), .unwrap(),
})) }))
@ -894,7 +907,7 @@ mod tests {
text_document: TextDocumentIdentifier { uri }, text_document: TextDocumentIdentifier { uri },
position: lsp_types::Position { line, character }, position: lsp_types::Position { line, character },
}, },
work_done_progress_params: Default::default(), work_done_progress_params: WorkDoneProgressParams::default(),
}) })
.unwrap(), .unwrap(),
})) }))
@ -957,7 +970,7 @@ mod tests {
serde_json::json!({ serde_json::json!({
"contents": { "contents": {
"kind": "markdown", "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 }, text_document: TextDocumentIdentifier { uri },
position: lsp_types::Position { line, character }, position: lsp_types::Position { line, character },
}, },
work_done_progress_params: Default::default(), work_done_progress_params: WorkDoneProgressParams::default(),
partial_result_params: Default::default(), partial_result_params: PartialResultParams::default(),
context: None, context: None,
}) })
.unwrap(), .unwrap(),

View File

@ -121,7 +121,7 @@ mod tests {
serde_json::json!({ serde_json::json!({
"contents": { "contents": {
"kind": "markdown", "kind": "markdown",
"value": "\n### Signature\n```\n let {flags} <var_name> <initial_value>\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} <var_name> <initial_value>\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!({ serde_json::json!({
"contents": { "contents": {
"kind": "markdown", "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!({ serde_json::json!({
"contents": { "contents": {
"kind": "markdown", "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"
} }
}) })
); );