Skip to content

Commit 5704a1c

Browse files
committed
update the diff gutter when changing diff source config or saving
1 parent d7db3bf commit 5704a1c

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

helix-term/src/application.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use helix_lsp::{
1313
use helix_view::{
1414
align_view,
1515
document::DocumentSavedEventResult,
16-
editor::{ConfigEvent, EditorEvent},
16+
editor::{ConfigEvent, DiffSource, EditorEvent},
1717
graphics::Rect,
1818
theme,
1919
tree::Layout,
@@ -359,11 +359,15 @@ impl Application {
359359
// the Application can apply it.
360360
ConfigEvent::Update(editor_config) => {
361361
let mut app_config = (*self.config.load().clone()).clone();
362+
let update_diff_base = app_config.editor.diff_source != editor_config.diff_source;
362363
app_config.editor = *editor_config;
363364
if let Err(err) = self.terminal.reconfigure(app_config.editor.clone().into()) {
364365
self.editor.set_error(err.to_string());
365366
};
366367
self.config.store(Arc::new(app_config));
368+
if update_diff_base {
369+
self.editor.update_diff_base();
370+
}
367371
}
368372
}
369373

@@ -565,6 +569,18 @@ impl Application {
565569
self.editor.refresh_language_servers(id);
566570
}
567571

572+
if self.editor.config().diff_source == DiffSource::File {
573+
// borrowing the same doc again to get around the borrow checker
574+
let doc = doc_mut!(self.editor, &doc_save_event.doc_id);
575+
if let Some(path) = doc.path().cloned() {
576+
doc.update_diff_base(
577+
diff_provider!(self.editor),
578+
&path,
579+
self.editor.redraw_handle.clone(),
580+
);
581+
}
582+
}
583+
568584
// TODO: fix being overwritten by lsp
569585
self.editor.set_status(format!(
570586
"'{}' written, {}L {}B",

helix-view/src/document.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,17 +1023,7 @@ impl Document {
10231023

10241024
self.detect_indent_and_line_ending();
10251025

1026-
match diff_provider.and_then(|provider| match provider.get_diff_base(&path) {
1027-
Ok(res) => Some(res),
1028-
Err(err) => {
1029-
log::info!("{err:#?}");
1030-
log::info!("failed to open diff base for {}", path.display());
1031-
None
1032-
}
1033-
}) {
1034-
Some(diff_base) => self.set_diff_base(diff_base, redraw_handle),
1035-
None => self.diff_handle = None,
1036-
}
1026+
self.update_diff_base(diff_provider, &path, redraw_handle);
10371027

10381028
self.version_control_head = vcs_registry
10391029
.root_for_file(&path)
@@ -1593,7 +1583,26 @@ impl Document {
15931583
}
15941584

15951585
/// Intialize/updates the differ for this document with a new base.
1596-
pub fn set_diff_base(&mut self, diff_base: Vec<u8>, redraw_handle: RedrawHandle) {
1586+
pub fn update_diff_base(
1587+
&mut self,
1588+
diff_provider: Option<&dyn DiffProvider>,
1589+
path: &Path,
1590+
redraw_handle: RedrawHandle,
1591+
) {
1592+
match diff_provider.and_then(|provider| match provider.get_diff_base(path) {
1593+
Ok(res) => Some(res),
1594+
Err(err) => {
1595+
log::info!("{err:#?}");
1596+
log::info!("failed to open diff base for {}", path.display());
1597+
None
1598+
}
1599+
}) {
1600+
Some(diff_base) => self.set_diff_base(diff_base, redraw_handle),
1601+
None => self.diff_handle = None,
1602+
}
1603+
}
1604+
1605+
fn set_diff_base(&mut self, diff_base: Vec<u8>, redraw_handle: RedrawHandle) {
15971606
if let Ok((diff_base, ..)) = from_reader(&mut diff_base.as_slice(), Some(self.encoding)) {
15981607
if let Some(differ) = &self.diff_handle {
15991608
differ.update_diff_base(diff_base);

helix-view/src/editor.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,18 +1468,7 @@ impl Editor {
14681468
self.config.clone(),
14691469
)?;
14701470

1471-
if let Some(diff_base) = diff_provider!(self).as_ref().and_then(|provider| {
1472-
match provider.get_diff_base(&path) {
1473-
Ok(res) => Some(res),
1474-
Err(err) => {
1475-
log::info!("{err:#?}");
1476-
log::info!("failed to open diff base for {}", path.display());
1477-
None
1478-
}
1479-
}
1480-
}) {
1481-
doc.set_diff_base(diff_base, self.redraw_handle.clone());
1482-
}
1471+
doc.update_diff_base(diff_provider!(self), &path, self.redraw_handle.clone());
14831472
doc.set_version_control_head(
14841473
self.vcs_providers
14851474
.root_for_file(&path)
@@ -1856,6 +1845,14 @@ impl Editor {
18561845
.as_ref()
18571846
.and_then(|debugger| debugger.current_stack_frame())
18581847
}
1848+
1849+
pub fn update_diff_base(&mut self) {
1850+
for doc in self.documents.values_mut() {
1851+
if let Some(path) = doc.path().cloned() {
1852+
doc.update_diff_base(diff_provider!(self), &path, self.redraw_handle.clone());
1853+
}
1854+
}
1855+
}
18591856
}
18601857

18611858
fn try_restore_indent(doc: &mut Document, view: &mut View) {

0 commit comments

Comments
 (0)