Skip to content

Commit

Permalink
Don't complain if scratch is modified on autosave
Browse files Browse the repository at this point in the history
  • Loading branch information
groves committed Aug 4, 2022
1 parent c84f3e5 commit c6222f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 35 deletions.
51 changes: 19 additions & 32 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,28 +535,24 @@ pub(super) fn buffers_remaining_impl(editor: &mut Editor) -> anyhow::Result<()>
Ok(())
}

fn write_all_impl(
pub fn write_all_impl(
cx: &mut compositor::Context,
_args: &[Cow<str>],
event: PromptEvent,
quit: bool,
force: bool,
write_scratch: bool,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let mut errors = String::new();
let auto_format = cx.editor.config().auto_format;
let jobs = &mut cx.jobs;
// save all documents
for doc in &mut cx.editor.documents.values_mut() {
if doc.path().is_none() {
errors.push_str("cannot write a buffer without a filename\n");
if !doc.is_modified() {
continue;
}

if !doc.is_modified() {
if doc.path().is_none() {
if write_scratch {
errors.push_str("cannot write a buffer without a filename\n");
}
continue;
}

Expand All @@ -579,55 +575,46 @@ fn write_all_impl(
jobs.add(Job::new(future).wait_before_exiting());
}

if quit {
if !force {
buffers_remaining_impl(cx.editor)?;
}

// close all views
let views: Vec<_> = cx.editor.tree.views().map(|(view, _)| view.id).collect();
for view_id in views {
cx.editor.close(view_id);
}
match errors.len() {
0 => Ok(()),
_ => bail!(errors),
}

bail!(errors)
}

pub fn write_all(
fn write_all(
cx: &mut compositor::Context,
args: &[Cow<str>],
_args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

write_all_impl(cx, args, event, false, false)
write_all_impl(cx, false, true)
}

fn write_all_quit(
cx: &mut compositor::Context,
args: &[Cow<str>],
_args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

write_all_impl(cx, args, event, true, false)
write_all_impl(cx, false, true)?;
quit_all_impl(cx.editor, false)
}

fn force_write_all_quit(
cx: &mut compositor::Context,
args: &[Cow<str>],
_args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

write_all_impl(cx, args, event, true, true)
let _ = write_all_impl(cx, true, true);
quit_all_impl(cx.editor, true)
}

fn quit_all_impl(editor: &mut Editor, force: bool) -> anyhow::Result<()> {
Expand Down
4 changes: 1 addition & 3 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{
compositor::{Component, Context, EventResult},
job, key,
keymap::{KeymapResult, Keymaps},
ui::prompt::PromptEvent,
ui::{Completion, ProgressSpinners},
};

Expand Down Expand Up @@ -1237,8 +1236,7 @@ impl Component for EditorView {
Event::FocusGained => EventResult::Ignored(None),
Event::FocusLost => {
if context.editor.config().auto_save {
if let Err(e) = commands::typed::write_all(context, &[], PromptEvent::Validate)
{
if let Err(e) = commands::typed::write_all_impl(context, false, false) {
context.editor.set_error(format!("{}", e));
}
}
Expand Down

0 comments on commit c6222f5

Please sign in to comment.