feat: Add default docs for aliases, generated from the command they point to (#10825)

This commit is contained in:
Poliorcetics 2023-12-04 19:56:46 +01:00 committed by GitHub
parent c9aa6ba0f3
commit fc06afd051
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View file

@ -155,3 +155,11 @@ fn alias_ordering() {
let actual = nu!(r#"alias bar = echo; def echo [] { 'dummy echo' }; bar 'foo'"#);
assert_eq!(actual.out, "foo");
}
#[test]
fn alias_default_help() {
let actual = nu!("alias teapot = echo 'I am a beautiful teapot'; help teapot");
// There must be at least one line of help
let first_help_line = actual.out.lines().next().unwrap();
assert!(first_help_line.starts_with("Alias for `echo 'I am a beautiful teapot'`"));
}

View file

@ -856,7 +856,6 @@ pub fn parse_alias(
if let Some(decl_id) = working_set.find_decl(b"alias") {
let (command_spans, rest_spans) = spans.split_at(split_id);
let (usage, extra_usage) = working_set.build_usage(&lite_command.comments);
let original_starting_error_count = working_set.parse_errors.len();
@ -865,6 +864,7 @@ pub fn parse_alias(
output,
..
} = parse_internal_call(working_set, span(command_spans), rest_spans, decl_id);
working_set
.parse_errors
.truncate(original_starting_error_count);
@ -1010,6 +1010,27 @@ pub fn parse_alias(
}
};
// Tries to build a useful usage string
let (usage, extra_usage) = match lite_command.comments.is_empty() {
// First from comments, if any are present
false => working_set.build_usage(&lite_command.comments),
// Then from the command itself
true => match alias_call.arguments.get(1) {
Some(Argument::Positional(Expression {
expr: Expr::Keyword(.., expr),
..
})) => {
let aliased = working_set.get_span_contents(expr.span);
(
format!("Alias for `{}`", String::from_utf8_lossy(aliased)),
String::new(),
)
}
// Then with a default.
_ => ("User declared alias".into(), String::new()),
},
};
let decl = Alias {
name: alias_name,
command,