From 473eb23e2853a17234ffcabe29b3d03c87b47025 Mon Sep 17 00:00:00 2001 From: Evan Mesterhazy Date: Sun, 5 May 2024 07:32:12 -0400 Subject: [PATCH] Hide context around collapsed editable sections This makes the file view tidier by hiding context sections which aren't contiguous to at least editable section that is expanded. --- src/ui.rs | 21 +++++++- tests/test_scm_record.rs | 105 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 122 insertions(+), 4 deletions(-) diff --git a/src/ui.rs b/src/ui.rs index 1692a54..c2fdbe8 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -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 = 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(); diff --git a/tests/test_scm_record.rs b/tests/test_scm_record.rs index 4db5bbf..76e2693 100644 --- a/tests/test_scm_record.rs +++ b/tests/test_scm_record.rs @@ -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 (-)" " ⋮ " @@ -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(()) }