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

First attempt at sync formatting. #285

Merged
merged 5 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl Application {
}
self.render();
}
Some(callback) = self.jobs.next() => {
Some(callback) = self.jobs.next_job() => {
self.jobs.handle_callback(&mut self.editor, &mut self.compositor, callback);
self.render();
}
Expand Down
37 changes: 17 additions & 20 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1878,31 +1878,28 @@ fn append_to_line(cx: &mut Context) {

// 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.
fn make_format_callback(
async fn make_format_callback(
doc_id: DocumentId,
doc_version: i32,
set_unmodified: bool,
jneem marked this conversation as resolved.
Show resolved Hide resolved
format: impl Future<Output = helix_lsp::util::LspFormatting> + Send + 'static,
) -> impl Future<Output = anyhow::Result<job::Callback>> {
async move {
let format = format.await;
let call: job::Callback =
Box::new(move |editor: &mut Editor, compositor: &mut Compositor| {
let view_id = view!(editor).id;
if let Some(doc) = editor.document_mut(doc_id) {
if doc.version() == doc_version {
doc.apply(&Transaction::from(format), view_id);
doc.append_changes_to_history(view_id);
if set_unmodified {
doc.reset_modified();
}
} else {
log::info!("discarded formatting changes because the document changed");
}
) -> anyhow::Result<job::Callback> {
let format = format.await;
let call: job::Callback = Box::new(move |editor: &mut Editor, compositor: &mut Compositor| {
let view_id = view!(editor).id;
if let Some(doc) = editor.document_mut(doc_id) {
if doc.version() == doc_version {
doc.apply(&Transaction::from(format), view_id);
doc.append_changes_to_history(view_id);
if set_unmodified {
doc.reset_modified();
}
});
Ok(call)
}
} else {
log::info!("discarded formatting changes because the document changed");
}
}
});
Ok(call)
}

enum Open {
Expand Down
8 changes: 4 additions & 4 deletions helix-term/src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Job {
f: F,
) -> Job {
Job {
future: f.map(|r| r.map(|x| Some(x))).boxed(),
future: f.map(|r| r.map(Some)).boxed(),
archseer marked this conversation as resolved.
Show resolved Hide resolved
wait: false,
}
}
Expand Down Expand Up @@ -77,9 +77,9 @@ impl Jobs {
}
}

pub fn next<'a>(
&'a mut self,
) -> impl Future<Output = Option<anyhow::Result<Option<Callback>>>> + 'a {
pub fn next_job(
&mut self,
) -> impl Future<Output = Option<anyhow::Result<Option<Callback>>>> + '_ {
future::select(self.futures.next(), self.wait_futures.next())
.map(|either| either.factor_first().0)
}
Expand Down