From fc06afd0513372536cfa7f809b0f20cfdfea1920 Mon Sep 17 00:00:00 2001 From: Poliorcetics Date: Mon, 4 Dec 2023 19:56:46 +0100 Subject: [PATCH] feat: Add default docs for aliases, generated from the command they point to (#10825) --- crates/nu-command/tests/commands/alias.rs | 8 ++++++++ crates/nu-parser/src/parse_keywords.rs | 23 ++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/tests/commands/alias.rs b/crates/nu-command/tests/commands/alias.rs index 97af0ccda9..65fbbc1d4f 100644 --- a/crates/nu-command/tests/commands/alias.rs +++ b/crates/nu-command/tests/commands/alias.rs @@ -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'`")); +} diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 8a7ecf8268..ec70292a03 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -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,