@@ -4,14 +4,8 @@ use crate::{
4 text::Text,
4 text::Text,
5 widgets::{Block, Widget},
5 widgets::{Block, Widget},
8− strength::{MEDIUM, REQUIRED, WEAK},
10− {Expression, Solver},
12use helix_core::unicode::width::UnicodeWidthStr;
7use helix_core::unicode::width::UnicodeWidthStr;
13use helix_view::graphics::{Rect, Style};
8use helix_view::graphics::{Rect, Style};
14−use std::collections::HashMap;
16/// A [`Cell`] contains the [`Text`] to be displayed in a [`Row`] of a [`Table`].
10/// A [`Cell`] contains the [`Text`] to be displayed in a [`Row`] of a [`Table`].
@@ -268,69 +262,32 @@ impl<'a> Table<'a> {
270 fn get_columns_widths(&self, max_width: u16, has_selection: bool) -> Vec<u16> {
264 fn get_columns_widths(&self, max_width: u16, has_selection: bool) -> Vec<u16> {
271− let mut solver = Solver::new();
265+ let mut constraints = Vec::with_capacity(self.widths.len() * 2 + 1);
272− let mut var_indices = HashMap::new();
273− let mut ccs = Vec::new();
274− let mut variables = Vec::new();
275− for i in 0..self.widths.len() {
276− let var = cassowary::Variable::new();
277− variables.push(var);
278− var_indices.insert(var, i);
280− let spacing_width = (variables.len() as u16).saturating_sub(1) * self.column_spacing;
281− let mut available_width = max_width.saturating_sub(spacing_width);
282 if has_selection {
266 if has_selection {
283 let highlight_symbol_width =
267 let highlight_symbol_width =
284 self.highlight_symbol.map(|s| s.width() as u16).unwrap_or(0);
268 self.highlight_symbol.map(|s| s.width() as u16).unwrap_or(0);
285− available_width = available_width.saturating_sub(highlight_symbol_width);
269+ constraints.push(Constraint::Length(highlight_symbol_width));
287− for (i, constraint) in self.widths.iter().enumerate() {
271+ for constraint in self.widths {
288− ccs.push(variables[i] | GE(WEAK) | 0.);
272+ constraints.push(*constraint);
289− ccs.push(match *constraint {
273+ constraints.push(Constraint::Length(self.column_spacing));
290− Constraint::Length(v) => variables[i] | EQ(MEDIUM) | f64::from(v),
291− Constraint::Percentage(v) => {
292− variables[i] | EQ(WEAK) | (f64::from(v * available_width) / 100.0)
294− Constraint::Ratio(n, d) => {
297− | (f64::from(available_width) * f64::from(n) / f64::from(d))
299− Constraint::Min(v) => variables[i] | GE(WEAK) | f64::from(v),
300− Constraint::Max(v) => variables[i] | LE(WEAK) | f64::from(v),
303− solver
275+ if !self.widths.is_empty() {
304− .add_constraint(
276+ constraints.pop();
307− .fold(Expression::from_constant(0.), |acc, v| acc + *v)
309− | f64::from(available_width),
312− solver.add_constraints(&ccs).unwrap();
313− let mut widths = vec![0; variables.len()];
314− for &(var, value) in solver.fetch_changes() {
315− let index = var_indices[&var];
316− let value = if value.is_sign_negative() {
319− value.round() as u16
321− widths[index] = value;
323− // Cassowary could still return columns widths greater than the max width when there are
278+ let mut chunks = crate::layout::Layout::default()
324− // fixed length constraints that cannot be satisfied. Therefore, we clamp the widths from
279+ .direction(crate::layout::Direction::Horizontal)
325− // left to right.
280+ .constraints(constraints)
326− let mut available_width = max_width;
284+ width: max_width,
327− for w in &mut widths {
285+ height: 1,
328− *w = available_width.min(*w);
286+ });
329− available_width = available_width
287+ if has_selection {
330− .saturating_sub(*w)
288+ chunks.remove(0);
331− .saturating_sub(self.column_spacing);
333− widths
290+ chunks.iter().step_by(2).map(|c| c.width).collect()
336 fn get_row_bounds(
293 fn get_row_bounds(