Skip to content

Commit

Permalink
Make overwrite protection configurable + small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
divarvel committed Feb 3, 2023
1 parent db1fbcf commit d04efed
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ on unix operating systems.
| `rulers` | List of column positions at which to display the rulers. Can be overridden by language specific `rulers` in `languages.toml` file. | `[]` |
| `bufferline` | Renders a line at the top of the editor displaying open buffers. Can be `always`, `never` or `multiple` (only shown if more than one buffer is in use) | `never` |
| `color-modes` | Whether to color the mode indicator with different colors depending on the mode itself | `false` |
| `prevent-external-modifications` | Prevent saving a file that has been modified from the outside, based on its last modification date (`mtime`). Disabling this check can be useful on systems where `mtime` values are unreliable | `true` |

### `[editor.statusline]` Section

Expand Down
2 changes: 1 addition & 1 deletion helix-term/tests/test/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ impl AppBuilder {
}
}

pub async fn run_event_loop_to_idle(app: &mut Application) {
pub async fn run_event_loop_until_idle(app: &mut Application) {
let (_, rx) = tokio::sync::mpsc::unbounded_channel();
let mut rx_stream = UnboundedReceiverStream::new(rx);
app.event_loop_until_idle(&mut rx_stream).await;
Expand Down
2 changes: 1 addition & 1 deletion helix-term/tests/test/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async fn test_overwrite_protection() -> anyhow::Result<()> {
.with_file(file.path(), None)
.build()?;

helpers::run_event_loop_to_idle(&mut app).await;
helpers::run_event_loop_until_idle(&mut app).await;

file.as_file_mut()
.write_all(helpers::platform_line("extremely important content").as_bytes())?;
Expand Down
7 changes: 5 additions & 2 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ impl fmt::Debug for Document {
.field("changes", &self.changes)
.field("old_state", &self.old_state)
// .field("history", &self.history)
.field("last_saved_time", &self.last_saved_time)
.field("last_saved_revision", &self.last_saved_revision)
.field("version", &self.version)
.field("modified_since_accessed", &self.modified_since_accessed)
Expand Down Expand Up @@ -585,6 +586,8 @@ impl Document {

let last_saved_time = self.last_saved_time;

let prevent_external_modifications = self.config.load().prevent_external_modifications;

// We encode the file according to the `Document`'s encoding.
let future = async move {
use tokio::{fs, fs::File};
Expand All @@ -600,11 +603,11 @@ impl Document {
}

// Protect against overwriting changes made externally
if !force {
if !force && prevent_external_modifications {
if let Ok(metadata) = fs::metadata(&path).await {
if let Ok(mtime) = metadata.modified() {
if last_saved_time < mtime {
bail!("file modified by an external process, use :w! to force");
bail!("file modified by an external process, use :w! to overwrite");
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ pub struct Config {
/// Whether to color modes with different colors. Defaults to `false`.
pub color_modes: bool,
pub soft_wrap: SoftWrap,
/// Whether to check for external modifications upon saving. Defaults to `true`.
pub prevent_external_modifications: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand Down Expand Up @@ -764,6 +766,7 @@ impl Default for Config {
indent_guides: IndentGuidesConfig::default(),
color_modes: false,
soft_wrap: SoftWrap::default(),
prevent_external_modifications: true,
}
}
}
Expand Down

0 comments on commit d04efed

Please sign in to comment.