Skip to content

Commit

Permalink
Expand tilde after brace expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
krobelus committed Jul 23, 2024
1 parent 4e0cb5d commit c3cf379
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ fn unescape_string_internal(input: &wstr, flags: UnescapeFlags) -> Option<WStrin
// We only read braces as expanders if there's a variable expansion or "," in them.
let mut vars_or_seps = vec![];
let mut brace_count = 0;
let mut potential_word_start = None;

let mut errored = false;
#[derive(PartialEq, Eq)]
Expand Down Expand Up @@ -554,7 +555,9 @@ fn unescape_string_internal(input: &wstr, flags: UnescapeFlags) -> Option<WStrin
}
}
'~' => {
if unescape_special && input_position == 0 {
if unescape_special
&& (input_position == 0 || Some(input_position) == potential_word_start)
{
to_append_or_none = Some(HOME_DIRECTORY);
}
}
Expand Down Expand Up @@ -605,6 +608,7 @@ fn unescape_string_internal(input: &wstr, flags: UnescapeFlags) -> Option<WStrin
to_append_or_none = Some(BRACE_BEGIN);
// We need to store where the brace *ends up* in the output.
braces.push(result.len());
potential_word_start = Some(input_position + 1);
}
}
'}' => {
Expand Down Expand Up @@ -645,6 +649,7 @@ fn unescape_string_internal(input: &wstr, flags: UnescapeFlags) -> Option<WStrin
if unescape_special && brace_count > 0 {
to_append_or_none = Some(BRACE_SEP);
vars_or_seps.push(input_position);
potential_word_start = Some(input_position + 1);
}
}
' ' => {
Expand Down
6 changes: 3 additions & 3 deletions src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,9 @@ fn get_home_directory_name<'a>(input: &'a wstr, out_tail_idx: &mut usize) -> &'a

/// Attempts tilde expansion of the string specified, modifying it in place.
fn expand_home_directory(input: &mut WString, vars: &dyn Environment) {
if input.as_char_slice().first() != Some(&HOME_DIRECTORY) {
let starts_with_tilde = input.as_char_slice().first() == Some(&HOME_DIRECTORY);
*input = input.replace([HOME_DIRECTORY], L!("~"));
if !starts_with_tilde {
return;
}

Expand Down Expand Up @@ -1171,8 +1173,6 @@ fn expand_home_directory(input: &mut WString, vars: &dyn Environment) {

if let Some(home) = home {
input.replace_range(..tail_idx, &normalize_path(&home, true));
} else {
input.replace_range(0..1, L!("~"));
}
}

Expand Down
7 changes: 7 additions & 0 deletions tests/checks/expansion.fish
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,10 @@ echo foo | $pager
#CHECKERR: checks/expansion.fish (line 339): The expanded command is a keyword.
#CHECKERR: echo foo | $pager
#CHECKERR: ^~~~~^

echo {~,asdf}
# CHECK: /{{.*}} asdf
echo {asdf,~}
# CHECK: asdf /{{.*}}
echo {~}
# CHECK: {~}

0 comments on commit c3cf379

Please sign in to comment.