mirror of
https://github.com/helix-editor/helix.git
synced 2025-01-19 13:37:06 +04:00
Add progress spinners to status line
This commit is contained in:
parent
b2804b14b1
commit
cc357d5096
@ -347,6 +347,10 @@ pub fn progress_map(&self, id: usize) -> Option<&HashMap<lsp::ProgressToken, Pro
|
||||
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`.
|
||||
pub fn progress(&self, id: usize, token: &lsp::ProgressToken) -> Option<&ProgressStatus> {
|
||||
self.0.get(&id).and_then(|values| values.get(token))
|
||||
|
@ -2,11 +2,18 @@
|
||||
use helix_lsp::{lsp, LspProgressMap};
|
||||
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 std::{
|
||||
collections::HashMap,
|
||||
future::Future,
|
||||
io::{self, stdout, Stdout, Write},
|
||||
path::PathBuf,
|
||||
@ -42,7 +49,7 @@ pub struct Application {
|
||||
callbacks: LspCallbacks,
|
||||
|
||||
lsp_progress: LspProgressMap,
|
||||
lsp_progress_enabled: bool,
|
||||
lsp_display_messages: bool,
|
||||
}
|
||||
|
||||
impl Application {
|
||||
@ -62,7 +69,7 @@ pub fn new(mut args: Args, config: Config) -> Result<Self, Error> {
|
||||
.as_deref()
|
||||
.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) {
|
||||
Ok(theme) => theme,
|
||||
Err(e) => {
|
||||
@ -112,7 +119,7 @@ pub fn new(mut args: Args, config: Config) -> Result<Self, Error> {
|
||||
syn_loader,
|
||||
callbacks: FuturesUnordered::new(),
|
||||
lsp_progress: LspProgressMap::new(),
|
||||
lsp_progress_enabled: config.global.lsp_progress,
|
||||
lsp_display_messages: config.lsp.display_messages,
|
||||
};
|
||||
|
||||
Ok(app)
|
||||
@ -305,11 +312,23 @@ pub async fn handle_language_server_message(
|
||||
(None, message, &None)
|
||||
} else {
|
||||
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();
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let token_d: &dyn std::fmt::Display = match &token {
|
||||
lsp::NumberOrString::Number(n) => n,
|
||||
lsp::NumberOrString::String(s) => s,
|
||||
@ -342,14 +361,23 @@ pub async fn handle_language_server_message(
|
||||
|
||||
if let lsp::WorkDoneProgress::End(_) = work {
|
||||
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 {
|
||||
self.lsp_progress.update(server_id, token, work);
|
||||
}
|
||||
|
||||
if self.lsp_progress_enabled {
|
||||
if self.lsp_display_messages {
|
||||
self.editor.set_status(status);
|
||||
self.render();
|
||||
}
|
||||
self.render();
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
@ -372,6 +400,17 @@ pub async fn handle_language_server_message(
|
||||
MethodCall::WorkDoneProgressCreate(params) => {
|
||||
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| {
|
||||
doc.language_server()
|
||||
.map(|server| server.id() == server_id)
|
||||
|
@ -19,7 +19,7 @@
|
||||
use helix_lsp::{
|
||||
lsp,
|
||||
util::{lsp_pos_to_pos, lsp_range_to_range, pos_to_lsp_pos, range_to_lsp_range},
|
||||
OffsetEncoding,
|
||||
LspProgressMap, OffsetEncoding,
|
||||
};
|
||||
use insert::*;
|
||||
use movement::Movement;
|
||||
|
@ -1,9 +1,10 @@
|
||||
// Each component declares it's own size constraints and gets fitted based on it's parent.
|
||||
// Q: how does this work with popups?
|
||||
// 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 helix_core::Position;
|
||||
use tui::{buffer::Buffer as Surface, layout::Rect, terminal::CursorKind};
|
||||
|
||||
pub type Callback = Box<dyn FnOnce(&mut Compositor)>;
|
||||
|
@ -1,35 +1,28 @@
|
||||
use anyhow::{Error, Result};
|
||||
use std::{collections::HashMap, str::FromStr};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::{de::Error as SerdeError, Deserialize, Serialize};
|
||||
|
||||
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)]
|
||||
pub struct Config {
|
||||
pub global: GlobalConfig,
|
||||
pub theme: Option<String>,
|
||||
pub lsp: LspConfig,
|
||||
pub keymaps: Keymaps,
|
||||
}
|
||||
|
||||
#[derive(Default, Serialize, Deserialize)]
|
||||
pub struct LspConfig {
|
||||
pub display_messages: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
struct TomlConfig {
|
||||
theme: Option<String>,
|
||||
lsp_progress: Option<bool>,
|
||||
#[serde(default)]
|
||||
lsp: LspConfig,
|
||||
keys: Option<HashMap<String, HashMap<String, String>>>,
|
||||
}
|
||||
|
||||
@ -40,10 +33,8 @@ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
{
|
||||
let config = TomlConfig::deserialize(deserializer)?;
|
||||
Ok(Self {
|
||||
global: GlobalConfig {
|
||||
lsp_progress: config.lsp_progress.unwrap_or(true),
|
||||
theme: config.theme,
|
||||
},
|
||||
theme: config.theme,
|
||||
lsp: config.lsp,
|
||||
keymaps: config
|
||||
.keys
|
||||
.map(|r| parse_keymaps(&r))
|
||||
|
@ -3,7 +3,7 @@
|
||||
compositor::{Component, Compositor, Context, EventResult},
|
||||
key,
|
||||
keymap::{self, Keymaps},
|
||||
ui::Completion,
|
||||
ui::{Completion, ProgressSpinners},
|
||||
};
|
||||
|
||||
use helix_core::{
|
||||
@ -11,6 +11,7 @@
|
||||
syntax::{self, HighlightEvent},
|
||||
Position, Range,
|
||||
};
|
||||
use helix_lsp::LspProgressMap;
|
||||
use helix_view::{document::Mode, Document, Editor, Theme, View};
|
||||
use std::borrow::Cow;
|
||||
|
||||
@ -31,6 +32,7 @@ pub struct EditorView {
|
||||
on_next_key: Option<Box<dyn FnOnce(&mut commands::Context, KeyEvent)>>,
|
||||
last_insert: (commands::Command, Vec<KeyEvent>),
|
||||
completion: Option<Completion>,
|
||||
spinners: ProgressSpinners,
|
||||
}
|
||||
|
||||
const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
|
||||
@ -48,9 +50,15 @@ pub fn new(keymaps: Keymaps) -> Self {
|
||||
on_next_key: None,
|
||||
last_insert: (commands::Command::normal_mode, Vec::new()),
|
||||
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(
|
||||
&self,
|
||||
doc: &Document,
|
||||
@ -458,6 +466,7 @@ pub fn render_diagnostics(
|
||||
);
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn render_statusline(
|
||||
&self,
|
||||
doc: &Document,
|
||||
@ -476,6 +485,15 @@ pub fn render_statusline(
|
||||
Mode::Select => "SEL",
|
||||
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 {
|
||||
theme.get("ui.statusline")
|
||||
} else {
|
||||
@ -486,13 +504,14 @@ pub fn render_statusline(
|
||||
if is_focused {
|
||||
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() {
|
||||
let path = path.to_string_lossy();
|
||||
|
||||
let title = format!("{}{}", path, if doc.is_modified() { "[+]" } else { "" });
|
||||
surface.set_stringn(
|
||||
viewport.x + 6,
|
||||
viewport.x + 8,
|
||||
viewport.y,
|
||||
title,
|
||||
viewport.width.saturating_sub(6) as usize,
|
||||
|
Loading…
Reference in New Issue
Block a user