Skip to content

Commit

Permalink
Hide context around collapsed editable sections
Browse files Browse the repository at this point in the history
This makes the file view tidier by hiding context sections which aren't
contiguous to at least editable section that is expanded.
  • Loading branch information
emesterhazy committed May 9, 2024
1 parent 62fa435 commit 473eb23
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 4 deletions.
21 changes: 20 additions & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2524,7 +2524,26 @@ impl Component for FileView<'_> {
if self.is_expanded() {
let x = x + 2;
let mut section_y = y + file_view_header_rect.height.unwrap_isize();
for section_view in section_views {
let expanded_sections: HashSet<usize> = section_views
.iter()
.enumerate()
.filter_map(|(i, view)| {
if view.is_expanded() && view.section.is_editable() {
return Some(i);
}
None
})
.collect();
for (i, section_view) in section_views.iter().enumerate() {
// Skip this section if it is an un-editable context section and
// none of the editable sections surrounding it are expanded.
let context_section = !section_view.section.is_editable();
let prev_is_collapsed = i == 0 || !expanded_sections.contains(&(i - 1));
let next_is_collapsed = !expanded_sections.contains(&(i + 1));
if context_section && prev_is_collapsed && next_is_collapsed {
continue;
}

let section_rect = viewport.draw_component(x, section_y, section_view);
section_y += section_rect.height.unwrap_isize();

Expand Down
105 changes: 102 additions & 3 deletions tests/test_scm_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,15 +770,34 @@ fn test_abbreviate_unchanged_sections() -> eyre::Result<()> {
}],
};

let screenshot = TestingScreenshot::default();
let initial = TestingScreenshot::default();
let collapse_bottom = TestingScreenshot::default();
let collapse_top = TestingScreenshot::default();
let expand_bottom = TestingScreenshot::default();
let mut input = TestingInput::new(
80,
24,
[Event::ExpandAll, screenshot.event(), Event::QuitAccept],
[
Event::ExpandAll,
initial.event(),
Event::FocusNext,
Event::FocusNext,
Event::FocusNext,
Event::ExpandItem,
collapse_bottom.event(),
Event::FocusPrev,
Event::FocusPrev,
Event::ExpandItem,
collapse_top.event(),
Event::FocusNext,
Event::ExpandItem,
expand_bottom.event(),
Event::QuitAccept,
],
);
let recorder = Recorder::new(state, &mut input);
recorder.run()?;
insta::assert_snapshot!(screenshot, @r###"
insta::assert_snapshot!(initial, @r###"
"[File] [Edit] [Select] [View] "
"( ) foo (-)"
" ⋮ "
Expand All @@ -804,6 +823,86 @@ fn test_abbreviate_unchanged_sections() -> eyre::Result<()> {
" "
" "
"###);
// Unchanged sections are collapsed unless there's at least one changed
// section expanded before or after them.
insta::assert_snapshot!(collapse_bottom, @r###"
"[File] [Edit] [Select] [View] "
"[ ] foo [~]"
" ⋮ "
" 4 start line 4/6⏎ "
" 5 start line 5/6⏎ "
" 6 start line 6/6⏎ "
" [ ] Section 1/2 [-]"
" [ ] + changed⏎ "
" 7 middle line 1/7⏎ "
" 8 middle line 2/7⏎ "
" 9 middle line 3/7⏎ "
" ⋮ "
" 11 middle line 5/7⏎ "
" 12 middle line 6/7⏎ "
" 13 middle line 7/7⏎ "
" ( ) Section 2/2 (+)"
" "
" "
" "
" "
" "
" "
" "
" "
"###);
insta::assert_snapshot!(collapse_top, @r###"
"[File] [Edit] [Select] [View] "
"[ ] foo [~]"
" ( ) Section 1/2 (+)"
" [ ] Section 2/2 [+]"
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
"###);
insta::assert_snapshot!(expand_bottom, @r###"
"[File] [Edit] [Select] [View] "
"[ ] foo [~]"
" [ ] Section 1/2 [+]"
" 7 middle line 1/7⏎ "
" 8 middle line 2/7⏎ "
" 9 middle line 3/7⏎ "
" ⋮ "
" 11 middle line 5/7⏎ "
" 12 middle line 6/7⏎ "
" 13 middle line 7/7⏎ "
" ( ) Section 2/2 (-)"
" [ ] + changed⏎ "
" 14 end line 1/6⏎ "
" 15 end line 2/6⏎ "
" 16 end line 3/6⏎ "
" ⋮ "
" "
" "
" "
" "
" "
" "
" "
" "
"###);

Ok(())
}
Expand Down

0 comments on commit 473eb23

Please sign in to comment.