aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/commands.rs
diff options
context:
space:
mode:
Diffstat (limited to 'helix-term/src/commands.rs')
-rw-r--r--helix-term/src/commands.rs63
1 files changed, 34 insertions, 29 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 5073651b..87bbd6c6 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -47,12 +47,13 @@ use movement::Movement;
use crate::{
args,
compositor::{self, Component, Compositor},
+ job::Callback,
keymap::ReverseKeymap,
ui::{self, overlay::overlayed, FilePicker, Picker, Popup, Prompt, PromptEvent},
};
-use crate::job::{self, Job, Jobs};
-use futures_util::{FutureExt, StreamExt};
+use crate::job::{self, Jobs};
+use futures_util::StreamExt;
use std::{collections::HashMap, fmt, future::Future};
use std::{collections::HashSet, num::NonZeroUsize};
@@ -107,10 +108,11 @@ impl<'a> Context<'a> {
let callback = Box::pin(async move {
let json = call.await?;
let response = serde_json::from_value(json)?;
- let call: job::Callback =
- Box::new(move |editor: &mut Editor, compositor: &mut Compositor| {
+ let call: job::Callback = Callback::EditorCompositor(Box::new(
+ move |editor: &mut Editor, compositor: &mut Compositor| {
callback(editor, compositor, response)
- });
+ },
+ ));
Ok(call)
});
self.jobs.callback(callback);
@@ -1925,8 +1927,8 @@ fn global_search(cx: &mut Context) {
let show_picker = async move {
let all_matches: Vec<FileResult> =
UnboundedReceiverStream::new(all_matches_rx).collect().await;
- let call: job::Callback =
- Box::new(move |editor: &mut Editor, compositor: &mut Compositor| {
+ let call: job::Callback = Callback::EditorCompositor(Box::new(
+ move |editor: &mut Editor, compositor: &mut Compositor| {
if all_matches.is_empty() {
editor.set_status("No matches found");
return;
@@ -1962,7 +1964,8 @@ fn global_search(cx: &mut Context) {
},
);
compositor.push(Box::new(overlayed(picker)));
- });
+ },
+ ));
Ok(call)
};
cx.jobs.callback(show_picker);
@@ -2504,13 +2507,6 @@ fn insert_at_line_end(cx: &mut Context) {
doc.set_selection(view.id, selection);
}
-/// Sometimes when applying formatting changes we want to mark the buffer as unmodified, for
-/// example because we just applied the same changes while saving.
-enum Modified {
- SetUnmodified,
- LeaveModified,
-}
-
// Creates an LspCallback that waits for formatting changes to be computed. When they're done,
// it applies them, but only if the doc hasn't changed.
//
@@ -2519,11 +2515,12 @@ enum Modified {
async fn make_format_callback(
doc_id: DocumentId,
doc_version: i32,
- modified: Modified,
format: impl Future<Output = Result<Transaction, FormatterError>> + Send + 'static,
+ write: Option<(Option<PathBuf>, bool)>,
) -> anyhow::Result<job::Callback> {
- let format = format.await?;
- let call: job::Callback = Box::new(move |editor, _compositor| {
+ let format = format.await;
+
+ let call: job::Callback = Callback::Editor(Box::new(move |editor| {
if !editor.documents.contains_key(&doc_id) {
return;
}
@@ -2531,22 +2528,30 @@ async fn make_format_callback(
let scrolloff = editor.config().scrolloff;
let doc = doc_mut!(editor, &doc_id);
let view = view_mut!(editor);
- if doc.version() == doc_version {
- apply_transaction(&format, doc, view);
- doc.append_changes_to_history(view.id);
- doc.detect_indent_and_line_ending();
- view.ensure_cursor_in_view(doc, scrolloff);
- if let Modified::SetUnmodified = modified {
- doc.reset_modified();
+
+ if let Ok(format) = format {
+ if doc.version() == doc_version {
+ apply_transaction(&format, doc, view);
+ doc.append_changes_to_history(view.id);
+ doc.detect_indent_and_line_ending();
+ view.ensure_cursor_in_view(doc, scrolloff);
+ } else {
+ log::info!("discarded formatting changes because the document changed");
}
- } else {
- log::info!("discarded formatting changes because the document changed");
}
- });
+
+ if let Some((path, force)) = write {
+ let id = doc.id();
+ if let Err(err) = editor.save(id, path, force) {
+ editor.set_error(format!("Error saving: {}", err));
+ }
+ }
+ }));
+
Ok(call)
}
-#[derive(PartialEq)]
+#[derive(PartialEq, Eq)]
pub enum Open {
Below,
Above,