1480 lines
52 KiB
Rust
1480 lines
52 KiB
Rust
//! Pure prompts, preparation requests, and confirmation for entry-level actions.
|
|
//!
|
|
//! This module never touches a filesystem or starts a process. Create and rename plans are built
|
|
//! entirely from confirmed pane metadata. Trash and permanent delete require a read-only planner
|
|
//! result before their confirmation can start queued work.
|
|
|
|
use std::collections::BTreeMap;
|
|
use std::ffi::OsString;
|
|
|
|
use crate::actions::{
|
|
EditorCommand, EditorOutcome, EntryMutationKind, EntryMutationPlan, PreparedUndo,
|
|
PreparedUndoKind, TrashRecord, UndoEntry, validate_entry_name,
|
|
};
|
|
use crate::domain::{Entry, EntryId, EntryKind, Location, SourceId};
|
|
use crate::fs::FsError;
|
|
use crate::operations::{
|
|
CollisionPolicy, CommitOutcome, EntryFingerprint, ItemId, OperationEvent, PlanGeneration,
|
|
PlanId, SourceItem, TaskId, TaskStatus, WorkKind, WorkPlan,
|
|
};
|
|
|
|
use super::{
|
|
AppState, Effect, Overlay, PaneId, PaneSnapshot, PaneState, SessionEpoch, next_generation,
|
|
};
|
|
|
|
/// Opaque reference to an owned workspace retained by the effect runner.
|
|
///
|
|
/// The non-`Eq` temporary directory never becomes part of application state, commands, or effects.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
|
pub struct EditorWorkspaceId(u64);
|
|
|
|
impl EditorWorkspaceId {
|
|
pub const fn new(value: u64) -> Self {
|
|
Self(value)
|
|
}
|
|
|
|
pub const fn get(self) -> u64 {
|
|
self.0
|
|
}
|
|
}
|
|
|
|
/// A fixed editor source and the pane/session snapshot against which results are validated.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct PrepareEditorRequest {
|
|
pub pane: PaneId,
|
|
pub source: Location,
|
|
pub entry: Entry,
|
|
pub listing_generation: super::Generation,
|
|
pub session_epoch: Option<SessionEpoch>,
|
|
}
|
|
|
|
impl PrepareEditorRequest {
|
|
pub fn into_result(self, result: Result<PreparedEditor, FsError>) -> PrepareEditorResult {
|
|
PrepareEditorResult {
|
|
request: self,
|
|
result,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Process input produced by local preparation or an owned SSH download.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct PreparedEditor {
|
|
pub command: EditorCommand,
|
|
pub path: std::path::PathBuf,
|
|
pub workspace_id: Option<EditorWorkspaceId>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct PrepareEditorResult {
|
|
pub request: PrepareEditorRequest,
|
|
pub result: Result<PreparedEditor, FsError>,
|
|
}
|
|
|
|
/// The event loop, rather than the effect runner, executes this request while the TUI is suspended.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct RunEditorRequest {
|
|
pub request: PrepareEditorRequest,
|
|
pub prepared: PreparedEditor,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct EditorProcessResult {
|
|
pub request: PrepareEditorRequest,
|
|
pub workspace_id: Option<EditorWorkspaceId>,
|
|
pub result: Result<EditorOutcome, FsError>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct EditorChangeResult {
|
|
pub request: PrepareEditorRequest,
|
|
pub workspace_id: Option<EditorWorkspaceId>,
|
|
pub result: Result<bool, FsError>,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum EditorPhase {
|
|
Preparing,
|
|
Running,
|
|
ConfirmUpload,
|
|
}
|
|
|
|
/// Reducer-owned metadata for one editor invocation. Owned bytes remain behind a workspace ID.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct EditorSessionState {
|
|
pub request: PrepareEditorRequest,
|
|
pub prepared: Option<PreparedEditor>,
|
|
pub phase: EditorPhase,
|
|
}
|
|
|
|
/// State retained while an entry name, read-only preparation, or confirmation is open.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct EntryActionSession {
|
|
pub kind: EntryMutationKind,
|
|
pub pane: PaneId,
|
|
pub snapshot: PaneSnapshot,
|
|
/// Hovered entry captured for rename. Other selected entries are deliberately irrelevant.
|
|
pub source_entry: Option<Entry>,
|
|
/// Hovered/selected roots captured for trash or permanent delete.
|
|
pub sources: Vec<EntryId>,
|
|
pub input: String,
|
|
pub request: Option<PrepareEntryActionRequest>,
|
|
pub plan: Option<EntryMutationPlan>,
|
|
pub trash_records: Vec<TrashRecord>,
|
|
pub notice: Option<String>,
|
|
}
|
|
|
|
/// A fully prepared action and the restore records which must survive queueing.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct PreparedEntryAction {
|
|
pub plan: EntryMutationPlan,
|
|
pub trash_records: Vec<TrashRecord>,
|
|
}
|
|
|
|
impl PreparedEntryAction {
|
|
pub fn without_trash_records(plan: EntryMutationPlan) -> Self {
|
|
Self {
|
|
plan,
|
|
trash_records: Vec::new(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A read-only planner request for a fixed set of trash/delete roots.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct PrepareEntryActionRequest {
|
|
pub plan_id: PlanId,
|
|
pub generation: PlanGeneration,
|
|
pub kind: EntryMutationKind,
|
|
pub pane: PaneSnapshot,
|
|
pub sources: Vec<EntryId>,
|
|
pub endpoint_epochs: BTreeMap<SourceId, SessionEpoch>,
|
|
}
|
|
|
|
impl PrepareEntryActionRequest {
|
|
pub fn into_result(
|
|
self,
|
|
result: Result<PreparedEntryAction, FsError>,
|
|
) -> PrepareEntryActionResult {
|
|
PrepareEntryActionResult {
|
|
request: self,
|
|
result,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct PrepareEntryActionResult {
|
|
pub request: PrepareEntryActionRequest,
|
|
pub result: Result<PreparedEntryAction, FsError>,
|
|
}
|
|
|
|
/// Read-only inverse planning for the journal's newest entry.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct PrepareUndoRequest {
|
|
pub plan_id: PlanId,
|
|
pub generation: PlanGeneration,
|
|
pub entry: UndoEntry,
|
|
}
|
|
|
|
impl PrepareUndoRequest {
|
|
pub fn into_result(self, result: Result<UndoSummary, FsError>) -> PrepareUndoResult {
|
|
PrepareUndoResult {
|
|
request: self,
|
|
result,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct PrepareUndoResult {
|
|
pub request: PrepareUndoRequest,
|
|
pub result: Result<UndoSummary, FsError>,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum UndoInverseKind {
|
|
DeleteCreated,
|
|
RenameBack,
|
|
MoveBack,
|
|
RestoreTrash,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum UndoConsequence {
|
|
RemoveOnlyUnchangedCreatedEntry,
|
|
RestoreOriginalName,
|
|
ReturnToOriginalLocation,
|
|
RestoreOriginalPathFromTrash,
|
|
}
|
|
|
|
/// Typed, `Eq` description of inverse work. The backend-owning `PreparedUndo` never crosses into
|
|
/// application state; runtime retains it under the same fresh plan identity.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct UndoSummary {
|
|
pub plan_id: PlanId,
|
|
pub generation: PlanGeneration,
|
|
pub inverse_kind: UndoInverseKind,
|
|
pub source: Location,
|
|
pub destination: Option<Location>,
|
|
pub sources: Vec<SourceItem>,
|
|
pub consequence: UndoConsequence,
|
|
}
|
|
|
|
impl UndoSummary {
|
|
pub fn from_prepared(prepared: &PreparedUndo) -> Self {
|
|
let (inverse_kind, sources, destination, consequence) = match prepared.kind() {
|
|
PreparedUndoKind::Create { delete } => (
|
|
UndoInverseKind::DeleteCreated,
|
|
vec![SourceItem::new(
|
|
delete.nodes()[0].id(),
|
|
delete.nodes()[0].location().clone(),
|
|
)],
|
|
None,
|
|
UndoConsequence::RemoveOnlyUnchangedCreatedEntry,
|
|
),
|
|
PreparedUndoKind::Rename { rename } => (
|
|
UndoInverseKind::RenameBack,
|
|
vec![SourceItem::new(
|
|
rename.nodes()[0].id(),
|
|
rename.source().clone(),
|
|
)],
|
|
rename.destination().cloned(),
|
|
UndoConsequence::RestoreOriginalName,
|
|
),
|
|
PreparedUndoKind::Move { operation } => (
|
|
UndoInverseKind::MoveBack,
|
|
operation.plan().sources().to_vec(),
|
|
Some(operation.plan().destination().clone()),
|
|
UndoConsequence::ReturnToOriginalLocation,
|
|
),
|
|
PreparedUndoKind::Trash { restores, .. } => (
|
|
UndoInverseKind::RestoreTrash,
|
|
restores
|
|
.iter()
|
|
.map(|restore| SourceItem::new(restore.id(), restore.trashed().clone()))
|
|
.collect(),
|
|
restores[0].original().parent(),
|
|
UndoConsequence::RestoreOriginalPathFromTrash,
|
|
),
|
|
};
|
|
let source = sources[0].location().clone();
|
|
Self {
|
|
plan_id: prepared.id(),
|
|
generation: prepared.generation(),
|
|
inverse_kind,
|
|
source,
|
|
destination,
|
|
sources,
|
|
consequence,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Reducer-owned state for preparation and explicit confirmation of one undo.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct UndoSession {
|
|
pub request: PrepareUndoRequest,
|
|
pub summary: Option<UndoSummary>,
|
|
pub notice: Option<String>,
|
|
}
|
|
|
|
/// Successful mutation snapshots delivered by runtime after post-mutation metadata is available.
|
|
/// These commands never change the original mutation's task result.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub enum UndoRecord {
|
|
Create {
|
|
plan: EntryMutationPlan,
|
|
created_fingerprint: EntryFingerprint,
|
|
outcomes: Vec<crate::operations::ItemOutcome>,
|
|
},
|
|
Rename {
|
|
plan: EntryMutationPlan,
|
|
renamed_fingerprint: EntryFingerprint,
|
|
outcomes: Vec<crate::operations::ItemOutcome>,
|
|
},
|
|
Move {
|
|
operation: crate::operations::PreparedOperation,
|
|
destination_fingerprints: BTreeMap<ItemId, EntryFingerprint>,
|
|
outcomes: Vec<crate::operations::ItemOutcome>,
|
|
},
|
|
Trash {
|
|
plan: EntryMutationPlan,
|
|
records: Vec<TrashRecord>,
|
|
outcomes: Vec<crate::operations::ItemOutcome>,
|
|
},
|
|
}
|
|
|
|
impl UndoRecord {
|
|
pub fn plan_id(&self) -> PlanId {
|
|
match self {
|
|
Self::Create { plan, .. } | Self::Rename { plan, .. } | Self::Trash { plan, .. } => {
|
|
plan.id()
|
|
}
|
|
Self::Move { operation, .. } => operation.plan().id(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum DeferredActionKind {
|
|
Undo,
|
|
Edit,
|
|
}
|
|
|
|
/// Empty undo history or a non-executable editor validation/process error notice.
|
|
/// Regular-file editing uses `EditorSessionState`; no editor intent is deferred by this type.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct DeferredAction {
|
|
pub kind: DeferredActionKind,
|
|
pub pane: PaneId,
|
|
pub target: Option<Location>,
|
|
pub entry_kind: Option<EntryKind>,
|
|
pub message: String,
|
|
}
|
|
|
|
impl AppState {
|
|
pub(crate) fn begin_create(&mut self, kind: EntryMutationKind) -> Vec<Effect> {
|
|
if self.overlay != Overlay::None {
|
|
return Vec::new();
|
|
}
|
|
debug_assert!(matches!(
|
|
kind,
|
|
EntryMutationKind::CreateFile | EntryMutationKind::CreateDirectory
|
|
));
|
|
let pane = self.active;
|
|
let snapshot = pane_snapshot(pane, self.pane(pane));
|
|
self.entry_action = Some(EntryActionSession {
|
|
kind,
|
|
pane,
|
|
snapshot,
|
|
source_entry: None,
|
|
sources: Vec::new(),
|
|
input: String::new(),
|
|
request: None,
|
|
plan: None,
|
|
trash_records: Vec::new(),
|
|
notice: None,
|
|
});
|
|
self.deferred_action = None;
|
|
self.set_overlay(Overlay::EntryPrompt);
|
|
Vec::new()
|
|
}
|
|
|
|
pub(crate) fn begin_rename(&mut self) -> Vec<Effect> {
|
|
if self.overlay != Overlay::None {
|
|
return Vec::new();
|
|
}
|
|
let pane_id = self.active;
|
|
let pane = self.pane(pane_id);
|
|
let Some(entry) = pane.selected_entry().cloned() else {
|
|
self.active_pane_mut().error = Some(FsError::Other(
|
|
"Cannot rename without a hovered entry".into(),
|
|
));
|
|
return Vec::new();
|
|
};
|
|
let snapshot = pane_snapshot(pane_id, pane);
|
|
let source = snapshot.location.entry_id(&entry.name);
|
|
let (input, notice) = match entry.name.to_str() {
|
|
Some(name) => (name.to_owned(), None),
|
|
None => (
|
|
String::new(),
|
|
Some("Non-UTF-8 name: enter a complete new UTF-8 name; original bytes are preserved until then".into()),
|
|
),
|
|
};
|
|
self.entry_action = Some(EntryActionSession {
|
|
kind: EntryMutationKind::Rename,
|
|
pane: pane_id,
|
|
snapshot,
|
|
source_entry: Some(entry.clone()),
|
|
sources: vec![source],
|
|
input,
|
|
request: None,
|
|
plan: None,
|
|
trash_records: Vec::new(),
|
|
notice,
|
|
});
|
|
self.deferred_action = None;
|
|
self.set_overlay(Overlay::EntryPrompt);
|
|
Vec::new()
|
|
}
|
|
|
|
pub(crate) fn prepare_entry_action(&mut self, kind: EntryMutationKind) -> Vec<Effect> {
|
|
if self.overlay != Overlay::None {
|
|
return Vec::new();
|
|
}
|
|
debug_assert!(matches!(
|
|
kind,
|
|
EntryMutationKind::Trash | EntryMutationKind::Delete
|
|
));
|
|
let pane_id = self.active;
|
|
let sources = self.active_operation_sources();
|
|
if sources.is_empty() {
|
|
self.active_pane_mut().error = Some(FsError::Other(
|
|
"Cannot prepare an action without a hovered or selected entry".into(),
|
|
));
|
|
return Vec::new();
|
|
}
|
|
|
|
let snapshot = pane_snapshot(pane_id, self.pane(pane_id));
|
|
let (plan_id, generation) = self.allocate_entry_plan_identity();
|
|
let endpoint_epochs = snapshot_epochs(&snapshot);
|
|
let request = PrepareEntryActionRequest {
|
|
plan_id,
|
|
generation,
|
|
kind,
|
|
pane: snapshot.clone(),
|
|
sources: sources.clone(),
|
|
endpoint_epochs,
|
|
};
|
|
self.entry_action = Some(EntryActionSession {
|
|
kind,
|
|
pane: pane_id,
|
|
snapshot,
|
|
source_entry: None,
|
|
sources,
|
|
input: String::new(),
|
|
request: Some(request.clone()),
|
|
plan: None,
|
|
trash_records: Vec::new(),
|
|
notice: None,
|
|
});
|
|
self.deferred_action = None;
|
|
self.set_overlay(Overlay::ConfirmEntryAction);
|
|
vec![Effect::PrepareEntryAction(request)]
|
|
}
|
|
|
|
pub(crate) fn entry_input(&mut self, character: char) {
|
|
if self.overlay == Overlay::EntryPrompt
|
|
&& let Some(session) = &mut self.entry_action
|
|
&& matches!(
|
|
session.kind,
|
|
EntryMutationKind::CreateFile
|
|
| EntryMutationKind::CreateDirectory
|
|
| EntryMutationKind::Rename
|
|
)
|
|
{
|
|
session.input.push(character);
|
|
session.notice = None;
|
|
}
|
|
}
|
|
|
|
pub(crate) fn entry_backspace(&mut self) {
|
|
if self.overlay == Overlay::EntryPrompt
|
|
&& let Some(session) = &mut self.entry_action
|
|
{
|
|
session.input.pop();
|
|
session.notice = None;
|
|
}
|
|
}
|
|
|
|
pub(crate) fn submit_entry(&mut self) -> Vec<Effect> {
|
|
if self.overlay != Overlay::EntryPrompt {
|
|
return Vec::new();
|
|
}
|
|
let Some(session) = &self.entry_action else {
|
|
self.set_overlay(Overlay::None);
|
|
return Vec::new();
|
|
};
|
|
if !self.entry_snapshot_is_current(&session.snapshot) {
|
|
self.set_entry_notice("The pane changed; start the action again".into());
|
|
return Vec::new();
|
|
}
|
|
|
|
let name = OsString::from(&session.input);
|
|
if let Err(error) = validate_entry_name(&name) {
|
|
self.set_entry_notice(format!("Invalid entry name: {error}"));
|
|
return Vec::new();
|
|
}
|
|
let (plan_id, generation) = self.allocate_entry_plan_identity();
|
|
let Some(session) = &self.entry_action else {
|
|
return Vec::new();
|
|
};
|
|
let epochs = snapshot_epochs(&session.snapshot);
|
|
let result = match session.kind {
|
|
EntryMutationKind::CreateFile | EntryMutationKind::CreateDirectory => {
|
|
EntryMutationPlan::create(
|
|
plan_id,
|
|
generation,
|
|
session.kind,
|
|
ItemId::new(1),
|
|
session.snapshot.location.clone(),
|
|
name,
|
|
epochs,
|
|
)
|
|
}
|
|
EntryMutationKind::Rename => {
|
|
let Some(entry) = &session.source_entry else {
|
|
self.set_entry_notice("The rename source is no longer available".into());
|
|
return Vec::new();
|
|
};
|
|
let source = session.snapshot.location.join(&entry.name);
|
|
if !self.rename_source_is_current(session) {
|
|
self.set_entry_notice("The hovered entry changed; start rename again".into());
|
|
return Vec::new();
|
|
}
|
|
EntryMutationPlan::rename(
|
|
plan_id,
|
|
generation,
|
|
ItemId::new(1),
|
|
source,
|
|
EntryFingerprint::from(entry),
|
|
name,
|
|
epochs,
|
|
)
|
|
}
|
|
EntryMutationKind::Trash | EntryMutationKind::Delete => {
|
|
self.set_entry_notice("This action requires read-only preparation".into());
|
|
return Vec::new();
|
|
}
|
|
};
|
|
match result {
|
|
Ok(plan) => {
|
|
if let Some(session) = &mut self.entry_action {
|
|
session.plan = Some(plan);
|
|
session.notice = None;
|
|
}
|
|
self.set_overlay(Overlay::ConfirmEntryAction);
|
|
}
|
|
Err(error) => self.set_entry_notice(format!("Invalid entry name: {error}")),
|
|
}
|
|
Vec::new()
|
|
}
|
|
|
|
pub(crate) fn finish_entry_action_preparation(
|
|
&mut self,
|
|
result: PrepareEntryActionResult,
|
|
) -> Vec<Effect> {
|
|
let Some(session) = &self.entry_action else {
|
|
return Vec::new();
|
|
};
|
|
if session.request.as_ref() != Some(&result.request) {
|
|
return Vec::new();
|
|
}
|
|
if !self.entry_snapshot_is_current(&session.snapshot) {
|
|
self.set_entry_notice("The pane changed; the prepared action is stale".into());
|
|
return Vec::new();
|
|
}
|
|
|
|
match result.result {
|
|
Err(error) => {
|
|
self.set_entry_notice(format!("Preparation failed: {}", error.user_message()));
|
|
}
|
|
Ok(prepared) => {
|
|
if !prepared_plan_matches(&prepared.plan, &result.request) {
|
|
self.set_entry_notice("Planner returned a mismatched entry action".into());
|
|
return Vec::new();
|
|
}
|
|
if let Some(session) = &mut self.entry_action {
|
|
session.plan = Some(prepared.plan);
|
|
session.trash_records = prepared.trash_records;
|
|
session.notice = None;
|
|
}
|
|
self.set_overlay(Overlay::ConfirmEntryAction);
|
|
}
|
|
}
|
|
Vec::new()
|
|
}
|
|
|
|
pub(crate) fn confirm_entry_action(&mut self) -> Vec<Effect> {
|
|
if self.overlay != Overlay::ConfirmEntryAction {
|
|
return Vec::new();
|
|
}
|
|
let Some(session) = &self.entry_action else {
|
|
return Vec::new();
|
|
};
|
|
if !self.entry_snapshot_is_current(&session.snapshot) {
|
|
self.set_entry_notice("The pane changed; start the action again".into());
|
|
return Vec::new();
|
|
}
|
|
if session.kind == EntryMutationKind::Rename && !self.rename_source_is_current(session) {
|
|
self.set_entry_notice("The hovered entry changed; start rename again".into());
|
|
return Vec::new();
|
|
}
|
|
let Some(plan) = session.plan.clone() else {
|
|
return Vec::new();
|
|
};
|
|
if self.confirmed_plans.contains(&plan.id()) {
|
|
return Vec::new();
|
|
}
|
|
let work_kind = match plan.kind() {
|
|
EntryMutationKind::CreateFile | EntryMutationKind::CreateDirectory => WorkKind::Create,
|
|
EntryMutationKind::Rename => WorkKind::Rename,
|
|
EntryMutationKind::Trash => WorkKind::Trash,
|
|
EntryMutationKind::Delete => WorkKind::Delete,
|
|
};
|
|
let work = match work_plan(&plan, work_kind) {
|
|
Ok(work) => work,
|
|
Err(error) => {
|
|
self.set_entry_notice(format!("Cannot queue entry action: {error}"));
|
|
return Vec::new();
|
|
}
|
|
};
|
|
|
|
let pane = session.pane;
|
|
let trash_records = session.trash_records.clone();
|
|
self.confirmed_plans.insert(plan.id());
|
|
self.entry_action_plans.insert(plan.id(), plan.clone());
|
|
self.entry_action_panes.insert(plan.id(), pane);
|
|
self.entry_action_records.insert(plan.id(), trash_records);
|
|
self.task_manager.start_work(work);
|
|
self.entry_action = None;
|
|
self.set_overlay(Overlay::None);
|
|
self.next_execution_effect().into_iter().collect()
|
|
}
|
|
|
|
pub(crate) fn cancel_entry_action(&mut self) {
|
|
self.entry_action = None;
|
|
self.set_overlay(Overlay::None);
|
|
}
|
|
|
|
pub(crate) fn begin_undo(&mut self) -> Vec<Effect> {
|
|
if self.overlay != Overlay::None {
|
|
return Vec::new();
|
|
}
|
|
let Some(entry) = self.undo_journal.latest().cloned() else {
|
|
self.deferred_action = Some(DeferredAction {
|
|
kind: DeferredActionKind::Undo,
|
|
pane: self.active,
|
|
target: None,
|
|
entry_kind: None,
|
|
message: "Undo history is empty. No action was performed.".into(),
|
|
});
|
|
self.set_overlay(Overlay::Undo);
|
|
return Vec::new();
|
|
};
|
|
|
|
let (plan_id, generation) = self.allocate_entry_plan_identity();
|
|
let request = PrepareUndoRequest {
|
|
plan_id,
|
|
generation,
|
|
entry,
|
|
};
|
|
self.undo = Some(UndoSession {
|
|
request: request.clone(),
|
|
summary: None,
|
|
notice: None,
|
|
});
|
|
self.deferred_action = None;
|
|
self.set_overlay(Overlay::Undo);
|
|
vec![Effect::PrepareUndo(request)]
|
|
}
|
|
|
|
pub(crate) fn finish_undo_preparation(&mut self, result: PrepareUndoResult) -> Vec<Effect> {
|
|
let Some(session) = &self.undo else {
|
|
return vec![Effect::DiscardUndo(result.request.plan_id)];
|
|
};
|
|
if session.request != result.request {
|
|
return vec![Effect::DiscardUndo(result.request.plan_id)];
|
|
}
|
|
if self.undo_journal.latest() != Some(&result.request.entry) {
|
|
if let Some(session) = &mut self.undo {
|
|
session.summary = None;
|
|
session.notice =
|
|
Some("Undo history changed; start again for a fresh review".into());
|
|
}
|
|
return vec![Effect::DiscardUndo(result.request.plan_id)];
|
|
}
|
|
|
|
match result.result {
|
|
Err(error) => {
|
|
if let Some(session) = &mut self.undo {
|
|
session.summary = None;
|
|
session.notice =
|
|
Some(format!("Undo preparation failed: {}", error.user_message()));
|
|
}
|
|
Vec::new()
|
|
}
|
|
Ok(summary) if undo_summary_matches(&summary, &result.request) => {
|
|
if let Some(session) = &mut self.undo {
|
|
session.summary = Some(summary);
|
|
session.notice = None;
|
|
}
|
|
Vec::new()
|
|
}
|
|
Ok(_) => {
|
|
if let Some(session) = &mut self.undo {
|
|
session.summary = None;
|
|
session.notice = Some("Undo planner returned a mismatched summary".into());
|
|
}
|
|
vec![Effect::DiscardUndo(result.request.plan_id)]
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn confirm_undo(&mut self) -> Vec<Effect> {
|
|
if self.overlay != Overlay::Undo {
|
|
return Vec::new();
|
|
}
|
|
let Some(session) = self.undo.clone() else {
|
|
// The empty-journal notice retains the old deferred-overlay Enter-to-close behavior.
|
|
self.deferred_action = None;
|
|
self.set_overlay(Overlay::None);
|
|
return Vec::new();
|
|
};
|
|
if self.undo_journal.latest() != Some(&session.request.entry) {
|
|
if let Some(session) = &mut self.undo {
|
|
session.summary = None;
|
|
session.notice = Some("The undo entry is stale; start again".into());
|
|
}
|
|
return vec![Effect::DiscardUndo(session.request.plan_id)];
|
|
}
|
|
let Some(summary) = session.summary else {
|
|
return Vec::new();
|
|
};
|
|
let plan_id = session.request.plan_id;
|
|
if self.confirmed_plans.contains(&plan_id) {
|
|
return Vec::new();
|
|
}
|
|
let work = match undo_work_plan(&session.request.entry, &summary) {
|
|
Ok(work) => work,
|
|
Err(error) => {
|
|
if let Some(session) = &mut self.undo {
|
|
session.summary = None;
|
|
session.notice = Some(format!("Cannot queue undo: {error}"));
|
|
}
|
|
return vec![Effect::DiscardUndo(plan_id)];
|
|
}
|
|
};
|
|
|
|
self.confirmed_plans.insert(plan_id);
|
|
self.undo_plans
|
|
.insert(plan_id, (work, session.request.entry));
|
|
self.entry_action_panes.insert(plan_id, self.active);
|
|
self.task_manager.start_work(
|
|
self.undo_plans
|
|
.get(&plan_id)
|
|
.expect("the confirmed undo was just inserted")
|
|
.0
|
|
.clone(),
|
|
);
|
|
self.undo = None;
|
|
self.set_overlay(Overlay::None);
|
|
self.next_execution_effect().into_iter().collect()
|
|
}
|
|
|
|
pub(crate) fn cancel_undo(&mut self) -> Vec<Effect> {
|
|
let plan_id = self.undo.take().map(|session| session.request.plan_id);
|
|
self.deferred_action = None;
|
|
self.set_overlay(Overlay::None);
|
|
plan_id.map(Effect::DiscardUndo).into_iter().collect()
|
|
}
|
|
|
|
pub(crate) fn record_undo(&mut self, record: UndoRecord) {
|
|
let plan_id = record.plan_id();
|
|
let result = match record {
|
|
UndoRecord::Create {
|
|
plan,
|
|
created_fingerprint,
|
|
outcomes,
|
|
} => self
|
|
.undo_journal
|
|
.record_create(plan, created_fingerprint, &outcomes),
|
|
UndoRecord::Rename {
|
|
plan,
|
|
renamed_fingerprint,
|
|
outcomes,
|
|
} => self
|
|
.undo_journal
|
|
.record_rename(plan, renamed_fingerprint, &outcomes),
|
|
UndoRecord::Move {
|
|
operation,
|
|
destination_fingerprints,
|
|
outcomes,
|
|
} => self
|
|
.undo_journal
|
|
.record_move(operation, destination_fingerprints, &outcomes),
|
|
UndoRecord::Trash {
|
|
plan,
|
|
records,
|
|
outcomes,
|
|
} => self.undo_journal.record_trash(plan, records, &outcomes),
|
|
};
|
|
let task_id = self
|
|
.task_manager
|
|
.tasks()
|
|
.find(|task| task.plan().id() == plan_id)
|
|
.map(|task| task.id());
|
|
for warning in self.undo_journal.take_warnings() {
|
|
if let Some(task_id) = task_id {
|
|
self.add_task_diagnostic(super::TaskDiagnostic {
|
|
task_id,
|
|
message: warning,
|
|
partial: None,
|
|
});
|
|
} else {
|
|
self.active_pane_mut().error = Some(FsError::Other(warning));
|
|
}
|
|
}
|
|
if let Err(error) = result {
|
|
let message =
|
|
format!("Mutation completed, but its undo entry could not be recorded: {error}");
|
|
if let Some(task_id) = task_id {
|
|
self.add_task_diagnostic(super::TaskDiagnostic {
|
|
task_id,
|
|
message,
|
|
partial: None,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn consume_undo_success(&mut self, plan_id: PlanId, expected: UndoEntry) {
|
|
let Some(task) = self
|
|
.task_manager
|
|
.tasks()
|
|
.find(|task| task.plan().id() == plan_id)
|
|
else {
|
|
return;
|
|
};
|
|
if task.plan().work_kind() != WorkKind::Undo
|
|
|| task.status() != TaskStatus::Running
|
|
|| task.plan().sources().iter().any(|source| {
|
|
task.outcome(source.id()).is_none_or(|outcome| {
|
|
outcome.phase() != crate::operations::ItemPhase::Succeeded
|
|
|| outcome.commit() != crate::operations::CommitOutcome::Committed
|
|
|| !matches!(
|
|
outcome.source_cleanup(),
|
|
crate::operations::SourceCleanupOutcome::NotRequired
|
|
| crate::operations::SourceCleanupOutcome::Removed
|
|
)
|
|
})
|
|
})
|
|
{
|
|
return;
|
|
}
|
|
self.undo_journal.consume_latest(&expected);
|
|
}
|
|
|
|
pub(crate) fn begin_edit(&mut self) -> Vec<Effect> {
|
|
if self.overlay != Overlay::None {
|
|
return Vec::new();
|
|
}
|
|
let pane_id = self.active;
|
|
let pane = self.pane(pane_id);
|
|
let Some(entry) = pane.selected_entry().cloned() else {
|
|
self.show_editor_error(
|
|
pane_id,
|
|
None,
|
|
None,
|
|
"Select a regular file to edit. No editor was started.".into(),
|
|
);
|
|
return Vec::new();
|
|
};
|
|
if entry.kind != EntryKind::File {
|
|
let target = pane.location.join(&entry.name);
|
|
self.show_editor_error(
|
|
pane_id,
|
|
Some(target),
|
|
Some(entry.kind),
|
|
"External editing supports only regular files. No editor was started.".into(),
|
|
);
|
|
return Vec::new();
|
|
}
|
|
if matches!(pane.location.source, SourceId::Ssh(_)) && pane.session_epoch.is_none() {
|
|
self.show_editor_error(
|
|
pane_id,
|
|
Some(pane.location.join(&entry.name)),
|
|
Some(entry.kind),
|
|
FsError::Disconnected.user_message(),
|
|
);
|
|
return Vec::new();
|
|
}
|
|
|
|
let request = PrepareEditorRequest {
|
|
pane: pane_id,
|
|
source: pane.location.join(&entry.name),
|
|
entry,
|
|
listing_generation: pane.listing_generation,
|
|
session_epoch: pane.session_epoch,
|
|
};
|
|
self.editor = Some(EditorSessionState {
|
|
request: request.clone(),
|
|
prepared: None,
|
|
phase: EditorPhase::Preparing,
|
|
});
|
|
self.deferred_action = None;
|
|
self.set_overlay(Overlay::Edit);
|
|
vec![Effect::PrepareEditor(request)]
|
|
}
|
|
|
|
pub(crate) fn finish_editor_preparation(&mut self, result: PrepareEditorResult) -> Vec<Effect> {
|
|
let workspace_id = result
|
|
.result
|
|
.as_ref()
|
|
.ok()
|
|
.and_then(|prepared| prepared.workspace_id);
|
|
if self
|
|
.editor
|
|
.as_ref()
|
|
.is_none_or(|session| session.request != result.request)
|
|
{
|
|
return cleanup_workspace(workspace_id);
|
|
}
|
|
if !self.editor_source_is_current(&result.request) {
|
|
self.editor = None;
|
|
self.show_editor_error(
|
|
result.request.pane,
|
|
Some(result.request.source.clone()),
|
|
Some(result.request.entry.kind),
|
|
"The pane or hovered entry changed; start editing again".into(),
|
|
);
|
|
return cleanup_workspace(workspace_id);
|
|
}
|
|
|
|
match result.result {
|
|
Err(error) => {
|
|
let request = result.request;
|
|
self.editor = None;
|
|
self.show_editor_error(
|
|
request.pane,
|
|
Some(request.source.clone()),
|
|
Some(request.entry.kind),
|
|
format!("Cannot prepare editor: {}", error.user_message()),
|
|
);
|
|
Vec::new()
|
|
}
|
|
Ok(prepared) => {
|
|
let valid = match result.request.source.source {
|
|
SourceId::Local => {
|
|
prepared.workspace_id.is_none()
|
|
&& prepared.path == result.request.source.path
|
|
}
|
|
SourceId::Ssh(_) => prepared.workspace_id.is_some(),
|
|
};
|
|
if !valid {
|
|
self.editor = None;
|
|
self.show_editor_error(
|
|
result.request.pane,
|
|
Some(result.request.source.clone()),
|
|
Some(result.request.entry.kind),
|
|
"Editor preparation returned a mismatched workspace".into(),
|
|
);
|
|
return cleanup_workspace(workspace_id);
|
|
}
|
|
let request = result.request;
|
|
self.editor = Some(EditorSessionState {
|
|
request: request.clone(),
|
|
prepared: Some(prepared.clone()),
|
|
phase: EditorPhase::Running,
|
|
});
|
|
vec![Effect::RunEditor(RunEditorRequest { request, prepared })]
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn finish_editor_process(&mut self, result: EditorProcessResult) -> Vec<Effect> {
|
|
if !self.editor_request_is_running(&result.request) {
|
|
return cleanup_workspace(result.workspace_id);
|
|
}
|
|
if !self.editor_source_is_current(&result.request) {
|
|
let request = result.request;
|
|
self.take_editor_workspace();
|
|
self.show_editor_error(
|
|
request.pane,
|
|
Some(request.source.clone()),
|
|
Some(request.entry.kind),
|
|
"The pane, source, or SSH session changed; editor result was discarded".into(),
|
|
);
|
|
return cleanup_workspace(result.workspace_id);
|
|
}
|
|
|
|
match result.result {
|
|
Ok(EditorOutcome::Success) if result.request.source.source == SourceId::Local => {
|
|
let pane = result.request.pane;
|
|
let target = self.pane(pane).location.clone();
|
|
self.editor = None;
|
|
self.set_overlay(Overlay::None);
|
|
self.request_listing(pane, target, None)
|
|
.into_iter()
|
|
.collect()
|
|
}
|
|
Ok(EditorOutcome::Success) => {
|
|
let request = result.request;
|
|
let workspace = self.take_editor_workspace();
|
|
self.show_editor_error(
|
|
request.pane,
|
|
Some(request.source.clone()),
|
|
Some(request.entry.kind),
|
|
"Remote editor completed without temporary-file change detection".into(),
|
|
);
|
|
cleanup_workspace(workspace)
|
|
}
|
|
Ok(EditorOutcome::NonZero(code)) => {
|
|
let request = result.request;
|
|
let workspace = self.take_editor_workspace();
|
|
let message = code.map_or_else(
|
|
|| "Editor terminated without a success status".into(),
|
|
|code| format!("Editor exited with status {code}"),
|
|
);
|
|
self.show_editor_error(
|
|
request.pane,
|
|
Some(request.source),
|
|
Some(request.entry.kind),
|
|
message,
|
|
);
|
|
cleanup_workspace(workspace)
|
|
}
|
|
Err(error) => {
|
|
let request = result.request;
|
|
let workspace = self.take_editor_workspace();
|
|
self.show_editor_error(
|
|
request.pane,
|
|
Some(request.source),
|
|
Some(request.entry.kind),
|
|
format!("Cannot run editor: {}", error.user_message()),
|
|
);
|
|
cleanup_workspace(workspace)
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn finish_editor_change_detection(
|
|
&mut self,
|
|
result: EditorChangeResult,
|
|
) -> Vec<Effect> {
|
|
if !self.editor_request_is_running(&result.request) {
|
|
return cleanup_workspace(result.workspace_id);
|
|
}
|
|
if !self.editor_source_is_current(&result.request) {
|
|
let request = result.request;
|
|
self.take_editor_workspace();
|
|
self.show_editor_error(
|
|
request.pane,
|
|
Some(request.source.clone()),
|
|
Some(request.entry.kind),
|
|
"The pane, source, or SSH session changed; editor result was discarded".into(),
|
|
);
|
|
return cleanup_workspace(result.workspace_id);
|
|
}
|
|
match result.result {
|
|
Ok(false) => {
|
|
let workspace = self.take_editor_workspace();
|
|
self.set_overlay(Overlay::None);
|
|
cleanup_workspace(workspace)
|
|
}
|
|
Ok(true) => {
|
|
if let Some(session) = &mut self.editor {
|
|
session.phase = EditorPhase::ConfirmUpload;
|
|
}
|
|
self.set_overlay(Overlay::Edit);
|
|
Vec::new()
|
|
}
|
|
Err(error) => {
|
|
let request = result.request;
|
|
let workspace = self.take_editor_workspace();
|
|
self.show_editor_error(
|
|
request.pane,
|
|
Some(request.source),
|
|
Some(request.entry.kind),
|
|
format!(
|
|
"Cannot inspect edited temporary file: {}",
|
|
error.user_message()
|
|
),
|
|
);
|
|
cleanup_workspace(workspace)
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn confirm_edit_upload(&mut self) -> Vec<Effect> {
|
|
if self.overlay != Overlay::Edit {
|
|
return Vec::new();
|
|
}
|
|
let Some(session) = &self.editor else {
|
|
return Vec::new();
|
|
};
|
|
if session.phase != EditorPhase::ConfirmUpload {
|
|
return Vec::new();
|
|
}
|
|
let request = session.request.clone();
|
|
let Some(workspace_id) = session
|
|
.prepared
|
|
.as_ref()
|
|
.and_then(|prepared| prepared.workspace_id)
|
|
else {
|
|
return Vec::new();
|
|
};
|
|
if !self.editor_source_is_current(&request) {
|
|
let workspace = self.take_editor_workspace();
|
|
self.show_editor_error(
|
|
request.pane,
|
|
Some(request.source),
|
|
Some(request.entry.kind),
|
|
"The pane, source, or SSH session changed; upload was not queued".into(),
|
|
);
|
|
return cleanup_workspace(workspace);
|
|
}
|
|
let Some(epoch) = request.session_epoch else {
|
|
return Vec::new();
|
|
};
|
|
let (plan_id, generation) = self.allocate_entry_plan_identity();
|
|
let work = match WorkPlan::new(
|
|
plan_id,
|
|
generation,
|
|
WorkKind::EditUpload,
|
|
vec![SourceItem::new(ItemId::new(1), request.source.clone())],
|
|
request.source.clone(),
|
|
CollisionPolicy::Overwrite,
|
|
Some(1),
|
|
None,
|
|
BTreeMap::from([(request.source.source.clone(), epoch)]),
|
|
) {
|
|
Ok(work) => work,
|
|
Err(error) => {
|
|
self.show_editor_error(
|
|
request.pane,
|
|
Some(request.source),
|
|
Some(request.entry.kind),
|
|
format!("Cannot queue editor upload: {error}"),
|
|
);
|
|
return cleanup_workspace(Some(workspace_id));
|
|
}
|
|
};
|
|
if self.confirmed_plans.contains(&plan_id) {
|
|
return Vec::new();
|
|
}
|
|
|
|
self.confirmed_plans.insert(plan_id);
|
|
self.edit_upload_workspaces.insert(plan_id, workspace_id);
|
|
self.entry_action_panes.insert(plan_id, request.pane);
|
|
self.task_manager.start_work(work);
|
|
self.editor = None;
|
|
self.set_overlay(Overlay::None);
|
|
self.next_execution_effect().into_iter().collect()
|
|
}
|
|
|
|
pub(crate) fn cancel_editor(&mut self) -> Vec<Effect> {
|
|
let workspace = self.take_editor_workspace();
|
|
self.deferred_action = None;
|
|
self.set_overlay(Overlay::None);
|
|
cleanup_workspace(workspace)
|
|
}
|
|
|
|
fn editor_request_is_running(&self, request: &PrepareEditorRequest) -> bool {
|
|
self.editor.as_ref().is_some_and(|session| {
|
|
session.phase == EditorPhase::Running
|
|
&& session.request == *request
|
|
&& session.prepared.is_some()
|
|
})
|
|
}
|
|
|
|
fn editor_source_is_current(&self, request: &PrepareEditorRequest) -> bool {
|
|
let pane = self.pane(request.pane);
|
|
pane.location.join(&request.entry.name) == request.source
|
|
&& pane.selected_entry() == Some(&request.entry)
|
|
&& pane.listing_generation == request.listing_generation
|
|
&& pane.session_epoch == request.session_epoch
|
|
}
|
|
|
|
fn take_editor_workspace(&mut self) -> Option<EditorWorkspaceId> {
|
|
let workspace = self
|
|
.editor
|
|
.as_ref()
|
|
.and_then(|session| session.prepared.as_ref())
|
|
.and_then(|prepared| prepared.workspace_id);
|
|
self.editor = None;
|
|
workspace
|
|
}
|
|
|
|
fn show_editor_error(
|
|
&mut self,
|
|
pane: PaneId,
|
|
target: Option<Location>,
|
|
entry_kind: Option<EntryKind>,
|
|
message: String,
|
|
) {
|
|
self.editor = None;
|
|
self.deferred_action = Some(DeferredAction {
|
|
kind: DeferredActionKind::Edit,
|
|
pane,
|
|
target,
|
|
entry_kind,
|
|
message,
|
|
});
|
|
self.set_overlay(Overlay::Edit);
|
|
}
|
|
|
|
pub(crate) fn close_deferred_action(&mut self) {
|
|
self.deferred_action = None;
|
|
self.set_overlay(Overlay::None);
|
|
}
|
|
|
|
/// Captures affected current locations before the manager advances its active queue entry.
|
|
/// The returned task is still checked for actual commits after the terminal event is reduced.
|
|
pub(crate) fn completion_refresh(&self, event: &OperationEvent) -> Vec<(TaskId, PaneId)> {
|
|
if !matches!(
|
|
event.kind,
|
|
crate::operations::OperationEventKind::Completed
|
|
| crate::operations::OperationEventKind::CancellationAcknowledged
|
|
) {
|
|
return Vec::new();
|
|
}
|
|
let Some(active) = self.task_manager.active_operation() else {
|
|
return Vec::new();
|
|
};
|
|
if active.operation_id != event.operation_id || active.plan_id != event.plan_id {
|
|
return Vec::new();
|
|
}
|
|
let Some(task) = self.task_manager.task(active.task_id) else {
|
|
return Vec::new();
|
|
};
|
|
if task.status() != TaskStatus::Running && task.status() != TaskStatus::Cancelling {
|
|
return Vec::new();
|
|
}
|
|
|
|
let mut candidates: Vec<(PaneId, Location, Option<SessionEpoch>)> = Vec::new();
|
|
if task.plan().work_kind() == WorkKind::CopyMove {
|
|
let Some(planned) = self.planned_operations.get(&event.plan_id) else {
|
|
return Vec::new();
|
|
};
|
|
let request = &planned.request;
|
|
candidates.push((
|
|
request.source.pane,
|
|
request.source.location.clone(),
|
|
request.source.session_epoch,
|
|
));
|
|
let destination_pane = request
|
|
.destination_pane
|
|
.as_ref()
|
|
.map_or(request.source.pane, |snapshot| snapshot.pane);
|
|
let destination_epoch = request
|
|
.destination_pane
|
|
.as_ref()
|
|
.map_or(request.source.session_epoch, |snapshot| {
|
|
snapshot.session_epoch
|
|
});
|
|
candidates.push((
|
|
destination_pane,
|
|
planned.plan.destination().clone(),
|
|
destination_epoch,
|
|
));
|
|
} else {
|
|
let Some(pane) = self.entry_action_panes.get(&event.plan_id).copied() else {
|
|
return Vec::new();
|
|
};
|
|
for source in task.plan().sources() {
|
|
if let Some(parent) = source.location().parent() {
|
|
let epoch = task.plan().endpoint_epoch(&parent.source);
|
|
candidates.push((pane, parent, epoch));
|
|
}
|
|
}
|
|
let destination = task.plan().destination().clone();
|
|
let destination = match task.plan().work_kind() {
|
|
WorkKind::Trash | WorkKind::Undo => Some(destination),
|
|
_ => destination.parent(),
|
|
};
|
|
if let Some(destination) = destination {
|
|
let epoch = task.plan().endpoint_epoch(&destination.source);
|
|
candidates.push((pane, destination, epoch));
|
|
}
|
|
}
|
|
|
|
let mut result = Vec::new();
|
|
for (pane, location, epoch) in candidates {
|
|
let current = self.pane(pane);
|
|
if current.location == location
|
|
&& current.session_epoch == epoch
|
|
&& !current.loading
|
|
&& current.pending_target.is_none()
|
|
&& !result.iter().any(|(_, existing)| existing == &pane)
|
|
{
|
|
result.push((active.task_id, pane));
|
|
}
|
|
}
|
|
result
|
|
}
|
|
|
|
pub(crate) fn completion_has_commits(&self, task_id: TaskId) -> bool {
|
|
self.task_manager.task(task_id).is_some_and(|task| {
|
|
task.outcomes()
|
|
.any(|outcome| outcome.commit() == CommitOutcome::Committed)
|
|
})
|
|
}
|
|
|
|
fn rename_source_is_current(&self, session: &EntryActionSession) -> bool {
|
|
let Some(expected) = &session.source_entry else {
|
|
return false;
|
|
};
|
|
self.pane(session.pane).selected_entry() == Some(expected)
|
|
}
|
|
|
|
fn entry_snapshot_is_current(&self, snapshot: &PaneSnapshot) -> bool {
|
|
let pane = self.pane(snapshot.pane);
|
|
pane.location == snapshot.location
|
|
&& pane.listing_generation == snapshot.listing_generation
|
|
&& pane.session_epoch == snapshot.session_epoch
|
|
}
|
|
|
|
fn set_entry_notice(&mut self, notice: String) {
|
|
if let Some(session) = &mut self.entry_action {
|
|
session.plan = None;
|
|
session.notice = Some(notice);
|
|
}
|
|
}
|
|
|
|
fn allocate_entry_plan_identity(&mut self) -> (PlanId, PlanGeneration) {
|
|
let id = PlanId::new(self.next_plan_id);
|
|
self.next_plan_id = self
|
|
.next_plan_id
|
|
.checked_add(1)
|
|
.expect("entry plan identifier space exhausted");
|
|
let generation = PlanGeneration::new(self.next_plan_generation);
|
|
self.next_plan_generation = next_generation(self.next_plan_generation);
|
|
(id, generation)
|
|
}
|
|
}
|
|
|
|
fn cleanup_workspace(workspace_id: Option<EditorWorkspaceId>) -> Vec<Effect> {
|
|
workspace_id
|
|
.map(Effect::CleanupEditorWorkspace)
|
|
.into_iter()
|
|
.collect()
|
|
}
|
|
|
|
fn pane_snapshot(pane: PaneId, state: &PaneState) -> PaneSnapshot {
|
|
PaneSnapshot {
|
|
pane,
|
|
location: state.location.clone(),
|
|
listing_generation: state.listing_generation,
|
|
session_epoch: state.session_epoch,
|
|
}
|
|
}
|
|
|
|
fn snapshot_epochs(snapshot: &PaneSnapshot) -> BTreeMap<SourceId, SessionEpoch> {
|
|
snapshot
|
|
.session_epoch
|
|
.map(|epoch| BTreeMap::from([(snapshot.location.source.clone(), epoch)]))
|
|
.unwrap_or_default()
|
|
}
|
|
|
|
fn prepared_plan_matches(plan: &EntryMutationPlan, request: &PrepareEntryActionRequest) -> bool {
|
|
plan.id() == request.plan_id
|
|
&& plan.generation() == request.generation
|
|
&& plan.kind() == request.kind
|
|
&& plan.source() == &request.pane.location
|
|
&& plan.endpoint_epochs() == &request.endpoint_epochs
|
|
&& request.sources.iter().all(|source| {
|
|
plan.nodes().iter().any(|node| {
|
|
node.location().source == source.source && node.location().path == source.path
|
|
})
|
|
})
|
|
}
|
|
|
|
fn undo_summary_matches(summary: &UndoSummary, request: &PrepareUndoRequest) -> bool {
|
|
summary.plan_id == request.plan_id
|
|
&& summary.generation == request.generation
|
|
&& undo_sources(&request.entry).is_some_and(|sources| {
|
|
summary.sources == sources
|
|
&& summary.sources.first().map(SourceItem::location) == Some(&summary.source)
|
|
&& summary.destination == undo_destination(&request.entry)
|
|
&& summary.inverse_kind
|
|
== match &request.entry {
|
|
UndoEntry::Create { .. } => UndoInverseKind::DeleteCreated,
|
|
UndoEntry::Rename { .. } => UndoInverseKind::RenameBack,
|
|
UndoEntry::Move { .. } => UndoInverseKind::MoveBack,
|
|
UndoEntry::Trash { .. } => UndoInverseKind::RestoreTrash,
|
|
}
|
|
&& summary.consequence
|
|
== match &request.entry {
|
|
UndoEntry::Create { .. } => {
|
|
UndoConsequence::RemoveOnlyUnchangedCreatedEntry
|
|
}
|
|
UndoEntry::Rename { .. } => UndoConsequence::RestoreOriginalName,
|
|
UndoEntry::Move { .. } => UndoConsequence::ReturnToOriginalLocation,
|
|
UndoEntry::Trash { .. } => UndoConsequence::RestoreOriginalPathFromTrash,
|
|
}
|
|
})
|
|
}
|
|
|
|
fn undo_sources(entry: &UndoEntry) -> Option<Vec<SourceItem>> {
|
|
let sources = match entry {
|
|
UndoEntry::Create { plan, .. } | UndoEntry::Rename { plan, .. } => {
|
|
vec![SourceItem::new(
|
|
plan.nodes()[0].id(),
|
|
plan.destination()?.clone(),
|
|
)]
|
|
}
|
|
UndoEntry::Move { operation, .. } => operation
|
|
.plan()
|
|
.sources()
|
|
.iter()
|
|
.map(|source| {
|
|
operation
|
|
.items()
|
|
.iter()
|
|
.find(|item| item.id() == source.id())
|
|
.map(|item| SourceItem::new(source.id(), item.destination().clone()))
|
|
})
|
|
.collect::<Option<Vec<_>>>()?,
|
|
UndoEntry::Trash { plan, records } => plan
|
|
.nodes()
|
|
.iter()
|
|
.filter(|node| node.is_root())
|
|
.map(|node| {
|
|
records
|
|
.iter()
|
|
.find(|record| record.original() == node.location())
|
|
.map(|record| SourceItem::new(node.id(), record.trashed().clone()))
|
|
})
|
|
.collect::<Option<Vec<_>>>()?,
|
|
};
|
|
(!sources.is_empty()).then_some(sources)
|
|
}
|
|
|
|
fn undo_destination(entry: &UndoEntry) -> Option<Location> {
|
|
match entry {
|
|
UndoEntry::Create { .. } => None,
|
|
UndoEntry::Rename { plan, .. } => Some(plan.source().clone()),
|
|
UndoEntry::Move { operation, .. } => operation.plan().sources()[0].location().parent(),
|
|
UndoEntry::Trash { plan, .. } => Some(plan.source().clone()),
|
|
}
|
|
}
|
|
|
|
fn undo_work_plan(
|
|
entry: &UndoEntry,
|
|
summary: &UndoSummary,
|
|
) -> Result<WorkPlan, crate::operations::PlanValidationError> {
|
|
WorkPlan::new(
|
|
summary.plan_id,
|
|
summary.generation,
|
|
WorkKind::Undo,
|
|
summary.sources.clone(),
|
|
summary
|
|
.destination
|
|
.clone()
|
|
.unwrap_or_else(|| summary.source.clone()),
|
|
CollisionPolicy::Ask,
|
|
Some(summary.sources.len() as u64),
|
|
None,
|
|
entry.endpoint_epochs().clone(),
|
|
)
|
|
}
|
|
|
|
fn work_plan(
|
|
plan: &EntryMutationPlan,
|
|
kind: WorkKind,
|
|
) -> Result<WorkPlan, crate::operations::PlanValidationError> {
|
|
let sources = plan
|
|
.nodes()
|
|
.iter()
|
|
.filter(|node| !matches!(kind, WorkKind::Trash | WorkKind::Delete) || node.is_root())
|
|
.map(|node| SourceItem::new(node.id(), node.location().clone()))
|
|
.collect::<Vec<_>>();
|
|
let expected_items = sources.len() as u64;
|
|
let destination = plan
|
|
.destination()
|
|
.cloned()
|
|
.unwrap_or_else(|| plan.source().clone());
|
|
WorkPlan::new(
|
|
plan.id(),
|
|
plan.generation(),
|
|
kind,
|
|
sources,
|
|
destination,
|
|
CollisionPolicy::Skip,
|
|
Some(expected_items),
|
|
None,
|
|
plan.endpoint_epochs().clone(),
|
|
)
|
|
}
|