on master help sign in
Replace menu::Item::{row, label} with format()
commit5c7db7aed54f52b3af6102517f81c9d70bb6d1fb authorGokul Soumya <gokulps15@gmail.com> dateSun 25 Dec 2022, 11:24 (4 years ago) parentb2837ff
7 files changed+39−36
helix-term/src/commands.rs+5−5 helix-term/src/commands/dap.rs+4−4 helix-term/src/commands/lsp.rs+10−6 helix-term/src/ui/completion.rs+1−6 helix-term/src/ui/menu.rs+10−12 helix-term/src/ui/picker.rs+3−3 helix-tui/src/widgets/table.rs+6
helix-term/src/commands.rs+5−5
@@ -5,7 +5,7 @@ pub(crate) mod typed;
55pub use dap::*;
66use helix_vcs::Hunk;
77pub use lsp::*;
8−use tui::text::Spans;
8+use tui::widgets::Row;
99pub use typed::*;
1010
1111use helix_core::{
@@ -1864,7 +1864,7 @@ fn global_search(cx: &mut Context) {
18641864 impl ui::menu::Item for FileResult {
18651865 type Data = Option<PathBuf>;
18661866
1867− fn label(&self, current_path: &Self::Data) -> Spans {
1867+ fn format(&self, current_path: &Self::Data) -> Row {
18681868 let relative_path = helix_core::path::get_relative_path(&self.path)
18691869 .to_string_lossy()
18701870 .into_owned();
@@ -2312,7 +2312,7 @@ fn buffer_picker(cx: &mut Context) {
23122312 impl ui::menu::Item for BufferMeta {
23132313 type Data = ();
23142314
2315− fn label(&self, _data: &Self::Data) -> Spans {
2315+ fn format(&self, _data: &Self::Data) -> Row {
23162316 let path = self
23172317 .path
23182318 .as_deref()
@@ -2381,7 +2381,7 @@ fn jumplist_picker(cx: &mut Context) {
23812381 impl ui::menu::Item for JumpMeta {
23822382 type Data = ();
23832383
2384− fn label(&self, _data: &Self::Data) -> Spans {
2384+ fn format(&self, _data: &Self::Data) -> Row {
23852385 let path = self
23862386 .path
23872387 .as_deref()
@@ -2454,7 +2454,7 @@ fn jumplist_picker(cx: &mut Context) {
24542454impl ui::menu::Item for MappableCommand {
24552455 type Data = ReverseKeymap;
24562456
2457− fn label(&self, keymap: &Self::Data) -> Spans {
2457+ fn format(&self, keymap: &Self::Data) -> Row {
24582458 let fmt_binding = |bindings: &Vec<Vec<KeyEvent>>| -> String {
24592459 bindings.iter().fold(String::new(), |mut acc, bind| {
24602460 if !acc.is_empty() {
helix-term/src/commands/dap.rs+4−4
@@ -12,7 +12,7 @@ use helix_view::editor::Breakpoint;
1212
1313use serde_json::{to_value, Value};
1414use tokio_stream::wrappers::UnboundedReceiverStream;
15−use tui::text::Spans;
15+use tui::{text::Spans, widgets::Row};
1616
1717use std::collections::HashMap;
1818use std::future::Future;
@@ -25,7 +25,7 @@ use helix_view::handlers::dap::{breakpoints_changed, jump_to_stack_frame, select
2525impl ui::menu::Item for StackFrame {
2626 type Data = ();
2727
28− fn label(&self, _data: &Self::Data) -> Spans {
28+ fn format(&self, _data: &Self::Data) -> Row {
2929 self.name.as_str().into() // TODO: include thread_states in the label
3030 }
3131}
@@ -33,7 +33,7 @@ impl ui::menu::Item for StackFrame {
3333impl ui::menu::Item for DebugTemplate {
3434 type Data = ();
3535
36− fn label(&self, _data: &Self::Data) -> Spans {
36+ fn format(&self, _data: &Self::Data) -> Row {
3737 self.name.as_str().into()
3838 }
3939}
@@ -41,7 +41,7 @@ impl ui::menu::Item for DebugTemplate {
4141impl ui::menu::Item for Thread {
4242 type Data = ThreadStates;
4343
44− fn label(&self, thread_states: &Self::Data) -> Spans {
44+ fn format(&self, thread_states: &Self::Data) -> Row {
4545 format!(
4646 "{} ({})",
4747 self.name,
helix-term/src/commands/lsp.rs+10−6
@@ -5,7 +5,10 @@ use helix_lsp::{
55 util::{diagnostic_to_lsp_diagnostic, lsp_pos_to_pos, lsp_range_to_range, range_to_lsp_range},
66 OffsetEncoding,
77};
8−use tui::text::{Span, Spans};
8+use tui::{
9+ text::{Span, Spans},
10+ widgets::Row,
11+};
912
1013use super::{align_view, push_jump, Align, Context, Editor, Open};
1114
@@ -46,7 +49,7 @@ impl ui::menu::Item for lsp::Location {
4649 /// Current working directory.
4750 type Data = PathBuf;
4851
49− fn label(&self, cwdir: &Self::Data) -> Spans {
52+ fn format(&self, cwdir: &Self::Data) -> Row {
5053 // The preallocation here will overallocate a few characters since it will account for the
5154 // URL's scheme, which is not used most of the time since that scheme will be "file://".
5255 // Those extra chars will be used to avoid allocating when writing the line number (in the
@@ -80,7 +83,7 @@ impl ui::menu::Item for lsp::SymbolInformation {
8083 /// Path to currently focussed document
8184 type Data = Option<lsp::Url>;
8285
83− fn label(&self, current_doc_path: &Self::Data) -> Spans {
86+ fn format(&self, current_doc_path: &Self::Data) -> Row {
8487 if current_doc_path.as_ref() == Some(&self.location.uri) {
8588 self.name.as_str().into()
8689 } else {
@@ -110,7 +113,7 @@ struct PickerDiagnostic {
110113impl ui::menu::Item for PickerDiagnostic {
111114 type Data = (DiagnosticStyles, DiagnosticsFormat);
112115
113− fn label(&self, (styles, format): &Self::Data) -> Spans {
116+ fn format(&self, (styles, format): &Self::Data) -> Row {
114117 let mut style = self
115118 .diag
116119 .severity
@@ -149,6 +152,7 @@ impl ui::menu::Item for PickerDiagnostic {
149152 Span::styled(&self.diag.message, style),
150153 Span::styled(code, style),
151154 ])
155+ .into()
152156 }
153157}
154158
@@ -467,7 +471,7 @@ pub fn workspace_diagnostics_picker(cx: &mut Context) {
467471
468472impl ui::menu::Item for lsp::CodeActionOrCommand {
469473 type Data = ();
470− fn label(&self, _data: &Self::Data) -> Spans {
474+ fn format(&self, _data: &Self::Data) -> Row {
471475 match self {
472476 lsp::CodeActionOrCommand::CodeAction(action) => action.title.as_str().into(),
473477 lsp::CodeActionOrCommand::Command(command) => command.title.as_str().into(),
@@ -662,7 +666,7 @@ pub fn code_action(cx: &mut Context) {
662666
663667impl ui::menu::Item for lsp::Command {
664668 type Data = ();
665− fn label(&self, _data: &Self::Data) -> Spans {
669+ fn format(&self, _data: &Self::Data) -> Row {
666670 self.title.as_str().into()
667671 }
668672}
helix-term/src/ui/completion.rs+1−6
@@ -1,7 +1,6 @@
11use crate::compositor::{Component, Context, Event, EventResult};
22use helix_view::{apply_transaction, editor::CompleteAction, ViewId};
33use tui::buffer::Buffer as Surface;
4−use tui::text::Spans;
54
65use std::borrow::Cow;
76
@@ -33,11 +32,7 @@ impl menu::Item for CompletionItem {
3332 .into()
3433 }
3534
36− fn label(&self, _data: &Self::Data) -> Spans {
37− self.label.as_str().into()
38− }
39−
40− fn row(&self, _data: &Self::Data) -> menu::Row {
35+ fn format(&self, _data: &Self::Data) -> menu::Row {
4136 menu::Row::new(vec![
4237 menu::Cell::from(self.label.as_str()),
4338 menu::Cell::from(match self.kind {
helix-term/src/ui/menu.rs+10−12
@@ -4,7 +4,7 @@ use crate::{
44 compositor::{Callback, Component, Compositor, Context, Event, EventResult},
55 ctrl, key, shift,
66};
7−use tui::{buffer::Buffer as Surface, text::Spans, widgets::Table};
7+use tui::{buffer::Buffer as Surface, widgets::Table};
88
99pub use tui::widgets::{Cell, Row};
1010
@@ -18,28 +18,24 @@ pub trait Item {
1818 /// Additional editor state that is used for label calculation.
1919 type Data;
2020
21− fn label(&self, data: &Self::Data) -> Spans;
21+ fn format(&self, data: &Self::Data) -> Row;
2222
2323 fn sort_text(&self, data: &Self::Data) -> Cow<str> {
24− let label: String = self.label(data).into();
24+ let label: String = self.format(data).cell_text().collect();
2525 label.into()
2626 }
2727
2828 fn filter_text(&self, data: &Self::Data) -> Cow<str> {
29− let label: String = self.label(data).into();
29+ let label: String = self.format(data).cell_text().collect();
3030 label.into()
3131 }
32−
33− fn row(&self, data: &Self::Data) -> Row {
34− Row::new(vec![Cell::from(self.label(data))])
35− }
3632}
3733
3834impl Item for PathBuf {
3935 /// Root prefix to strip.
4036 type Data = PathBuf;
4137
42− fn label(&self, root_path: &Self::Data) -> Spans {
38+ fn format(&self, root_path: &Self::Data) -> Row {
4339 self.strip_prefix(root_path)
4440 .unwrap_or(self)
4541 .to_string_lossy()
@@ -144,10 +140,10 @@ impl<T: Item> Menu<T> {
144140 let n = self
145141 .options
146142 .first()
147− .map(|option| option.row(&self.editor_data).cells.len())
143+ .map(|option| option.format(&self.editor_data).cells.len())
148144 .unwrap_or_default();
149145 let max_lens = self.options.iter().fold(vec![0; n], |mut acc, option| {
150− let row = option.row(&self.editor_data);
146+ let row = option.format(&self.editor_data);
151147 // maintain max for each column
152148 for (acc, cell) in acc.iter_mut().zip(row.cells.iter()) {
153149 let width = cell.content.width();
@@ -331,7 +327,9 @@ impl<T: Item + 'static> Component for Menu<T> {
331327 (a + b - 1) / b
332328 }
333329
334− let rows = options.iter().map(|option| option.row(&self.editor_data));
330+ let rows = options
331+ .iter()
332+ .map(|option| option.format(&self.editor_data));
335333 let table = Table::new(rows)
336334 .style(style)
337335 .highlight_style(selected)
helix-term/src/ui/picker.rs+3−3
@@ -410,10 +410,10 @@ impl<T: Item> Picker<T> {
410410
411411 let n = options
412412 .first()
413− .map(|option| option.row(&editor_data).cells.len())
413+ .map(|option| option.format(&editor_data).cells.len())
414414 .unwrap_or_default();
415415 let max_lens = options.iter().fold(vec![0; n], |mut acc, option| {
416− let row = option.row(&editor_data);
416+ let row = option.format(&editor_data);
417417 // maintain max for each column
418418 for (acc, cell) in acc.iter_mut().zip(row.cells.iter()) {
419419 let width = cell.content.width();
@@ -729,7 +729,7 @@ impl<T: Item + 'static> Component for Picker<T> {
729729 .skip(offset)
730730 .take(rows as usize)
731731 .map(|pmatch| &self.options[pmatch.index])
732− .map(|option| option.row(&self.editor_data))
732+ .map(|option| option.format(&self.editor_data))
733733 .map(|mut row| {
734734 const TEMP_CELL_SEP: &str = " ";
735735
helix-tui/src/widgets/table.rs+6
@@ -127,6 +127,12 @@ impl<'a> Row<'a> {
127127 }
128128}
129129
130+impl<'a, T: Into<Cell<'a>>> From<T> for Row<'a> {
131+ fn from(cell: T) -> Self {
132+ Row::new(vec![cell.into()])
133+ }
134+}
135+
130136/// A widget to display data in formatted columns.
131137///
132138/// It is a collection of [`Row`]s, themselves composed of [`Cell`]s: