2022-05-09 02:05:40 +08:00
|
|
|
use helix_core::{coords_at_pos, encoding};
|
2022-05-08 19:02:15 +08:00
|
|
|
use helix_view::{
|
|
|
|
document::{Mode, SCRATCH_BUFFER_NAME},
|
|
|
|
graphics::Rect,
|
2022-05-08 21:31:53 +08:00
|
|
|
theme::Style,
|
|
|
|
Document, Editor, Theme, View,
|
2022-05-08 19:02:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
use crate::ui::ProgressSpinners;
|
|
|
|
|
|
|
|
use tui::buffer::Buffer as Surface;
|
2022-05-08 21:31:53 +08:00
|
|
|
use tui::text::{Span, Spans};
|
|
|
|
|
2022-05-09 02:05:40 +08:00
|
|
|
/// A status line element contains the information about a component which can be displayed in the status line.
|
2022-05-08 21:31:53 +08:00
|
|
|
struct StatusLineElement {
|
|
|
|
/// The element
|
|
|
|
pub text: String,
|
|
|
|
|
|
|
|
/// The style to be used to render the element (this style will be merged with the base style).
|
|
|
|
/// If not set, a default base style will be used.
|
|
|
|
pub style: Option<Style>,
|
|
|
|
}
|
2022-05-08 19:02:15 +08:00
|
|
|
|
2022-05-09 02:05:40 +08:00
|
|
|
struct RenderBuffer<'a> {
|
|
|
|
pub left: Spans<'a>,
|
|
|
|
pub center: Spans<'a>,
|
|
|
|
pub right: Spans<'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> RenderBuffer<'a> {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
return Self {
|
|
|
|
left: Spans::default(),
|
|
|
|
center: Spans::default(),
|
|
|
|
right: Spans::default(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-08 19:02:15 +08:00
|
|
|
pub struct StatusLine;
|
|
|
|
|
|
|
|
impl StatusLine {
|
|
|
|
pub fn render(
|
|
|
|
editor: &Editor,
|
|
|
|
doc: &Document,
|
|
|
|
view: &View,
|
|
|
|
viewport: Rect,
|
|
|
|
surface: &mut Surface,
|
|
|
|
is_focused: bool,
|
|
|
|
spinners: &ProgressSpinners,
|
|
|
|
) {
|
2022-05-09 02:05:40 +08:00
|
|
|
let mut buffer = RenderBuffer::new();
|
|
|
|
|
2022-05-08 19:02:15 +08:00
|
|
|
// Left side of the status line.
|
|
|
|
|
|
|
|
let base_style = if is_focused {
|
|
|
|
editor.theme.get("ui.statusline")
|
|
|
|
} else {
|
|
|
|
editor.theme.get("ui.statusline.inactive")
|
|
|
|
};
|
2022-05-08 21:31:53 +08:00
|
|
|
|
2022-05-08 19:02:15 +08:00
|
|
|
surface.set_style(viewport.with_height(1), base_style);
|
2022-05-08 21:31:53 +08:00
|
|
|
|
2022-05-09 02:05:40 +08:00
|
|
|
let mode_element = Self::render_mode(doc, is_focused);
|
|
|
|
Self::append_left(&mut buffer, &base_style, mode_element);
|
|
|
|
|
|
|
|
let spinner_element = Self::render_lsp_spinner(doc, spinners);
|
|
|
|
Self::append_left(&mut buffer, &base_style, spinner_element);
|
2022-05-08 21:31:53 +08:00
|
|
|
|
2022-05-09 02:05:40 +08:00
|
|
|
// TODO: why x+1?
|
|
|
|
surface.set_spans(
|
|
|
|
viewport.x + 1,
|
2022-05-08 21:31:53 +08:00
|
|
|
viewport.y,
|
2022-05-09 02:05:40 +08:00
|
|
|
&buffer.left,
|
|
|
|
buffer.left.width() as u16,
|
2022-05-08 21:31:53 +08:00
|
|
|
);
|
2022-05-08 19:02:15 +08:00
|
|
|
|
|
|
|
// Right side of the status line.
|
|
|
|
|
|
|
|
// Diagnostics
|
|
|
|
let diags = doc.diagnostics().iter().fold((0, 0), |mut counts, diag| {
|
|
|
|
use helix_core::diagnostic::Severity;
|
|
|
|
match diag.severity {
|
|
|
|
Some(Severity::Warning) => counts.0 += 1,
|
|
|
|
Some(Severity::Error) | None => counts.1 += 1,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
counts
|
|
|
|
});
|
|
|
|
let (warnings, errors) = diags;
|
2022-05-08 21:31:53 +08:00
|
|
|
|
2022-05-08 19:02:15 +08:00
|
|
|
for i in 0..2 {
|
2022-05-08 21:31:53 +08:00
|
|
|
let (count, state_element, count_element) = match i {
|
|
|
|
0 => (
|
|
|
|
warnings,
|
|
|
|
Self::render_diagnostics_warning_state(&editor.theme),
|
|
|
|
Self::render_diagnostics_warning_count(warnings),
|
|
|
|
),
|
|
|
|
1 => (
|
|
|
|
errors,
|
|
|
|
Self::render_diagnostics_error_state(&editor.theme),
|
|
|
|
Self::render_diagnostics_error_count(errors),
|
|
|
|
),
|
2022-05-08 19:02:15 +08:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2022-05-08 21:31:53 +08:00
|
|
|
|
|
|
|
if count > 0 {
|
2022-05-09 02:05:40 +08:00
|
|
|
Self::append_right(&mut buffer, &base_style, state_element);
|
|
|
|
Self::append_right(&mut buffer, &base_style, count_element);
|
2022-05-08 19:02:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Selections
|
|
|
|
let sels_count = doc.selection(view.id).len();
|
2022-05-08 21:31:53 +08:00
|
|
|
let selections_element = Self::render_selections(sels_count);
|
2022-05-09 02:05:40 +08:00
|
|
|
Self::append_right(&mut buffer, &base_style, selections_element);
|
2022-05-08 19:02:15 +08:00
|
|
|
|
|
|
|
// Position
|
2022-05-09 02:05:40 +08:00
|
|
|
let position_element = Self::render_position(doc, view);
|
|
|
|
Self::append_right(&mut buffer, &base_style, position_element);
|
2022-05-08 19:02:15 +08:00
|
|
|
|
|
|
|
// Encoding
|
2022-05-09 02:05:40 +08:00
|
|
|
if let Some(encoding_element) = Self::render_encoding(doc) {
|
|
|
|
Self::append_right(&mut buffer, &base_style, encoding_element);
|
2022-05-08 19:02:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// File type
|
2022-05-09 02:05:40 +08:00
|
|
|
let file_type_element = Self::render_file_type(doc);
|
|
|
|
Self::append_right(&mut buffer, &base_style, file_type_element);
|
2022-05-08 19:02:15 +08:00
|
|
|
|
|
|
|
// Render to the statusline.
|
|
|
|
surface.set_spans(
|
2022-05-09 02:05:40 +08:00
|
|
|
viewport.x + viewport.width.saturating_sub(buffer.right.width() as u16),
|
2022-05-08 19:02:15 +08:00
|
|
|
viewport.y,
|
2022-05-09 02:05:40 +08:00
|
|
|
&buffer.right,
|
|
|
|
buffer.right.width() as u16,
|
2022-05-08 19:02:15 +08:00
|
|
|
);
|
|
|
|
|
2022-05-09 02:05:40 +08:00
|
|
|
// Center of the status line.
|
|
|
|
|
2022-05-08 21:31:53 +08:00
|
|
|
let title_element = Self::render_file_name(doc);
|
2022-05-09 02:05:40 +08:00
|
|
|
Self::append_center(&mut buffer, &base_style, title_element);
|
|
|
|
|
|
|
|
// Width of the empty space between the left and center area and between the center and right area.
|
|
|
|
let spacing = 1u16;
|
|
|
|
|
|
|
|
let edge_width = buffer.left.width().max(buffer.right.width()) as u16;
|
|
|
|
//let center_width = viewport.width
|
|
|
|
// - (buffer.left.width() as u16 + buffer.right.width() as u16 + 2 * spacing);
|
|
|
|
let center_max_width = viewport.width - (2 * edge_width + 2 * spacing);
|
|
|
|
let center_width = center_max_width.min(buffer.center.width() as u16);
|
2022-05-08 19:02:15 +08:00
|
|
|
|
2022-05-09 02:05:40 +08:00
|
|
|
surface.set_spans(
|
|
|
|
viewport.x + viewport.width / 2 - center_width / 2,
|
2022-05-08 19:02:15 +08:00
|
|
|
viewport.y,
|
2022-05-09 02:05:40 +08:00
|
|
|
&buffer.center,
|
|
|
|
center_width,
|
2022-05-08 19:02:15 +08:00
|
|
|
);
|
|
|
|
}
|
2022-05-08 21:31:53 +08:00
|
|
|
|
2022-05-09 02:05:40 +08:00
|
|
|
fn append_left(buffer: &mut RenderBuffer, base_style: &Style, element: StatusLineElement) {
|
|
|
|
buffer.left.0.push(Span::styled(
|
|
|
|
element.text,
|
|
|
|
element
|
|
|
|
.style
|
|
|
|
.map_or(*base_style, |s| base_style.clone().patch(s)),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn append_center(buffer: &mut RenderBuffer, base_style: &Style, element: StatusLineElement) {
|
|
|
|
buffer.center.0.push(Span::styled(
|
|
|
|
element.text,
|
|
|
|
element
|
|
|
|
.style
|
|
|
|
.map_or(*base_style, |s| base_style.clone().patch(s)),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn append_right(buffer: &mut RenderBuffer, base_style: &Style, element: StatusLineElement) {
|
|
|
|
buffer.right.0.push(Span::styled(
|
|
|
|
element.text,
|
|
|
|
element
|
|
|
|
.style
|
|
|
|
.map_or(*base_style, |s| base_style.clone().patch(s)),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_mode(doc: &Document, is_focused: bool) -> StatusLineElement {
|
2022-05-08 21:31:53 +08:00
|
|
|
return StatusLineElement {
|
|
|
|
text: format!(
|
|
|
|
"{}",
|
|
|
|
match doc.mode() {
|
2022-05-09 02:05:40 +08:00
|
|
|
Mode::Insert if is_focused => "INS",
|
|
|
|
Mode::Select if is_focused => "SEL",
|
|
|
|
Mode::Normal if is_focused => "NOR",
|
|
|
|
// If not focused, explicitly leave an empty space instead of returning None.
|
|
|
|
_ => " ",
|
2022-05-08 21:31:53 +08:00
|
|
|
}
|
|
|
|
),
|
|
|
|
style: None,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_lsp_spinner(doc: &Document, spinners: &ProgressSpinners) -> StatusLineElement {
|
|
|
|
return StatusLineElement {
|
|
|
|
text: format!(
|
2022-05-09 02:05:40 +08:00
|
|
|
" {} ",
|
2022-05-08 21:31:53 +08:00
|
|
|
doc.language_server()
|
|
|
|
.and_then(|srv| spinners.get(srv.id()).and_then(|spinner| spinner.frame()))
|
2022-05-09 02:05:40 +08:00
|
|
|
// Even if there's no spinner; reserve its space to avoid elements frequently shifting.
|
|
|
|
.unwrap_or(" ")
|
2022-05-08 21:31:53 +08:00
|
|
|
),
|
|
|
|
style: None,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_diagnostics_warning_state(theme: &Theme) -> StatusLineElement {
|
|
|
|
return StatusLineElement {
|
|
|
|
text: format!("●"),
|
|
|
|
style: Some(theme.get("warning")),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_diagnostics_warning_count(warnings: usize) -> StatusLineElement {
|
|
|
|
return StatusLineElement {
|
|
|
|
text: format!(" {} ", warnings),
|
|
|
|
style: None,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_diagnostics_error_state(theme: &Theme) -> StatusLineElement {
|
|
|
|
return StatusLineElement {
|
|
|
|
text: format!("●"),
|
|
|
|
style: Some(theme.get("error")),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_diagnostics_error_count(errors: usize) -> StatusLineElement {
|
|
|
|
return StatusLineElement {
|
|
|
|
text: format!(" {} ", errors),
|
|
|
|
style: None,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_selections(selections: usize) -> StatusLineElement {
|
|
|
|
return StatusLineElement {
|
|
|
|
text: format!(
|
|
|
|
" {} sel{} ",
|
|
|
|
&selections,
|
|
|
|
if selections == 1 { "" } else { "s" }
|
|
|
|
),
|
|
|
|
style: None,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-09 02:05:40 +08:00
|
|
|
fn render_position(doc: &Document, view: &View) -> StatusLineElement {
|
|
|
|
let position = coords_at_pos(
|
|
|
|
doc.text().slice(..),
|
|
|
|
doc.selection(view.id)
|
|
|
|
.primary()
|
|
|
|
.cursor(doc.text().slice(..)),
|
|
|
|
);
|
|
|
|
|
2022-05-08 21:31:53 +08:00
|
|
|
return StatusLineElement {
|
|
|
|
text: format!(" {}:{} ", position.row + 1, position.col + 1),
|
|
|
|
style: None,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-09 02:05:40 +08:00
|
|
|
fn render_encoding(doc: &Document) -> Option<StatusLineElement> {
|
|
|
|
let enc = doc.encoding();
|
|
|
|
|
|
|
|
if enc != encoding::UTF_8 {
|
|
|
|
return Some(StatusLineElement {
|
|
|
|
text: format!(" {} ", enc.name()),
|
|
|
|
style: None,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
2022-05-08 21:31:53 +08:00
|
|
|
}
|
|
|
|
|
2022-05-09 02:05:40 +08:00
|
|
|
fn render_file_type(doc: &Document) -> StatusLineElement {
|
|
|
|
let file_type = doc.language_id().unwrap_or("text");
|
|
|
|
|
2022-05-08 21:31:53 +08:00
|
|
|
return StatusLineElement {
|
|
|
|
text: format!(" {} ", file_type),
|
|
|
|
style: None,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_file_name(doc: &Document) -> StatusLineElement {
|
|
|
|
let title = {
|
|
|
|
let rel_path = doc.relative_path();
|
|
|
|
let path = rel_path
|
|
|
|
.as_ref()
|
|
|
|
.map(|p| p.to_string_lossy())
|
|
|
|
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());
|
|
|
|
format!("{}{}", path, if doc.is_modified() { "[+]" } else { "" })
|
|
|
|
};
|
|
|
|
|
|
|
|
return StatusLineElement {
|
|
|
|
text: title,
|
|
|
|
style: None,
|
|
|
|
};
|
|
|
|
}
|
2022-05-08 19:02:15 +08:00
|
|
|
}
|