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

adding asynchronous job tracking support for statusline #9536

Closed
Closed
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
1 change: 1 addition & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ The following statusline elements can be configured:
| `spacer` | Inserts a space between elements (multiple/contiguous spacers may be specified) |
| `version-control` | The current branch name or detached commit hash of the opened workspace |
| `register` | The current selected register |
| `jobs` | The amount of asynchronous jobs currently scheduled |

### `[editor.lsp]` Section

Expand Down
52 changes: 43 additions & 9 deletions helix-term/src/job.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::atomic::{AtomicUsize, Ordering};

use helix_event::status::StatusMessage;
use helix_event::{runtime_local, send_blocking};
use helix_view::Editor;
Expand All @@ -12,26 +14,50 @@ use tokio::sync::mpsc::{channel, Receiver, Sender};
pub type EditorCompositorCallback = Box<dyn FnOnce(&mut Editor, &mut Compositor) + Send>;
pub type EditorCallback = Box<dyn FnOnce(&mut Editor) + Send>;

static CURRENT_JOBS: AtomicUsize = AtomicUsize::new(0);

runtime_local! {
static JOB_QUEUE: OnceCell<Sender<Callback>> = OnceCell::new();
}

pub async fn dispatch_callback(job: Callback) {
pub fn dispatch_callback(job: Callback) -> impl Future<Output = ()> {
CURRENT_JOBS.fetch_add(1, Ordering::Release);
dispatch_callback_impl(job)
}

pub fn dispatch(
job: impl FnOnce(&mut Editor, &mut Compositor) + Send + 'static,
) -> impl Future<Output = ()> {
CURRENT_JOBS.fetch_add(1, Ordering::Release);
dispatch_impl(job)
}

pub fn dispatch_blocking(job: impl FnOnce(&mut Editor, &mut Compositor) + Send + 'static) {
CURRENT_JOBS.fetch_add(1, Ordering::Release);
dispatch_blocking_impl(job)
}

async fn dispatch_callback_impl(job: Callback) {
let _ = JOB_QUEUE.wait().send(job).await;
}

pub async fn dispatch(job: impl FnOnce(&mut Editor, &mut Compositor) + Send + 'static) {
async fn dispatch_impl(job: impl FnOnce(&mut Editor, &mut Compositor) + Send + 'static) {
let _ = JOB_QUEUE
.wait()
.send(Callback::EditorCompositor(Box::new(job)))
.await;
}

pub fn dispatch_blocking(job: impl FnOnce(&mut Editor, &mut Compositor) + Send + 'static) {
fn dispatch_blocking_impl(job: impl FnOnce(&mut Editor, &mut Compositor) + Send + 'static) {
let jobs = JOB_QUEUE.wait();
send_blocking(jobs, Callback::EditorCompositor(Box::new(job)))
}

#[inline(always)]
pub fn job_count() -> usize {
CURRENT_JOBS.load(Ordering::Acquire)
}

pub enum Callback {
EditorCompositor(EditorCompositorCallback),
Editor(EditorCallback),
Expand All @@ -54,6 +80,7 @@ pub struct Jobs {

impl Job {
pub fn new<F: Future<Output = anyhow::Result<()>> + Send + 'static>(f: F) -> Self {
CURRENT_JOBS.fetch_add(1, Ordering::Release);
Self {
future: f.map(|r| r.map(|()| None)).boxed(),
wait: false,
Expand All @@ -63,6 +90,7 @@ impl Job {
pub fn with_callback<F: Future<Output = anyhow::Result<Callback>> + Send + 'static>(
f: F,
) -> Self {
CURRENT_JOBS.fetch_add(1, Ordering::Release);
Self {
future: f.map(|r| r.map(Some)).boxed(),
wait: false,
Expand Down Expand Up @@ -106,11 +134,16 @@ impl Jobs {
call: anyhow::Result<Option<Callback>>,
) {
match call {
Ok(None) => {}
Ok(Some(call)) => match call {
Callback::EditorCompositor(call) => call(editor, compositor),
Callback::Editor(call) => call(editor),
},
Ok(None) => {
CURRENT_JOBS.fetch_sub(1, Ordering::Release);
}
Ok(Some(call)) => {
match call {
Callback::EditorCompositor(call) => call(editor, compositor),
Callback::Editor(call) => call(editor),
};
CURRENT_JOBS.fetch_sub(1, Ordering::Release);
}
Err(e) => {
editor.set_error(format!("Async job failed: {}", e));
}
Expand All @@ -123,7 +156,7 @@ impl Jobs {
} else {
tokio::spawn(async move {
match j.future.await {
Ok(Some(cb)) => dispatch_callback(cb).await,
Ok(Some(cb)) => dispatch_callback_impl(cb).await,
Ok(None) => (),
Err(err) => helix_event::status::report(err).await,
}
Expand Down Expand Up @@ -157,6 +190,7 @@ impl Jobs {
// skip callbacks for which we don't have the necessary references
_ => (),
}
CURRENT_JOBS.fetch_sub(1, Ordering::Release);
}
}
Err(e) => {
Expand Down
12 changes: 12 additions & 0 deletions helix-term/src/ui/statusline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use helix_view::{
Document, Editor, View,
};

use crate::job::job_count;
use crate::ui::ProgressSpinners;

use helix_view::editor::StatusLineElement as StatusLineElementID;
Expand Down Expand Up @@ -162,6 +163,7 @@ where
helix_view::editor::StatusLineElement::Spacer => render_spacer,
helix_view::editor::StatusLineElement::VersionControl => render_version_control,
helix_view::editor::StatusLineElement::Register => render_register,
helix_view::editor::StatusLineElement::Jobs => render_jobs,
}
}

Expand Down Expand Up @@ -514,3 +516,13 @@ where
write(context, format!(" reg={} ", reg), None)
}
}

fn render_jobs<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let job_count = job_count();
if job_count > 0 {
write(context, format!("# {}", job_count), None)
}
}
3 changes: 3 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,9 @@ pub enum StatusLineElement {

/// Indicator for selected register
Register,

/// Current amount of jobs
Jobs,
}

// Cursor shape is read and used on every rendered frame and so needs
Expand Down