aboutsummaryrefslogtreecommitdiff
path: root/helix-view
diff options
context:
space:
mode:
authorA-Walrus2022-10-03 15:07:21 +0000
committerGitHub2022-10-03 15:07:21 +0000
commit1d8bb2249b7153b2e9d3591534bdca32176c9975 (patch)
tree135488b20d03e43a8185eeb623f46614383e7b65 /helix-view
parent6764744ce9912a1f48502bf5ee025283e12fded5 (diff)
Change focus to modified docs on quit (#3872)
* Change focus to modified docs on quit When quitting with modified documents, automatically switch focus to one of them. * Update helix-term/src/commands/typed.rs Co-authored-by: Poliorcetics <poliorcetics@users.noreply.github.com> * Make it work with buffer-close-all and the like * Cleanup Use Cow instead of String, and rename DoesntExist -> DoesNotExist Co-authored-by: Poliorcetics <poliorcetics@users.noreply.github.com>
Diffstat (limited to 'helix-view')
-rw-r--r--helix-view/src/document.rs7
-rw-r--r--helix-view/src/editor.rs23
2 files changed, 20 insertions, 10 deletions
diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs
index 2ef99c6a..b6b2f664 100644
--- a/helix-view/src/document.rs
+++ b/helix-view/src/document.rs
@@ -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;
@@ -1038,6 +1039,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
diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs
index f21244a9..89759439 100644
--- a/helix-view/src/editor.rs
+++ b/helix-view/src/editor.rs
@@ -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,
@@ -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;
@@ -711,6 +711,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,
@@ -1070,19 +1078,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() {