Merge MutableAppContext into AppContext

There may have been a good reason for the difference at some point, or I was
still learning Rust. But now it's just &mut AppContext vs &AppContext.
This commit is contained in:
Nathan Sobo 2023-04-06 15:49:03 -06:00
parent dd00966cc6
commit de9bf6dfbd
112 changed files with 882 additions and 1041 deletions

View file

@ -1,15 +1,15 @@
use editor::{EditorBlurred, EditorFocused, EditorMode, EditorReleased, Event};
use gpui::MutableAppContext;
use gpui::AppContext;
use crate::{state::Mode, Vim};
pub fn init(cx: &mut MutableAppContext) {
pub fn init(cx: &mut AppContext) {
cx.subscribe_global(focused).detach();
cx.subscribe_global(blurred).detach();
cx.subscribe_global(released).detach();
}
fn focused(EditorFocused(editor): &EditorFocused, cx: &mut MutableAppContext) {
fn focused(EditorFocused(editor): &EditorFocused, cx: &mut AppContext) {
Vim::update(cx, |vim, cx| {
if let Some(previously_active_editor) = vim
.active_editor
@ -48,7 +48,7 @@ fn focused(EditorFocused(editor): &EditorFocused, cx: &mut MutableAppContext) {
});
}
fn blurred(EditorBlurred(editor): &EditorBlurred, cx: &mut MutableAppContext) {
fn blurred(EditorBlurred(editor): &EditorBlurred, cx: &mut AppContext) {
Vim::update(cx, |vim, cx| {
if let Some(previous_editor) = vim.active_editor.clone() {
if previous_editor == editor.clone() {
@ -59,7 +59,7 @@ fn blurred(EditorBlurred(editor): &EditorBlurred, cx: &mut MutableAppContext) {
})
}
fn released(EditorReleased(editor): &EditorReleased, cx: &mut MutableAppContext) {
fn released(EditorReleased(editor): &EditorReleased, cx: &mut AppContext) {
cx.update_default_global(|vim: &mut Vim, _| {
if let Some(previous_editor) = vim.active_editor.clone() {
if previous_editor == editor.clone() {
@ -69,7 +69,7 @@ fn released(EditorReleased(editor): &EditorReleased, cx: &mut MutableAppContext)
});
}
fn local_selections_changed(newest_empty: bool, cx: &mut MutableAppContext) {
fn local_selections_changed(newest_empty: bool, cx: &mut AppContext) {
Vim::update(cx, |vim, cx| {
if vim.enabled && vim.state.mode == Mode::Normal && !newest_empty {
vim.switch_mode(Mode::Visual { line: false }, false, cx)

View file

@ -1,12 +1,12 @@
use crate::{state::Mode, Vim};
use editor::{scroll::autoscroll::Autoscroll, Bias};
use gpui::{actions, MutableAppContext, ViewContext};
use gpui::{actions, AppContext, ViewContext};
use language::SelectionGoal;
use workspace::Workspace;
actions!(vim, [NormalBefore]);
pub fn init(cx: &mut MutableAppContext) {
pub fn init(cx: &mut AppContext) {
cx.add_action(normal_before);
}

View file

@ -5,7 +5,7 @@ use editor::{
display_map::{DisplaySnapshot, ToDisplayPoint},
movement, Bias, CharKind, DisplayPoint, ToOffset,
};
use gpui::{actions, impl_actions, MutableAppContext};
use gpui::{actions, impl_actions, AppContext};
use language::{Point, Selection, SelectionGoal};
use serde::Deserialize;
use workspace::Workspace;
@ -80,7 +80,7 @@ actions!(
);
impl_actions!(vim, [NextWordStart, NextWordEnd, PreviousWordStart]);
pub fn init(cx: &mut MutableAppContext) {
pub fn init(cx: &mut AppContext) {
cx.add_action(|_: &mut Workspace, _: &Left, cx: _| motion(Motion::Left, cx));
cx.add_action(|_: &mut Workspace, _: &Backspace, cx: _| motion(Motion::Backspace, cx));
cx.add_action(|_: &mut Workspace, _: &Down, cx: _| motion(Motion::Down, cx));
@ -116,7 +116,7 @@ pub fn init(cx: &mut MutableAppContext) {
cx.add_action(|_: &mut Workspace, &NextLineStart, cx: _| motion(Motion::NextLineStart, cx))
}
pub(crate) fn motion(motion: Motion, cx: &mut MutableAppContext) {
pub(crate) fn motion(motion: Motion, cx: &mut AppContext) {
if let Some(Operator::Namespace(_))
| Some(Operator::FindForward { .. })
| Some(Operator::FindBackward { .. }) = Vim::read(cx).active_operator()

View file

@ -16,7 +16,7 @@ use editor::{
scroll::{autoscroll::Autoscroll, scroll_amount::ScrollAmount},
Anchor, Bias, ClipboardSelection, DisplayPoint, Editor,
};
use gpui::{actions, impl_actions, MutableAppContext, ViewContext};
use gpui::{actions, impl_actions, AppContext, ViewContext};
use language::{AutoindentMode, Point, SelectionGoal};
use log::error;
use serde::Deserialize;
@ -50,7 +50,7 @@ actions!(
impl_actions!(vim, [Scroll]);
pub fn init(cx: &mut MutableAppContext) {
pub fn init(cx: &mut AppContext) {
cx.add_action(insert_after);
cx.add_action(insert_first_non_whitespace);
cx.add_action(insert_end_of_line);
@ -94,7 +94,7 @@ pub fn normal_motion(
motion: Motion,
operator: Option<Operator>,
times: usize,
cx: &mut MutableAppContext,
cx: &mut AppContext,
) {
Vim::update(cx, |vim, cx| {
match operator {
@ -110,7 +110,7 @@ pub fn normal_motion(
});
}
pub fn normal_object(object: Object, cx: &mut MutableAppContext) {
pub fn normal_object(object: Object, cx: &mut AppContext) {
Vim::update(cx, |vim, cx| {
match vim.state.operator_stack.pop() {
Some(Operator::Object { around }) => match vim.state.operator_stack.pop() {
@ -129,7 +129,7 @@ pub fn normal_object(object: Object, cx: &mut MutableAppContext) {
})
}
fn move_cursor(vim: &mut Vim, motion: Motion, times: usize, cx: &mut MutableAppContext) {
fn move_cursor(vim: &mut Vim, motion: Motion, times: usize, cx: &mut AppContext) {
vim.update_active_editor(cx, |editor, cx| {
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
s.move_cursors_with(|map, cursor, goal| {
@ -424,7 +424,7 @@ fn scroll(editor: &mut Editor, amount: &ScrollAmount, cx: &mut ViewContext<Edito
}
}
pub(crate) fn normal_replace(text: Arc<str>, cx: &mut MutableAppContext) {
pub(crate) fn normal_replace(text: Arc<str>, cx: &mut AppContext) {
Vim::update(cx, |vim, cx| {
vim.update_active_editor(cx, |editor, cx| {
editor.transact(cx, |editor, cx| {

View file

@ -3,10 +3,10 @@ use editor::{
char_kind, display_map::DisplaySnapshot, movement, scroll::autoscroll::Autoscroll, CharKind,
DisplayPoint,
};
use gpui::MutableAppContext;
use gpui::AppContext;
use language::Selection;
pub fn change_motion(vim: &mut Vim, motion: Motion, times: usize, cx: &mut MutableAppContext) {
pub fn change_motion(vim: &mut Vim, motion: Motion, times: usize, cx: &mut AppContext) {
// Some motions ignore failure when switching to normal mode
let mut motion_succeeded = matches!(
motion,
@ -38,7 +38,7 @@ pub fn change_motion(vim: &mut Vim, motion: Motion, times: usize, cx: &mut Mutab
}
}
pub fn change_object(vim: &mut Vim, object: Object, around: bool, cx: &mut MutableAppContext) {
pub fn change_object(vim: &mut Vim, object: Object, around: bool, cx: &mut AppContext) {
let mut objects_found = false;
vim.update_active_editor(cx, |editor, cx| {
// We are swapping to insert mode anyway. Just set the line end clipping behavior now

View file

@ -1,9 +1,9 @@
use crate::{motion::Motion, object::Object, utils::copy_selections_content, Vim};
use collections::{HashMap, HashSet};
use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Bias};
use gpui::MutableAppContext;
use gpui::AppContext;
pub fn delete_motion(vim: &mut Vim, motion: Motion, times: usize, cx: &mut MutableAppContext) {
pub fn delete_motion(vim: &mut Vim, motion: Motion, times: usize, cx: &mut AppContext) {
vim.update_active_editor(cx, |editor, cx| {
editor.transact(cx, |editor, cx| {
editor.set_clip_at_line_ends(false, cx);
@ -36,7 +36,7 @@ pub fn delete_motion(vim: &mut Vim, motion: Motion, times: usize, cx: &mut Mutab
});
}
pub fn delete_object(vim: &mut Vim, object: Object, around: bool, cx: &mut MutableAppContext) {
pub fn delete_object(vim: &mut Vim, object: Object, around: bool, cx: &mut AppContext) {
vim.update_active_editor(cx, |editor, cx| {
editor.transact(cx, |editor, cx| {
editor.set_clip_at_line_ends(false, cx);

View file

@ -1,8 +1,8 @@
use crate::{motion::Motion, object::Object, utils::copy_selections_content, Vim};
use collections::HashMap;
use gpui::MutableAppContext;
use gpui::AppContext;
pub fn yank_motion(vim: &mut Vim, motion: Motion, times: usize, cx: &mut MutableAppContext) {
pub fn yank_motion(vim: &mut Vim, motion: Motion, times: usize, cx: &mut AppContext) {
vim.update_active_editor(cx, |editor, cx| {
editor.transact(cx, |editor, cx| {
editor.set_clip_at_line_ends(false, cx);
@ -25,7 +25,7 @@ pub fn yank_motion(vim: &mut Vim, motion: Motion, times: usize, cx: &mut Mutable
});
}
pub fn yank_object(vim: &mut Vim, object: Object, around: bool, cx: &mut MutableAppContext) {
pub fn yank_object(vim: &mut Vim, object: Object, around: bool, cx: &mut AppContext) {
vim.update_active_editor(cx, |editor, cx| {
editor.transact(cx, |editor, cx| {
editor.set_clip_at_line_ends(false, cx);

View file

@ -1,7 +1,7 @@
use std::ops::Range;
use editor::{char_kind, display_map::DisplaySnapshot, movement, Bias, CharKind, DisplayPoint};
use gpui::{actions, impl_actions, MutableAppContext};
use gpui::{actions, impl_actions, AppContext};
use language::Selection;
use serde::Deserialize;
use workspace::Workspace;
@ -43,7 +43,7 @@ actions!(
);
impl_actions!(vim, [Word]);
pub fn init(cx: &mut MutableAppContext) {
pub fn init(cx: &mut AppContext) {
cx.add_action(
|_: &mut Workspace, &Word { ignore_punctuation }: &Word, cx: _| {
object(Object::Word { ignore_punctuation }, cx)
@ -61,7 +61,7 @@ pub fn init(cx: &mut MutableAppContext) {
cx.add_action(|_: &mut Workspace, _: &AngleBrackets, cx: _| object(Object::AngleBrackets, cx));
}
fn object(object: Object, cx: &mut MutableAppContext) {
fn object(object: Object, cx: &mut AppContext) {
match Vim::read(cx).state.mode {
Mode::Normal => normal_object(object, cx),
Mode::Visual { .. } => visual_object(object, cx),

View file

@ -1,7 +1,7 @@
use editor::{ClipboardSelection, Editor};
use gpui::{ClipboardItem, MutableAppContext};
use gpui::{AppContext, ClipboardItem};
pub fn copy_selections_content(editor: &mut Editor, linewise: bool, cx: &mut MutableAppContext) {
pub fn copy_selections_content(editor: &mut Editor, linewise: bool, cx: &mut AppContext) {
let selections = editor.selections.all_adjusted(cx);
let buffer = editor.buffer().read(cx).snapshot(cx);
let mut text = String::new();

View file

@ -15,7 +15,7 @@ use std::sync::Arc;
use collections::CommandPaletteFilter;
use editor::{Bias, Cancel, Editor, EditorMode};
use gpui::{
actions, impl_actions, MutableAppContext, Subscription, ViewContext, ViewHandle, WeakViewHandle,
actions, impl_actions, AppContext, Subscription, ViewContext, ViewHandle, WeakViewHandle,
};
use language::CursorShape;
use motion::Motion;
@ -38,7 +38,7 @@ struct Number(u8);
actions!(vim, [Tab, Enter]);
impl_actions!(vim, [Number, SwitchMode, PushOperator]);
pub fn init(cx: &mut MutableAppContext) {
pub fn init(cx: &mut AppContext) {
editor_events::init(cx);
normal::init(cx);
visual::init(cx);
@ -65,7 +65,7 @@ pub fn init(cx: &mut MutableAppContext) {
// Otherwise forward cancel on to the editor
let vim = Vim::read(cx);
if vim.state.mode != Mode::Normal || vim.active_operator().is_some() {
MutableAppContext::defer(cx, |cx| {
AppContext::defer(cx, |cx| {
Vim::update(cx, |state, cx| {
state.switch_mode(Mode::Normal, false, cx);
});
@ -95,7 +95,7 @@ pub fn init(cx: &mut MutableAppContext) {
.detach();
}
pub fn observe_keystrokes(window_id: usize, cx: &mut MutableAppContext) {
pub fn observe_keystrokes(window_id: usize, cx: &mut AppContext) {
cx.observe_keystrokes(window_id, |_keystroke, _result, handled_by, cx| {
if let Some(handled_by) = handled_by {
// Keystroke is handled by the vim system, so continue forward
@ -131,20 +131,20 @@ pub struct Vim {
}
impl Vim {
fn read(cx: &mut MutableAppContext) -> &Self {
fn read(cx: &mut AppContext) -> &Self {
cx.default_global()
}
fn update<F, S>(cx: &mut MutableAppContext, update: F) -> S
fn update<F, S>(cx: &mut AppContext, update: F) -> S
where
F: FnOnce(&mut Self, &mut MutableAppContext) -> S,
F: FnOnce(&mut Self, &mut AppContext) -> S,
{
cx.update_default_global(update)
}
fn update_active_editor<S>(
&self,
cx: &mut MutableAppContext,
cx: &mut AppContext,
update: impl FnOnce(&mut Editor, &mut ViewContext<Editor>) -> S,
) -> Option<S> {
self.active_editor
@ -153,7 +153,7 @@ impl Vim {
.map(|ae| ae.update(cx, update))
}
fn switch_mode(&mut self, mode: Mode, leave_selections: bool, cx: &mut MutableAppContext) {
fn switch_mode(&mut self, mode: Mode, leave_selections: bool, cx: &mut AppContext) {
self.state.mode = mode;
self.state.operator_stack.clear();
@ -188,12 +188,12 @@ impl Vim {
}
}
fn push_operator(&mut self, operator: Operator, cx: &mut MutableAppContext) {
fn push_operator(&mut self, operator: Operator, cx: &mut AppContext) {
self.state.operator_stack.push(operator);
self.sync_vim_settings(cx);
}
fn push_number(&mut self, Number(number): &Number, cx: &mut MutableAppContext) {
fn push_number(&mut self, Number(number): &Number, cx: &mut AppContext) {
if let Some(Operator::Number(current_number)) = self.active_operator() {
self.pop_operator(cx);
self.push_operator(Operator::Number(current_number * 10 + *number as usize), cx);
@ -202,14 +202,14 @@ impl Vim {
}
}
fn pop_operator(&mut self, cx: &mut MutableAppContext) -> Operator {
fn pop_operator(&mut self, cx: &mut AppContext) -> Operator {
let popped_operator = self.state.operator_stack.pop()
.expect("Operator popped when no operator was on the stack. This likely means there is an invalid keymap config");
self.sync_vim_settings(cx);
popped_operator
}
fn pop_number_operator(&mut self, cx: &mut MutableAppContext) -> usize {
fn pop_number_operator(&mut self, cx: &mut AppContext) -> usize {
let mut times = 1;
if let Some(Operator::Number(number)) = self.active_operator() {
times = number;
@ -218,7 +218,7 @@ impl Vim {
times
}
fn clear_operator(&mut self, cx: &mut MutableAppContext) {
fn clear_operator(&mut self, cx: &mut AppContext) {
self.state.operator_stack.clear();
self.sync_vim_settings(cx);
}
@ -227,7 +227,7 @@ impl Vim {
self.state.operator_stack.last().copied()
}
fn active_editor_input_ignored(text: Arc<str>, cx: &mut MutableAppContext) {
fn active_editor_input_ignored(text: Arc<str>, cx: &mut AppContext) {
if text.is_empty() {
return;
}
@ -248,7 +248,7 @@ impl Vim {
}
}
fn set_enabled(&mut self, enabled: bool, cx: &mut MutableAppContext) {
fn set_enabled(&mut self, enabled: bool, cx: &mut AppContext) {
if self.enabled != enabled {
self.enabled = enabled;
self.state = Default::default();
@ -259,7 +259,7 @@ impl Vim {
}
}
fn sync_vim_settings(&self, cx: &mut MutableAppContext) {
fn sync_vim_settings(&self, cx: &mut AppContext) {
let state = &self.state;
let cursor_shape = state.cursor_shape();
@ -291,7 +291,7 @@ impl Vim {
}
}
fn unhook_vim_settings(&self, editor: ViewHandle<Editor>, cx: &mut MutableAppContext) {
fn unhook_vim_settings(&self, editor: ViewHandle<Editor>, cx: &mut AppContext) {
editor.update(cx, |editor, cx| {
editor.set_cursor_shape(CursorShape::Bar, cx);
editor.set_clip_at_line_ends(false, cx);

View file

@ -4,7 +4,7 @@ use collections::HashMap;
use editor::{
display_map::ToDisplayPoint, movement, scroll::autoscroll::Autoscroll, Bias, ClipboardSelection,
};
use gpui::{actions, MutableAppContext, ViewContext};
use gpui::{actions, AppContext, ViewContext};
use language::{AutoindentMode, SelectionGoal};
use workspace::Workspace;
@ -18,14 +18,14 @@ use crate::{
actions!(vim, [VisualDelete, VisualChange, VisualYank, VisualPaste]);
pub fn init(cx: &mut MutableAppContext) {
pub fn init(cx: &mut AppContext) {
cx.add_action(change);
cx.add_action(delete);
cx.add_action(yank);
cx.add_action(paste);
}
pub fn visual_motion(motion: Motion, times: usize, cx: &mut MutableAppContext) {
pub fn visual_motion(motion: Motion, times: usize, cx: &mut AppContext) {
Vim::update(cx, |vim, cx| {
vim.update_active_editor(cx, |editor, cx| {
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
@ -56,7 +56,7 @@ pub fn visual_motion(motion: Motion, times: usize, cx: &mut MutableAppContext) {
});
}
pub fn visual_object(object: Object, cx: &mut MutableAppContext) {
pub fn visual_object(object: Object, cx: &mut AppContext) {
Vim::update(cx, |vim, cx| {
if let Operator::Object { around } = vim.pop_operator(cx) {
vim.update_active_editor(cx, |editor, cx| {
@ -313,7 +313,7 @@ pub fn paste(_: &mut Workspace, _: &VisualPaste, cx: &mut ViewContext<Workspace>
});
}
pub(crate) fn visual_replace(text: Arc<str>, line: bool, cx: &mut MutableAppContext) {
pub(crate) fn visual_replace(text: Arc<str>, line: bool, cx: &mut AppContext) {
Vim::update(cx, |vim, cx| {
vim.update_active_editor(cx, |editor, cx| {
editor.transact(cx, |editor, cx| {
@ -648,7 +648,7 @@ mod test {
cx.assert_state(
indoc! {"
The quick brown
the
the
ˇfox jumps over
dog"},
Mode::Normal,