Skip to content

Commit 8619c07

Browse files
Open editor in diff view (#2904)
1 parent 7c538e3 commit 8619c07

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Changed
1111
* use [tombi](https://github.com/tombi-toml/tombi) for all toml file formatting
12+
* open the external editor from the status diff view [[@WaterWhisperer](https://github.com/WaterWhisperer)] ([#2805](https://github.com/gitui-org/gitui/issues/2805))
1213

1314
### Fixes
1415
* crash when opening submodule ([#2895](https://github.com/gitui-org/gitui/issues/2895))

src/components/diff.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ impl DiffComponent {
152152
(self.current.path.clone(), self.current.is_stage)
153153
}
154154
///
155+
const fn can_edit_file(&self) -> bool {
156+
!self.is_immutable && !self.current.path.is_empty()
157+
}
158+
///
155159
pub fn clear(&mut self, pending: bool) {
156160
self.current = Current::default();
157161
self.diff = None;
@@ -770,6 +774,11 @@ impl Component for DiffComponent {
770774
);
771775

772776
if !self.is_immutable {
777+
out.push(CommandInfo::new(
778+
strings::commands::edit_item(&self.key_config),
779+
self.can_edit_file(),
780+
self.focused() && self.can_edit_file(),
781+
));
773782
out.push(CommandInfo::new(
774783
strings::commands::diff_hunk_remove(&self.key_config),
775784
self.selected_hunk.is_some(),
@@ -876,6 +885,15 @@ impl Component for DiffComponent {
876885
) {
877886
self.diff_hunk_move_up_down(-1);
878887
Ok(EventState::Consumed)
888+
} else if key_match(e, self.key_config.keys.edit_file)
889+
&& self.can_edit_file()
890+
{
891+
self.queue.push(
892+
InternalEvent::OpenExternalEditor(Some(
893+
self.current.path.clone(),
894+
)),
895+
);
896+
Ok(EventState::Consumed)
879897
} else if key_match(
880898
e,
881899
self.key_config.keys.stage_unstage_item,
@@ -945,7 +963,10 @@ impl Component for DiffComponent {
945963
#[cfg(test)]
946964
mod tests {
947965
use super::*;
948-
use crate::ui::style::Theme;
966+
use crate::{
967+
app::Environment, queue::InternalEvent, ui::style::Theme,
968+
};
969+
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
949970
use std::io::Write;
950971
use std::rc::Rc;
951972
use tempfile::NamedTempFile;
@@ -1012,4 +1033,30 @@ mod tests {
10121033
);
10131034
}
10141035
}
1036+
1037+
#[test]
1038+
fn diff_component_opens_editor_for_current_file() {
1039+
let env = Environment::test_env();
1040+
let mut diff = DiffComponent::new(&env, false);
1041+
1042+
diff.focus(true);
1043+
diff.current.path = String::from("src/main.rs");
1044+
1045+
let event = Event::Key(KeyEvent::new(
1046+
KeyCode::Char('e'),
1047+
KeyModifiers::empty(),
1048+
));
1049+
1050+
assert!(matches!(
1051+
diff.event(&event).unwrap(),
1052+
EventState::Consumed
1053+
));
1054+
1055+
let event = env.queue.pop();
1056+
assert!(matches!(
1057+
event,
1058+
Some(InternalEvent::OpenExternalEditor(Some(path)))
1059+
if path == "src/main.rs"
1060+
));
1061+
}
10151062
}

0 commit comments

Comments
 (0)