Skip to content

Commit baced41

Browse files
committed
refactor(lsp): align markdown doc string with output of --help
1 parent 1c6c85d commit baced41

File tree

4 files changed

+148
-101
lines changed

4 files changed

+148
-101
lines changed

crates/nu-lsp/src/hover.rs

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use lsp_types::{Hover, HoverContents, HoverParams, MarkupContent, MarkupKind};
2-
use nu_protocol::engine::Command;
2+
use nu_protocol::{engine::Command, PositionalArg};
33

4-
use crate::{Id, LanguageServer};
4+
use crate::{
5+
signature::{display_flag, doc_for_arg, get_signature_label},
6+
Id, LanguageServer,
7+
};
58

69
impl LanguageServer {
710
pub(crate) fn get_decl_description(decl: &dyn Command, skip_description: bool) -> String {
@@ -19,35 +22,27 @@ impl LanguageServer {
1922
// Usage
2023
description.push_str("---\n### Usage \n```nu\n");
2124
let signature = decl.signature();
22-
description.push_str(&Self::get_signature_label(&signature));
25+
description.push_str(&get_signature_label(&signature));
2326
description.push_str("\n```\n");
2427

2528
// Flags
2629
if !signature.named.is_empty() {
2730
description.push_str("\n### Flags\n\n");
2831
let mut first = true;
29-
for named in &signature.named {
32+
for named in signature.named {
3033
if first {
3134
first = false;
3235
} else {
3336
description.push('\n');
3437
}
3538
description.push_str(" ");
36-
if let Some(short_flag) = &named.short {
37-
description.push_str(&format!("`-{short_flag}`"));
38-
}
39-
if !named.long.is_empty() {
40-
if named.short.is_some() {
41-
description.push_str(", ");
42-
}
43-
description.push_str(&format!("`--{}`", named.long));
44-
}
45-
if let Some(arg) = &named.arg {
46-
description.push_str(&format!(" `<{}>`", arg.to_type()));
47-
}
48-
if !named.desc.is_empty() {
49-
description.push_str(&format!(" - {}", named.desc));
50-
}
39+
description.push_str(&display_flag(&named, true));
40+
description.push_str(&doc_for_arg(
41+
named.arg,
42+
named.desc,
43+
named.default_value,
44+
false,
45+
));
5146
description.push('\n');
5247
}
5348
description.push('\n');
@@ -60,46 +55,38 @@ impl LanguageServer {
6055
{
6156
description.push_str("\n### Parameters\n\n");
6257
let mut first = true;
63-
for required_arg in &signature.required_positional {
58+
let mut write_arg = |arg: PositionalArg, optional: bool| {
6459
if first {
6560
first = false;
6661
} else {
6762
description.push('\n');
6863
}
69-
description.push_str(&format!(
70-
" `{}: {}`",
71-
required_arg.name,
72-
required_arg.shape.to_type()
64+
description.push_str(&format!(" `{}`", arg.name));
65+
description.push_str(&doc_for_arg(
66+
Some(arg.shape),
67+
arg.desc,
68+
arg.default_value,
69+
optional,
7370
));
74-
if !required_arg.desc.is_empty() {
75-
description.push_str(&format!(" - {}", required_arg.desc));
76-
}
7771
description.push('\n');
72+
};
73+
for required_arg in signature.required_positional {
74+
write_arg(required_arg, false);
7875
}
79-
for optional_arg in &signature.optional_positional {
80-
if first {
81-
first = false;
82-
} else {
83-
description.push('\n');
84-
}
85-
description.push_str(&format!(
86-
" `{}: {}`",
87-
optional_arg.name,
88-
optional_arg.shape.to_type()
89-
));
90-
if !optional_arg.desc.is_empty() {
91-
description.push_str(&format!(" - {}", optional_arg.desc));
92-
}
93-
description.push('\n');
76+
for optional_arg in signature.optional_positional {
77+
write_arg(optional_arg, true);
9478
}
95-
if let Some(arg) = &signature.rest_positional {
79+
if let Some(arg) = signature.rest_positional {
9680
if !first {
9781
description.push('\n');
9882
}
99-
description.push_str(&format!(" `...{}: {}`", arg.name, arg.shape.to_type()));
100-
if !arg.desc.is_empty() {
101-
description.push_str(&format!(" - {}", arg.desc));
102-
}
83+
description.push_str(&format!(" `...{}`", arg.name));
84+
description.push_str(&doc_for_arg(
85+
Some(arg.shape),
86+
arg.desc,
87+
arg.default_value,
88+
false,
89+
));
10390
description.push('\n');
10491
}
10592
description.push('\n');
@@ -378,7 +365,7 @@ mod hover_tests {
378365
serde_json::json!({
379366
"contents": {
380367
"kind": "markdown",
381-
"value": "Concatenate multiple strings into a single string, with an optional separator between each.\n---\n### Usage \n```nu\n str join {flags} <separator?>\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n\n\n### Parameters\n\n `separator: string` - Optional separator to use when creating string.\n\n\n### Input/output types\n\n```nu\n list<any> | string\n string | string\n\n```\n### Example(s)\n Create a string from input\n```nu\n ['nu', 'shell'] | str join\n```\n Create a string from input with a separator\n```nu\n ['nu', 'shell'] | str join '-'\n```\n"
368+
"value": "Concatenate multiple strings into a single string, with an optional separator between each.\n---\n### Usage \n```nu\n str join {flags} (separator)\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n\n\n### Parameters\n\n `separator`: `<string>` - Optional separator to use when creating string. (optional)\n\n\n### Input/output types\n\n```nu\n list<any> | string\n string | string\n\n```\n### Example(s)\n Create a string from input\n```nu\n ['nu', 'shell'] | str join\n```\n Create a string from input with a separator\n```nu\n ['nu', 'shell'] | str join '-'\n```\n"
382369
}
383370
})
384371
);

crates/nu-lsp/src/notification.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ mod tests {
162162
serde_json::json!({
163163
"contents": {
164164
"kind": "markdown",
165-
"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---\n### Usage \n```nu\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```nu\n any | nothing\n\n```\n### Example(s)\n Set a variable to a value\n```nu\n let x = 10\n```\n Set a variable to the result of an expression\n```nu\n let x = 10 + 100\n```\n Set a variable based on the condition\n```nu\n let x = if false { -1 } else { 1 }\n```\n"
165+
"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---\n### Usage \n```nu\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`: `<vardecl>` - Variable name.\n\n `initial_value`: `<variable>` - Equals sign followed by value.\n\n\n### Input/output types\n\n```nu\n any | nothing\n\n```\n### Example(s)\n Set a variable to a value\n```nu\n let x = 10\n```\n Set a variable to the result of an expression\n```nu\n let x = 10 + 100\n```\n Set a variable based on the condition\n```nu\n let x = if false { -1 } else { 1 }\n```\n"
166166
}
167167
})
168168
);

0 commit comments

Comments
 (0)