refactor(statusline): make clippy happy

pull/2434/head
etienne-k 3 years ago
parent 90db789577
commit 5ecd26fb10

@ -28,6 +28,8 @@ use std::borrow::Cow;
use crossterm::event::{Event, MouseButton, MouseEvent, MouseEventKind}; use crossterm::event::{Event, MouseButton, MouseEvent, MouseEventKind};
use tui::buffer::Buffer as Surface; use tui::buffer::Buffer as Surface;
use super::statusline;
pub struct EditorView { pub struct EditorView {
pub keymaps: Keymaps, pub keymaps: Keymaps,
on_next_key: Option<Box<dyn FnOnce(&mut commands::Context, KeyEvent)>>, on_next_key: Option<Box<dyn FnOnce(&mut commands::Context, KeyEvent)>>,
@ -160,7 +162,16 @@ impl EditorView {
.area .area
.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
self.render_statusline(editor, doc, view, statusline_area, surface, is_focused);
let context = statusline::RenderContext {
doc,
view,
theme,
focused: is_focused,
spinners: &self.spinners,
};
StatusLine::render(editor, &context, statusline_area, surface);
} }
pub fn render_rulers( pub fn render_rulers(
@ -738,15 +749,15 @@ impl EditorView {
surface: &mut Surface, surface: &mut Surface,
is_focused: bool, is_focused: bool,
) { ) {
StatusLine::render( let context = statusline::RenderContext {
editor,
doc, doc,
view, view,
viewport, theme: &editor.theme,
surface, focused: is_focused,
is_focused, spinners: &self.spinners,
&self.spinners, };
);
StatusLine::render(editor, &context, viewport, surface);
} }
/// Handle events by looking them up in `self.keymaps`. Returns None /// Handle events by looking them up in `self.keymaps`. Returns None

@ -22,8 +22,8 @@ struct StatusLineElement {
pub style: Option<Style>, pub style: Option<Style>,
} }
struct RenderContext<'a> { pub struct RenderContext<'a> {
pub document: &'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,
@ -38,39 +38,24 @@ struct RenderBuffer<'a> {
impl<'a> RenderBuffer<'a> { impl<'a> RenderBuffer<'a> {
pub fn new() -> Self { pub fn new() -> Self {
return Self { Self {
left: Spans::default(), left: Spans::default(),
center: Spans::default(), center: Spans::default(),
right: Spans::default(), right: Spans::default(),
}; }
} }
} }
pub struct StatusLine; pub struct StatusLine;
impl StatusLine { impl StatusLine {
pub fn render( pub fn render(editor: &Editor, context: &RenderContext, viewport: Rect, surface: &mut Surface) {
editor: &Editor,
doc: &Document,
view: &View,
viewport: Rect,
surface: &mut Surface,
is_focused: bool,
spinners: &ProgressSpinners,
) {
let context = RenderContext {
document: doc,
view: view,
theme: &editor.theme,
focused: is_focused,
spinners: spinners,
};
let mut buffer = RenderBuffer::new(); let mut buffer = RenderBuffer::new();
let base_style = if is_focused { let base_style = if context.focused {
editor.theme.get("ui.statusline") context.theme.get("ui.statusline")
} else { } else {
editor.theme.get("ui.statusline.inactive") context.theme.get("ui.statusline.inactive")
}; };
surface.set_style(viewport.with_height(1), base_style); surface.set_style(viewport.with_height(1), base_style);
@ -78,7 +63,7 @@ impl StatusLine {
// Left side of the status line. // Left side of the status line.
for element_id in &editor.config().status_line.left { for element_id in &editor.config().status_line.left {
let elements = Self::render_element(&context, *element_id); let elements = Self::render_element(context, *element_id);
for element in elements { for element in elements {
Self::append_left(&mut buffer, &base_style, element); Self::append_left(&mut buffer, &base_style, element);
} }
@ -94,7 +79,7 @@ impl StatusLine {
// Right side of the status line. // Right side of the status line.
for element_id in &editor.config().status_line.right { for element_id in &editor.config().status_line.right {
let elements = Self::render_element(&context, *element_id); let elements = Self::render_element(context, *element_id);
for element in elements { for element in elements {
Self::append_right(&mut buffer, &base_style, element); Self::append_right(&mut buffer, &base_style, element);
} }
@ -110,7 +95,7 @@ impl StatusLine {
// Center of the status line. // Center of the status line.
for element_id in &editor.config().status_line.center { for element_id in &editor.config().status_line.center {
let elements = Self::render_element(&context, *element_id); let elements = Self::render_element(context, *element_id);
for element in elements { for element in elements {
Self::append_center(&mut buffer, &base_style, element); Self::append_center(&mut buffer, &base_style, element);
} }
@ -136,7 +121,7 @@ impl StatusLine {
element.text, element.text,
element element
.style .style
.map_or(*base_style, |s| base_style.clone().patch(s)), .map_or(*base_style, |s| (*base_style).patch(s)),
)); ));
} }
@ -145,7 +130,7 @@ impl StatusLine {
element.text, element.text,
element element
.style .style
.map_or(*base_style, |s| base_style.clone().patch(s)), .map_or(*base_style, |s| (*base_style).patch(s)),
)); ));
} }
@ -154,7 +139,7 @@ impl StatusLine {
element.text, element.text,
element element
.style .style
.map_or(*base_style, |s| base_style.clone().patch(s)), .map_or(*base_style, |s| (*base_style).patch(s)),
)); ));
} }
@ -162,7 +147,7 @@ impl StatusLine {
context: &RenderContext, context: &RenderContext,
element_id: StatusLineElementID, element_id: StatusLineElementID,
) -> Vec<StatusLineElement> { ) -> Vec<StatusLineElement> {
return match element_id { match element_id {
helix_view::editor::StatusLineElement::Mode => vec![Self::render_mode(context)], helix_view::editor::StatusLineElement::Mode => vec![Self::render_mode(context)],
helix_view::editor::StatusLineElement::Spinner => { helix_view::editor::StatusLineElement::Spinner => {
vec![Self::render_lsp_spinner(context)] vec![Self::render_lsp_spinner(context)]
@ -181,7 +166,7 @@ impl StatusLine {
vec![Self::render_selections(context)] vec![Self::render_selections(context)]
} }
helix_view::editor::StatusLineElement::Position => vec![Self::render_position(context)], helix_view::editor::StatusLineElement::Position => vec![Self::render_position(context)],
}; }
} }
fn render_mode(context: &RenderContext) -> StatusLineElement { fn render_mode(context: &RenderContext) -> StatusLineElement {
@ -189,7 +174,7 @@ impl StatusLine {
return StatusLineElement { return StatusLineElement {
text: format!( text: format!(
" {} ", " {} ",
match context.document.mode() { match context.doc.mode() {
Mode::Insert if visible => "INS", Mode::Insert if visible => "INS",
Mode::Select if visible => "SEL", Mode::Select if visible => "SEL",
Mode::Normal if visible => "NOR", Mode::Normal if visible => "NOR",
@ -206,7 +191,7 @@ impl StatusLine {
text: format!( text: format!(
" {} ", " {} ",
context context
.document .doc
.language_server() .language_server()
.and_then(|srv| context .and_then(|srv| context
.spinners .spinners
@ -224,7 +209,7 @@ impl StatusLine {
let mut elements: Vec<StatusLineElement> = Vec::with_capacity(4); let mut elements: Vec<StatusLineElement> = Vec::with_capacity(4);
let diags = context let diags = context
.document .doc
.diagnostics() .diagnostics()
.iter() .iter()
.fold((0, 0), |mut counts, diag| { .fold((0, 0), |mut counts, diag| {
@ -259,59 +244,59 @@ impl StatusLine {
} }
} }
return elements; elements
} }
fn render_diagnostics_warning_state(context: &RenderContext) -> StatusLineElement { fn render_diagnostics_warning_state(context: &RenderContext) -> StatusLineElement {
return StatusLineElement { StatusLineElement {
text: format!("●"), text: "●".to_string(),
style: Some(context.theme.get("warning")), style: Some(context.theme.get("warning")),
}; }
} }
fn render_diagnostics_warning_count( fn render_diagnostics_warning_count(
_context: &RenderContext, _context: &RenderContext,
warning_count: usize, warning_count: usize,
) -> StatusLineElement { ) -> StatusLineElement {
return StatusLineElement { StatusLineElement {
text: format!(" {} ", warning_count), text: format!(" {} ", warning_count),
style: None, style: None,
}; }
} }
fn render_diagnostics_error_state(context: &RenderContext) -> StatusLineElement { fn render_diagnostics_error_state(context: &RenderContext) -> StatusLineElement {
return StatusLineElement { StatusLineElement {
text: format!("●"), text: "●".to_string(),
style: Some(context.theme.get("error")), style: Some(context.theme.get("error")),
}; }
} }
fn render_diagnostics_error_count( fn render_diagnostics_error_count(
_context: &RenderContext, _context: &RenderContext,
error_count: usize, error_count: usize,
) -> StatusLineElement { ) -> StatusLineElement {
return StatusLineElement { StatusLineElement {
text: format!(" {} ", error_count), text: format!(" {} ", error_count),
style: None, style: None,
}; }
} }
fn render_selections(context: &RenderContext) -> StatusLineElement { fn render_selections(context: &RenderContext) -> StatusLineElement {
let count = context.document.selection(context.view.id).len(); let count = context.doc.selection(context.view.id).len();
return StatusLineElement { StatusLineElement {
text: format!(" {} sel{} ", count, if count == 1 { "" } else { "s" }), text: format!(" {} sel{} ", count, if count == 1 { "" } else { "s" }),
style: None, style: None,
}; }
} }
fn render_position(context: &RenderContext) -> StatusLineElement { fn render_position(context: &RenderContext) -> StatusLineElement {
let position = coords_at_pos( let position = coords_at_pos(
context.document.text().slice(..), context.doc.text().slice(..),
context context
.document .doc
.selection(context.view.id) .selection(context.view.id)
.primary() .primary()
.cursor(context.document.text().slice(..)), .cursor(context.doc.text().slice(..)),
); );
return StatusLineElement { return StatusLineElement {
@ -321,30 +306,30 @@ impl StatusLine {
} }
fn render_file_encoding(context: &RenderContext) -> Option<StatusLineElement> { fn render_file_encoding(context: &RenderContext) -> Option<StatusLineElement> {
let enc = context.document.encoding(); let enc = context.doc.encoding();
if enc != encoding::UTF_8 { if enc != encoding::UTF_8 {
return Some(StatusLineElement { Some(StatusLineElement {
text: format!(" {} ", enc.name()), text: format!(" {} ", enc.name()),
style: None, style: None,
}); })
} else { } else {
return None; None
} }
} }
fn render_file_type(context: &RenderContext) -> StatusLineElement { fn render_file_type(context: &RenderContext) -> StatusLineElement {
let file_type = context.document.language_id().unwrap_or("text"); let file_type = context.doc.language_id().unwrap_or("text");
return StatusLineElement { StatusLineElement {
text: format!(" {} ", file_type), text: format!(" {} ", file_type),
style: None, style: None,
}; }
} }
fn render_file_name(context: &RenderContext) -> StatusLineElement { fn render_file_name(context: &RenderContext) -> StatusLineElement {
let title = { let title = {
let rel_path = context.document.relative_path(); let rel_path = context.doc.relative_path();
let path = rel_path let path = rel_path
.as_ref() .as_ref()
.map(|p| p.to_string_lossy()) .map(|p| p.to_string_lossy())
@ -352,17 +337,13 @@ impl StatusLine {
format!( format!(
" {}{} ", " {}{} ",
path, path,
if context.document.is_modified() { if context.doc.is_modified() { "[+]" } else { "" }
"[+]"
} else {
""
}
) )
}; };
return StatusLineElement { StatusLineElement {
text: title, text: title,
style: None, style: None,
}; }
} }
} }

@ -193,11 +193,11 @@ impl Default for StatusLineConfig {
fn default() -> Self { fn default() -> Self {
use StatusLineElement as E; use StatusLineElement as E;
return Self { Self {
left: vec![E::Mode, E::Spinner, E::FileName], left: vec![E::Mode, E::Spinner, E::FileName],
center: vec![], center: vec![],
right: vec![E::Diagnostics, E::Selections, E::Position, E::FileEncoding], right: vec![E::Diagnostics, E::Selections, E::Position, E::FileEncoding],
}; }
} }
} }

Loading…
Cancel
Save