-
Notifications
You must be signed in to change notification settings - Fork 4
✨ feat(repl): add /edit command to open external editor #1044
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||||||||||||||||||
| use std::{fmt, fs}; | ||||||||||||||||||||||
| use std::{fmt, fs, io::Write, process::Command as ProcessCommand}; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| #[cfg(all(feature = "clipboard", not(target_os = "android")))] | ||||||||||||||||||||||
| use arboard::Clipboard; | ||||||||||||||||||||||
|
|
@@ -15,6 +15,7 @@ pub enum CommandOutput { | |||||||||||||||||||||
| #[derive(Debug, Clone, strum::EnumIter)] | ||||||||||||||||||||||
| pub enum Command { | ||||||||||||||||||||||
| Copy, | ||||||||||||||||||||||
| Edit, | ||||||||||||||||||||||
| Env(String, String), | ||||||||||||||||||||||
| Help, | ||||||||||||||||||||||
| Quit, | ||||||||||||||||||||||
|
|
@@ -29,6 +30,7 @@ impl fmt::Display for Command { | |||||||||||||||||||||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||||||||||||||||||||||
| match self { | ||||||||||||||||||||||
| Command::Copy => write!(f, "/copy"), | ||||||||||||||||||||||
| Command::Edit => write!(f, "/edit"), | ||||||||||||||||||||||
| Command::Env(_, _) => { | ||||||||||||||||||||||
| write!(f, "/env") | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
@@ -47,6 +49,7 @@ impl Command { | |||||||||||||||||||||
| pub fn help(&self) -> String { | ||||||||||||||||||||||
| match self { | ||||||||||||||||||||||
| Command::Copy => format!("{:<12}{}", "/copy", "Copy the execution results to the clipboard"), | ||||||||||||||||||||||
| Command::Edit => format!("{:<12}{}", "/edit", "Edit the current buffer in external editor"), | ||||||||||||||||||||||
|
||||||||||||||||||||||
| Command::Env(_, _) => { | ||||||||||||||||||||||
| format!("{:<12}{}", "/env", "Set environment variables (key value)") | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
@@ -65,6 +68,7 @@ impl From<String> for Command { | |||||||||||||||||||||
| fn from(s: String) -> Self { | ||||||||||||||||||||||
| match s.as_str().split_whitespace().collect::<Vec<&str>>().as_slice() { | ||||||||||||||||||||||
| ["/copy"] => Command::Copy, | ||||||||||||||||||||||
| ["/edit"] => Command::Edit, | ||||||||||||||||||||||
| ["/env", name, value] => Command::Env(name.to_string(), value.to_string()), | ||||||||||||||||||||||
| ["/help"] => Command::Help, | ||||||||||||||||||||||
| ["/quit"] => Command::Quit, | ||||||||||||||||||||||
|
|
@@ -135,6 +139,57 @@ impl CommandContext { | |||||||||||||||||||||
| Err(miette!("Clipboard functionality is not available on this platform")) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| Command::Edit => { | ||||||||||||||||||||||
| // Get editor from environment variables | ||||||||||||||||||||||
| let editor = std::env::var("EDITOR") | ||||||||||||||||||||||
| .or_else(|_| std::env::var("VISUAL")) | ||||||||||||||||||||||
| .unwrap_or_else(|_| "vi".to_string()); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Create a temporary file | ||||||||||||||||||||||
| let mut temp_file = tempfile::Builder::new() | ||||||||||||||||||||||
| .prefix("mq-edit-") | ||||||||||||||||||||||
| .suffix(".mq") | ||||||||||||||||||||||
| .tempfile() | ||||||||||||||||||||||
| .into_diagnostic()?; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Write current buffer to temp file (empty for now) | ||||||||||||||||||||||
harehare marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
| temp_file.write_all(b"").into_diagnostic()?; | ||||||||||||||||||||||
| temp_file.flush().into_diagnostic()?; | ||||||||||||||||||||||
|
Comment on lines
+155
to
+157
|
||||||||||||||||||||||
| // Write current buffer to temp file (empty for now) | |
| temp_file.write_all(b"").into_diagnostic()?; | |
| temp_file.flush().into_diagnostic()?; | |
| // Start with an empty temp file; the user will provide code in the editor. |
Copilot
AI
Jan 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Silently ignoring cleanup errors with .ok() could hide issues. Consider logging a warning if cleanup fails, or use a comment explaining why failures are acceptable here.
Copilot
AI
Jan 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The .ok() silently ignores any cleanup errors. Consider logging a warning if cleanup fails, as this could lead to accumulation of temporary files over time, especially if the REPL is used in long-running sessions.
| // Clean up temp file | |
| fs::remove_file(&temp_path).ok(); | |
| // Clean up temp file (best-effort; log on failure) | |
| if let Err(err) = fs::remove_file(&temp_path) { | |
| eprintln!( | |
| "warning: failed to remove temporary file {}: {}", | |
| temp_path.display(), | |
| err | |
| ); | |
| } |
Copilot
AI
Jan 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When evaluation fails, the error is propagated without additional context about the source (external editor). Consider wrapping the error with context like 'Failed to evaluate code from external editor' to help users understand where the error originated.
Uh oh!
There was an error while loading. Please reload this page.