Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change focus to modified docs on quit #3872

Merged
merged 4 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ops::Deref;

use super::*;

use helix_view::editor::{Action, ConfigEvent};
use helix_view::editor::{Action, CloseError, ConfigEvent};
use ui::completers::{self, Completer};

#[derive(Clone)]
Expand Down Expand Up @@ -71,8 +71,29 @@ fn buffer_close_by_ids_impl(
doc_ids: &[DocumentId],
force: bool,
) -> anyhow::Result<()> {
for &doc_id in doc_ids {
editor.close_document(doc_id, force)?;
let (modified_ids, modified_names): (Vec<_>, Vec<_>) = doc_ids
.iter()
.filter_map(|&doc_id| {
if let Err(CloseError::BufferModified(name)) = editor.close_document(doc_id, force) {
Some((doc_id, name))
} else {
None
}
})
.unzip();

if let Some(first) = modified_ids.first() {
let current = doc!(editor);
// If the current document is unmodified, and there are modified
// documents, switch focus to the first modified doc.
if !modified_ids.contains(&current.id()) {
editor.switch(*first, Action::Replace);
}
bail!(
"{} unsaved buffer(s) remaining: {:?}",
modified_names.len(),
modified_names
);
}

Ok(())
Expand Down Expand Up @@ -513,23 +534,26 @@ fn force_write_quit(
force_quit(cx, &[], event)
}

/// Results an error if there are modified buffers remaining and sets editor error,
/// otherwise returns `Ok(())`
/// Results in an error if there are modified buffers remaining and sets editor
/// error, otherwise returns `Ok(())`. If the current document is unmodified,
/// and there are modified documents, switches focus to one of them.
pub(super) fn buffers_remaining_impl(editor: &mut Editor) -> anyhow::Result<()> {
A-Walrus marked this conversation as resolved.
Show resolved Hide resolved
let modified: Vec<_> = editor
let (modified_ids, modified_names): (Vec<_>, Vec<_>) = editor
.documents()
.filter(|doc| doc.is_modified())
.map(|doc| {
doc.relative_path()
.map(|path| path.to_string_lossy().to_string())
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into())
})
.collect();
if !modified.is_empty() {
.map(|doc| (doc.id(), doc.display_name()))
.unzip();
if let Some(first) = modified_ids.first() {
let current = doc!(editor);
// If the current document is unmodified, and there are modified
// documents, switch focus to the first modified doc.
if !modified_ids.contains(&current.id()) {
editor.switch(*first, Action::Replace);
}
bail!(
"{} unsaved buffer(s) remaining: {:?}",
modified.len(),
modified
modified_names.len(),
modified_names
);
}
Ok(())
Expand Down
7 changes: 7 additions & 0 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use helix_core::auto_pairs::AutoPairs;
use helix_core::Range;
use serde::de::{self, Deserialize, Deserializer};
use serde::Serialize;
use std::borrow::Cow;
use std::cell::Cell;
use std::collections::HashMap;
use std::fmt::Display;
Expand Down Expand Up @@ -1036,6 +1037,12 @@ impl Document {
.map(helix_core::path::get_relative_path)
}

pub fn display_name(&self) -> Cow<'static, str> {
self.relative_path()
.map(|path| path.to_string_lossy().to_string().into())
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into())
}

// transact(Fn) ?

// -- LSP methods
Expand Down
23 changes: 13 additions & 10 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
clipboard::{get_clipboard_provider, ClipboardProvider},
document::{Mode, SCRATCH_BUFFER_NAME},
document::Mode,
graphics::{CursorKind, Rect},
info::Info,
input::KeyEvent,
Expand Down Expand Up @@ -28,7 +28,7 @@ use tokio::{
time::{sleep, Duration, Instant, Sleep},
};

use anyhow::{bail, Error};
use anyhow::Error;

pub use helix_core::diagnostic::Severity;
pub use helix_core::register::Registers;
Expand Down Expand Up @@ -686,6 +686,14 @@ pub enum Action {
VerticalSplit,
}

/// Error thrown on failed document closed
pub enum CloseError {
/// Document doesn't exist
DoesNotExist,
/// Buffer is modified
BufferModified(String),
}

impl Editor {
pub fn new(
mut area: Rect,
Expand Down Expand Up @@ -1044,19 +1052,14 @@ impl Editor {
self._refresh();
}

pub fn close_document(&mut self, doc_id: DocumentId, force: bool) -> anyhow::Result<()> {
pub fn close_document(&mut self, doc_id: DocumentId, force: bool) -> Result<(), CloseError> {
let doc = match self.documents.get(&doc_id) {
Some(doc) => doc,
None => bail!("document does not exist"),
None => return Err(CloseError::DoesNotExist),
};

if !force && doc.is_modified() {
bail!(
"buffer {:?} is modified",
doc.relative_path()
.map(|path| path.to_string_lossy().to_string())
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into())
);
return Err(CloseError::BufferModified(doc.display_name().into_owned()));
}

if let Some(language_server) = doc.language_server() {
Expand Down