Add progress spinners to status line

imgbot
wojciechkepka 3 years ago committed by Blaž Hrastnik
parent b2804b14b1
commit cc357d5096

@ -347,6 +347,10 @@ impl LspProgressMap {
self.0.get(&id) self.0.get(&id)
} }
pub fn is_progressing(&self, id: usize) -> bool {
self.0.get(&id).map(|it| !it.is_empty()).unwrap_or_default()
}
/// Returns last progress status for a given server with `id` and `token`. /// Returns last progress status for a given server with `id` and `token`.
pub fn progress(&self, id: usize, token: &lsp::ProgressToken) -> Option<&ProgressStatus> { pub fn progress(&self, id: usize, token: &lsp::ProgressToken) -> Option<&ProgressStatus> {
self.0.get(&id).and_then(|values| values.get(token)) self.0.get(&id).and_then(|values| values.get(token))

@ -2,11 +2,18 @@ use helix_core::syntax;
use helix_lsp::{lsp, LspProgressMap}; use helix_lsp::{lsp, LspProgressMap};
use helix_view::{document::Mode, theme, Document, Editor, Theme, View}; use helix_view::{document::Mode, theme, Document, Editor, Theme, View};
use crate::{args::Args, compositor::Compositor, config::Config, keymap::Keymaps, ui}; use crate::{
args::Args,
compositor::Compositor,
config::Config,
keymap::Keymaps,
ui::{self, Spinner},
};
use log::{error, info}; use log::{error, info};
use std::{ use std::{
collections::HashMap,
future::Future, future::Future,
io::{self, stdout, Stdout, Write}, io::{self, stdout, Stdout, Write},
path::PathBuf, path::PathBuf,
@ -42,7 +49,7 @@ pub struct Application {
callbacks: LspCallbacks, callbacks: LspCallbacks,
lsp_progress: LspProgressMap, lsp_progress: LspProgressMap,
lsp_progress_enabled: bool, lsp_display_messages: bool,
} }
impl Application { impl Application {
@ -62,7 +69,7 @@ impl Application {
.as_deref() .as_deref()
.unwrap_or(include_bytes!("../../languages.toml")); .unwrap_or(include_bytes!("../../languages.toml"));
let theme = if let Some(theme) = &config.global.theme { let theme = if let Some(theme) = &config.theme {
match theme_loader.load(theme) { match theme_loader.load(theme) {
Ok(theme) => theme, Ok(theme) => theme,
Err(e) => { Err(e) => {
@ -112,7 +119,7 @@ impl Application {
syn_loader, syn_loader,
callbacks: FuturesUnordered::new(), callbacks: FuturesUnordered::new(),
lsp_progress: LspProgressMap::new(), lsp_progress: LspProgressMap::new(),
lsp_progress_enabled: config.global.lsp_progress, lsp_display_messages: config.lsp.display_messages,
}; };
Ok(app) Ok(app)
@ -305,11 +312,23 @@ impl Application {
(None, message, &None) (None, message, &None)
} else { } else {
self.lsp_progress.end_progress(server_id, &token); self.lsp_progress.end_progress(server_id, &token);
if !self.lsp_progress.is_progressing(server_id) {
let ui = self
.compositor
.find(std::any::type_name::<ui::EditorView>())
.unwrap();
if let Some(ui) =
ui.as_any_mut().downcast_mut::<ui::EditorView>()
{
ui.spinners_mut().get_or_create(server_id).stop();
};
}
self.editor.clear_status(); self.editor.clear_status();
return; return;
} }
} }
}; };
let token_d: &dyn std::fmt::Display = match &token { let token_d: &dyn std::fmt::Display = match &token {
lsp::NumberOrString::Number(n) => n, lsp::NumberOrString::Number(n) => n,
lsp::NumberOrString::String(s) => s, lsp::NumberOrString::String(s) => s,
@ -342,14 +361,23 @@ impl Application {
if let lsp::WorkDoneProgress::End(_) = work { if let lsp::WorkDoneProgress::End(_) = work {
self.lsp_progress.end_progress(server_id, &token); self.lsp_progress.end_progress(server_id, &token);
if !self.lsp_progress.is_progressing(server_id) {
let ui = self
.compositor
.find(std::any::type_name::<ui::EditorView>())
.unwrap();
if let Some(ui) = ui.as_any_mut().downcast_mut::<ui::EditorView>() {
ui.spinners_mut().get_or_create(server_id).stop();
};
}
} else { } else {
self.lsp_progress.update(server_id, token, work); self.lsp_progress.update(server_id, token, work);
} }
if self.lsp_progress_enabled { if self.lsp_display_messages {
self.editor.set_status(status); self.editor.set_status(status);
self.render();
} }
self.render();
} }
_ => unreachable!(), _ => unreachable!(),
} }
@ -372,6 +400,17 @@ impl Application {
MethodCall::WorkDoneProgressCreate(params) => { MethodCall::WorkDoneProgressCreate(params) => {
self.lsp_progress.create(server_id, params.token); self.lsp_progress.create(server_id, params.token);
let ui = self
.compositor
.find(std::any::type_name::<ui::EditorView>())
.unwrap();
if let Some(ui) = ui.as_any_mut().downcast_mut::<ui::EditorView>() {
let spinner = ui.spinners_mut().get_or_create(server_id);
if spinner.is_stopped() {
spinner.start();
}
};
let doc = self.editor.documents().find(|doc| { let doc = self.editor.documents().find(|doc| {
doc.language_server() doc.language_server()
.map(|server| server.id() == server_id) .map(|server| server.id() == server_id)

@ -19,7 +19,7 @@ use anyhow::anyhow;
use helix_lsp::{ use helix_lsp::{
lsp, lsp,
util::{lsp_pos_to_pos, lsp_range_to_range, pos_to_lsp_pos, range_to_lsp_range}, util::{lsp_pos_to_pos, lsp_range_to_range, pos_to_lsp_pos, range_to_lsp_range},
OffsetEncoding, LspProgressMap, OffsetEncoding,
}; };
use insert::*; use insert::*;
use movement::Movement; use movement::Movement;

@ -1,9 +1,10 @@
// Each component declares it's own size constraints and gets fitted based on it's parent. // Each component declares it's own size constraints and gets fitted based on it's parent.
// Q: how does this work with popups? // Q: how does this work with popups?
// cursive does compositor.screen_mut().add_layer_at(pos::absolute(x, y), <component>) // cursive does compositor.screen_mut().add_layer_at(pos::absolute(x, y), <component>)
use helix_core::Position;
use helix_lsp::LspProgressMap;
use crossterm::event::Event; use crossterm::event::Event;
use helix_core::Position;
use tui::{buffer::Buffer as Surface, layout::Rect, terminal::CursorKind}; use tui::{buffer::Buffer as Surface, layout::Rect, terminal::CursorKind};
pub type Callback = Box<dyn FnOnce(&mut Compositor)>; pub type Callback = Box<dyn FnOnce(&mut Compositor)>;

@ -1,35 +1,28 @@
use anyhow::{Error, Result}; use anyhow::{Error, Result};
use std::{collections::HashMap, str::FromStr}; use std::collections::HashMap;
use serde::{de::Error as SerdeError, Deserialize, Serialize}; use serde::{de::Error as SerdeError, Deserialize, Serialize};
use crate::keymap::{parse_keymaps, Keymaps}; use crate::keymap::{parse_keymaps, Keymaps};
pub struct GlobalConfig {
pub theme: Option<String>,
pub lsp_progress: bool,
}
impl Default for GlobalConfig {
fn default() -> Self {
Self {
lsp_progress: true,
theme: None,
}
}
}
#[derive(Default)] #[derive(Default)]
pub struct Config { pub struct Config {
pub global: GlobalConfig, pub theme: Option<String>,
pub lsp: LspConfig,
pub keymaps: Keymaps, pub keymaps: Keymaps,
} }
#[derive(Default, Serialize, Deserialize)]
pub struct LspConfig {
pub display_messages: bool,
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
struct TomlConfig { struct TomlConfig {
theme: Option<String>, theme: Option<String>,
lsp_progress: Option<bool>, #[serde(default)]
lsp: LspConfig,
keys: Option<HashMap<String, HashMap<String, String>>>, keys: Option<HashMap<String, HashMap<String, String>>>,
} }
@ -40,10 +33,8 @@ impl<'de> Deserialize<'de> for Config {
{ {
let config = TomlConfig::deserialize(deserializer)?; let config = TomlConfig::deserialize(deserializer)?;
Ok(Self { Ok(Self {
global: GlobalConfig { theme: config.theme,
lsp_progress: config.lsp_progress.unwrap_or(true), lsp: config.lsp,
theme: config.theme,
},
keymaps: config keymaps: config
.keys .keys
.map(|r| parse_keymaps(&r)) .map(|r| parse_keymaps(&r))

@ -3,7 +3,7 @@ use crate::{
compositor::{Component, Compositor, Context, EventResult}, compositor::{Component, Compositor, Context, EventResult},
key, key,
keymap::{self, Keymaps}, keymap::{self, Keymaps},
ui::Completion, ui::{Completion, ProgressSpinners},
}; };
use helix_core::{ use helix_core::{
@ -11,6 +11,7 @@ use helix_core::{
syntax::{self, HighlightEvent}, syntax::{self, HighlightEvent},
Position, Range, Position, Range,
}; };
use helix_lsp::LspProgressMap;
use helix_view::{document::Mode, Document, Editor, Theme, View}; use helix_view::{document::Mode, Document, Editor, Theme, View};
use std::borrow::Cow; use std::borrow::Cow;
@ -31,6 +32,7 @@ pub struct EditorView {
on_next_key: Option<Box<dyn FnOnce(&mut commands::Context, KeyEvent)>>, on_next_key: Option<Box<dyn FnOnce(&mut commands::Context, KeyEvent)>>,
last_insert: (commands::Command, Vec<KeyEvent>), last_insert: (commands::Command, Vec<KeyEvent>),
completion: Option<Completion>, completion: Option<Completion>,
spinners: ProgressSpinners,
} }
const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
@ -48,9 +50,15 @@ impl EditorView {
on_next_key: None, on_next_key: None,
last_insert: (commands::Command::normal_mode, Vec::new()), last_insert: (commands::Command::normal_mode, Vec::new()),
completion: None, completion: None,
spinners: ProgressSpinners::default(),
} }
} }
pub fn spinners_mut(&mut self) -> &mut ProgressSpinners {
&mut self.spinners
}
#[allow(clippy::too_many_arguments)]
pub fn render_view( pub fn render_view(
&self, &self,
doc: &Document, doc: &Document,
@ -458,6 +466,7 @@ impl EditorView {
); );
} }
#[allow(clippy::too_many_arguments)]
pub fn render_statusline( pub fn render_statusline(
&self, &self,
doc: &Document, doc: &Document,
@ -476,6 +485,15 @@ impl EditorView {
Mode::Select => "SEL", Mode::Select => "SEL",
Mode::Normal => "NOR", Mode::Normal => "NOR",
}; };
let progress = doc
.language_server()
.and_then(|srv| {
self.spinners
.get(srv.id())
.and_then(|spinner| spinner.frame())
})
.unwrap_or("");
let style = if is_focused { let style = if is_focused {
theme.get("ui.statusline") theme.get("ui.statusline")
} else { } else {
@ -486,13 +504,14 @@ impl EditorView {
if is_focused { if is_focused {
surface.set_string(viewport.x + 1, viewport.y, mode, style); surface.set_string(viewport.x + 1, viewport.y, mode, style);
} }
surface.set_string(viewport.x + 5, viewport.y, progress, style);
if let Some(path) = doc.relative_path() { if let Some(path) = doc.relative_path() {
let path = path.to_string_lossy(); let path = path.to_string_lossy();
let title = format!("{}{}", path, if doc.is_modified() { "[+]" } else { "" }); let title = format!("{}{}", path, if doc.is_modified() { "[+]" } else { "" });
surface.set_stringn( surface.set_stringn(
viewport.x + 6, viewport.x + 8,
viewport.y, viewport.y,
title, title,
viewport.width.saturating_sub(6) as usize, viewport.width.saturating_sub(6) as usize,

Loading…
Cancel
Save