Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix clippy lint type_complexity #74

Merged
merged 2 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions src/cmd_line/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ 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;
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;

Expand Down Expand Up @@ -61,7 +61,7 @@ impl Level {
level
}

fn replace_line(&mut self, lines: Vec<Vec<(Rc<Highlight>, Vec<String>)>>, append: bool) {
fn replace_line(&mut self, lines: Vec<HighlightedLine>, append: bool) {
if append {
self.model_layout.layout_append(lines);
} else {
Expand Down Expand Up @@ -93,7 +93,7 @@ impl Level {
}

pub fn from_lines(
lines: Vec<Vec<(Rc<Highlight>, Vec<String>)>>,
lines: Vec<HighlightedLine>,
max_width: i32,
render_state: &shell::RenderState,
) -> Self {
Expand Down Expand Up @@ -134,12 +134,15 @@ fn prompt_lines(
prompt: &str,
indent: u64,
hl: &HighlightMap,
) -> (usize, Vec<(Rc<Highlight>, Vec<String>)>) {
let prompt: Vec<(Rc<Highlight>, Vec<String>)> = if !firstc.is_empty() {
) -> (usize, Vec<HighlightedRange>) {
let prompt: Vec<HighlightedRange> = 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()))
Expand All @@ -149,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)
}
Expand Down Expand Up @@ -539,25 +543,23 @@ impl<'a> CmdLineContext<'a> {
}

struct LineContent {
lines: Vec<Vec<(Rc<Highlight>, Vec<String>)>>,
lines: Vec<HighlightedLine>,
prompt_offset: usize,
}

trait ToAttributedModelContent {
fn to_attributed_content(&self, hl: &HighlightMap) -> Vec<Vec<(Rc<Highlight>, Vec<String>)>>;
fn to_attributed_content(&self, hl: &HighlightMap) -> Vec<HighlightedLine>;
}

impl ToAttributedModelContent for Vec<Vec<(u64, String)>> {
fn to_attributed_content(&self, hl: &HighlightMap) -> Vec<Vec<(Rc<Highlight>, Vec<String>)>> {
fn to_attributed_content(&self, hl: &HighlightMap) -> Vec<HighlightedLine> {
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()
})
Expand All @@ -566,14 +568,12 @@ impl ToAttributedModelContent for Vec<Vec<(u64, String)>> {
}

impl ToAttributedModelContent for Vec<(u64, String)> {
fn to_attributed_content(&self, hl: &HighlightMap) -> Vec<Vec<(Rc<Highlight>, Vec<String>)>> {
fn to_attributed_content(&self, hl: &HighlightMap) -> Vec<HighlightedLine> {
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()]
}
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![windows_subsystem = "windows"]
#![allow(clippy::new_without_default)]
#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::comparison_chain)]
#![allow(clippy::await_holding_refcell_ref)]
Expand Down
6 changes: 4 additions & 2 deletions src/nvim/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,14 @@ pub struct NvimSession {
runtime: Arc<Runtime>,
}

type IoFuture<'a> = BoxFuture<'a, Result<(), Box<LoopError>>>;

impl NvimSession {
pub fn new_child<'a>(
mut cmd: Command,
handler: NvimHandler,
timeout: Duration,
) -> Result<(NvimSession, BoxFuture<'a, Result<(), Box<LoopError>>>), NvimInitError> {
) -> Result<(NvimSession, IoFuture<'a>), NvimInitError> {
let runtime = Arc::new(Runtime::new().map_err(|e| NvimInitError::new(&cmd, e))?);
let mut child = runtime
.block_on(async move { cmd.spawn().map_err(|e| NvimInitError::new(&cmd, e)) })?;
Expand Down Expand Up @@ -365,7 +367,7 @@ pub fn start<'a>(
nvim_bin_path: Option<String>,
timeout: Option<Duration>,
args_for_neovim: Vec<String>,
) -> result::Result<(NvimSession, BoxFuture<'a, Result<(), Box<LoopError>>>), NvimInitError> {
) -> result::Result<(NvimSession, IoFuture<'a>), NvimInitError> {
let mut cmd = if let Some(path) = nvim_bin_path {
Command::new(path)
} else {
Expand Down
10 changes: 7 additions & 3 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ impl ActionWidgets {
}
}

type CommandCallback = Box<dyn FnMut(&mut State, nvim::NvimCommand) + Send + 'static>;
type DetachedCallback = Box<RefCell<dyn FnMut() + Send + 'static>>;
type NvimStartedCallback = Box<RefCell<dyn FnMut() + Send + 'static>>;

pub struct State {
pub grids: GridMap,

Expand Down Expand Up @@ -211,9 +215,9 @@ pub struct State {
pub options: RefCell<ShellOptions>,
transparency_settings: TransparencySettings,

detach_cb: Option<Box<RefCell<dyn FnMut() + Send + 'static>>>,
nvim_started_cb: Option<Box<RefCell<dyn FnMut() + Send + 'static>>>,
command_cb: Option<Box<dyn FnMut(&mut State, nvim::NvimCommand) + Send + 'static>>,
detach_cb: Option<DetachedCallback>,
nvim_started_cb: Option<NvimStartedCallback>,
command_cb: Option<CommandCallback>,

subscriptions: RefCell<Subscriptions>,

Expand Down
2 changes: 1 addition & 1 deletion src/ui_model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
95 changes: 76 additions & 19 deletions src/ui_model/model_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Highlight>,
pub graphemes: Vec<String>,
Lyude marked this conversation as resolved.
Show resolved Hide resolved
}

impl HighlightedRange {
pub fn new(highlight: Rc<Highlight>, graphemes: Vec<String>) -> Self {
Self {
highlight,
graphemes,
}
}
}

pub type HighlightedLine = Vec<HighlightedRange>;
Lyude marked this conversation as resolved.
Show resolved Hide resolved

pub struct ModelLayout {
pub model: UiModel,
rows_filled: usize,
cols_filled: usize,
lines: Vec<Vec<(Rc<Highlight>, Vec<String>)>>,
lines: Vec<HighlightedLine>,
}

impl ModelLayout {
Expand All @@ -25,7 +42,7 @@ impl ModelLayout {
}
}

pub fn layout_append(&mut self, mut lines: Vec<Vec<(Rc<Highlight>, Vec<String>)>>) {
pub fn layout_append(&mut self, mut lines: Vec<HighlightedLine>) {
let rows_filled = self.rows_filled;
let take_from = self.lines.len();

Expand All @@ -34,7 +51,7 @@ impl ModelLayout {
self.layout_replace(rows_filled, take_from);
}

pub fn layout(&mut self, lines: Vec<Vec<(Rc<Highlight>, Vec<String>)>>) {
pub fn layout(&mut self, lines: Vec<HighlightedLine>) {
self.lines = lines;
self.layout_replace(0, 0);
}
Expand Down Expand Up @@ -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();
}
}
}
Expand All @@ -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());

Expand Down Expand Up @@ -156,11 +177,11 @@ impl ModelLayout {
}
}

fn count_lines(lines: &[Vec<(Rc<Highlight>, Vec<String>)>], 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;
}

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down