Fix item closing overly triggering save dialogues (#21374)

Closes https://github.com/zed-industries/zed/issues/12029

Allows to introspect project items inside items more deeply, checking
them for being dirty.
For that:
* renames `project::Item` into `project::ProjectItem`
* adds an `is_dirty(&self) -> bool` method to the renamed trait
* changes the closing logic to only care about dirty project items when
checking for save prompts conditions
* save prompts are raised only if the item is singleton without a
project path; or if the item has dirty project items that are not open
elsewhere

Release Notes:

- Fixed item closing overly triggering save dialogues
This commit is contained in:
Kirill Bulatov 2024-12-01 01:48:31 +02:00 committed by GitHub
parent c2cd84a749
commit 28849dd2a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 600 additions and 85 deletions

View file

@ -1,7 +1,7 @@
use crate::{
search::SearchQuery,
worktree_store::{WorktreeStore, WorktreeStoreEvent},
Item, ProjectPath,
ProjectItem as _, ProjectPath,
};
use ::git::{parse_git_remote_url, BuildPermalinkParams, GitHostingProviderRegistry};
use anyhow::{anyhow, Context as _, Result};

View file

@ -1,6 +1,6 @@
use crate::{
worktree_store::{WorktreeStore, WorktreeStoreEvent},
Project, ProjectEntryId, ProjectPath,
Project, ProjectEntryId, ProjectItem, ProjectPath,
};
use anyhow::{Context as _, Result};
use collections::{hash_map, HashMap, HashSet};
@ -114,7 +114,7 @@ impl ImageItem {
}
}
impl crate::Item for ImageItem {
impl ProjectItem for ImageItem {
fn try_open(
project: &Model<Project>,
path: &ProjectPath,
@ -151,6 +151,10 @@ impl crate::Item for ImageItem {
fn project_path(&self, cx: &AppContext) -> Option<ProjectPath> {
Some(self.project_path(cx).clone())
}
fn is_dirty(&self) -> bool {
false
}
}
trait ImageStoreImpl {

View file

@ -10,7 +10,7 @@ use crate::{
toolchain_store::{EmptyToolchainStore, ToolchainStoreEvent},
worktree_store::{WorktreeStore, WorktreeStoreEvent},
yarn::YarnPathStore,
CodeAction, Completion, CoreCompletion, Hover, InlayHint, Item as _, ProjectPath,
CodeAction, Completion, CoreCompletion, Hover, InlayHint, ProjectItem as _, ProjectPath,
ProjectTransaction, ResolveState, Symbol, ToolchainStore,
};
use anyhow::{anyhow, Context as _, Result};

View file

@ -111,7 +111,7 @@ const MAX_PROJECT_SEARCH_HISTORY_SIZE: usize = 500;
const MAX_SEARCH_RESULT_FILES: usize = 5_000;
const MAX_SEARCH_RESULT_RANGES: usize = 10_000;
pub trait Item {
pub trait ProjectItem {
fn try_open(
project: &Model<Project>,
path: &ProjectPath,
@ -121,6 +121,7 @@ pub trait Item {
Self: Sized;
fn entry_id(&self, cx: &AppContext) -> Option<ProjectEntryId>;
fn project_path(&self, cx: &AppContext) -> Option<ProjectPath>;
fn is_dirty(&self) -> bool;
}
#[derive(Clone)]
@ -4354,7 +4355,7 @@ impl ResolvedPath {
}
}
impl Item for Buffer {
impl ProjectItem for Buffer {
fn try_open(
project: &Model<Project>,
path: &ProjectPath,
@ -4373,6 +4374,10 @@ impl Item for Buffer {
path: file.path().clone(),
})
}
fn is_dirty(&self) -> bool {
self.is_dirty()
}
}
impl Completion {