Remove concept of git diff refresh from Item trait

This commit is contained in:
Julia 2023-05-25 14:29:28 -04:00
parent cede296b04
commit 8d662edb6c
11 changed files with 109 additions and 127 deletions

View file

@ -609,15 +609,6 @@ impl Item for ProjectDiagnosticsEditor {
unreachable!() unreachable!()
} }
fn git_diff_recalc(
&mut self,
project: ModelHandle<Project>,
cx: &mut ViewContext<Self>,
) -> Task<Result<()>> {
self.editor
.update(cx, |editor, cx| editor.git_diff_recalc(project, cx))
}
fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> { fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
Editor::to_item_events(event) Editor::to_item_events(event)
} }

View file

@ -40,7 +40,10 @@ use language::{
language_settings::ShowWhitespaceSetting, Bias, CursorShape, DiagnosticSeverity, OffsetUtf16, language_settings::ShowWhitespaceSetting, Bias, CursorShape, DiagnosticSeverity, OffsetUtf16,
Selection, Selection,
}; };
use project::ProjectPath; use project::{
project_settings::{GitGutterSetting, ProjectSettings},
ProjectPath,
};
use smallvec::SmallVec; use smallvec::SmallVec;
use std::{ use std::{
borrow::Cow, borrow::Cow,
@ -51,7 +54,7 @@ use std::{
sync::Arc, sync::Arc,
}; };
use text::Point; use text::Point;
use workspace::{item::Item, GitGutterSetting, WorkspaceSettings}; use workspace::item::Item;
enum FoldMarkers {} enum FoldMarkers {}
@ -551,11 +554,8 @@ impl EditorElement {
let scroll_top = scroll_position.y() * line_height; let scroll_top = scroll_position.y() * line_height;
let show_gutter = matches!( let show_gutter = matches!(
settings::get::<WorkspaceSettings>(cx) settings::get::<ProjectSettings>(cx).git.git_gutter,
.git Some(GitGutterSetting::TrackedFiles)
.git_gutter
.unwrap_or_default(),
GitGutterSetting::TrackedFiles
); );
if show_gutter { if show_gutter {

View file

@ -720,17 +720,6 @@ impl Item for Editor {
}) })
} }
fn git_diff_recalc(
&mut self,
_project: ModelHandle<Project>,
cx: &mut ViewContext<Self>,
) -> Task<Result<()>> {
self.buffer().update(cx, |multibuffer, cx| {
multibuffer.git_diff_recalc(cx);
});
Task::ready(Ok(()))
}
fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> { fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
let mut result = SmallVec::new(); let mut result = SmallVec::new();
match event { match event {

View file

@ -343,17 +343,6 @@ impl MultiBuffer {
self.read(cx).symbols_containing(offset, theme) self.read(cx).symbols_containing(offset, theme)
} }
pub fn git_diff_recalc(&mut self, _: &mut ModelContext<Self>) {
// let buffers = self.buffers.borrow();
// for buffer_state in buffers.values() {
// if buffer_state.buffer.read(cx).needs_git_diff_recalc() {
// buffer_state
// .buffer
// .update(cx, |buffer, cx| buffer.git_diff_recalc(cx))
// }
// }
}
pub fn edit<I, S, T>( pub fn edit<I, S, T>(
&mut self, &mut self,
edits: I, edits: I,

View file

@ -620,7 +620,6 @@ impl Buffer {
cx, cx,
); );
} }
self.git_diff_recalc(cx);
cx.emit(Event::Reloaded); cx.emit(Event::Reloaded);
cx.notify(); cx.notify();
} }

View file

