refactor(statusline): avoid intermediate StatusLineObject

Use a more functional approach to obtain render functions and write to
the buffers, and avoid an intermediate StatusLineElement object.
pull/2434/head
etienne-k 3 years ago
parent 5ecd26fb10
commit 82120df409

@ -163,15 +163,10 @@ impl EditorView {
.clip_top(view.area.height.saturating_sub(1)) .clip_top(view.area.height.saturating_sub(1))
.clip_bottom(1); // -1 from bottom to remove commandline .clip_bottom(1); // -1 from bottom to remove commandline
let context = statusline::RenderContext { let mut context =
doc, statusline::RenderContext::new(doc, view, theme, is_focused, &self.spinners);
view,
theme,
focused: is_focused,
spinners: &self.spinners,
};
StatusLine::render(editor, &context, statusline_area, surface); StatusLine::render(editor, &mut context, statusline_area, surface);
} }
pub fn render_rulers( pub fn render_rulers(

@ -12,25 +12,35 @@ use helix_view::editor::StatusLineElement as StatusLineElementID;
use tui::buffer::Buffer as Surface; use tui::buffer::Buffer as Surface;
use tui::text::{Span, Spans}; use tui::text::{Span, Spans};
/// A status line element contains the information about a component which can be displayed in the status line.
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>,
}
pub struct RenderContext<'a> { pub struct RenderContext<'a> {
pub doc: &'a Document, pub doc: &'a Document,
pub view: &'a View, pub view: &'a View,
pub theme: &'a Theme, pub theme: &'a Theme,
pub focused: bool, pub focused: bool,
pub spinners: &'a ProgressSpinners, pub spinners: &'a ProgressSpinners,
pub buffer: RenderBuffer<'a>,
} }
struct RenderBuffer<'a> { impl<'a> RenderContext<'a> {
pub fn new(
doc: &'a Document,
view: &'a View,
theme: &'a Theme,
focused: bool,
spinners: &'a ProgressSpinners,
) -> Self {
RenderContext {
doc,
view,
theme,
focused,
spinners,
buffer: RenderBuffer::new(),
}
}
}
pub struct RenderBuffer<'a> {
pub left: Spans<'a>, pub left: Spans<'a>,
pub center: Spans<'a>, pub center: Spans<'a>,
pub right: Spans<'a>, pub right: Spans<'a>,
@ -49,9 +59,12 @@ impl<'a> RenderBuffer<'a> {
pub struct StatusLine; pub struct StatusLine;
impl StatusLine { impl StatusLine {
pub fn render(editor: &Editor, context: &RenderContext, viewport: Rect, surface: &mut Surface) { pub fn render(
let mut buffer = RenderBuffer::new(); editor: &Editor,
context: &mut RenderContext,
viewport: Rect,
surface: &mut Surface,
) {
let base_style = if context.focused { let base_style = if context.focused {
context.theme.get("ui.statusline") context.theme.get("ui.statusline")
} else { } else {
@ -60,119 +73,113 @@ impl StatusLine {
surface.set_style(viewport.with_height(1), base_style); surface.set_style(viewport.with_height(1), base_style);
let write_left = |context: &mut RenderContext, text, style| {
Self::append(&mut context.buffer.left, text, &base_style, style)
};
let write_center = |context: &mut RenderContext, text, style| {
Self::append(&mut context.buffer.center, text, &base_style, style)
};
let write_right = |context: &mut RenderContext, text, style| {
Self::append(&mut context.buffer.right, text, &base_style, style)
};
// Left side of the status line. // Left side of the status line.
for element_id in &editor.config().status_line.left { let config = &editor.config().status_line.left;
let elements = Self::render_element(context, *element_id); for render in config.into_iter().map(|id| Self::get_render_function(*id)) {
for element in elements { render(context, write_left);
Self::append_left(&mut buffer, &base_style, element);
}
} }
let element_ids = &editor.config().status_line.left;
element_ids
.into_iter()
.map(|element_id| Self::get_render_function(*element_id))
.for_each(|render| render(context, write_left));
surface.set_spans( surface.set_spans(
viewport.x, viewport.x,
viewport.y, viewport.y,
&buffer.left, &context.buffer.left,
buffer.left.width() as u16, context.buffer.left.width() as u16,
); );
// Right side of the status line. // Right side of the status line.
for element_id in &editor.config().status_line.right { let element_ids = &editor.config().status_line.right;
let elements = Self::render_element(context, *element_id); element_ids
for element in elements { .into_iter()
Self::append_right(&mut buffer, &base_style, element); .map(|element_id| Self::get_render_function(*element_id))
} .for_each(|render| render(context, write_right));
}
surface.set_spans( surface.set_spans(
viewport.x + viewport.width.saturating_sub(buffer.right.width() as u16), viewport.x
+ viewport
.width
.saturating_sub(context.buffer.right.width() as u16),
viewport.y, viewport.y,
&buffer.right, &context.buffer.right,
buffer.right.width() as u16, context.buffer.right.width() as u16,
); );
// Center of the status line. // Center of the status line.
for element_id in &editor.config().status_line.center { let element_ids = &editor.config().status_line.center;
let elements = Self::render_element(context, *element_id); element_ids
for element in elements { .into_iter()
Self::append_center(&mut buffer, &base_style, element); .map(|element_id| Self::get_render_function(*element_id))
} .for_each(|render| render(context, write_center));
}
// Width of the empty space between the left and center area and between the center and right area. // Width of the empty space between the left and center area and between the center and right area.
let spacing = 1u16; let spacing = 1u16;
let edge_width = buffer.left.width().max(buffer.right.width()) as u16; let edge_width = context
.buffer
.left
.width()
.max(context.buffer.right.width()) as u16;
let center_max_width = viewport.width - (2 * edge_width + 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); let center_width = center_max_width.min(context.buffer.center.width() as u16);
surface.set_spans( surface.set_spans(
viewport.x + viewport.width / 2 - center_width / 2, viewport.x + viewport.width / 2 - center_width / 2,
viewport.y, viewport.y,
&buffer.center, &context.buffer.center,
center_width, center_width,
); );
} }
fn append_left(buffer: &mut RenderBuffer, base_style: &Style, element: StatusLineElement) { fn append(buffer: &mut Spans, text: String, base_style: &Style, style: Option<Style>) {
buffer.left.0.push(Span::styled( buffer.0.push(Span::styled(
element.text, text,
element style.map_or(*base_style, |s| (*base_style).patch(s)),
.style
.map_or(*base_style, |s| (*base_style).patch(s)),
)); ));
} }
fn append_center(buffer: &mut RenderBuffer, base_style: &Style, element: StatusLineElement) { fn get_render_function<F>(element_id: StatusLineElementID) -> impl Fn(&mut RenderContext, F)
buffer.center.0.push(Span::styled( where
element.text, F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
element {
.style
.map_or(*base_style, |s| (*base_style).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).patch(s)),
));
}
fn render_element(
context: &RenderContext,
element_id: StatusLineElementID,
) -> Vec<StatusLineElement> {
match element_id { match element_id {
helix_view::editor::StatusLineElement::Mode => vec![Self::render_mode(context)], helix_view::editor::StatusLineElement::Mode => Self::render_mode,
helix_view::editor::StatusLineElement::Spinner => { helix_view::editor::StatusLineElement::Spinner => Self::render_lsp_spinner,
vec![Self::render_lsp_spinner(context)] helix_view::editor::StatusLineElement::FileName => Self::render_file_name,
} helix_view::editor::StatusLineElement::FileEncoding => Self::render_file_encoding,
helix_view::editor::StatusLineElement::FileName => { helix_view::editor::StatusLineElement::FileType => Self::render_file_type,
vec![Self::render_file_name(context)] helix_view::editor::StatusLineElement::Diagnostics => Self::render_diagnostics,
} helix_view::editor::StatusLineElement::Selections => Self::render_selections,
helix_view::editor::StatusLineElement::FileEncoding => { helix_view::editor::StatusLineElement::Position => Self::render_position,
Self::render_file_encoding(context).map_or(Vec::with_capacity(0), |e| vec![e])
}
helix_view::editor::StatusLineElement::FileType => {
vec![Self::render_file_type(context)]
}
helix_view::editor::StatusLineElement::Diagnostics => Self::render_diagnostics(context),
helix_view::editor::StatusLineElement::Selections => {
vec![Self::render_selections(context)]
}
helix_view::editor::StatusLineElement::Position => vec![Self::render_position(context)],
} }
} }
fn render_mode(context: &RenderContext) -> StatusLineElement { fn render_mode<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let visible = context.focused; let visible = context.focused;
return StatusLineElement {
text: format!( write(
context,
format!(
" {} ", " {} ",
match context.doc.mode() { match context.doc.mode() {
Mode::Insert if visible => "INS", Mode::Insert if visible => "INS",
@ -182,13 +189,17 @@ impl StatusLine {
_ => " ", _ => " ",
} }
), ),
style: None, None,
}; );
} }
fn render_lsp_spinner(context: &RenderContext) -> StatusLineElement { fn render_lsp_spinner<F>(context: &mut RenderContext, write: F)
return StatusLineElement { where
text: format!( F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
write(
context,
format!(
" {} ", " {} ",
context context
.doc .doc
@ -200,15 +211,16 @@ impl StatusLine {
// Even if there's no spinner; reserve its space to avoid elements frequently shifting. // Even if there's no spinner; reserve its space to avoid elements frequently shifting.
.unwrap_or(" ") .unwrap_or(" ")
), ),
style: None, None,
}; );
} }
fn render_diagnostics(context: &RenderContext) -> Vec<StatusLineElement> { fn render_diagnostics<F>(context: &mut RenderContext, write: F)
// 2 diagnostics types, each consisting of 2 elements (state + count) where
let mut elements: Vec<StatusLineElement> = Vec::with_capacity(4); F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let diags = context let (warnings, errors) =
context
.doc .doc
.diagnostics() .diagnostics()
.iter() .iter()
@ -221,75 +233,65 @@ impl StatusLine {
} }
counts counts
}); });
let (warnings, errors) = diags;
for i in 0..2 {
let (count, state_element, count_element) = match i {
0 => (
warnings,
Self::render_diagnostics_warning_state(context),
Self::render_diagnostics_warning_count(context, warnings),
),
1 => (
errors,
Self::render_diagnostics_error_state(context),
Self::render_diagnostics_error_count(context, errors),
),
_ => unreachable!(),
};
if count > 0 { if warnings > 0 {
elements.push(state_element); Self::render_diagnostics_warning_state(context, write);
elements.push(count_element); Self::render_diagnostics_warning_count(context, warnings, write);
}
} }
elements if errors > 0 {
Self::render_diagnostics_error_state(context, write);
Self::render_diagnostics_error_count(context, errors, write);
} }
fn render_diagnostics_warning_state(context: &RenderContext) -> StatusLineElement {
StatusLineElement {
text: "●".to_string(),
style: Some(context.theme.get("warning")),
} }
fn render_diagnostics_warning_state<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
write(context, "●".to_string(), Some(context.theme.get("warning")));
} }
fn render_diagnostics_warning_count( fn render_diagnostics_warning_count<F>(
_context: &RenderContext, context: &mut RenderContext,
warning_count: usize, warning_count: usize,
) -> StatusLineElement { write: F,
StatusLineElement { ) where
text: format!(" {} ", warning_count), F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
style: None, {
} write(context, format!(" {} ", warning_count), None);
} }
fn render_diagnostics_error_state(context: &RenderContext) -> StatusLineElement { fn render_diagnostics_error_state<F>(context: &mut RenderContext, write: F)
StatusLineElement { where
text: "●".to_string(), F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
style: Some(context.theme.get("error")), {
} write(context, "●".to_string(), Some(context.theme.get("error")));
} }
fn render_diagnostics_error_count( fn render_diagnostics_error_count<F>(context: &mut RenderContext, error_count: usize, write: F)
_context: &RenderContext, where
error_count: usize, F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
) -> StatusLineElement { {
StatusLineElement { write(context, format!(" {} ", error_count), None);
text: format!(" {} ", error_count),
style: None,
}
} }
fn render_selections(context: &RenderContext) -> StatusLineElement { fn render_selections<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let count = context.doc.selection(context.view.id).len(); let count = context.doc.selection(context.view.id).len();
StatusLineElement { write(
text: format!(" {} sel{} ", count, if count == 1 { "" } else { "s" }), context,
style: None, format!(" {} sel{} ", count, if count == 1 { "" } else { "s" }),
} None,
);
} }
fn render_position(context: &RenderContext) -> StatusLineElement { fn render_position<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let position = coords_at_pos( let position = coords_at_pos(
context.doc.text().slice(..), context.doc.text().slice(..),
context context
@ -299,35 +301,37 @@ impl StatusLine {
.cursor(context.doc.text().slice(..)), .cursor(context.doc.text().slice(..)),
); );
return StatusLineElement { write(
text: format!(" {}:{} ", position.row + 1, position.col + 1), context,
style: None, format!(" {}:{} ", position.row + 1, position.col + 1),
}; None,
);
} }
fn render_file_encoding(context: &RenderContext) -> Option<StatusLineElement> { fn render_file_encoding<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let enc = context.doc.encoding(); let enc = context.doc.encoding();
if enc != encoding::UTF_8 { if enc != encoding::UTF_8 {
Some(StatusLineElement { write(context, format!(" {} ", enc.name()), None);
text: format!(" {} ", enc.name()),
style: None,
})
} else {
None
} }
} }
fn render_file_type(context: &RenderContext) -> StatusLineElement { fn render_file_type<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let file_type = context.doc.language_id().unwrap_or("text"); let file_type = context.doc.language_id().unwrap_or("text");
StatusLineElement { write(context, format!(" {} ", file_type), None);
text: format!(" {} ", file_type),
style: None,
}
} }
fn render_file_name(context: &RenderContext) -> StatusLineElement { fn render_file_name<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let title = { let title = {
let rel_path = context.doc.relative_path(); let rel_path = context.doc.relative_path();
let path = rel_path let path = rel_path
@ -341,9 +345,6 @@ impl StatusLine {
) )
}; };
StatusLineElement { write(context, title, None);
text: title,
style: None,
}
} }
} }

Loading…
Cancel
Save