style: rename writing_strategy -> backup_strategy

This commit is contained in:
aster 2025-02-28 15:25:24 +09:00
parent c8bb71755c
commit b983409980
2 changed files with 23 additions and 23 deletions

View file

@ -68,11 +68,11 @@ pub enum Mode {
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Serialize, serde::Deserialize)] #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)] #[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub enum WritingStrategy { pub enum BackupStrategy {
#[default] #[default]
MoveBackup, Move,
CopyBackup, Copy,
Overwrite, None,
} }
impl Display for Mode { impl Display for Mode {
@ -961,7 +961,7 @@ impl Document {
let encoding_with_bom_info = (self.encoding, self.has_bom); let encoding_with_bom_info = (self.encoding, self.has_bom);
let last_saved_time = self.last_saved_time; let last_saved_time = self.last_saved_time;
let writing_strategy_config = self.config.load().writing_strategy; let backup_strategy_config = self.config.load().backup_strategy;
// We encode the file according to the `Document`'s encoding. // We encode the file according to the `Document`'s encoding.
let future = async move { let future = async move {
use tokio::fs; use tokio::fs;
@ -1005,19 +1005,19 @@ impl Document {
)); ));
} }
let writing_strategy = { let backup_strategy = {
// Assume it is a hardlink to prevent data loss if the metadata cant be read (e.g. on certain Windows configurations) // Assume it is a hardlink to prevent data loss if the metadata cant be read (e.g. on certain Windows configurations)
let is_hardlink = helix_stdx::faccess::hardlink_count(&write_path).unwrap_or(2) > 1; let is_hardlink = helix_stdx::faccess::hardlink_count(&write_path).unwrap_or(2) > 1;
if is_hardlink { if is_hardlink {
WritingStrategy::CopyBackup BackupStrategy::Copy
} else { } else {
writing_strategy_config backup_strategy_config
} }
}; };
let backup = match writing_strategy { let backup = match backup_strategy {
WritingStrategy::MoveBackup | WritingStrategy::CopyBackup if path.exists() => { BackupStrategy::Move | BackupStrategy::Copy if path.exists() => {
let path_ = write_path.clone(); let path_ = write_path.clone();
// hacks: we use tempfile to handle the complex task of creating // hacks: we use tempfile to handle the complex task of creating
// non clobbered temporary path for us we don't want // non clobbered temporary path for us we don't want
@ -1029,14 +1029,14 @@ impl Document {
.prefix(path_.file_name()?) .prefix(path_.file_name()?)
.suffix(".bck") .suffix(".bck")
.make_in(path_.parent()?, |backup| { .make_in(path_.parent()?, |backup| {
match writing_strategy { match backup_strategy {
WritingStrategy::CopyBackup => { BackupStrategy::Copy => {
std::fs::copy(&path_, backup)?; std::fs::copy(&path_, backup)?;
} }
WritingStrategy::MoveBackup => { BackupStrategy::Move => {
std::fs::rename(&path_, backup)?; std::fs::rename(&path_, backup)?;
} }
WritingStrategy::Overwrite => unreachable!(), BackupStrategy::None => unreachable!(),
} }
Ok(()) Ok(())
}) })
@ -1067,20 +1067,20 @@ impl Document {
if let Err(err) = write_result { if let Err(err) = write_result {
if let Some(backup) = backup { if let Some(backup) = backup {
match writing_strategy { match backup_strategy {
WritingStrategy::CopyBackup => { BackupStrategy::Copy => {
// Restore backup // Restore backup
if let Err(e) = tokio::fs::copy(&backup, &write_path).await { if let Err(e) = tokio::fs::copy(&backup, &write_path).await {
log::error!("Failed to restore backup on write failure: {e}") log::error!("Failed to restore backup on write failure: {e}")
} }
} }
WritingStrategy::MoveBackup => { BackupStrategy::Move => {
// restore backup // restore backup
if let Err(e) = tokio::fs::rename(&backup, &write_path).await { if let Err(e) = tokio::fs::rename(&backup, &write_path).await {
log::error!("Failed to restore backup on write failure: {e}"); log::error!("Failed to restore backup on write failure: {e}");
} }
} }
WritingStrategy::Overwrite => unreachable!(), BackupStrategy::None => unreachable!(),
} }
} else { } else {
log::error!( log::error!(
@ -1089,7 +1089,7 @@ impl Document {
} }
} else if let Some(backup) = backup { } else if let Some(backup) = backup {
// backup exists & successfully saved. delete backup // backup exists & successfully saved. delete backup
if writing_strategy == WritingStrategy::MoveBackup { if backup_strategy == BackupStrategy::Move {
// the file is newly created one, therefore the metadata must be copied // the file is newly created one, therefore the metadata must be copied
let backup = backup.clone(); let backup = backup.clone();
let _ = tokio::task::spawn_blocking(move || { let _ = tokio::task::spawn_blocking(move || {

View file

@ -2,8 +2,8 @@ use crate::{
annotations::diagnostics::{DiagnosticFilter, InlineDiagnosticsConfig}, annotations::diagnostics::{DiagnosticFilter, InlineDiagnosticsConfig},
clipboard::ClipboardProvider, clipboard::ClipboardProvider,
document::{ document::{
DocumentOpenError, DocumentSavedEventFuture, DocumentSavedEventResult, Mode, SavePoint, BackupStrategy, DocumentOpenError, DocumentSavedEventFuture, DocumentSavedEventResult,
WritingStrategy, Mode, SavePoint,
}, },
events::{DocumentDidClose, DocumentDidOpen, DocumentFocusLost}, events::{DocumentDidClose, DocumentDidOpen, DocumentFocusLost},
graphics::{CursorKind, Rect}, graphics::{CursorKind, Rect},
@ -371,7 +371,7 @@ pub struct Config {
/// Whether to read settings from [EditorConfig](https://editorconfig.org) files. Defaults to /// Whether to read settings from [EditorConfig](https://editorconfig.org) files. Defaults to
/// `true`. /// `true`.
pub editor_config: bool, pub editor_config: bool,
pub writing_strategy: WritingStrategy, pub backup_strategy: BackupStrategy,
} }
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq, PartialOrd, Ord)]
@ -1019,7 +1019,7 @@ impl Default for Config {
end_of_line_diagnostics: DiagnosticFilter::Disable, end_of_line_diagnostics: DiagnosticFilter::Disable,
clipboard_provider: ClipboardProvider::default(), clipboard_provider: ClipboardProvider::default(),
editor_config: true, editor_config: true,
writing_strategy: WritingStrategy::default(), backup_strategy: BackupStrategy::default(),
} }
} }
} }