on master help sign in
Add progress spinners to status line
commitcc357d50964ecd5760f1367a8a7b63fd45a0f1ce authorwojciechkepka <wojtek.kepka@protonmail.com> dateSun 20 Jun 2021, 21:31 (5 years ago) parentb2804b1
6 files changed+85−31
helix-lsp/src/lib.rs+4 helix-term/src/application.rs+45−6 helix-term/src/commands.rs+1−1 helix-term/src/compositor.rs+2−1 helix-term/src/config.rs+12−21 helix-term/src/ui/editor.rs+21−2
helix-lsp/src/lib.rs+4
@@ -347,6 +347,10 @@ impl LspProgressMap {
347347 self.0.get(&id)
348348 }
349349
350+ pub fn is_progressing(&self, id: usize) -> bool {
351+ self.0.get(&id).map(|it| !it.is_empty()).unwrap_or_default()
352+ }
353+
350354 /// Returns last progress status for a given server with `id` and `token`.
351355 pub fn progress(&self, id: usize, token: &lsp::ProgressToken) -> Option<&ProgressStatus> {
352356 self.0.get(&id).and_then(|values| values.get(token))
helix-term/src/application.rs+45−6
@@ -2,11 +2,18 @@ use helix_core::syntax;
22use helix_lsp::{lsp, LspProgressMap};
33use helix_view::{document::Mode, theme, Document, Editor, Theme, View};
44
5−use crate::{args::Args, compositor::Compositor, config::Config, keymap::Keymaps, ui};
5+use crate::{
6+ args::Args,
7+ compositor::Compositor,
8+ config::Config,
9+ keymap::Keymaps,
10+ ui::{self, Spinner},
11+};
612
713use log::{error, info};
814
915use std::{
16+ collections::HashMap,
1017 future::Future,
1118 io::{self, stdout, Stdout, Write},
1219 path::PathBuf,
@@ -42,7 +49,7 @@ pub struct Application {
4249 callbacks: LspCallbacks,
4350
4451 lsp_progress: LspProgressMap,
45− lsp_progress_enabled: bool,
52+ lsp_display_messages: bool,
4653}
4754
4855impl Application {
@@ -62,7 +69,7 @@ impl Application {
6269 .as_deref()
6370 .unwrap_or(include_bytes!("../../languages.toml"));
6471
65− let theme = if let Some(theme) = &config.global.theme {
72+ let theme = if let Some(theme) = &config.theme {
6673 match theme_loader.load(theme) {
6774 Ok(theme) => theme,
6875 Err(e) => {
@@ -112,7 +119,7 @@ impl Application {
112119 syn_loader,
113120 callbacks: FuturesUnordered::new(),
114121 lsp_progress: LspProgressMap::new(),
115− lsp_progress_enabled: config.global.lsp_progress,
122+ lsp_display_messages: config.lsp.display_messages,
116123 };
117124
118125 Ok(app)
@@ -305,11 +312,23 @@ impl Application {
305312 (None, message, &None)
306313 } else {
307314 self.lsp_progress.end_progress(server_id, &token);
315+ if !self.lsp_progress.is_progressing(server_id) {
316+ let ui = self
317+ .compositor
318+ .find(std::any::type_name::<ui::EditorView>())
319+ .unwrap();
320+ if let Some(ui) =
321+ ui.as_any_mut().downcast_mut::<ui::EditorView>()
322+ {
323+ ui.spinners_mut().get_or_create(server_id).stop();
324+ };
325+ }
308326 self.editor.clear_status();
309327 return;
310328 }
311329 }
312330 };
331+
313332 let token_d: &dyn std::fmt::Display = match &token {
314333 lsp::NumberOrString::Number(n) => n,
315334 lsp::NumberOrString::String(s) => s,
@@ -342,14 +361,23 @@ impl Application {
342361
343362 if let lsp::WorkDoneProgress::End(_) = work {
344363 self.lsp_progress.end_progress(server_id, &token);
364+ if !self.lsp_progress.is_progressing(server_id) {
365+ let ui = self
366+ .compositor
367+ .find(std::any::type_name::<ui::EditorView>())
368+ .unwrap();
369+ if let Some(ui) = ui.as_any_mut().downcast_mut::<ui::EditorView>() {
370+ ui.spinners_mut().get_or_create(server_id).stop();
371+ };
372+ }
345373 } else {
346374 self.lsp_progress.update(server_id, token, work);
347375 }
348376
349− if self.lsp_progress_enabled {
377+ if self.lsp_display_messages {
350378 self.editor.set_status(status);
351− self.render();
352379 }
380+ self.render();
353381 }
354382 _ => unreachable!(),
355383 }
@@ -372,6 +400,17 @@ impl Application {
372400 MethodCall::WorkDoneProgressCreate(params) => {
373401 self.lsp_progress.create(server_id, params.token);
374402
403+ let ui = self
404+ .compositor
405+ .find(std::any::type_name::<ui::EditorView>())
406+ .unwrap();
407+ if let Some(ui) = ui.as_any_mut().downcast_mut::<ui::EditorView>() {
408+ let spinner = ui.spinners_mut().get_or_create(server_id);
409+ if spinner.is_stopped() {
410+ spinner.start();
411+ }
412+ };
413+
375414 let doc = self.editor.documents().find(|doc| {
376415 doc.language_server()
377416 .map(|server| server.id() == server_id)
helix-term/src/commands.rs+1−1
@@ -19,7 +19,7 @@ use anyhow::anyhow;
1919use helix_lsp::{
2020 lsp,
2121 util::{lsp_pos_to_pos, lsp_range_to_range, pos_to_lsp_pos, range_to_lsp_range},
22− OffsetEncoding,
22+ LspProgressMap, OffsetEncoding,
2323};
2424use insert::*;
2525use movement::Movement;
helix-term/src/compositor.rs+2−1
@@ -1,9 +1,10 @@
11// Each component declares it's own size constraints and gets fitted based on it's parent.
22// Q: how does this work with popups?
33// cursive does compositor.screen_mut().add_layer_at(pos::absolute(x, y), <component>)
4+use helix_core::Position;
5+use helix_lsp::LspProgressMap;
46
57use crossterm::event::Event;
6−use helix_core::Position;
78use tui::{buffer::Buffer as Surface, layout::Rect, terminal::CursorKind};
89
910pub type Callback = Box<dyn FnOnce(&mut Compositor)>;
helix-term/src/config.rs+12−21
@@ -1,35 +1,28 @@
11use anyhow::{Error, Result};
2−use std::{collections::HashMap, str::FromStr};
2+use std::collections::HashMap;
33
44use serde::{de::Error as SerdeError, Deserialize, Serialize};
55
66use crate::keymap::{parse_keymaps, Keymaps};
77
8−pub struct GlobalConfig {
9− pub theme: Option<String>,
10− pub lsp_progress: bool,
11−}
12−
13−impl Default for GlobalConfig {
14− fn default() -> Self {
15− Self {
16− lsp_progress: true,
17− theme: None,
18− }
19− }
20−}
21−
228#[derive(Default)]
239pub struct Config {
24− pub global: GlobalConfig,
10+ pub theme: Option<String>,
11+ pub lsp: LspConfig,
2512 pub keymaps: Keymaps,
2613}
2714
15+#[derive(Default, Serialize, Deserialize)]
16+pub struct LspConfig {
17+ pub display_messages: bool,
18+}
19+
2820#[derive(Serialize, Deserialize)]
2921#[serde(rename_all = "kebab-case")]
3022struct TomlConfig {
3123 theme: Option<String>,
32− lsp_progress: Option<bool>,
24+ #[serde(default)]
25+ lsp: LspConfig,
3326 keys: Option<HashMap<String, HashMap<String, String>>>,
3427}
3528
@@ -40,10 +33,8 @@ impl<'de> Deserialize<'de> for Config {
4033 {
4134 let config = TomlConfig::deserialize(deserializer)?;
4235 Ok(Self {
43− global: GlobalConfig {
44− lsp_progress: config.lsp_progress.unwrap_or(true),
45− theme: config.theme,
46− },
36+ theme: config.theme,
37+ lsp: config.lsp,
4738 keymaps: config
4839 .keys
4940 .map(|r| parse_keymaps(&r))
helix-term/src/ui/editor.rs+21−2
@@ -3,7 +3,7 @@ use crate::{
33 compositor::{Component, Compositor, Context, EventResult},
44 key,
55 keymap::{self, Keymaps},
6− ui::Completion,
6+ ui::{Completion, ProgressSpinners},
77};
88
99use helix_core::{
@@ -11,6 +11,7 @@ use helix_core::{
1111 syntax::{self, HighlightEvent},
1212 Position, Range,
1313};
14+use helix_lsp::LspProgressMap;
1415use helix_view::{document::Mode, Document, Editor, Theme, View};
1516use std::borrow::Cow;
1617
@@ -31,6 +32,7 @@ pub struct EditorView {
3132 on_next_key: Option<Box<dyn FnOnce(&mut commands::Context, KeyEvent)>>,
3233 last_insert: (commands::Command, Vec<KeyEvent>),
3334 completion: Option<Completion>,
35+ spinners: ProgressSpinners,
3436}
3537
3638const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
@@ -48,9 +50,15 @@ impl EditorView {
4850 on_next_key: None,
4951 last_insert: (commands::Command::normal_mode, Vec::new()),
5052 completion: None,
53+ spinners: ProgressSpinners::default(),
5154 }
5255 }
5356
57+ pub fn spinners_mut(&mut self) -> &mut ProgressSpinners {
58+ &mut self.spinners
59+ }
60+
61+ #[allow(clippy::too_many_arguments)]
5462 pub fn render_view(
5563 &self,
5664 doc: &Document,
@@ -458,6 +466,7 @@ impl EditorView {
458466 );
459467 }
460468
469+ #[allow(clippy::too_many_arguments)]
461470 pub fn render_statusline(
462471 &self,
463472 doc: &Document,
@@ -476,6 +485,15 @@ impl EditorView {
476485 Mode::Select => "SEL",
477486 Mode::Normal => "NOR",
478487 };
488+ let progress = doc
489+ .language_server()
490+ .and_then(|srv| {
491+ self.spinners
492+ .get(srv.id())
493+ .and_then(|spinner| spinner.frame())
494+ })
495+ .unwrap_or("");
496+
479497 let style = if is_focused {
480498 theme.get("ui.statusline")
481499 } else {
@@ -486,13 +504,14 @@ impl EditorView {
486504 if is_focused {
487505 surface.set_string(viewport.x + 1, viewport.y, mode, style);
488506 }
507+ surface.set_string(viewport.x + 5, viewport.y, progress, style);
489508
490509 if let Some(path) = doc.relative_path() {
491510 let path = path.to_string_lossy();
492511
493512 let title = format!("{}{}", path, if doc.is_modified() { "[+]" } else { "" });
494513 surface.set_stringn(
495− viewport.x + 6,
514+ viewport.x + 8,
496515 viewport.y,
497516 title,
498517 viewport.width.saturating_sub(6) as usize,