fix(cli): edit cwd (#2201)

* fix(cli): properly set cwd for edit panes

* fix(layouts): properly set cwd for edit panes

* style(fmt): rustfmt
This commit is contained in:
Aram Drevekenin 2023-02-27 16:34:31 +01:00 committed by GitHub
parent d20720f443
commit 149cafc6d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 71 additions and 30 deletions

View file

@ -277,7 +277,12 @@ fn spawn_terminal(
// secondary fd // secondary fd
let mut failover_cmd_args = None; let mut failover_cmd_args = None;
let cmd = match terminal_action { let cmd = match terminal_action {
TerminalAction::OpenFile(file_to_open, line_number) => { TerminalAction::OpenFile(mut file_to_open, line_number, cwd) => {
if file_to_open.is_relative() {
if let Some(cwd) = cwd.as_ref() {
file_to_open = cwd.join(file_to_open);
}
}
let mut command = default_editor.unwrap_or_else(|| { let mut command = default_editor.unwrap_or_else(|| {
PathBuf::from( PathBuf::from(
env::var("EDITOR") env::var("EDITOR")
@ -318,7 +323,7 @@ fn spawn_terminal(
RunCommand { RunCommand {
command, command,
args, args,
cwd: None, cwd,
hold_on_close: false, hold_on_close: false,
hold_on_start: false, hold_on_start: false,
} }

View file

@ -691,7 +691,7 @@ fn host_open_file(plugin_env: &PluginEnv) {
plugin_env plugin_env
.senders .senders
.send_to_pty(PtyInstruction::SpawnTerminal( .send_to_pty(PtyInstruction::SpawnTerminal(
Some(TerminalAction::OpenFile(path, None)), Some(TerminalAction::OpenFile(path, None, None)),
None, None,
None, None,
ClientOrTabIndex::TabIndex(plugin_env.tab_index), ClientOrTabIndex::TabIndex(plugin_env.tab_index),

View file

@ -166,7 +166,7 @@ pub(crate) fn pty_thread_main(mut pty: Pty, layout: Box<Layout>) -> Result<()> {
|| format!("failed to open in-place editor for client {}", client_id); || format!("failed to open in-place editor for client {}", client_id);
match pty.spawn_terminal( match pty.spawn_terminal(
Some(TerminalAction::OpenFile(temp_file, line_number)), Some(TerminalAction::OpenFile(temp_file, line_number, None)),
ClientOrTabIndex::ClientId(client_id), ClientOrTabIndex::ClientId(client_id),
) { ) {
Ok((pid, _starts_held)) => { Ok((pid, _starts_held)) => {
@ -822,7 +822,7 @@ impl Pty {
}, },
} }
}, },
Some(Run::EditFile(path_to_file, line_number)) => { Some(Run::EditFile(path_to_file, line_number, cwd)) => {
let starts_held = false; // we do not hold edit panes (for now?) let starts_held = false; // we do not hold edit panes (for now?)
match self match self
.bus .bus
@ -831,7 +831,7 @@ impl Pty {
.context("no OS I/O interface found") .context("no OS I/O interface found")
.with_context(err_context)? .with_context(err_context)?
.spawn_terminal( .spawn_terminal(
TerminalAction::OpenFile(path_to_file, line_number), TerminalAction::OpenFile(path_to_file, line_number, cwd),
quit_cb, quit_cb,
self.default_editor.clone(), self.default_editor.clone(),
) )

View file

@ -284,9 +284,9 @@ pub(crate) fn route_action(
.send_to_pty(pty_instr) .send_to_pty(pty_instr)
.with_context(err_context)?; .with_context(err_context)?;
}, },
Action::EditFile(path_to_file, line_number, split_direction, should_float) => { Action::EditFile(path_to_file, line_number, cwd, split_direction, should_float) => {
let title = format!("Editing: {}", path_to_file.display()); let title = format!("Editing: {}", path_to_file.display());
let open_file = TerminalAction::OpenFile(path_to_file, line_number); let open_file = TerminalAction::OpenFile(path_to_file, line_number, cwd);
let pty_instr = match (split_direction, should_float) { let pty_instr = match (split_direction, should_float) {
(Some(Direction::Left), false) => { (Some(Direction::Left), false) => {
PtyInstruction::SpawnTerminalVertically(Some(open_file), Some(title), client_id) PtyInstruction::SpawnTerminalVertically(Some(open_file), Some(title), client_id)

View file

@ -1,6 +1,6 @@
--- ---
source: zellij-server/src/./unit/screen_tests.rs source: zellij-server/src/./unit/screen_tests.rs
assertion_line: 1944 assertion_line: 2102
expression: "format!(\"{:?}\", * received_pty_instructions.lock().unwrap())" expression: "format!(\"{:?}\", * received_pty_instructions.lock().unwrap())"
--- ---
[SpawnTerminal(Some(OpenFile("/file/to/edit", None)), Some(false), Some("Editing: /file/to/edit"), ClientId(10)), UpdateActivePane(Some(Terminal(0)), 1), UpdateActivePane(Some(Terminal(0)), 1), Exit] [SpawnTerminal(Some(OpenFile("/file/to/edit", None, Some("."))), Some(false), Some("Editing: /file/to/edit"), ClientId(10)), UpdateActivePane(Some(Terminal(0)), 1), UpdateActivePane(Some(Terminal(0)), 1), Exit]

View file

@ -1,6 +1,6 @@
--- ---
source: zellij-server/src/./unit/screen_tests.rs source: zellij-server/src/./unit/screen_tests.rs
assertion_line: 1989 assertion_line: 2140
expression: "format!(\"{:?}\", * received_pty_instructions.lock().unwrap())" expression: "format!(\"{:?}\", * received_pty_instructions.lock().unwrap())"
--- ---
[SpawnTerminal(Some(OpenFile("/file/to/edit", Some(100))), Some(false), Some("Editing: /file/to/edit"), ClientId(10)), UpdateActivePane(Some(Terminal(0)), 1), UpdateActivePane(Some(Terminal(0)), 1), Exit] [SpawnTerminal(Some(OpenFile("/file/to/edit", Some(100), Some("."))), Some(false), Some("Editing: /file/to/edit"), ClientId(10)), UpdateActivePane(Some(Terminal(0)), 1), UpdateActivePane(Some(Terminal(0)), 1), Exit]

View file

@ -1,6 +1,6 @@
--- ---
source: zellij-server/src/./unit/screen_tests.rs source: zellij-server/src/./unit/screen_tests.rs
assertion_line: 2018 assertion_line: 2178
expression: "format!(\"{:?}\", * received_pty_instructions.lock().unwrap())" expression: "format!(\"{:?}\", * received_pty_instructions.lock().unwrap())"
--- ---
[SpawnTerminalHorizontally(Some(OpenFile("/file/to/edit", None)), Some("Editing: /file/to/edit"), 10), UpdateActivePane(Some(Terminal(0)), 1), UpdateActivePane(Some(Terminal(0)), 1), Exit] [SpawnTerminalHorizontally(Some(OpenFile("/file/to/edit", None, Some("."))), Some("Editing: /file/to/edit"), 10), UpdateActivePane(Some(Terminal(0)), 1), UpdateActivePane(Some(Terminal(0)), 1), Exit]

View file

@ -353,7 +353,7 @@ pub enum CliAction {
layout: Option<PathBuf>, layout: Option<PathBuf>,
/// Default folder to look for layouts /// Default folder to look for layouts
#[clap(short, long, value_parser, requires("layout"))] #[clap(long, value_parser, requires("layout"))]
layout_dir: Option<PathBuf>, layout_dir: Option<PathBuf>,
/// Name of the new tab /// Name of the new tab

View file

@ -152,7 +152,13 @@ pub enum Action {
/// If no direction is specified, will try to use the biggest available space. /// If no direction is specified, will try to use the biggest available space.
NewPane(Option<Direction>, Option<String>), // String is an optional pane name NewPane(Option<Direction>, Option<String>), // String is an optional pane name
/// Open the file in a new pane using the default editor /// Open the file in a new pane using the default editor
EditFile(PathBuf, Option<usize>, Option<Direction>, bool), // usize is an optional line number, bool is floating true/false EditFile(
PathBuf,
Option<usize>,
Option<PathBuf>,
Option<Direction>,
bool,
), // usize is an optional line number, Option<PathBuf> is an optional cwd, bool is floating true/false
/// Open a new floating pane /// Open a new floating pane
NewFloatingPane(Option<RunCommandAction>, Option<String>), // String is an optional pane name NewFloatingPane(Option<RunCommandAction>, Option<String>), // String is an optional pane name
/// Open a new tiled (embedded, non-floating) pane /// Open a new tiled (embedded, non-floating) pane
@ -317,13 +323,14 @@ impl Action {
.map(|cwd| current_dir.join(cwd)) .map(|cwd| current_dir.join(cwd))
.or_else(|| Some(current_dir)); .or_else(|| Some(current_dir));
if file.is_relative() { if file.is_relative() {
if let Some(cwd) = cwd { if let Some(cwd) = cwd.as_ref() {
file = cwd.join(file); file = cwd.join(file);
} }
} }
Ok(vec![Action::EditFile( Ok(vec![Action::EditFile(
file, file,
line_number, line_number,
cwd,
direction, direction,
floating, floating,
)]) )])

View file

@ -5,7 +5,8 @@ use std::path::PathBuf;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum TerminalAction { pub enum TerminalAction {
OpenFile(PathBuf, Option<usize>), // path to file and optional line_number OpenFile(PathBuf, Option<usize>, Option<PathBuf>), // path to file (should be absolute), optional line_number and an
// optional cwd
RunCommand(RunCommand), RunCommand(RunCommand),
} }

View file

@ -73,7 +73,7 @@ pub enum Run {
Plugin(RunPlugin), Plugin(RunPlugin),
#[serde(rename = "command")] #[serde(rename = "command")]
Command(RunCommand), Command(RunCommand),
EditFile(PathBuf, Option<usize>), // TODO: merge this with TerminalAction::OpenFile EditFile(PathBuf, Option<usize>, Option<PathBuf>), // TODO: merge this with TerminalAction::OpenFile
Cwd(PathBuf), Cwd(PathBuf),
} }
@ -108,13 +108,26 @@ impl Run {
}, },
( (
Some(Run::Command(base_run_command)), Some(Run::Command(base_run_command)),
Some(Run::EditFile(file_to_edit, line_number)), Some(Run::EditFile(file_to_edit, line_number, edit_cwd)),
) => match &base_run_command.cwd { ) => match &base_run_command.cwd {
Some(cwd) => Some(Run::EditFile(cwd.join(&file_to_edit), *line_number)), Some(cwd) => Some(Run::EditFile(
None => Some(Run::EditFile(file_to_edit.clone(), *line_number)), cwd.join(&file_to_edit),
*line_number,
Some(cwd.join(edit_cwd.clone().unwrap_or_default())),
)),
None => Some(Run::EditFile(
file_to_edit.clone(),
*line_number,
edit_cwd.clone(),
)),
}, },
(Some(Run::Cwd(cwd)), Some(Run::EditFile(file_to_edit, line_number))) => { (Some(Run::Cwd(cwd)), Some(Run::EditFile(file_to_edit, line_number, edit_cwd))) => {
Some(Run::EditFile(cwd.join(&file_to_edit), *line_number)) let cwd = edit_cwd.clone().unwrap_or(cwd.clone());
Some(Run::EditFile(
cwd.join(&file_to_edit),
*line_number,
Some(cwd),
))
}, },
(Some(_base), Some(other)) => Some(other.clone()), (Some(_base), Some(other)) => Some(other.clone()),
(Some(base), _) => Some(base.clone()), (Some(base), _) => Some(base.clone()),
@ -132,7 +145,15 @@ impl Run {
run_command.cwd = Some(cwd.clone()); run_command.cwd = Some(cwd.clone());
}, },
}, },
Run::EditFile(path_to_file, _line_number) => { Run::EditFile(path_to_file, _line_number, edit_cwd) => {
match edit_cwd.as_mut() {
Some(edit_cwd) => {
*edit_cwd = cwd.join(&edit_cwd);
},
None => {
let _ = edit_cwd.insert(cwd.clone());
},
};
*path_to_file = cwd.join(&path_to_file); *path_to_file = cwd.join(&path_to_file);
}, },
Run::Cwd(path) => { Run::Cwd(path) => {

View file

@ -1,6 +1,6 @@
--- ---
source: zellij-utils/src/input/./unit/layout_test.rs source: zellij-utils/src/input/./unit/layout_test.rs
assertion_line: 1558 assertion_line: 1614
expression: "format!(\"{:#?}\", layout)" expression: "format!(\"{:#?}\", layout)"
--- ---
Layout { Layout {
@ -21,6 +21,9 @@ Layout {
EditFile( EditFile(
"/tmp/foo/bar", "/tmp/foo/bar",
None, None,
Some(
"/tmp/foo",
),
), ),
), ),
borderless: false, borderless: false,

View file

@ -1,6 +1,6 @@
--- ---
source: zellij-utils/src/input/./unit/layout_test.rs source: zellij-utils/src/input/./unit/layout_test.rs
assertion_line: 1574 assertion_line: 1630
expression: "format!(\"{:#?}\", layout)" expression: "format!(\"{:#?}\", layout)"
--- ---
Layout { Layout {
@ -21,6 +21,9 @@ Layout {
EditFile( EditFile(
"/tmp/foo/bar", "/tmp/foo/bar",
None, None,
Some(
"/tmp/foo/",
),
), ),
), ),
borderless: false, borderless: false,

View file

@ -374,8 +374,10 @@ impl<'a> KdlLayoutParser<'a> {
hold_on_close, hold_on_close,
hold_on_start, hold_on_start,
}))), }))),
(None, Some(edit), Some(cwd)) => Ok(Some(Run::EditFile(cwd.join(edit), None))), (None, Some(edit), Some(cwd)) => {
(None, Some(edit), None) => Ok(Some(Run::EditFile(edit, None))), Ok(Some(Run::EditFile(cwd.join(edit), None, Some(cwd))))
},
(None, Some(edit), None) => Ok(Some(Run::EditFile(edit, None, None))),
(Some(_command), Some(_edit), _) => Err(ConfigError::new_layout_kdl_error( (Some(_command), Some(_edit), _) => Err(ConfigError::new_layout_kdl_error(
"cannot have both a command and an edit instruction for the same pane".into(), "cannot have both a command and an edit instruction for the same pane".into(),
pane_node.span().offset(), pane_node.span().offset(),
@ -962,7 +964,6 @@ impl<'a> KdlLayoutParser<'a> {
.unwrap_or(false); .unwrap_or(false);
let split_size = self.parse_split_size(kdl_node)?; let split_size = self.parse_split_size(kdl_node)?;
let children_split_direction = self.parse_split_direction(kdl_node)?; let children_split_direction = self.parse_split_direction(kdl_node)?;
let is_part_of_stack = false;
let (external_children_index, pane_parts) = match kdl_children_nodes!(kdl_node) { let (external_children_index, pane_parts) = match kdl_children_nodes!(kdl_node) {
Some(children) => { Some(children) => {
self.parse_child_pane_nodes_for_pane(&children, children_are_stacked)? self.parse_child_pane_nodes_for_pane(&children, children_are_stacked)?