From 03015ed33fd95c0df9250a284e72fd0d0c971b5f Mon Sep 17 00:00:00 2001 From: Alex Ionescu Date: Tue, 29 Oct 2024 14:08:55 +0100 Subject: [PATCH] Show `?` for optional entries when displaying `CellPath`s (#14042) # Description This PR makes the `Display` implementation for `CellPath` show a `?` suffix on every optional entry, which makes the output consistent with the language syntax. Before this PR, the printing of cell paths was confusing, e.g. `$.x` and `$.x?` were both printed as `x`. Now, the second one is printed as `x?`. # User-Facing Changes The formatting of cell paths now matches the syntax used to create them, reducing confusion. # Tests + Formatting All tests pass, including `stdlib` tests. # After Submitting --- crates/nu-command/tests/commands/select.rs | 11 +++++++---- crates/nu-protocol/src/ast/cell_path.rs | 10 ++++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/crates/nu-command/tests/commands/select.rs b/crates/nu-command/tests/commands/select.rs index 004aa16d6b..f47845d6ed 100644 --- a/crates/nu-command/tests/commands/select.rs +++ b/crates/nu-command/tests/commands/select.rs @@ -166,7 +166,7 @@ fn select_ignores_errors_successfully1() { fn select_ignores_errors_successfully2() { let actual = nu!("[{a: 1} {a: 2} {a: 3}] | select b? | to nuon"); - assert_eq!(actual.out, "[[b]; [null], [null], [null]]".to_string()); + assert_eq!(actual.out, "[[b?]; [null], [null], [null]]".to_string()); assert!(actual.err.is_empty()); } @@ -174,7 +174,7 @@ fn select_ignores_errors_successfully2() { fn select_ignores_errors_successfully3() { let actual = nu!("{foo: bar} | select invalid_key? | to nuon"); - assert_eq!(actual.out, "{invalid_key: null}".to_string()); + assert_eq!(actual.out, "{invalid_key?: null}".to_string()); assert!(actual.err.is_empty()); } @@ -184,7 +184,10 @@ fn select_ignores_errors_successfully4() { r#""key val\na 1\nb 2\n" | lines | split column --collapse-empty " " | select foo? | to nuon"# ); - assert_eq!(actual.out, r#"[[foo]; [null], [null], [null]]"#.to_string()); + assert_eq!( + actual.out, + r#"[[foo?]; [null], [null], [null]]"#.to_string() + ); assert!(actual.err.is_empty()); } @@ -234,7 +237,7 @@ fn ignore_errors_works() { [{}] | select -i $path | to nuon "#); - assert_eq!(actual.out, "[[foo]; [null]]"); + assert_eq!(actual.out, "[[foo?]; [null]]"); } #[test] diff --git a/crates/nu-protocol/src/ast/cell_path.rs b/crates/nu-protocol/src/ast/cell_path.rs index 0880537557..f208dd8120 100644 --- a/crates/nu-protocol/src/ast/cell_path.rs +++ b/crates/nu-protocol/src/ast/cell_path.rs @@ -182,8 +182,14 @@ impl Display for CellPath { write!(f, ".")?; } match elem { - PathMember::Int { val, .. } => write!(f, "{val}")?, - PathMember::String { val, .. } => write!(f, "{val}")?, + PathMember::Int { val, optional, .. } => { + let question_mark = if *optional { "?" } else { "" }; + write!(f, "{val}{question_mark}")? + } + PathMember::String { val, optional, .. } => { + let question_mark = if *optional { "?" } else { "" }; + write!(f, "{val}{question_mark}")? + } } } Ok(())