Pass the 'Save' format trigger when formatting on save

In an earlier refactor, I accidentally caused the 'Manual'
trigger to *always* be passed.

Co-authored-by: Mikayla Maki <mikayla@zed.dev>
This commit is contained in:
Max Brunsfeld 2023-02-24 17:11:35 -08:00
parent 0704d9dcdb
commit 5fea49e639
3 changed files with 11 additions and 8 deletions

View file

@ -5459,21 +5459,20 @@ impl Editor {
None => return None, None => return None,
}; };
Some(self.perform_format(project, cx)) Some(self.perform_format(project, FormatTrigger::Manual, cx))
} }
fn perform_format( fn perform_format(
&mut self, &mut self,
project: ModelHandle<Project>, project: ModelHandle<Project>,
trigger: FormatTrigger,
cx: &mut ViewContext<'_, Self>, cx: &mut ViewContext<'_, Self>,
) -> Task<Result<()>> { ) -> Task<Result<()>> {
let buffer = self.buffer().clone(); let buffer = self.buffer().clone();
let buffers = buffer.read(cx).all_buffers(); let buffers = buffer.read(cx).all_buffers();
let mut timeout = cx.background().timer(FORMAT_TIMEOUT).fuse(); let mut timeout = cx.background().timer(FORMAT_TIMEOUT).fuse();
let format = project.update(cx, |project, cx| { let format = project.update(cx, |project, cx| project.format(buffers, true, trigger, cx));
project.format(buffers, true, FormatTrigger::Manual, cx)
});
cx.spawn(|_, mut cx| async move { cx.spawn(|_, mut cx| async move {
let transaction = futures::select_biased! { let transaction = futures::select_biased! {

View file

@ -4193,7 +4193,9 @@ async fn test_document_format_manual_trigger(cx: &mut gpui::TestAppContext) {
let (_, editor) = cx.add_window(|cx| build_editor(buffer, cx)); let (_, editor) = cx.add_window(|cx| build_editor(buffer, cx));
editor.update(cx, |editor, cx| editor.set_text("one\ntwo\nthree\n", cx)); editor.update(cx, |editor, cx| editor.set_text("one\ntwo\nthree\n", cx));
let format = editor.update(cx, |editor, cx| editor.perform_format(project.clone(), cx)); let format = editor.update(cx, |editor, cx| {
editor.perform_format(project.clone(), FormatTrigger::Manual, cx)
});
fake_server fake_server
.handle_request::<lsp::request::Formatting, _, _>(move |params, _| async move { .handle_request::<lsp::request::Formatting, _, _>(move |params, _| async move {
assert_eq!( assert_eq!(
@ -4225,7 +4227,9 @@ async fn test_document_format_manual_trigger(cx: &mut gpui::TestAppContext) {
futures::future::pending::<()>().await; futures::future::pending::<()>().await;
unreachable!() unreachable!()
}); });
let format = editor.update(cx, |editor, cx| editor.perform_format(project, cx)); let format = editor.update(cx, |editor, cx| {
editor.perform_format(project, FormatTrigger::Manual, cx)
});
cx.foreground().advance_clock(super::FORMAT_TIMEOUT); cx.foreground().advance_clock(super::FORMAT_TIMEOUT);
cx.foreground().start_waiting(); cx.foreground().start_waiting();
format.await.unwrap(); format.await.unwrap();

View file

@ -14,7 +14,7 @@ use language::{
proto::serialize_anchor as serialize_text_anchor, Bias, Buffer, OffsetRangeExt, Point, proto::serialize_anchor as serialize_text_anchor, Bias, Buffer, OffsetRangeExt, Point,
SelectionGoal, SelectionGoal,
}; };
use project::{Item as _, Project, ProjectPath}; use project::{FormatTrigger, Item as _, Project, ProjectPath};
use rpc::proto::{self, update_view}; use rpc::proto::{self, update_view};
use settings::Settings; use settings::Settings;
use smallvec::SmallVec; use smallvec::SmallVec;
@ -608,7 +608,7 @@ impl Item for Editor {
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Task<Result<()>> { ) -> Task<Result<()>> {
self.report_event("save editor", cx); self.report_event("save editor", cx);
let format = self.perform_format(project.clone(), cx); let format = self.perform_format(project.clone(), FormatTrigger::Save, cx);
let buffers = self.buffer().clone().read(cx).all_buffers(); let buffers = self.buffer().clone().read(cx).all_buffers();
cx.as_mut().spawn(|mut cx| async move { cx.as_mut().spawn(|mut cx| async move {
format.await?; format.await?;