Skip to content

Commit

Permalink
Fix usage parsing for commands defined in CRLF (windows) files (#13212)
Browse files Browse the repository at this point in the history
Fixes #13207

# Description
This fixes the parsing of command usage when that command comes from a
file with CRLF line endings.

See #13207 for more details.

# User-Facing Changes
Users on Windows will get correct autocompletion for `std` commands.
  • Loading branch information
weirdan committed Jun 23, 2024
1 parent 9b7f899 commit 4509944
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion crates/nu-protocol/src/engine/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ pub(super) fn build_usage(comment_lines: &[&[u8]]) -> (String, String) {
usage.push_str(&comment_line);
}

if let Some((brief_usage, extra_usage)) = usage.split_once("\n\n") {
if let Some((brief_usage, extra_usage)) = usage.split_once("\r\n\r\n") {
(brief_usage.to_string(), extra_usage.to_string())
} else if let Some((brief_usage, extra_usage)) = usage.split_once("\n\n") {
(brief_usage.to_string(), extra_usage.to_string())
} else {
(usage, String::default())
Expand Down
16 changes: 16 additions & 0 deletions tests/repl/test_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,22 @@ fn commands_have_usage() -> TestResult {
)
}

#[test]
fn commands_from_crlf_source_have_short_usage() -> TestResult {
run_test_contains(
"# This is a test\r\n#\r\n# To see if I have cool usage\r\ndef foo [] {}\r\nscope commands | where name == foo | get usage.0",
"This is a test",
)
}

#[test]
fn commands_from_crlf_source_have_extra_usage() -> TestResult {
run_test_contains(
"# This is a test\r\n#\r\n# To see if I have cool usage\r\ndef foo [] {}\r\nscope commands | where name == foo | get extra_usage.0",
"To see if I have cool usage",
)
}

#[test]
fn equals_separates_long_flag() -> TestResult {
run_test(
Expand Down

0 comments on commit 4509944

Please sign in to comment.