@@ -4,14 +4,8 @@ use crate::{
44 text::Text,
55 widgets::{Block, Widget},
66};
7−use cassowary::{
8− strength::{MEDIUM, REQUIRED, WEAK},
9− WeightedRelation::*,
10− {Expression, Solver},
11−};
127use helix_core::unicode::width::UnicodeWidthStr;
138use helix_view::graphics::{Rect, Style};
14−use std::collections::HashMap;
159
1610/// A [`Cell`] contains the [`Text`] to be displayed in a [`Row`] of a [`Table`].
1711///
@@ -268,69 +262,32 @@ impl<'a> Table<'a> {
268262 }
269263
270264 fn get_columns_widths(&self, max_width: u16, has_selection: bool) -> Vec<u16> {
271− let mut solver = Solver::new();
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);
279− }
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);
265+ let mut constraints = Vec::with_capacity(self.widths.len() * 2 + 1);
282266 if has_selection {
283267 let highlight_symbol_width =
284268 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));
286270 }
287− for (i, constraint) in self.widths.iter().enumerate() {
288− ccs.push(variables[i] | GE(WEAK) | 0.);
289− ccs.push(match *constraint {
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)
293− }
294− Constraint::Ratio(n, d) => {
295− variables[i]
296− | EQ(WEAK)
297− | (f64::from(available_width) * f64::from(n) / f64::from(d))
298− }
299− Constraint::Min(v) => variables[i] | GE(WEAK) | f64::from(v),
300− Constraint::Max(v) => variables[i] | LE(WEAK) | f64::from(v),
301− })
271+ for constraint in self.widths {
272+ constraints.push(*constraint);
273+ constraints.push(Constraint::Length(self.column_spacing));
302274 }
303− solver
304− .add_constraint(
305− variables
306− .iter()
307− .fold(Expression::from_constant(0.), |acc, v| acc + *v)
308− | LE(REQUIRED)
309− | f64::from(available_width),
310− )
311− .unwrap();
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() {
317− 0
318− } else {
319− value.round() as u16
320− };
321− widths[index] = value;
275+ if !self.widths.is_empty() {
276+ constraints.pop();
322277 }
323− // Cassowary could still return columns widths greater than the max width when there are
324− // fixed length constraints that cannot be satisfied. Therefore, we clamp the widths from
325− // left to right.
326− let mut available_width = max_width;
327− for w in &mut widths {
328− *w = available_width.min(*w);
329− available_width = available_width
330− .saturating_sub(*w)
331− .saturating_sub(self.column_spacing);
278+ let mut chunks = crate::layout::Layout::default()
279+ .direction(crate::layout::Direction::Horizontal)
280+ .constraints(constraints)
281+ .split(Rect {
282+ x: 0,
283+ y: 0,
284+ width: max_width,
285+ height: 1,
286+ });
287+ if has_selection {
288+ chunks.remove(0);
332289 }
333− widths
290+ chunks.iter().step_by(2).map(|c| c.width).collect()
334291 }
335292
336293 fn get_row_bounds(