-
Notifications
You must be signed in to change notification settings - Fork 6.6k
feat: open prompt in configured external editor #7606
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
388e21d
b20290d
f83aa7f
ca9b2a8
4819fbf
f277011
2e3d333
d0a88fa
845c59f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,11 @@ use crate::app_event_sender::AppEventSender; | |
| use crate::bottom_pane::ApprovalRequest; | ||
| use crate::chatwidget::ChatWidget; | ||
| use crate::diff_render::DiffSummary; | ||
| use crate::editor; | ||
| use crate::editor::EditorError; | ||
| use crate::exec_command::strip_bash_lc_and_escape; | ||
| use crate::file_search::FileSearchManager; | ||
| use crate::history_cell; | ||
| use crate::history_cell::HistoryCell; | ||
| use crate::model_migration::ModelMigrationOutcome; | ||
| use crate::model_migration::migration_copy_for_config; | ||
|
|
@@ -1008,6 +1011,70 @@ impl App { | |
| self.config.model_reasoning_effort = effort; | ||
| } | ||
|
|
||
| async fn launch_external_editor(&mut self, tui: &mut tui::Tui) { | ||
| let editor_cmd = match editor::resolve_editor_command() { | ||
| Ok(cmd) => cmd, | ||
| Err(EditorError::MissingEditor) => { | ||
| self.chat_widget | ||
| .add_to_history(history_cell::new_error_event( | ||
| "Cannot open external editor: set $VISUAL or $EDITOR".to_string(), | ||
| )); | ||
| return; | ||
| } | ||
| Err(err) => { | ||
| self.chat_widget | ||
| .add_to_history(history_cell::new_error_event(format!( | ||
| "Failed to open editor: {err}", | ||
| ))); | ||
| return; | ||
| } | ||
| }; | ||
|
|
||
| let seed = self.chat_widget.composer_text_with_pending(); | ||
|
|
||
| // Leave alt screen if active to avoid conflicts with editor. | ||
| // This is defensive as we gate the external editor launch on there being no overlay. | ||
| let was_alt_screen = tui.is_alt_screen_active(); | ||
| if was_alt_screen { | ||
| let _ = tui.leave_alt_screen(); | ||
| } | ||
|
|
||
| let restore_modes = tui::restore(); | ||
| if let Err(err) = restore_modes { | ||
| tracing::warn!("failed to restore terminal modes before editor: {err}"); | ||
| } | ||
|
|
||
| let editor_result = editor::run_editor(&seed, &editor_cmd).await; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does this look like if
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested this, it opens a window as expected, and the text propagates back to the composer on save+close.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does the terminal window look like while the editor is running?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it looks "the same" (just as if you tabbed away from the terminal, so the cursor isn't blinking). if you tab back into the terminal while the
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm... but didn't we disable all our terminal modes? we shouldn't be running the TUI at the same time as the editor is open, I think. e.g. what happens if you end up in an odd state, e.g. an approval dialog is showing, or you're in transcript mode?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also consider what happens if you open a terminal editor like vim while a turn is running.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think this should behave roughly the same as
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
yeah, you can still open + use the editor while an approval dialog is showing, and the results will persist to the prompt editor, but you won't see that until you dismiss the dialog. I think we could easily block opening the external editor while a dialog is displayed.
the
this is allowed, and we block on the terminal editor closing, so new events (like SSE updates) are accumulated but not processed in the main loop. Once you exit the terminal editor, they're processed and the TUI updates. If we switched to the I think we could go either way. Thoughts? |
||
|
|
||
| if let Err(err) = tui::set_modes() { | ||
| tracing::warn!("failed to re-enable terminal modes after editor: {err}"); | ||
| } | ||
|
|
||
| if was_alt_screen { | ||
| let _ = tui.enter_alt_screen(); | ||
| } | ||
|
|
||
| match editor_result { | ||
| Ok(Some(new_text)) => { | ||
| // Trim trailing whitespace | ||
| let cleaned = new_text.trim_end().to_string(); | ||
| self.chat_widget.apply_external_edit(cleaned); | ||
| tui.frame_requester().schedule_frame(); | ||
| } | ||
| Ok(None) => { | ||
| // Empty file clears the composer. | ||
| self.chat_widget.apply_external_edit(String::new()); | ||
| tui.frame_requester().schedule_frame(); | ||
| } | ||
sayan-oai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Err(err) => { | ||
| self.chat_widget | ||
| .add_to_history(history_cell::new_error_event(format!( | ||
| "Failed to open editor: {err}", | ||
| ))); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async fn handle_key_event(&mut self, tui: &mut tui::Tui, key_event: KeyEvent) { | ||
| match key_event { | ||
| KeyEvent { | ||
|
|
@@ -1021,6 +1088,16 @@ impl App { | |
| self.overlay = Some(Overlay::new_transcript(self.transcript_cells.clone())); | ||
| tui.frame_requester().schedule_frame(); | ||
| } | ||
| KeyEvent { | ||
| code: KeyCode::Char('g'), | ||
| modifiers: crossterm::event::KeyModifiers::CONTROL, | ||
| kind: KeyEventKind::Press, | ||
| .. | ||
| } => { | ||
| if self.overlay.is_none() { | ||
| self.launch_external_editor(tui).await; | ||
| } | ||
| } | ||
| // Esc primes/advances backtracking only in normal (not working) mode | ||
| // with the composer focused and empty. In any other state, forward | ||
| // Esc so the active UI (e.g. status indicator, modals, popups) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.