@ -1,6 +1,6 @@
mod ignore; mod ignore;
mod lsp_command; mod lsp_command;
mod project_settings; pub mod project_settings;
pub mod search; pub mod search;
pub mod terminals; pub mod terminals;
pub mod worktree; pub mod worktree;
@ -14,7 +14,10 @@ use clock::ReplicaId;
use collections::{hash_map, BTreeMap, HashMap, HashSet}; use collections::{hash_map, BTreeMap, HashMap, HashSet};
use copilot::Copilot; use copilot::Copilot;
use futures::{ use futures::{
channel::mpsc::{self, UnboundedReceiver}, channel::{
mpsc::{self, UnboundedReceiver},
oneshot,
},
future::{try_join_all, Shared}, future::{try_join_all, Shared},
stream::FuturesUnordered, stream::FuturesUnordered,
AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt, AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt,
@ -131,6 +134,7 @@ pub struct Project {
buffer_snapshots: HashMap<u64, HashMap<LanguageServerId, Vec<LspBufferSnapshot>>>, // buffer_id -> server_id -> vec of snapshots buffer_snapshots: HashMap<u64, HashMap<LanguageServerId, Vec<LspBufferSnapshot>>>, // buffer_id -> server_id -> vec of snapshots
buffers_being_formatted: HashSet<u64>, buffers_being_formatted: HashSet<u64>,
buffers_needing_diff: HashSet<WeakModelHandle<Buffer>>, buffers_needing_diff: HashSet<WeakModelHandle<Buffer>>,
git_diff_debouncer: DelayedDebounced,
nonce: u128, nonce: u128,
_maintain_buffer_languages: Task<()>, _maintain_buffer_languages: Task<()>,
_maintain_workspace_config: Task<()>, _maintain_workspace_config: Task<()>,
@ -138,6 +142,49 @@ pub struct Project {
copilot_enabled: bool, copilot_enabled: bool,
} }
struct DelayedDebounced {
task: Option<Task<()>>,
cancel_channel: Option<oneshot::Sender<()>>,
}
impl DelayedDebounced {
fn new() -> DelayedDebounced {
DelayedDebounced {
task: None,
cancel_channel: None,
}
}
fn fire_new<F>(&mut self, delay: Duration, cx: &mut ModelContext<Project>, func: F)
where
F: 'static + FnOnce(&mut Project, &mut ModelContext<Project>) -> Task<()>,
{
if let Some(channel) = self.cancel_channel.take() {
_ = channel.send(());
}
let (sender, mut receiver) = oneshot::channel::<()>();
self.cancel_channel = Some(sender);
let previous_task = self.task.take();
self.task = Some(cx.spawn(|workspace, mut cx| async move {
let mut timer = cx.background().timer(delay).fuse();
if let Some(previous_task) = previous_task {
previous_task.await;
}
futures::select_biased! {
_ = receiver => return,
_ = timer => {}
}
workspace
.update(&mut cx, |workspace, cx| (func)(workspace, cx))
.await;
}));
}
}
struct LspBufferSnapshot { struct LspBufferSnapshot {
version: i32, version: i32,
snapshot: TextBufferSnapshot, snapshot: TextBufferSnapshot,
@ -486,6 +533,7 @@ impl Project {
last_workspace_edits_by_language_server: Default::default(), last_workspace_edits_by_language_server: Default::default(),
buffers_being_formatted: Default::default(), buffers_being_formatted: Default::default(),
buffers_needing_diff: Default::default(), buffers_needing_diff: Default::default(),
git_diff_debouncer: DelayedDebounced::new(),
nonce: StdRng::from_entropy().gen(), nonce: StdRng::from_entropy().gen(),
terminals: Terminals { terminals: Terminals {
local_handles: Vec::new(), local_handles: Vec::new(),
@ -576,6 +624,7 @@ impl Project {
opened_buffers: Default::default(), opened_buffers: Default::default(),
buffers_being_formatted: Default::default(), buffers_being_formatted: Default::default(),
buffers_needing_diff: Default::default(), buffers_needing_diff: Default::default(),
git_diff_debouncer: DelayedDebounced::new(),
buffer_snapshots: Default::default(), buffer_snapshots: Default::default(),
nonce: StdRng::from_entropy().gen(), nonce: StdRng::from_entropy().gen(),
terminals: Terminals { terminals: Terminals {
@ -2077,19 +2126,36 @@ impl Project {
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) { ) {
self.buffers_needing_diff.insert(buffer.downgrade()); self.buffers_needing_diff.insert(buffer.downgrade());
if self.buffers_needing_diff.len() == 1 { let first_insertion = self.buffers_needing_diff.len() == 1;
let this = cx.weak_handle();
cx.defer(move |cx| { let settings = settings::get::<ProjectSettings>(cx);
if let Some(this) = this.upgrade(cx) { let delay = if let Some(delay) = settings.git.gutter_debounce {
this.update(cx, |this, cx| { delay
this.recalculate_buffer_diffs(cx); } else {
}); if first_insertion {
} let this = cx.weak_handle();
cx.defer(move |cx| {
if let Some(this) = this.upgrade(cx) {
this.update(cx, |this, cx| {
this.recalculate_buffer_diffs(cx).detach();
});
}
});
}
return;
};
const MIN_DELAY: u64 = 50;
let delay = delay.max(MIN_DELAY);
let duration = Duration::from_millis(delay);
self.git_diff_debouncer
.fire_new(duration, cx, move |this, cx| {
this.recalculate_buffer_diffs(cx)
}); });
}
} }
fn recalculate_buffer_diffs(&mut self, cx: &mut ModelContext<Self>) { fn recalculate_buffer_diffs(&mut self, cx: &mut ModelContext<Self>) -> Task<()> {
cx.spawn(|this, mut cx| async move { cx.spawn(|this, mut cx| async move {
let buffers: Vec<_> = this.update(&mut cx, |this, _| { let buffers: Vec<_> = this.update(&mut cx, |this, _| {
this.buffers_needing_diff.drain().collect() this.buffers_needing_diff.drain().collect()
@ -2109,7 +2175,7 @@ impl Project {
this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
if !this.buffers_needing_diff.is_empty() { if !this.buffers_needing_diff.is_empty() {
this.recalculate_buffer_diffs(cx); this.recalculate_buffer_diffs(cx).detach();
} else { } else {
// TODO: Would a `ModelContext<Project>.notify()` suffice here? // TODO: Would a `ModelContext<Project>.notify()` suffice here?
for buffer in buffers { for buffer in buffers {
@ -2120,7 +2186,6 @@ impl Project {
} }
}); });
}) })
.detach();
} }
fn language_servers_for_worktree( fn language_servers_for_worktree(

View file

@ -8,6 +8,22 @@ use std::sync::Arc;
pub struct ProjectSettings { pub struct ProjectSettings {
#[serde(default)] #[serde(default)]
pub lsp: HashMap<Arc<str>, LspSettings>, pub lsp: HashMap<Arc<str>, LspSettings>,
#[serde(default)]
pub git: GitSettings,
}
#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
pub struct GitSettings {
pub git_gutter: Option<GitGutterSetting>,
pub gutter_debounce: Option<u64>,
}
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum GitGutterSetting {
#[default]
TrackedFiles,
Hide,
} }
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]

View file

@ -360,15 +360,6 @@ impl Item for ProjectSearchView {
.update(cx, |editor, cx| editor.navigate(data, cx)) .update(cx, |editor, cx| editor.navigate(data, cx))
} }
fn git_diff_recalc(
&mut self,
project: ModelHandle<Project>,
cx: &mut ViewContext<Self>,
) -> Task<anyhow::Result<()>> {
self.results_editor
.update(cx, |editor, cx| editor.git_diff_recalc(project, cx))
}
fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> { fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
match event { match event {
ViewEvent::UpdateTab => { ViewEvent::UpdateTab => {

View file

@ -1,9 +1,8 @@
use crate::{ use crate::{
pane, persistence::model::ItemId, searchable::SearchableItemHandle, DelayedDebouncedEditAction, pane, persistence::model::ItemId, searchable::SearchableItemHandle, FollowableItemBuilders,
FollowableItemBuilders, ItemNavHistory, Pane, ToolbarItemLocation, ViewId, Workspace, ItemNavHistory, Pane, ToolbarItemLocation, ViewId, Workspace, WorkspaceId,
WorkspaceId,
}; };
use crate::{AutosaveSetting, WorkspaceSettings}; use crate::{AutosaveSetting, DelayedDebouncedEditAction, WorkspaceSettings};
use anyhow::Result; use anyhow::Result;
use client::{proto, Client}; use client::{proto, Client};
use gpui::{ use gpui::{
@ -102,13 +101,6 @@ pub trait Item: View {
) -> Task<Result<()>> { ) -> Task<Result<()>> {
unimplemented!("reload() must be implemented if can_save() returns true") unimplemented!("reload() must be implemented if can_save() returns true")
} }
fn git_diff_recalc(
&mut self,
_project: ModelHandle<Project>,
_cx: &mut ViewContext<Self>,
) -> Task<Result<()>> {
Task::ready(Ok(()))
}
fn to_item_events(_event: &Self::Event) -> SmallVec<[ItemEvent; 2]> { fn to_item_events(_event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
SmallVec::new() SmallVec::new()
} }
@ -221,11 +213,6 @@ pub trait ItemHandle: 'static + fmt::Debug {
cx: &mut WindowContext, cx: &mut WindowContext,
) -> Task<Result<()>>; ) -> Task<Result<()>>;
fn reload(&self, project: ModelHandle<Project>, cx: &mut WindowContext) -> Task<Result<()>>; fn reload(&self, project: ModelHandle<Project>, cx: &mut WindowContext) -> Task<Result<()>>;
fn git_diff_recalc(
&self,
project: ModelHandle<Project>,
cx: &mut WindowContext,
) -> Task<Result<()>>;
fn act_as_type<'a>(&'a self, type_id: TypeId, cx: &'a AppContext) -> Option<&'a AnyViewHandle>; fn act_as_type<'a>(&'a self, type_id: TypeId, cx: &'a AppContext) -> Option<&'a AnyViewHandle>;
fn to_followable_item_handle(&self, cx: &AppContext) -> Option<Box<dyn FollowableItemHandle>>; fn to_followable_item_handle(&self, cx: &AppContext) -> Option<Box<dyn FollowableItemHandle>>;
fn on_release( fn on_release(
@ -381,7 +368,6 @@ impl<T: Item> ItemHandle for ViewHandle<T> {
.is_none() .is_none()
{ {
let mut pending_autosave = DelayedDebouncedEditAction::new(); let mut pending_autosave = DelayedDebouncedEditAction::new();
let mut pending_git_update = DelayedDebouncedEditAction::new();
let pending_update = Rc::new(RefCell::new(None)); let pending_update = Rc::new(RefCell::new(None));
let pending_update_scheduled = Rc::new(AtomicBool::new(false)); let pending_update_scheduled = Rc::new(AtomicBool::new(false));
@ -450,48 +436,14 @@ impl<T: Item> ItemHandle for ViewHandle<T> {
} }
ItemEvent::Edit => { ItemEvent::Edit => {
let settings = settings::get::<WorkspaceSettings>(cx); let autosave = settings::get::<WorkspaceSettings>(cx).autosave;
let debounce_delay = settings.git.gutter_debounce; if let AutosaveSetting::AfterDelay { milliseconds } = autosave {
if let AutosaveSetting::AfterDelay { milliseconds } =
settings.autosave
{
let delay = Duration::from_millis(milliseconds); let delay = Duration::from_millis(milliseconds);
let item = item.clone(); let item = item.clone();
pending_autosave.fire_new(delay, cx, move |workspace, cx| { pending_autosave.fire_new(delay, cx, move |workspace, cx| {
Pane::autosave_item(&item, workspace.project().clone(), cx) Pane::autosave_item(&item, workspace.project().clone(), cx)
}); });
} }
let item = item.clone();
if let Some(delay) = debounce_delay {
const MIN_GIT_DELAY: u64 = 50;
let delay = delay.max(MIN_GIT_DELAY);
let duration = Duration::from_millis(delay);
pending_git_update.fire_new(
duration,
cx,
move |workspace, cx| {
item.git_diff_recalc(workspace.project().clone(), cx)
},
);
} else {
cx.spawn(|workspace, mut cx| async move {
workspace
.update(&mut cx, |workspace, cx| {
item.git_diff_recalc(
workspace.project().clone(),
cx,
)
})?
.await?;
anyhow::Ok(())
})
.detach_and_log_err(cx);
}
} }
_ => {} _ => {}
@ -576,14 +528,6 @@ impl<T: Item> ItemHandle for ViewHandle<T> {
self.update(cx, |item, cx| item.reload(project, cx)) self.update(cx, |item, cx| item.reload(project, cx))
} }
fn git_diff_recalc(
&self,
project: ModelHandle<Project>,
cx: &mut WindowContext,
) -> Task<Result<()>> {
self.update(cx, |item, cx| item.git_diff_recalc(project, cx))
}
fn act_as_type<'a>(&'a self, type_id: TypeId, cx: &'a AppContext) -> Option<&'a AnyViewHandle> { fn act_as_type<'a>(&'a self, type_id: TypeId, cx: &'a AppContext) -> Option<&'a AnyViewHandle> {
self.read(cx).act_as_type(type_id, self, cx) self.read(cx).act_as_type(type_id, self, cx)
} }

View file

@ -442,7 +442,7 @@ impl DelayedDebouncedEditAction {
} }
} }
fn fire_new<F>(&mut self, delay: Duration, cx: &mut ViewContext<Workspace>, f: F) fn fire_new<F>(&mut self, delay: Duration, cx: &mut ViewContext<Workspace>, func: F)
where where
F: 'static + FnOnce(&mut Workspace, &mut ViewContext<Workspace>) -> Task<Result<()>>, F: 'static + FnOnce(&mut Workspace, &mut ViewContext<Workspace>) -> Task<Result<()>>,
{ {
@ -466,7 +466,7 @@ impl DelayedDebouncedEditAction {
} }
if let Some(result) = workspace if let Some(result) = workspace
.update(&mut cx, |workspace, cx| (f)(workspace, cx)) .update(&mut cx, |workspace, cx| (func)(workspace, cx))
.log_err() .log_err()
{ {
result.await.log_err(); result.await.log_err();

View file

@ -8,7 +8,6 @@ pub struct WorkspaceSettings {
pub confirm_quit: bool, pub confirm_quit: bool,
pub show_call_status_icon: bool, pub show_call_status_icon: bool,
pub autosave: AutosaveSetting, pub autosave: AutosaveSetting,
pub git: GitSettings,
} }
#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)] #[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
@ -17,7 +16,6 @@ pub struct WorkspaceSettingsContent {
pub confirm_quit: Option<bool>, pub confirm_quit: Option<bool>,
pub show_call_status_icon: Option<bool>, pub show_call_status_icon: Option<bool>,
pub autosave: Option<AutosaveSetting>, pub autosave: Option<AutosaveSetting>,
pub git: Option<GitSettings>,
} }
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)] #[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]