From da7c1769b1c622ec684e59f5c7a864dd48ac7c13 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Thu, 5 Jan 2023 21:41:16 +0100 Subject: [PATCH] chore: use a proper type for Vec<(Rc, Vec)> --- src/cmd_line/mod.rs | 58 +++++++++++----------- src/ui_model/mod.rs | 2 +- src/ui_model/model_layout.rs | 95 ++++++++++++++++++++++++++++-------- 3 files changed, 105 insertions(+), 50 deletions(-) diff --git a/src/cmd_line/mod.rs b/src/cmd_line/mod.rs index f81f132b..436dd3a2 100644 --- a/src/cmd_line/mod.rs +++ b/src/cmd_line/mod.rs @@ -13,7 +13,7 @@ use gtk::prelude::*; use unicode_segmentation::UnicodeSegmentation; use crate::cursor; -use crate::highlight::{Highlight, HighlightMap}; +use crate::highlight::HighlightMap; use crate::mode; use crate::nvim::{self, *}; use crate::nvim_viewport::NvimViewport; @@ -21,7 +21,7 @@ use crate::popup_menu; use crate::render::{self, CellMetrics}; use crate::shell; use crate::ui::UiMutex; -use crate::ui_model::ModelLayout; +use crate::ui_model::{HighlightedLine, HighlightedRange, ModelLayout}; use viewport::CmdlineViewport; @@ -61,7 +61,7 @@ impl Level { level } - fn replace_line(&mut self, lines: Vec, Vec)>>, append: bool) { + fn replace_line(&mut self, lines: Vec, append: bool) { if append { self.model_layout.layout_append(lines); } else { @@ -93,7 +93,7 @@ impl Level { } pub fn from_lines( - lines: Vec, Vec)>>, + lines: Vec, max_width: i32, render_state: &shell::RenderState, ) -> Self { @@ -129,19 +129,20 @@ impl Level { } } -type PromptLine = (Rc, Vec); - fn prompt_lines( firstc: &str, prompt: &str, indent: u64, hl: &HighlightMap, -) -> (usize, Vec) { - let prompt: Vec = if !firstc.is_empty() { +) -> (usize, Vec) { + let prompt: Vec = if !firstc.is_empty() { if firstc.len() >= indent as usize { - vec![(hl.default_hl(), vec![firstc.to_owned()])] + vec![HighlightedRange { + highlight: hl.default_hl(), + graphemes: vec![firstc.to_owned()], + }] } else { - vec![( + vec![HighlightedRange::new( hl.default_hl(), iter::once(firstc.to_owned()) .chain((firstc.len()..indent as usize).map(|_| " ".to_owned())) @@ -151,18 +152,19 @@ fn prompt_lines( } else if !prompt.is_empty() { prompt .lines() - .map(|l| { - ( - hl.default_hl(), - l.graphemes(true).map(|g| g.to_owned()).collect(), - ) + .map(|l| HighlightedRange { + highlight: hl.default_hl(), + graphemes: l.graphemes(true).map(|g| g.to_owned()).collect(), }) .collect() } else { vec![] }; - let prompt_offset = prompt.last().map(|l| l.1.len()).unwrap_or(0); + let prompt_offset = prompt + .last() + .map(|range| range.graphemes.len()) + .unwrap_or(0); (prompt_offset, prompt) } @@ -541,25 +543,23 @@ impl<'a> CmdLineContext<'a> { } struct LineContent { - lines: Vec, Vec)>>, + lines: Vec, prompt_offset: usize, } trait ToAttributedModelContent { - fn to_attributed_content(&self, hl: &HighlightMap) -> Vec, Vec)>>; + fn to_attributed_content(&self, hl: &HighlightMap) -> Vec; } impl ToAttributedModelContent for Vec> { - fn to_attributed_content(&self, hl: &HighlightMap) -> Vec, Vec)>> { + fn to_attributed_content(&self, hl: &HighlightMap) -> Vec { self.iter() .map(|line_chars| { line_chars .iter() - .map(|c| { - ( - hl.get(c.0.into()), - c.1.graphemes(true).map(|g| g.to_owned()).collect(), - ) + .map(|c| HighlightedRange { + highlight: hl.get(c.0.into()), + graphemes: c.1.graphemes(true).map(|g| g.to_owned()).collect(), }) .collect() }) @@ -568,14 +568,12 @@ impl ToAttributedModelContent for Vec> { } impl ToAttributedModelContent for Vec<(u64, String)> { - fn to_attributed_content(&self, hl: &HighlightMap) -> Vec, Vec)>> { + fn to_attributed_content(&self, hl: &HighlightMap) -> Vec { vec![self .iter() - .map(|c| { - ( - hl.get(c.0.into()), - c.1.graphemes(true).map(|g| g.to_owned()).collect(), - ) + .map(|c| HighlightedRange { + highlight: hl.get(c.0.into()), + graphemes: c.1.graphemes(true).map(|g| g.to_owned()).collect(), }) .collect()] } diff --git a/src/ui_model/mod.rs b/src/ui_model/mod.rs index e168aee6..2cf24e4b 100644 --- a/src/ui_model/mod.rs +++ b/src/ui_model/mod.rs @@ -10,7 +10,7 @@ mod model_rect; pub use self::cell::Cell; pub use self::item::Item; pub use self::line::{Line, StyledLine}; -pub use self::model_layout::ModelLayout; +pub use self::model_layout::{HighlightedLine, HighlightedRange, ModelLayout}; pub use self::model_rect::ModelRect; #[derive(Default)] diff --git a/src/ui_model/model_layout.rs b/src/ui_model/model_layout.rs index b2f8174d..d55eadb6 100644 --- a/src/ui_model/model_layout.rs +++ b/src/ui_model/model_layout.rs @@ -6,11 +6,28 @@ use unicode_width::UnicodeWidthStr; use crate::highlight::Highlight; use crate::ui_model::UiModel; +#[derive(Clone)] +pub struct HighlightedRange { + pub highlight: Rc, + pub graphemes: Vec, +} + +impl HighlightedRange { + pub fn new(highlight: Rc, graphemes: Vec) -> Self { + Self { + highlight, + graphemes, + } + } +} + +pub type HighlightedLine = Vec; + pub struct ModelLayout { pub model: UiModel, rows_filled: usize, cols_filled: usize, - lines: Vec, Vec)>>, + lines: Vec, } impl ModelLayout { @@ -25,7 +42,7 @@ impl ModelLayout { } } - pub fn layout_append(&mut self, mut lines: Vec, Vec)>>) { + pub fn layout_append(&mut self, mut lines: Vec) { let rows_filled = self.rows_filled; let take_from = self.lines.len(); @@ -34,7 +51,7 @@ impl ModelLayout { self.layout_replace(rows_filled, take_from); } - pub fn layout(&mut self, lines: Vec, Vec)>>) { + pub fn layout(&mut self, lines: Vec) { self.lines = lines; self.layout_replace(0, 0); } @@ -85,18 +102,18 @@ impl ModelLayout { } fn insert_into_lines(&mut self, ch: String) { - let line = &mut self.lines[0]; + let highlight_ranges = &mut self.lines[0]; let (_, cur_col) = self.model.get_real_cursor(); let mut col_idx = 0; - for &mut (_, ref mut chars) in line { - if cur_col < col_idx + chars.len() { + for range in highlight_ranges.iter_mut() { + if cur_col < col_idx + range.graphemes.len() { let col_sub_idx = cur_col - col_idx; - chars.insert(col_sub_idx, ch); + range.graphemes.insert(col_sub_idx, ch); break; } else { - col_idx += chars.len(); + col_idx += range.graphemes.len(); } } } @@ -115,8 +132,12 @@ impl ModelLayout { let mut max_col_idx = 0; let mut col_idx = 0; let mut row_idx = row_offset; - for content in lines { - for (hl, ch_list) in content { + for highlight_ranges in lines { + for HighlightedRange { + highlight: hl, + graphemes: ch_list, + } in highlight_ranges + { for ch in ch_list { let ch_width = max(1, ch.width()); @@ -156,11 +177,11 @@ impl ModelLayout { } } - fn count_lines(lines: &[Vec<(Rc, Vec)>], max_columns: usize) -> usize { + fn count_lines(lines: &[HighlightedLine], max_columns: usize) -> usize { let mut row_count = 0; for line in lines { - let len: usize = line.iter().map(|c| c.1.len()).sum(); + let len: usize = line.iter().map(|range| range.graphemes.len()).sum(); row_count += len / (max_columns + 1) + 1; } @@ -174,7 +195,10 @@ mod tests { #[test] fn test_count_lines() { - let lines = vec![vec![(Rc::new(Highlight::new()), vec!["a".to_owned(); 5])]]; + let lines = vec![vec![HighlightedRange { + highlight: Rc::new(Highlight::new()), + graphemes: vec!["a".to_owned(); 5], + }]]; let rows = ModelLayout::count_lines(&lines, 4); assert_eq!(2, rows); @@ -183,7 +207,10 @@ mod tests { #[test] fn test_resize() { let lines = vec![ - vec![(Rc::new(Highlight::new()), vec!["a".to_owned(); 5])]; + vec![HighlightedRange { + highlight: Rc::new(Highlight::new()), + graphemes: vec!["a".to_owned(); 5] + }]; ModelLayout::ROWS_STEP ]; let mut model = ModelLayout::new(5); @@ -202,7 +229,13 @@ mod tests { #[test] fn test_cols_filled() { - let lines = vec![vec![(Rc::new(Highlight::new()), vec!["a".to_owned(); 3])]; 1]; + let lines = vec![ + vec![HighlightedRange::new( + Rc::new(Highlight::new()), + vec!["a".to_owned(); 3] + )]; + 1 + ]; let mut model = ModelLayout::new(5); model.layout(lines); @@ -212,7 +245,13 @@ mod tests { let (cols, _) = model.size(); assert_eq!(4, cols); // size is 3 and 4 - is with cursor position - let lines = vec![vec![(Rc::new(Highlight::new()), vec!["a".to_owned(); 2])]; 1]; + let lines = vec![ + vec![HighlightedRange::new( + Rc::new(Highlight::new()), + vec!["a".to_owned(); 2] + )]; + 1 + ]; model.layout_append(lines); model.set_cursor(2); @@ -222,7 +261,13 @@ mod tests { #[test] fn test_insert_shift() { - let lines = vec![vec![(Rc::new(Highlight::new()), vec!["a".to_owned(); 3])]; 1]; + let lines = vec![ + vec![HighlightedRange::new( + Rc::new(Highlight::new()), + vec!["a".to_owned(); 3] + )]; + 1 + ]; let mut model = ModelLayout::new(5); model.layout(lines); model.set_cursor(1); @@ -236,7 +281,13 @@ mod tests { #[test] fn test_insert_no_shift() { - let lines = vec![vec![(Rc::new(Highlight::new()), vec!["a".to_owned(); 3])]; 1]; + let lines = vec![ + vec![HighlightedRange::new( + Rc::new(Highlight::new()), + vec!["a".to_owned(); 3] + )]; + 1 + ]; let mut model = ModelLayout::new(5); model.layout(lines); model.set_cursor(1); @@ -250,7 +301,13 @@ mod tests { #[test] fn test_double_width() { - let lines = vec![vec![(Rc::new(Highlight::new()), vec!["あ".to_owned(); 3])]; 1]; + let lines = vec![ + vec![HighlightedRange::new( + Rc::new(Highlight::new()), + vec!["あ".to_owned(); 3] + )]; + 1 + ]; let mut model = ModelLayout::new(7); model.layout(lines); model.set_cursor(1);