Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust get_selected_contents behavior #62

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions scm-record/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,18 @@ impl File<'_> {
/// example, the first value would be suitable for staging or committing,
/// and the second value would be suitable for potentially recording again.
pub fn get_selected_contents(&self) -> (SelectedContents, SelectedContents) {
let mut acc_selected = SelectedContents::Absent;
let mut acc_unselected = SelectedContents::Absent;
let Self {
old_path: _,
path: _,
file_mode: _,
file_mode,
sections,
} = self;
let mut acc_selected = if file_mode.is_none() || file_mode == &Some(FileMode::absent()) {
SelectedContents::Absent
} else {
SelectedContents::Unchanged
};
let mut acc_unselected = SelectedContents::Unchanged;
for section in sections {
match section {
Section::Unchanged { lines } => {
Expand All @@ -303,6 +307,9 @@ impl File<'_> {
}
(ChangeType::Added, false) | (ChangeType::Removed, true) => {
acc_unselected.push_str(line);
if acc_selected == SelectedContents::Unchanged {
acc_selected.push_str("");
}
}
}
}
Expand All @@ -315,7 +322,13 @@ impl File<'_> {
} => {
if *is_checked && after == &FileMode::absent() {
acc_selected = SelectedContents::Absent;
} else if *is_checked && after != &FileMode::absent() {
acc_selected = SelectedContents::Present {
contents: "".to_string(),
};
} else if !is_checked && before == &FileMode::absent() {
acc_selected = SelectedContents::Absent;
} else if !is_checked && after == &FileMode::absent() {
acc_unselected = SelectedContents::Absent;
}
}
Expand All @@ -331,7 +344,6 @@ impl File<'_> {
};
if *is_checked {
acc_selected = selected_contents;
acc_unselected = SelectedContents::Unchanged;
} else {
acc_selected = SelectedContents::Unchanged;
acc_unselected = selected_contents;
Expand Down
2 changes: 1 addition & 1 deletion scm-record/tests/test_scm_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,7 @@ fn test_state_binary_selected_contents() -> TestResult {
// the UI to never allow selecting both).
assert_snapshot!(test(false, true), @r###"(Binary { old_description: Some("abc123 (123 bytes)"), new_description: Some("def456 (456 bytes)") }, Unchanged)"###);

assert_snapshot!(test(true, true), @r###"(Binary { old_description: Some("abc123 (123 bytes)"), new_description: Some("def456 (456 bytes)") }, Unchanged)"###);
assert_snapshot!(test(true, true), @r###"(Binary { old_description: Some("abc123 (123 bytes)"), new_description: Some("def456 (456 bytes)") }, Present { contents: "foo\n" })"###);

Ok(())
}
Expand Down
Loading