Deduplicate code

This commit is contained in:
Wojciech Kępka 2021-06-13 11:14:53 +02:00 committed by Blaž Hrastnik
parent 17c9a8499e
commit 52d3c29244

View File

@ -915,15 +915,7 @@ pub struct Command {
fn quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
// last view and we have unsaved changes
if editor.tree.views().count() == 1 {
let modified = _modified_left(editor);
if !modified.is_empty() {
let err = format!(
"{} unsaved buffer(s) remaining: {:?}",
modified.len(),
modified
);
editor.set_error(err);
if _buffers_remaining(editor) {
return;
}
}
@ -1025,8 +1017,10 @@ fn force_write_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
force_quit(editor, &[], event);
}
fn _modified_left(editor: &mut Editor) -> Vec<&str> {
editor
/// Returns `true` if there are modified buffers remaining and sets editor error,
/// otherwise returns `false`
fn _buffers_remaining(editor: &mut Editor) -> bool {
let modified: Vec<_> = editor
.documents()
.filter(|doc| doc.is_modified())
.map(|doc| {
@ -1034,7 +1028,18 @@ fn _modified_left(editor: &mut Editor) -> Vec<&str> {
.and_then(|path| path.to_str())
.unwrap_or("[scratch]")
})
.collect()
.collect();
if !modified.is_empty() {
let err = format!(
"{} unsaved buffer(s) remaining: {:?}",
modified.len(),
modified
);
editor.set_error(err);
true
} else {
false
}
}
fn _write_all(editor: &mut Editor, args: &[&str], event: PromptEvent, quit: bool, force: bool) {
@ -1052,16 +1057,7 @@ fn _write_all(editor: &mut Editor, args: &[&str], event: PromptEvent, quit: bool
if quit {
if !force {
// check if there are some unsaved buffers
let modified = _modified_left(editor);
if !modified.is_empty() {
let err = format!(
"{} unsaved buffer(s) remaining: {:?}",
modified.len(),
modified
);
editor.set_error(err);
if _buffers_remaining(editor) {
return;
}
}
@ -1088,15 +1084,7 @@ fn force_write_all_quit(editor: &mut Editor, args: &[&str], event: PromptEvent)
fn _quit_all(editor: &mut Editor, args: &[&str], event: PromptEvent, force: bool) {
if !force {
// check if there are some unsaved buffers
let modified = _modified_left(editor);
if !modified.is_empty() {
let err = format!(
"{} unsaved buffer(s) remaining: {:?}",
modified.len(),
modified
);
editor.set_error(err);
if _buffers_remaining(editor) {
return;
}
}