From c801a524923d3228cfdccf82b1f396a7c1579f10 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 7 Apr 2022 16:44:37 -0700 Subject: [PATCH] Convert some actions to use named fields Co-authored-by: Nathan Sobo Co-authored-by: Keith Simmons --- crates/editor/src/editor.rs | 89 +++++++++++++++++++++++++------ crates/workspace/src/workspace.rs | 19 +++---- crates/zed/src/main.rs | 4 +- 3 files changed, 82 insertions(+), 30 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index e201f5f366..e10fe9c17c 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -85,10 +85,14 @@ pub struct Input(pub String); pub struct Tab(pub Direction); #[derive(Clone)] -pub struct SelectToBeginningOfLine(pub bool); +pub struct SelectToBeginningOfLine { + stop_at_soft_wraps: bool, +} #[derive(Clone)] -pub struct SelectToEndOfLine(pub bool); +pub struct SelectToEndOfLine { + stop_at_soft_wraps: bool, +} #[derive(Clone)] pub struct ToggleCodeActions(pub bool); @@ -301,7 +305,9 @@ pub fn init(cx: &mut MutableAppContext) { Binding::new("alt-shift-F", SelectToNextWordEnd, Some("Editor")), Binding::new( "cmd-shift-left", - SelectToBeginningOfLine(true), + SelectToBeginningOfLine { + stop_at_soft_wraps: true, + }, Some("Editor"), ), Binding::new( @@ -312,11 +318,25 @@ pub fn init(cx: &mut MutableAppContext) { Binding::new("ctrl-alt-shift-F", SelectToNextSubwordEnd, Some("Editor")), Binding::new( "ctrl-shift-A", - SelectToBeginningOfLine(true), + SelectToBeginningOfLine { + stop_at_soft_wraps: true, + }, + Some("Editor"), + ), + Binding::new( + "cmd-shift-right", + SelectToEndOfLine { + stop_at_soft_wraps: true, + }, + Some("Editor"), + ), + Binding::new( + "ctrl-shift-E", + SelectToEndOfLine { + stop_at_soft_wraps: true, + }, Some("Editor"), ), - Binding::new("cmd-shift-right", SelectToEndOfLine(true), Some("Editor")), - Binding::new("ctrl-shift-E", SelectToEndOfLine(true), Some("Editor")), Binding::new("cmd-shift-up", SelectToBeginning, Some("Editor")), Binding::new("cmd-shift-down", SelectToEnd, Some("Editor")), Binding::new("cmd-a", SelectAll, Some("Editor")), @@ -3905,12 +3925,12 @@ impl Editor { pub fn select_to_beginning_of_line( &mut self, - SelectToBeginningOfLine(stop_at_soft_boundaries): &SelectToBeginningOfLine, + action: &SelectToBeginningOfLine, cx: &mut ViewContext, ) { self.move_selection_heads(cx, |map, head, _| { ( - movement::line_beginning(map, head, *stop_at_soft_boundaries), + movement::line_beginning(map, head, action.stop_at_soft_wraps), SelectionGoal::None, ) }); @@ -3922,7 +3942,12 @@ impl Editor { cx: &mut ViewContext, ) { self.transact(cx, |this, cx| { - this.select_to_beginning_of_line(&SelectToBeginningOfLine(false), cx); + this.select_to_beginning_of_line( + &SelectToBeginningOfLine { + stop_at_soft_wraps: false, + }, + cx, + ); this.backspace(&Backspace, cx); }); } @@ -3935,12 +3960,12 @@ impl Editor { pub fn select_to_end_of_line( &mut self, - SelectToEndOfLine(stop_at_soft_boundaries): &SelectToEndOfLine, + action: &SelectToEndOfLine, cx: &mut ViewContext, ) { self.move_selection_heads(cx, |map, head, _| { ( - movement::line_end(map, head, *stop_at_soft_boundaries), + movement::line_end(map, head, action.stop_at_soft_wraps), SelectionGoal::None, ) }); @@ -3948,14 +3973,24 @@ impl Editor { pub fn delete_to_end_of_line(&mut self, _: &DeleteToEndOfLine, cx: &mut ViewContext) { self.transact(cx, |this, cx| { - this.select_to_end_of_line(&SelectToEndOfLine(false), cx); + this.select_to_end_of_line( + &SelectToEndOfLine { + stop_at_soft_wraps: false, + }, + cx, + ); this.delete(&Delete, cx); }); } pub fn cut_to_end_of_line(&mut self, _: &CutToEndOfLine, cx: &mut ViewContext) { self.transact(cx, |this, cx| { - this.select_to_end_of_line(&SelectToEndOfLine(false), cx); + this.select_to_end_of_line( + &SelectToEndOfLine { + stop_at_soft_wraps: false, + }, + cx, + ); this.cut(&Cut, cx); }); } @@ -7296,7 +7331,12 @@ mod tests { view.update(cx, |view, cx| { view.move_left(&MoveLeft, cx); - view.select_to_beginning_of_line(&SelectToBeginningOfLine(true), cx); + view.select_to_beginning_of_line( + &SelectToBeginningOfLine { + stop_at_soft_wraps: true, + }, + cx, + ); assert_eq!( view.selected_display_ranges(cx), &[ @@ -7307,7 +7347,12 @@ mod tests { }); view.update(cx, |view, cx| { - view.select_to_beginning_of_line(&SelectToBeginningOfLine(true), cx); + view.select_to_beginning_of_line( + &SelectToBeginningOfLine { + stop_at_soft_wraps: true, + }, + cx, + ); assert_eq!( view.selected_display_ranges(cx), &[ @@ -7318,7 +7363,12 @@ mod tests { }); view.update(cx, |view, cx| { - view.select_to_beginning_of_line(&SelectToBeginningOfLine(true), cx); + view.select_to_beginning_of_line( + &SelectToBeginningOfLine { + stop_at_soft_wraps: true, + }, + cx, + ); assert_eq!( view.selected_display_ranges(cx), &[ @@ -7329,7 +7379,12 @@ mod tests { }); view.update(cx, |view, cx| { - view.select_to_end_of_line(&SelectToEndOfLine(true), cx); + view.select_to_end_of_line( + &SelectToEndOfLine { + stop_at_soft_wraps: true, + }, + cx, + ); assert_eq!( view.selected_display_ranges(cx), &[ diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index a320dca23c..6b6761b1f7 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -90,7 +90,10 @@ pub struct Open(pub Arc); pub struct OpenNew(pub Arc); #[derive(Clone)] -pub struct OpenPaths(pub OpenParams); +pub struct OpenPaths { + pub paths: Vec, + pub app_state: Arc, +} #[derive(Clone)] pub struct ToggleFollow(pub PeerId); @@ -109,7 +112,7 @@ pub fn init(client: &Arc, cx: &mut MutableAppContext) { cx.add_global_action(open); cx.add_global_action(move |action: &OpenPaths, cx: &mut MutableAppContext| { - open_paths(&action.0.paths, &action.0.app_state, cx).detach(); + open_paths(&action.paths, &action.app_state, cx).detach(); }); cx.add_global_action(move |action: &OpenNew, cx: &mut MutableAppContext| { open_new(&action.0, cx) @@ -211,12 +214,6 @@ pub struct AppState { ) -> Workspace, } -#[derive(Clone)] -pub struct OpenParams { - pub paths: Vec, - pub app_state: Arc, -} - #[derive(Clone)] pub struct JoinProjectParams { pub project_id: u64, @@ -2113,9 +2110,9 @@ impl Element for AvatarRibbon { } } -impl std::fmt::Debug for OpenParams { +impl std::fmt::Debug for OpenPaths { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("OpenParams") + f.debug_struct("OpenPaths") .field("paths", &self.paths) .finish() } @@ -2130,7 +2127,7 @@ fn open(action: &Open, cx: &mut MutableAppContext) { }); cx.spawn(|mut cx| async move { if let Some(paths) = paths.recv().await.flatten() { - cx.update(|cx| cx.dispatch_global_action(OpenPaths(OpenParams { paths, app_state }))); + cx.update(|cx| cx.dispatch_global_action(OpenPaths { paths, app_state })); } }) .detach(); diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index 653788187b..8633005899 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -14,7 +14,7 @@ use smol::process::Command; use std::{env, fs, path::PathBuf, sync::Arc}; use theme::{ThemeRegistry, DEFAULT_THEME_NAME}; use util::ResultExt; -use workspace::{self, AppState, OpenNew, OpenParams, OpenPaths}; +use workspace::{self, AppState, OpenNew, OpenPaths}; use zed::{ self, assets::Assets, @@ -158,7 +158,7 @@ fn main() { if paths.is_empty() { cx.dispatch_global_action(OpenNew(app_state.clone())); } else { - cx.dispatch_global_action(OpenPaths(OpenParams { paths, app_state })); + cx.dispatch_global_action(OpenPaths { paths, app_state }); } }); }