rumdl_lib/rules/md060_table_format/
mod.rs

1use crate::rule::{LintError, LintResult, LintWarning, Rule, Severity};
2use crate::utils::range_utils::calculate_line_range;
3use crate::utils::regex_cache::BLOCKQUOTE_PREFIX_RE;
4use crate::utils::table_utils::TableUtils;
5use unicode_width::UnicodeWidthStr;
6
7mod md060_config;
8use crate::md013_line_length::MD013Config;
9use md060_config::MD060Config;
10
11#[derive(Debug, Clone, Copy, PartialEq)]
12enum ColumnAlignment {
13    Left,
14    Center,
15    Right,
16}
17
18#[derive(Debug, Clone)]
19struct TableFormatResult {
20    lines: Vec<String>,
21    auto_compacted: bool,
22    aligned_width: Option<usize>,
23}
24
25/// Rule MD060: Table Column Alignment
26///
27/// See [docs/md060.md](../../docs/md060.md) for full documentation, configuration, and examples.
28///
29/// This rule enforces consistent column alignment in Markdown tables for improved readability
30/// in source form. When enabled, it ensures table columns are properly aligned with appropriate
31/// padding.
32///
33/// ## Purpose
34///
35/// - **Readability**: Aligned tables are significantly easier to read in source form
36/// - **Maintainability**: Properly formatted tables are easier to edit and review
37/// - **Consistency**: Ensures uniform table formatting throughout documents
38/// - **Developer Experience**: Makes working with tables in plain text more pleasant
39///
40/// ## Configuration Options
41///
42/// The rule supports the following configuration options:
43///
44/// ```toml
45/// [MD013]
46/// line-length = 100  # MD060 inherits this by default
47///
48/// [MD060]
49/// enabled = false      # Default: opt-in for conservative adoption
50/// style = "aligned"    # Can be "aligned", "compact", "tight", or "any"
51/// max-width = 0        # Default: inherit from MD013's line-length
52/// ```
53///
54/// ### Style Options
55///
56/// - **aligned**: Columns are padded with spaces for visual alignment (default)
57/// - **compact**: Minimal spacing with single spaces
58/// - **tight**: No spacing, pipes directly adjacent to content
59/// - **any**: Preserve existing formatting style
60///
61/// ### Max Width (auto-compact threshold)
62///
63/// Controls when tables automatically switch from aligned to compact formatting:
64///
65/// - **`max-width = 0`** (default): Smart inheritance from MD013
66/// - **`max-width = N`**: Explicit threshold, independent of MD013
67///
68/// When `max-width = 0`:
69/// - If MD013 is disabled β†’ unlimited (no auto-compact)
70/// - If MD013.tables = false β†’ unlimited (no auto-compact)
71/// - If MD013.line_length = 0 β†’ unlimited (no auto-compact)
72/// - Otherwise β†’ inherits MD013's line-length
73///
74/// This matches the behavior of Prettier's table formatting.
75///
76/// #### Examples
77///
78/// ```toml
79/// # Inherit from MD013 (recommended)
80/// [MD013]
81/// line-length = 100
82///
83/// [MD060]
84/// style = "aligned"
85/// max-width = 0  # Tables exceeding 100 chars will be compacted
86/// ```
87///
88/// ```toml
89/// # Explicit threshold
90/// [MD060]
91/// style = "aligned"
92/// max-width = 120  # Independent of MD013
93/// ```
94///
95/// ## Examples
96///
97/// ### Aligned Style (Good)
98///
99/// ```markdown
100/// | Name  | Age | City      |
101/// |-------|-----|-----------|
102/// | Alice | 30  | Seattle   |
103/// | Bob   | 25  | Portland  |
104/// ```
105///
106/// ### Unaligned (Bad)
107///
108/// ```markdown
109/// | Name | Age | City |
110/// |---|---|---|
111/// | Alice | 30 | Seattle |
112/// | Bob | 25 | Portland |
113/// ```
114///
115/// ## Unicode Support
116///
117/// This rule properly handles:
118/// - **CJK Characters**: Chinese, Japanese, Korean characters are correctly measured as double-width
119/// - **Basic Emoji**: Most emoji are handled correctly
120/// - **Inline Code**: Pipes in inline code blocks are properly masked
121///
122/// ## Known Limitations
123///
124/// **Complex Unicode Sequences**: Tables containing certain Unicode characters are automatically
125/// skipped to prevent alignment corruption. These include:
126/// - Zero-Width Joiner (ZWJ) emoji: πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦, πŸ‘©β€πŸ’»
127/// - Zero-Width Space (ZWS): Invisible word break opportunities
128/// - Zero-Width Non-Joiner (ZWNJ): Ligature prevention marks
129/// - Word Joiner (WJ): Non-breaking invisible characters
130///
131/// These characters have inconsistent or zero display widths across terminals and fonts,
132/// making accurate alignment impossible. The rule preserves these tables as-is rather than
133/// risk corrupting them.
134///
135/// This is an honest limitation of terminal display technology, similar to what other tools
136/// like markdownlint experience.
137///
138/// ## Fix Behavior
139///
140/// When applying automatic fixes, this rule:
141/// - Calculates proper display width for each column using Unicode width measurements
142/// - Pads cells with trailing spaces to align columns
143/// - Preserves cell content exactly (only spacing is modified)
144/// - Respects alignment indicators in delimiter rows (`:---`, `:---:`, `---:`)
145/// - Automatically switches to compact mode for tables exceeding max_width
146/// - Skips tables with ZWJ emoji to prevent corruption
147#[derive(Debug, Clone, Default)]
148pub struct MD060TableFormat {
149    config: MD060Config,
150    md013_config: MD013Config,
151    md013_disabled: bool,
152}
153
154impl MD060TableFormat {
155    pub fn new(enabled: bool, style: String) -> Self {
156        use crate::types::LineLength;
157        Self {
158            config: MD060Config {
159                enabled,
160                style,
161                max_width: LineLength::from_const(0),
162            },
163            md013_config: MD013Config::default(),
164            md013_disabled: false,
165        }
166    }
167
168    pub fn from_config_struct(config: MD060Config, md013_config: MD013Config, md013_disabled: bool) -> Self {
169        Self {
170            config,
171            md013_config,
172            md013_disabled,
173        }
174    }
175
176    /// Get the effective max width for table formatting.
177    ///
178    /// Priority order:
179    /// 1. Explicit `max_width > 0` always takes precedence
180    /// 2. When `max_width = 0` (inherit mode), check MD013 configuration:
181    ///    - If MD013 is globally disabled β†’ unlimited
182    ///    - If `MD013.tables = false` β†’ unlimited
183    ///    - If `MD013.line_length = 0` β†’ unlimited
184    ///    - Otherwise β†’ inherit MD013's line_length
185    fn effective_max_width(&self) -> usize {
186        // Explicit max_width always takes precedence
187        if !self.config.max_width.is_unlimited() {
188            return self.config.max_width.get();
189        }
190
191        // max_width = 0 means "inherit" - but inherit UNLIMITED if:
192        // 1. MD013 is globally disabled
193        // 2. MD013.tables = false (user doesn't care about table line length)
194        // 3. MD013.line_length = 0 (no line length limit at all)
195        if self.md013_disabled || !self.md013_config.tables || self.md013_config.line_length.is_unlimited() {
196            return usize::MAX; // Unlimited
197        }
198
199        // Otherwise inherit MD013's line-length
200        self.md013_config.line_length.get()
201    }
202
203    /// Check if text contains characters that break Unicode width calculations
204    ///
205    /// Tables with these characters are skipped to avoid alignment corruption:
206    /// - Zero-Width Joiner (ZWJ, U+200D): Complex emoji like πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦
207    /// - Zero-Width Space (ZWS, U+200B): Invisible word break opportunity
208    /// - Zero-Width Non-Joiner (ZWNJ, U+200C): Prevents ligature formation
209    /// - Word Joiner (WJ, U+2060): Prevents line breaks without taking space
210    ///
211    /// These characters have inconsistent display widths across terminals,
212    /// making accurate alignment impossible.
213    fn contains_problematic_chars(text: &str) -> bool {
214        text.contains('\u{200D}')  // ZWJ
215            || text.contains('\u{200B}')  // ZWS
216            || text.contains('\u{200C}')  // ZWNJ
217            || text.contains('\u{2060}') // Word Joiner
218    }
219
220    fn calculate_cell_display_width(cell_content: &str) -> usize {
221        let masked = TableUtils::mask_pipes_in_inline_code(cell_content);
222        masked.trim().width()
223    }
224
225    /// Parse a table row into cells using Standard flavor (default behavior).
226    /// Used for tests and backward compatibility.
227    #[cfg(test)]
228    fn parse_table_row(line: &str) -> Vec<String> {
229        TableUtils::split_table_row(line)
230    }
231
232    /// Parse a table row into cells, respecting flavor-specific behavior.
233    ///
234    /// For MkDocs flavor, pipes inside inline code are NOT cell delimiters.
235    /// For Standard/GFM flavor, all pipes (except escaped) are cell delimiters.
236    fn parse_table_row_with_flavor(line: &str, flavor: crate::config::MarkdownFlavor) -> Vec<String> {
237        TableUtils::split_table_row_with_flavor(line, flavor)
238    }
239
240    fn is_delimiter_row(row: &[String]) -> bool {
241        if row.is_empty() {
242            return false;
243        }
244        row.iter().all(|cell| {
245            let trimmed = cell.trim();
246            // A delimiter cell must contain at least one dash
247            // Empty cells are not delimiter cells
248            !trimmed.is_empty()
249                && trimmed.contains('-')
250                && trimmed.chars().all(|c| c == '-' || c == ':' || c.is_whitespace())
251        })
252    }
253
254    /// Extract blockquote prefix from a line (e.g., "> " or ">> ").
255    /// Returns (prefix, content_without_prefix).
256    fn extract_blockquote_prefix(line: &str) -> (&str, &str) {
257        if let Some(m) = BLOCKQUOTE_PREFIX_RE.find(line) {
258            (&line[..m.end()], &line[m.end()..])
259        } else {
260            ("", line)
261        }
262    }
263
264    fn parse_column_alignments(delimiter_row: &[String]) -> Vec<ColumnAlignment> {
265        delimiter_row
266            .iter()
267            .map(|cell| {
268                let trimmed = cell.trim();
269                let has_left_colon = trimmed.starts_with(':');
270                let has_right_colon = trimmed.ends_with(':');
271
272                match (has_left_colon, has_right_colon) {
273                    (true, true) => ColumnAlignment::Center,
274                    (false, true) => ColumnAlignment::Right,
275                    _ => ColumnAlignment::Left,
276                }
277            })
278            .collect()
279    }
280
281    fn calculate_column_widths(table_lines: &[&str], flavor: crate::config::MarkdownFlavor) -> Vec<usize> {
282        let mut column_widths = Vec::new();
283        let mut delimiter_cells: Option<Vec<String>> = None;
284
285        for line in table_lines {
286            let cells = Self::parse_table_row_with_flavor(line, flavor);
287
288            // Save delimiter row for later processing, but don't use it for width calculation
289            if Self::is_delimiter_row(&cells) {
290                delimiter_cells = Some(cells);
291                continue;
292            }
293
294            for (i, cell) in cells.iter().enumerate() {
295                let width = Self::calculate_cell_display_width(cell);
296                if i >= column_widths.len() {
297                    column_widths.push(width);
298                } else {
299                    column_widths[i] = column_widths[i].max(width);
300                }
301            }
302        }
303
304        // GFM requires delimiter rows to have at least 3 dashes per column.
305        // To ensure visual alignment, all columns must be at least width 3.
306        let mut final_widths: Vec<usize> = column_widths.iter().map(|&w| w.max(3)).collect();
307
308        // Adjust column widths to accommodate alignment indicators (colons) in delimiter row
309        // This ensures the delimiter row has the same length as content rows
310        if let Some(delimiter_cells) = delimiter_cells {
311            for (i, cell) in delimiter_cells.iter().enumerate() {
312                if i < final_widths.len() {
313                    let trimmed = cell.trim();
314                    let has_left_colon = trimmed.starts_with(':');
315                    let has_right_colon = trimmed.ends_with(':');
316                    let colon_count = (has_left_colon as usize) + (has_right_colon as usize);
317
318                    // Minimum width needed: 3 dashes + colons
319                    let min_width_for_delimiter = 3 + colon_count;
320                    final_widths[i] = final_widths[i].max(min_width_for_delimiter);
321                }
322            }
323        }
324
325        final_widths
326    }
327
328    fn format_table_row(
329        cells: &[String],
330        column_widths: &[usize],
331        column_alignments: &[ColumnAlignment],
332        is_delimiter: bool,
333        compact_delimiter: bool,
334    ) -> String {
335        let formatted_cells: Vec<String> = cells
336            .iter()
337            .enumerate()
338            .map(|(i, cell)| {
339                let target_width = column_widths.get(i).copied().unwrap_or(0);
340                if is_delimiter {
341                    let trimmed = cell.trim();
342                    let has_left_colon = trimmed.starts_with(':');
343                    let has_right_colon = trimmed.ends_with(':');
344
345                    // Delimiter rows use the same cell format as content rows: | content |
346                    // The "content" is dashes, possibly with colons for alignment
347                    // For compact_delimiter mode, we don't add spaces, so we need 2 extra dashes
348                    let extra_width = if compact_delimiter { 2 } else { 0 };
349                    let dash_count = if has_left_colon && has_right_colon {
350                        (target_width + extra_width).saturating_sub(2)
351                    } else if has_left_colon || has_right_colon {
352                        (target_width + extra_width).saturating_sub(1)
353                    } else {
354                        target_width + extra_width
355                    };
356
357                    let dashes = "-".repeat(dash_count.max(3)); // Minimum 3 dashes
358                    let delimiter_content = if has_left_colon && has_right_colon {
359                        format!(":{dashes}:")
360                    } else if has_left_colon {
361                        format!(":{dashes}")
362                    } else if has_right_colon {
363                        format!("{dashes}:")
364                    } else {
365                        dashes
366                    };
367
368                    // Add spaces around delimiter content unless compact_delimiter mode
369                    if compact_delimiter {
370                        delimiter_content
371                    } else {
372                        format!(" {delimiter_content} ")
373                    }
374                } else {
375                    let trimmed = cell.trim();
376                    let current_width = Self::calculate_cell_display_width(cell);
377                    let padding = target_width.saturating_sub(current_width);
378
379                    // Apply alignment based on column's alignment indicator
380                    let alignment = column_alignments.get(i).copied().unwrap_or(ColumnAlignment::Left);
381                    match alignment {
382                        ColumnAlignment::Left => {
383                            // Left: content on left, padding on right
384                            format!(" {trimmed}{} ", " ".repeat(padding))
385                        }
386                        ColumnAlignment::Center => {
387                            // Center: split padding on both sides
388                            let left_padding = padding / 2;
389                            let right_padding = padding - left_padding;
390                            format!(" {}{trimmed}{} ", " ".repeat(left_padding), " ".repeat(right_padding))
391                        }
392                        ColumnAlignment::Right => {
393                            // Right: padding on left, content on right
394                            format!(" {}{trimmed} ", " ".repeat(padding))
395                        }
396                    }
397                }
398            })
399            .collect();
400
401        format!("|{}|", formatted_cells.join("|"))
402    }
403
404    fn format_table_compact(cells: &[String]) -> String {
405        let formatted_cells: Vec<String> = cells.iter().map(|cell| format!(" {} ", cell.trim())).collect();
406        format!("|{}|", formatted_cells.join("|"))
407    }
408
409    fn format_table_tight(cells: &[String]) -> String {
410        let formatted_cells: Vec<String> = cells.iter().map(|cell| cell.trim().to_string()).collect();
411        format!("|{}|", formatted_cells.join("|"))
412    }
413
414    /// Checks if a table is already aligned with consistent column widths
415    /// and the delimiter row style matches the target style.
416    ///
417    /// A table is considered "already aligned" if:
418    /// 1. All rows have the same display length
419    /// 2. Each column has consistent cell width across all rows
420    /// 3. The delimiter row has valid minimum widths (at least 3 chars per cell)
421    /// 4. The delimiter row style matches the target style (compact_delimiter parameter)
422    ///
423    /// The `compact_delimiter` parameter indicates whether the target style is "aligned-no-space"
424    /// (true = no spaces around dashes, false = spaces around dashes).
425    fn is_table_already_aligned(
426        table_lines: &[&str],
427        flavor: crate::config::MarkdownFlavor,
428        compact_delimiter: bool,
429    ) -> bool {
430        if table_lines.len() < 2 {
431            return false;
432        }
433
434        // Check 1: All rows must have the same length
435        let first_len = table_lines[0].len();
436        if !table_lines.iter().all(|line| line.len() == first_len) {
437            return false;
438        }
439
440        // Parse all rows and check column count consistency
441        let parsed: Vec<Vec<String>> = table_lines
442            .iter()
443            .map(|line| Self::parse_table_row_with_flavor(line, flavor))
444            .collect();
445
446        if parsed.is_empty() {
447            return false;
448        }
449
450        let num_columns = parsed[0].len();
451        if !parsed.iter().all(|row| row.len() == num_columns) {
452            return false;
453        }
454
455        // Check delimiter row has valid minimum widths (3 chars: at least one dash + optional colons)
456        // Delimiter row is always at index 1
457        if let Some(delimiter_row) = parsed.get(1) {
458            if !Self::is_delimiter_row(delimiter_row) {
459                return false;
460            }
461            // Check each delimiter cell has at least one dash (minimum valid is "---" or ":--" etc)
462            for cell in delimiter_row {
463                let trimmed = cell.trim();
464                let dash_count = trimmed.chars().filter(|&c| c == '-').count();
465                if dash_count < 1 {
466                    return false;
467                }
468            }
469
470            // Check if delimiter row style matches the target style
471            // compact_delimiter=true means "aligned-no-space" (no spaces around dashes)
472            // compact_delimiter=false means "aligned" (spaces around dashes)
473            let delimiter_has_spaces = delimiter_row
474                .iter()
475                .all(|cell| cell.starts_with(' ') && cell.ends_with(' '));
476
477            // If target is compact (no spaces) but current has spaces, not aligned
478            // If target is spaced but current has no spaces, not aligned
479            if compact_delimiter && delimiter_has_spaces {
480                return false;
481            }
482            if !compact_delimiter && !delimiter_has_spaces {
483                return false;
484            }
485        }
486
487        // Check each column has consistent width across all content rows
488        // Use cell.width() to get display width INCLUDING padding, not trimmed content
489        // This correctly handles CJK characters (display width 2, byte length 3)
490        for col_idx in 0..num_columns {
491            let mut widths = Vec::new();
492            for (row_idx, row) in parsed.iter().enumerate() {
493                // Skip delimiter row for content width check
494                if row_idx == 1 {
495                    continue;
496                }
497                if let Some(cell) = row.get(col_idx) {
498                    widths.push(cell.width());
499                }
500            }
501            // All content cells in this column should have the same display width
502            if !widths.is_empty() && !widths.iter().all(|&w| w == widths[0]) {
503                return false;
504            }
505        }
506
507        true
508    }
509
510    fn detect_table_style(table_lines: &[&str], flavor: crate::config::MarkdownFlavor) -> Option<String> {
511        if table_lines.is_empty() {
512            return None;
513        }
514
515        // Check all rows (except delimiter) to determine consistent style
516        // A table is only "tight" or "compact" if ALL rows follow that pattern
517        let mut is_tight = true;
518        let mut is_compact = true;
519
520        for line in table_lines {
521            let cells = Self::parse_table_row_with_flavor(line, flavor);
522
523            if cells.is_empty() {
524                continue;
525            }
526
527            // Skip delimiter rows when detecting style
528            if Self::is_delimiter_row(&cells) {
529                continue;
530            }
531
532            // Check if this row has no padding
533            let row_has_no_padding = cells.iter().all(|cell| !cell.starts_with(' ') && !cell.ends_with(' '));
534
535            // Check if this row has exactly single-space padding
536            let row_has_single_space = cells.iter().all(|cell| {
537                let trimmed = cell.trim();
538                cell == &format!(" {trimmed} ")
539            });
540
541            // If any row doesn't match tight, the table isn't tight
542            if !row_has_no_padding {
543                is_tight = false;
544            }
545
546            // If any row doesn't match compact, the table isn't compact
547            if !row_has_single_space {
548                is_compact = false;
549            }
550
551            // Early exit: if neither tight nor compact, it must be aligned
552            if !is_tight && !is_compact {
553                return Some("aligned".to_string());
554            }
555        }
556
557        // Return the most restrictive style that matches
558        if is_tight {
559            Some("tight".to_string())
560        } else if is_compact {
561            Some("compact".to_string())
562        } else {
563            Some("aligned".to_string())
564        }
565    }
566
567    fn fix_table_block(
568        &self,
569        lines: &[&str],
570        table_block: &crate::utils::table_utils::TableBlock,
571        flavor: crate::config::MarkdownFlavor,
572    ) -> TableFormatResult {
573        let mut result = Vec::new();
574        let mut auto_compacted = false;
575        let mut aligned_width = None;
576
577        let table_lines: Vec<&str> = std::iter::once(lines[table_block.header_line])
578            .chain(std::iter::once(lines[table_block.delimiter_line]))
579            .chain(table_block.content_lines.iter().map(|&idx| lines[idx]))
580            .collect();
581
582        if table_lines.iter().any(|line| Self::contains_problematic_chars(line)) {
583            return TableFormatResult {
584                lines: table_lines.iter().map(|s| s.to_string()).collect(),
585                auto_compacted: false,
586                aligned_width: None,
587            };
588        }
589
590        // Extract blockquote prefix from the header line (first line of table)
591        // All lines in the same table should have the same blockquote level
592        let (blockquote_prefix, _) = Self::extract_blockquote_prefix(table_lines[0]);
593
594        // Strip blockquote prefix from all lines for processing
595        let stripped_lines: Vec<&str> = table_lines
596            .iter()
597            .map(|line| Self::extract_blockquote_prefix(line).1)
598            .collect();
599
600        let style = self.config.style.as_str();
601
602        match style {
603            "any" => {
604                let detected_style = Self::detect_table_style(&stripped_lines, flavor);
605                if detected_style.is_none() {
606                    return TableFormatResult {
607                        lines: table_lines.iter().map(|s| s.to_string()).collect(),
608                        auto_compacted: false,
609                        aligned_width: None,
610                    };
611                }
612
613                let target_style = detected_style.unwrap();
614
615                // Parse column alignments from delimiter row (always at index 1)
616                let delimiter_cells = Self::parse_table_row_with_flavor(stripped_lines[1], flavor);
617                let column_alignments = Self::parse_column_alignments(&delimiter_cells);
618
619                for line in &stripped_lines {
620                    let cells = Self::parse_table_row_with_flavor(line, flavor);
621                    match target_style.as_str() {
622                        "tight" => result.push(Self::format_table_tight(&cells)),
623                        "compact" => result.push(Self::format_table_compact(&cells)),
624                        _ => {
625                            let column_widths = Self::calculate_column_widths(&stripped_lines, flavor);
626                            let is_delimiter = Self::is_delimiter_row(&cells);
627                            result.push(Self::format_table_row(
628                                &cells,
629                                &column_widths,
630                                &column_alignments,
631                                is_delimiter,
632                                false,
633                            ));
634                        }
635                    }
636                }
637            }
638            "compact" => {
639                for line in &stripped_lines {
640                    let cells = Self::parse_table_row_with_flavor(line, flavor);
641                    result.push(Self::format_table_compact(&cells));
642                }
643            }
644            "tight" => {
645                for line in &stripped_lines {
646                    let cells = Self::parse_table_row_with_flavor(line, flavor);
647                    result.push(Self::format_table_tight(&cells));
648                }
649            }
650            "aligned" | "aligned-no-space" => {
651                let compact_delimiter = style == "aligned-no-space";
652
653                // If the table is already aligned with consistent column widths
654                // AND the delimiter style matches the target style, preserve as-is
655                if Self::is_table_already_aligned(&stripped_lines, flavor, compact_delimiter) {
656                    return TableFormatResult {
657                        lines: table_lines.iter().map(|s| s.to_string()).collect(),
658                        auto_compacted: false,
659                        aligned_width: None,
660                    };
661                }
662
663                let column_widths = Self::calculate_column_widths(&stripped_lines, flavor);
664
665                // Calculate aligned table width: 1 (leading pipe) + num_columns * 3 (| cell |) + sum(column_widths)
666                let num_columns = column_widths.len();
667                let calc_aligned_width = 1 + (num_columns * 3) + column_widths.iter().sum::<usize>();
668                aligned_width = Some(calc_aligned_width);
669
670                // Auto-compact: if aligned table exceeds max width, use compact formatting instead
671                if calc_aligned_width > self.effective_max_width() {
672                    auto_compacted = true;
673                    for line in &stripped_lines {
674                        let cells = Self::parse_table_row_with_flavor(line, flavor);
675                        result.push(Self::format_table_compact(&cells));
676                    }
677                } else {
678                    // Parse column alignments from delimiter row (always at index 1)
679                    let delimiter_cells = Self::parse_table_row_with_flavor(stripped_lines[1], flavor);
680                    let column_alignments = Self::parse_column_alignments(&delimiter_cells);
681
682                    for line in &stripped_lines {
683                        let cells = Self::parse_table_row_with_flavor(line, flavor);
684                        let is_delimiter = Self::is_delimiter_row(&cells);
685                        result.push(Self::format_table_row(
686                            &cells,
687                            &column_widths,
688                            &column_alignments,
689                            is_delimiter,
690                            compact_delimiter,
691                        ));
692                    }
693                }
694            }
695            _ => {
696                return TableFormatResult {
697                    lines: table_lines.iter().map(|s| s.to_string()).collect(),
698                    auto_compacted: false,
699                    aligned_width: None,
700                };
701            }
702        }
703
704        // Re-add blockquote prefix to all formatted lines
705        let prefixed_result: Vec<String> = result
706            .into_iter()
707            .map(|line| format!("{blockquote_prefix}{line}"))
708            .collect();
709
710        TableFormatResult {
711            lines: prefixed_result,
712            auto_compacted,
713            aligned_width,
714        }
715    }
716}
717
718impl Rule for MD060TableFormat {
719    fn name(&self) -> &'static str {
720        "MD060"
721    }
722
723    fn description(&self) -> &'static str {
724        "Table columns should be consistently aligned"
725    }
726
727    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
728        !self.config.enabled || !ctx.likely_has_tables()
729    }
730
731    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
732        if !self.config.enabled {
733            return Ok(Vec::new());
734        }
735
736        let content = ctx.content;
737        let line_index = &ctx.line_index;
738        let mut warnings = Vec::new();
739
740        let lines: Vec<&str> = content.lines().collect();
741        let table_blocks = &ctx.table_blocks;
742
743        for table_block in table_blocks {
744            let format_result = self.fix_table_block(&lines, table_block, ctx.flavor);
745
746            let table_line_indices: Vec<usize> = std::iter::once(table_block.header_line)
747                .chain(std::iter::once(table_block.delimiter_line))
748                .chain(table_block.content_lines.iter().copied())
749                .collect();
750
751            // Build the whole-table fix once for all warnings in this table
752            // This ensures that applying Quick Fix on any row fixes the entire table
753            let table_start_line = table_block.start_line + 1; // Convert to 1-indexed
754            let table_end_line = table_block.end_line + 1; // Convert to 1-indexed
755
756            // Build the complete fixed table content
757            let mut fixed_table_lines: Vec<String> = Vec::with_capacity(table_line_indices.len());
758            for (i, &line_idx) in table_line_indices.iter().enumerate() {
759                let fixed_line = &format_result.lines[i];
760                // Add newline for all lines except the last if the original didn't have one
761                if line_idx < lines.len() - 1 {
762                    fixed_table_lines.push(format!("{fixed_line}\n"));
763                } else {
764                    fixed_table_lines.push(fixed_line.clone());
765                }
766            }
767            let table_replacement = fixed_table_lines.concat();
768            let table_range = line_index.multi_line_range(table_start_line, table_end_line);
769
770            for (i, &line_idx) in table_line_indices.iter().enumerate() {
771                let original = lines[line_idx];
772                let fixed = &format_result.lines[i];
773
774                if original != fixed {
775                    let (start_line, start_col, end_line, end_col) = calculate_line_range(line_idx + 1, original);
776
777                    let message = if format_result.auto_compacted {
778                        if let Some(width) = format_result.aligned_width {
779                            format!(
780                                "Table too wide for aligned formatting ({} chars > max-width: {})",
781                                width,
782                                self.effective_max_width()
783                            )
784                        } else {
785                            "Table too wide for aligned formatting".to_string()
786                        }
787                    } else {
788                        "Table columns should be aligned".to_string()
789                    };
790
791                    // Each warning uses the same whole-table fix
792                    // This ensures Quick Fix on any row aligns the entire table
793                    warnings.push(LintWarning {
794                        rule_name: Some(self.name().to_string()),
795                        severity: Severity::Warning,
796                        message,
797                        line: start_line,
798                        column: start_col,
799                        end_line,
800                        end_column: end_col,
801                        fix: Some(crate::rule::Fix {
802                            range: table_range.clone(),
803                            replacement: table_replacement.clone(),
804                        }),
805                    });
806                }
807            }
808        }
809
810        Ok(warnings)
811    }
812
813    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
814        if !self.config.enabled {
815            return Ok(ctx.content.to_string());
816        }
817
818        let content = ctx.content;
819        let lines: Vec<&str> = content.lines().collect();
820        let table_blocks = &ctx.table_blocks;
821
822        let mut result_lines: Vec<String> = lines.iter().map(|&s| s.to_string()).collect();
823
824        for table_block in table_blocks {
825            let format_result = self.fix_table_block(&lines, table_block, ctx.flavor);
826
827            let table_line_indices: Vec<usize> = std::iter::once(table_block.header_line)
828                .chain(std::iter::once(table_block.delimiter_line))
829                .chain(table_block.content_lines.iter().copied())
830                .collect();
831
832            for (i, &line_idx) in table_line_indices.iter().enumerate() {
833                result_lines[line_idx] = format_result.lines[i].clone();
834            }
835        }
836
837        let mut fixed = result_lines.join("\n");
838        if content.ends_with('\n') && !fixed.ends_with('\n') {
839            fixed.push('\n');
840        }
841        Ok(fixed)
842    }
843
844    fn as_any(&self) -> &dyn std::any::Any {
845        self
846    }
847
848    fn default_config_section(&self) -> Option<(String, toml::Value)> {
849        let json_value = serde_json::to_value(&self.config).ok()?;
850        Some((
851            self.name().to_string(),
852            crate::rule_config_serde::json_to_toml_value(&json_value)?,
853        ))
854    }
855
856    fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
857    where
858        Self: Sized,
859    {
860        let rule_config = crate::rule_config_serde::load_rule_config::<MD060Config>(config);
861        let md013_config = crate::rule_config_serde::load_rule_config::<MD013Config>(config);
862
863        // Check if MD013 is globally disabled
864        let md013_disabled = config.global.disable.iter().any(|r| r == "MD013");
865
866        Box::new(Self::from_config_struct(rule_config, md013_config, md013_disabled))
867    }
868}
869
870#[cfg(test)]
871mod tests {
872    use super::*;
873    use crate::lint_context::LintContext;
874    use crate::types::LineLength;
875
876    /// Helper to create an MD013Config with a specific line length for testing
877    fn md013_with_line_length(line_length: usize) -> MD013Config {
878        MD013Config {
879            line_length: LineLength::from_const(line_length),
880            tables: true, // Default: tables are checked
881            ..Default::default()
882        }
883    }
884
885    #[test]
886    fn test_md060_disabled_by_default() {
887        let rule = MD060TableFormat::default();
888        let content = "| Name | Age |\n|---|---|\n| Alice | 30 |";
889        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
890
891        let warnings = rule.check(&ctx).unwrap();
892        assert_eq!(warnings.len(), 0);
893
894        let fixed = rule.fix(&ctx).unwrap();
895        assert_eq!(fixed, content);
896    }
897
898    #[test]
899    fn test_md060_align_simple_ascii_table() {
900        let rule = MD060TableFormat::new(true, "aligned".to_string());
901
902        let content = "| Name | Age |\n|---|---|\n| Alice | 30 |";
903        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
904
905        let fixed = rule.fix(&ctx).unwrap();
906        let expected = "| Name  | Age |\n| ----- | --- |\n| Alice | 30  |";
907        assert_eq!(fixed, expected);
908
909        // Verify all rows have equal length in aligned mode
910        let lines: Vec<&str> = fixed.lines().collect();
911        assert_eq!(lines[0].len(), lines[1].len());
912        assert_eq!(lines[1].len(), lines[2].len());
913    }
914
915    #[test]
916    fn test_md060_cjk_characters_aligned_correctly() {
917        let rule = MD060TableFormat::new(true, "aligned".to_string());
918
919        let content = "| Name | Age |\n|---|---|\n| δΈ­ζ–‡ | 30 |";
920        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
921
922        let fixed = rule.fix(&ctx).unwrap();
923
924        let lines: Vec<&str> = fixed.lines().collect();
925        let cells_line1 = MD060TableFormat::parse_table_row(lines[0]);
926        let cells_line3 = MD060TableFormat::parse_table_row(lines[2]);
927
928        let width1 = MD060TableFormat::calculate_cell_display_width(&cells_line1[0]);
929        let width3 = MD060TableFormat::calculate_cell_display_width(&cells_line3[0]);
930
931        assert_eq!(width1, width3);
932    }
933
934    #[test]
935    fn test_md060_basic_emoji() {
936        let rule = MD060TableFormat::new(true, "aligned".to_string());
937
938        let content = "| Status | Name |\n|---|---|\n| βœ… | Test |";
939        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
940
941        let fixed = rule.fix(&ctx).unwrap();
942        assert!(fixed.contains("Status"));
943    }
944
945    #[test]
946    fn test_md060_zwj_emoji_skipped() {
947        let rule = MD060TableFormat::new(true, "aligned".to_string());
948
949        let content = "| Emoji | Name |\n|---|---|\n| πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦ | Family |";
950        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
951
952        let fixed = rule.fix(&ctx).unwrap();
953        assert_eq!(fixed, content);
954    }
955
956    #[test]
957    fn test_md060_inline_code_with_escaped_pipes() {
958        // In GFM tables, bare pipes in inline code STILL act as cell delimiters.
959        // To include a literal pipe in table content (even in code), escape it with \|
960        let rule = MD060TableFormat::new(true, "aligned".to_string());
961
962        // CORRECT: `[0-9]\|[0-9]` - the \| is escaped, stays as content (2 columns)
963        let content = "| Pattern | Regex |\n|---|---|\n| Time | `[0-9]\\|[0-9]` |";
964        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
965
966        let fixed = rule.fix(&ctx).unwrap();
967        assert!(fixed.contains(r"`[0-9]\|[0-9]`"), "Escaped pipes should be preserved");
968    }
969
970    #[test]
971    fn test_md060_compact_style() {
972        let rule = MD060TableFormat::new(true, "compact".to_string());
973
974        let content = "| Name | Age |\n|---|---|\n| Alice | 30 |";
975        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
976
977        let fixed = rule.fix(&ctx).unwrap();
978        let expected = "| Name | Age |\n| --- | --- |\n| Alice | 30 |";
979        assert_eq!(fixed, expected);
980    }
981
982    #[test]
983    fn test_md060_tight_style() {
984        let rule = MD060TableFormat::new(true, "tight".to_string());
985
986        let content = "| Name | Age |\n|---|---|\n| Alice | 30 |";
987        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
988
989        let fixed = rule.fix(&ctx).unwrap();
990        let expected = "|Name|Age|\n|---|---|\n|Alice|30|";
991        assert_eq!(fixed, expected);
992    }
993
994    #[test]
995    fn test_md060_aligned_no_space_style() {
996        // Issue #277: aligned-no-space style has no spaces in delimiter row
997        let rule = MD060TableFormat::new(true, "aligned-no-space".to_string());
998
999        let content = "| Name | Age |\n|---|---|\n| Alice | 30 |";
1000        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1001
1002        let fixed = rule.fix(&ctx).unwrap();
1003
1004        // Content rows have spaces, delimiter row does not
1005        let lines: Vec<&str> = fixed.lines().collect();
1006        assert_eq!(lines[0], "| Name  | Age |", "Header should have spaces around content");
1007        assert_eq!(
1008            lines[1], "|-------|-----|",
1009            "Delimiter should have NO spaces around dashes"
1010        );
1011        assert_eq!(lines[2], "| Alice | 30  |", "Content should have spaces around content");
1012
1013        // All rows should have equal length
1014        assert_eq!(lines[0].len(), lines[1].len());
1015        assert_eq!(lines[1].len(), lines[2].len());
1016    }
1017
1018    #[test]
1019    fn test_md060_aligned_no_space_preserves_alignment_indicators() {
1020        // Alignment indicators (:) should be preserved
1021        let rule = MD060TableFormat::new(true, "aligned-no-space".to_string());
1022
1023        let content = "| Left | Center | Right |\n|:---|:---:|---:|\n| A | B | C |";
1024        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1025
1026        let fixed = rule.fix(&ctx).unwrap();
1027        let lines: Vec<&str> = fixed.lines().collect();
1028
1029        // Verify alignment indicators are preserved without spaces around them
1030        assert!(
1031            fixed.contains("|:"),
1032            "Should have left alignment indicator adjacent to pipe"
1033        );
1034        assert!(
1035            fixed.contains(":|"),
1036            "Should have right alignment indicator adjacent to pipe"
1037        );
1038        // Check for center alignment - the exact dash count depends on column width
1039        assert!(
1040            lines[1].contains(":---") && lines[1].contains("---:"),
1041            "Should have center alignment colons"
1042        );
1043    }
1044
1045    #[test]
1046    fn test_md060_aligned_no_space_three_column_table() {
1047        // Test the exact format from issue #277
1048        let rule = MD060TableFormat::new(true, "aligned-no-space".to_string());
1049
1050        let content = "| Header 1 | Header 2 | Header 3 |\n|---|---|---|\n| Row 1, Col 1 | Row 1, Col 2 | Row 1, Col 3 |\n| Row 2, Col 1 | Row 2, Col 2 | Row 2, Col 3 |";
1051        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1052
1053        let fixed = rule.fix(&ctx).unwrap();
1054        let lines: Vec<&str> = fixed.lines().collect();
1055
1056        // Verify delimiter row format: |--------------|--------------|--------------|
1057        assert!(lines[1].starts_with("|---"), "Delimiter should start with |---");
1058        assert!(lines[1].ends_with("---|"), "Delimiter should end with ---|");
1059        assert!(!lines[1].contains("| -"), "Delimiter should NOT have space after pipe");
1060        assert!(!lines[1].contains("- |"), "Delimiter should NOT have space before pipe");
1061    }
1062
1063    #[test]
1064    fn test_md060_aligned_no_space_auto_compacts_wide_tables() {
1065        // Auto-compact should work with aligned-no-space when table exceeds max-width
1066        let config = MD060Config {
1067            enabled: true,
1068            style: "aligned-no-space".to_string(),
1069            max_width: LineLength::from_const(50),
1070        };
1071        let rule = MD060TableFormat::from_config_struct(config, md013_with_line_length(80), false);
1072
1073        // Wide table that exceeds 50 chars when aligned
1074        let content = "| Very Long Column Header A | Very Long Column Header B | Very Long Column Header C |\n|---|---|---|\n| x | y | z |";
1075        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1076
1077        let fixed = rule.fix(&ctx).unwrap();
1078
1079        // Should auto-compact to compact style (not aligned-no-space)
1080        assert!(
1081            fixed.contains("| --- |"),
1082            "Should be compact format when exceeding max-width"
1083        );
1084    }
1085
1086    #[test]
1087    fn test_md060_aligned_no_space_cjk_characters() {
1088        // CJK characters should be handled correctly
1089        let rule = MD060TableFormat::new(true, "aligned-no-space".to_string());
1090
1091        let content = "| Name | City |\n|---|---|\n| δΈ­ζ–‡ | 東京 |";
1092        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1093
1094        let fixed = rule.fix(&ctx).unwrap();
1095        let lines: Vec<&str> = fixed.lines().collect();
1096
1097        // All rows should have equal DISPLAY width (not byte length)
1098        // CJK characters are double-width, so byte length differs from display width
1099        use unicode_width::UnicodeWidthStr;
1100        assert_eq!(
1101            lines[0].width(),
1102            lines[1].width(),
1103            "Header and delimiter should have same display width"
1104        );
1105        assert_eq!(
1106            lines[1].width(),
1107            lines[2].width(),
1108            "Delimiter and content should have same display width"
1109        );
1110
1111        // Delimiter should have no spaces
1112        assert!(!lines[1].contains("| -"), "Delimiter should NOT have space after pipe");
1113    }
1114
1115    #[test]
1116    fn test_md060_aligned_no_space_minimum_width() {
1117        // Minimum column width (3 dashes) should be respected
1118        let rule = MD060TableFormat::new(true, "aligned-no-space".to_string());
1119
1120        let content = "| A | B |\n|-|-|\n| 1 | 2 |";
1121        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1122
1123        let fixed = rule.fix(&ctx).unwrap();
1124        let lines: Vec<&str> = fixed.lines().collect();
1125
1126        // Should have at least 3 dashes per column (GFM requirement)
1127        assert!(lines[1].contains("---"), "Should have minimum 3 dashes");
1128        // All rows should have equal length
1129        assert_eq!(lines[0].len(), lines[1].len());
1130        assert_eq!(lines[1].len(), lines[2].len());
1131    }
1132
1133    #[test]
1134    fn test_md060_any_style_consistency() {
1135        let rule = MD060TableFormat::new(true, "any".to_string());
1136
1137        // Table is already compact, should stay compact
1138        let content = "| Name | Age |\n| --- | --- |\n| Alice | 30 |";
1139        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1140
1141        let fixed = rule.fix(&ctx).unwrap();
1142        assert_eq!(fixed, content);
1143
1144        // Table is aligned, should stay aligned
1145        let content_aligned = "| Name  | Age |\n| ----- | --- |\n| Alice | 30  |";
1146        let ctx_aligned = LintContext::new(content_aligned, crate::config::MarkdownFlavor::Standard, None);
1147
1148        let fixed_aligned = rule.fix(&ctx_aligned).unwrap();
1149        assert_eq!(fixed_aligned, content_aligned);
1150    }
1151
1152    #[test]
1153    fn test_md060_empty_cells() {
1154        let rule = MD060TableFormat::new(true, "aligned".to_string());
1155
1156        let content = "| A | B |\n|---|---|\n|  | X |";
1157        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1158
1159        let fixed = rule.fix(&ctx).unwrap();
1160        assert!(fixed.contains("|"));
1161    }
1162
1163    #[test]
1164    fn test_md060_mixed_content() {
1165        let rule = MD060TableFormat::new(true, "aligned".to_string());
1166
1167        let content = "| Name | Age | City |\n|---|---|---|\n| δΈ­ζ–‡ | 30 | NYC |";
1168        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1169
1170        let fixed = rule.fix(&ctx).unwrap();
1171        assert!(fixed.contains("δΈ­ζ–‡"));
1172        assert!(fixed.contains("NYC"));
1173    }
1174
1175    #[test]
1176    fn test_md060_preserve_alignment_indicators() {
1177        let rule = MD060TableFormat::new(true, "aligned".to_string());
1178
1179        let content = "| Left | Center | Right |\n|:---|:---:|---:|\n| A | B | C |";
1180        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1181
1182        let fixed = rule.fix(&ctx).unwrap();
1183
1184        assert!(fixed.contains(":---"), "Should contain left alignment");
1185        assert!(fixed.contains(":----:"), "Should contain center alignment");
1186        assert!(fixed.contains("----:"), "Should contain right alignment");
1187    }
1188
1189    #[test]
1190    fn test_md060_minimum_column_width() {
1191        let rule = MD060TableFormat::new(true, "aligned".to_string());
1192
1193        // Test with very short column content to ensure minimum width of 3
1194        // GFM requires at least 3 dashes in delimiter rows
1195        let content = "| ID | Name |\n|-|-|\n| 1 | A |";
1196        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1197
1198        let fixed = rule.fix(&ctx).unwrap();
1199
1200        let lines: Vec<&str> = fixed.lines().collect();
1201        assert_eq!(lines[0].len(), lines[1].len());
1202        assert_eq!(lines[1].len(), lines[2].len());
1203
1204        // Verify minimum width is enforced
1205        assert!(fixed.contains("ID "), "Short content should be padded");
1206        assert!(fixed.contains("---"), "Delimiter should have at least 3 dashes");
1207    }
1208
1209    #[test]
1210    fn test_md060_auto_compact_exceeds_default_threshold() {
1211        // Default max_width = 0, which inherits from default MD013 line_length = 80
1212        let config = MD060Config {
1213            enabled: true,
1214            style: "aligned".to_string(),
1215            max_width: LineLength::from_const(0),
1216        };
1217        let rule = MD060TableFormat::from_config_struct(config, md013_with_line_length(80), false);
1218
1219        // Table that would be 85 chars when aligned (exceeds 80)
1220        // Formula: 1 + (3 * 3) + (20 + 20 + 30) = 1 + 9 + 70 = 80 chars
1221        // But with actual content padding it will exceed
1222        let content = "| Very Long Column Header | Another Long Header | Third Very Long Header Column |\n|---|---|---|\n| Short | Data | Here |";
1223        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1224
1225        let fixed = rule.fix(&ctx).unwrap();
1226
1227        // Should use compact formatting (single spaces)
1228        assert!(fixed.contains("| Very Long Column Header | Another Long Header | Third Very Long Header Column |"));
1229        assert!(fixed.contains("| --- | --- | --- |"));
1230        assert!(fixed.contains("| Short | Data | Here |"));
1231
1232        // Verify it's compact (no extra padding)
1233        let lines: Vec<&str> = fixed.lines().collect();
1234        // In compact mode, lines can have different lengths
1235        assert!(lines[0].len() != lines[1].len() || lines[1].len() != lines[2].len());
1236    }
1237
1238    #[test]
1239    fn test_md060_auto_compact_exceeds_explicit_threshold() {
1240        // Explicit max_width = 50
1241        let config = MD060Config {
1242            enabled: true,
1243            style: "aligned".to_string(),
1244            max_width: LineLength::from_const(50),
1245        };
1246        let rule = MD060TableFormat::from_config_struct(config, md013_with_line_length(80), false); // MD013 setting doesn't matter
1247
1248        // Table that would exceed 50 chars when aligned
1249        // Column widths: 25 + 25 + 25 = 75 chars
1250        // Formula: 1 + (3 * 3) + 75 = 85 chars (exceeds 50)
1251        let content = "| Very Long Column Header A | Very Long Column Header B | Very Long Column Header C |\n|---|---|---|\n| Data | Data | Data |";
1252        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1253
1254        let fixed = rule.fix(&ctx).unwrap();
1255
1256        // Should use compact formatting (single spaces, no extra padding)
1257        assert!(
1258            fixed.contains("| Very Long Column Header A | Very Long Column Header B | Very Long Column Header C |")
1259        );
1260        assert!(fixed.contains("| --- | --- | --- |"));
1261        assert!(fixed.contains("| Data | Data | Data |"));
1262
1263        // Verify it's compact (lines have different lengths)
1264        let lines: Vec<&str> = fixed.lines().collect();
1265        assert!(lines[0].len() != lines[2].len());
1266    }
1267
1268    #[test]
1269    fn test_md060_stays_aligned_under_threshold() {
1270        // max_width = 100, table will be under this
1271        let config = MD060Config {
1272            enabled: true,
1273            style: "aligned".to_string(),
1274            max_width: LineLength::from_const(100),
1275        };
1276        let rule = MD060TableFormat::from_config_struct(config, md013_with_line_length(80), false);
1277
1278        // Small table that fits well under 100 chars
1279        let content = "| Name | Age |\n|---|---|\n| Alice | 30 |";
1280        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1281
1282        let fixed = rule.fix(&ctx).unwrap();
1283
1284        // Should use aligned formatting (all lines same length)
1285        let expected = "| Name  | Age |\n| ----- | --- |\n| Alice | 30  |";
1286        assert_eq!(fixed, expected);
1287
1288        let lines: Vec<&str> = fixed.lines().collect();
1289        assert_eq!(lines[0].len(), lines[1].len());
1290        assert_eq!(lines[1].len(), lines[2].len());
1291    }
1292
1293    #[test]
1294    fn test_md060_width_calculation_formula() {
1295        // Verify the width calculation formula: 1 + (num_columns * 3) + sum(column_widths)
1296        let config = MD060Config {
1297            enabled: true,
1298            style: "aligned".to_string(),
1299            max_width: LineLength::from_const(0),
1300        };
1301        let rule = MD060TableFormat::from_config_struct(config, md013_with_line_length(30), false);
1302
1303        // Create a table where we know exact column widths: 5 + 5 + 5 = 15
1304        // Expected aligned width: 1 + (3 * 3) + 15 = 1 + 9 + 15 = 25 chars
1305        // This is under 30, so should stay aligned
1306        let content = "| AAAAA | BBBBB | CCCCC |\n|---|---|---|\n| AAAAA | BBBBB | CCCCC |";
1307        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1308
1309        let fixed = rule.fix(&ctx).unwrap();
1310
1311        // Should be aligned
1312        let lines: Vec<&str> = fixed.lines().collect();
1313        assert_eq!(lines[0].len(), lines[1].len());
1314        assert_eq!(lines[1].len(), lines[2].len());
1315        assert_eq!(lines[0].len(), 25); // Verify formula
1316
1317        // Now test with threshold = 24 (just under aligned width)
1318        let config_tight = MD060Config {
1319            enabled: true,
1320            style: "aligned".to_string(),
1321            max_width: LineLength::from_const(24),
1322        };
1323        let rule_tight = MD060TableFormat::from_config_struct(config_tight, md013_with_line_length(80), false);
1324
1325        let fixed_compact = rule_tight.fix(&ctx).unwrap();
1326
1327        // Should be compact now (25 > 24)
1328        assert!(fixed_compact.contains("| AAAAA | BBBBB | CCCCC |"));
1329        assert!(fixed_compact.contains("| --- | --- | --- |"));
1330    }
1331
1332    #[test]
1333    fn test_md060_very_wide_table_auto_compacts() {
1334        let config = MD060Config {
1335            enabled: true,
1336            style: "aligned".to_string(),
1337            max_width: LineLength::from_const(0),
1338        };
1339        let rule = MD060TableFormat::from_config_struct(config, md013_with_line_length(80), false);
1340
1341        // Very wide table with many columns
1342        // 8 columns with widths of 12 chars each = 96 chars
1343        // Formula: 1 + (8 * 3) + 96 = 121 chars (exceeds 80)
1344        let content = "| Column One A | Column Two B | Column Three | Column Four D | Column Five E | Column Six FG | Column Seven | Column Eight |\n|---|---|---|---|---|---|---|---|\n| A | B | C | D | E | F | G | H |";
1345        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1346
1347        let fixed = rule.fix(&ctx).unwrap();
1348
1349        // Should be compact (table would be way over 80 chars aligned)
1350        assert!(fixed.contains("| Column One A | Column Two B | Column Three | Column Four D | Column Five E | Column Six FG | Column Seven | Column Eight |"));
1351        assert!(fixed.contains("| --- | --- | --- | --- | --- | --- | --- | --- |"));
1352    }
1353
1354    #[test]
1355    fn test_md060_inherit_from_md013_line_length() {
1356        // max_width = 0 should inherit from MD013's line_length
1357        let config = MD060Config {
1358            enabled: true,
1359            style: "aligned".to_string(),
1360            max_width: LineLength::from_const(0), // Inherit
1361        };
1362
1363        // Test with different MD013 line_length values
1364        let rule_80 = MD060TableFormat::from_config_struct(config.clone(), md013_with_line_length(80), false);
1365        let rule_120 = MD060TableFormat::from_config_struct(config.clone(), md013_with_line_length(120), false);
1366
1367        // Medium-sized table
1368        let content = "| Column Header A | Column Header B | Column Header C |\n|---|---|---|\n| Some Data | More Data | Even More |";
1369        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1370
1371        // With 80 char limit, likely compacts
1372        let _fixed_80 = rule_80.fix(&ctx).unwrap();
1373
1374        // With 120 char limit, likely stays aligned
1375        let fixed_120 = rule_120.fix(&ctx).unwrap();
1376
1377        // Verify 120 is aligned (all lines same length)
1378        let lines_120: Vec<&str> = fixed_120.lines().collect();
1379        assert_eq!(lines_120[0].len(), lines_120[1].len());
1380        assert_eq!(lines_120[1].len(), lines_120[2].len());
1381    }
1382
1383    #[test]
1384    fn test_md060_edge_case_exactly_at_threshold() {
1385        // Create table that's exactly at the threshold
1386        // Formula: 1 + (num_columns * 3) + sum(column_widths) = max_width
1387        // For 2 columns with widths 5 and 5: 1 + 6 + 10 = 17
1388        let config = MD060Config {
1389            enabled: true,
1390            style: "aligned".to_string(),
1391            max_width: LineLength::from_const(17),
1392        };
1393        let rule = MD060TableFormat::from_config_struct(config, md013_with_line_length(80), false);
1394
1395        let content = "| AAAAA | BBBBB |\n|---|---|\n| AAAAA | BBBBB |";
1396        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1397
1398        let fixed = rule.fix(&ctx).unwrap();
1399
1400        // At threshold (17 <= 17), should stay aligned
1401        let lines: Vec<&str> = fixed.lines().collect();
1402        assert_eq!(lines[0].len(), 17);
1403        assert_eq!(lines[0].len(), lines[1].len());
1404        assert_eq!(lines[1].len(), lines[2].len());
1405
1406        // Now test with threshold = 16 (just under)
1407        let config_under = MD060Config {
1408            enabled: true,
1409            style: "aligned".to_string(),
1410            max_width: LineLength::from_const(16),
1411        };
1412        let rule_under = MD060TableFormat::from_config_struct(config_under, md013_with_line_length(80), false);
1413
1414        let fixed_compact = rule_under.fix(&ctx).unwrap();
1415
1416        // Should compact (17 > 16)
1417        assert!(fixed_compact.contains("| AAAAA | BBBBB |"));
1418        assert!(fixed_compact.contains("| --- | --- |"));
1419    }
1420
1421    #[test]
1422    fn test_md060_auto_compact_warning_message() {
1423        // Verify that auto-compact generates an informative warning
1424        let config = MD060Config {
1425            enabled: true,
1426            style: "aligned".to_string(),
1427            max_width: LineLength::from_const(50),
1428        };
1429        let rule = MD060TableFormat::from_config_struct(config, md013_with_line_length(80), false);
1430
1431        // Table that will be auto-compacted (exceeds 50 chars when aligned)
1432        let content = "| Very Long Column Header A | Very Long Column Header B | Very Long Column Header C |\n|---|---|---|\n| Data | Data | Data |";
1433        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1434
1435        let warnings = rule.check(&ctx).unwrap();
1436
1437        // Should generate warnings with auto-compact message
1438        assert!(!warnings.is_empty(), "Should generate warnings");
1439
1440        let auto_compact_warnings: Vec<_> = warnings
1441            .iter()
1442            .filter(|w| w.message.contains("too wide for aligned formatting"))
1443            .collect();
1444
1445        assert!(!auto_compact_warnings.is_empty(), "Should have auto-compact warning");
1446
1447        // Verify the warning message includes the width and threshold
1448        let first_warning = auto_compact_warnings[0];
1449        assert!(first_warning.message.contains("85 chars > max-width: 50"));
1450        assert!(first_warning.message.contains("Table too wide for aligned formatting"));
1451    }
1452
1453    #[test]
1454    fn test_md060_issue_129_detect_style_from_all_rows() {
1455        // Issue #129: detect_table_style should check all rows, not just the first row
1456        // If header row has single-space padding but content rows have extra padding,
1457        // the table should be detected as "aligned" and preserved
1458        let rule = MD060TableFormat::new(true, "any".to_string());
1459
1460        // Table where header looks compact but content is aligned
1461        let content = "| a long heading | another long heading |\n\
1462                       | -------------- | -------------------- |\n\
1463                       | a              | 1                    |\n\
1464                       | b b            | 2                    |\n\
1465                       | c c c          | 3                    |";
1466        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1467
1468        let fixed = rule.fix(&ctx).unwrap();
1469
1470        // Should preserve the aligned formatting of content rows
1471        assert!(
1472            fixed.contains("| a              | 1                    |"),
1473            "Should preserve aligned padding in first content row"
1474        );
1475        assert!(
1476            fixed.contains("| b b            | 2                    |"),
1477            "Should preserve aligned padding in second content row"
1478        );
1479        assert!(
1480            fixed.contains("| c c c          | 3                    |"),
1481            "Should preserve aligned padding in third content row"
1482        );
1483
1484        // Entire table should remain unchanged because it's already properly aligned
1485        assert_eq!(fixed, content, "Table should be detected as aligned and preserved");
1486    }
1487
1488    #[test]
1489    fn test_md060_regular_alignment_warning_message() {
1490        // Verify that regular alignment (not auto-compact) generates normal warning
1491        let config = MD060Config {
1492            enabled: true,
1493            style: "aligned".to_string(),
1494            max_width: LineLength::from_const(100), // Large enough to not trigger auto-compact
1495        };
1496        let rule = MD060TableFormat::from_config_struct(config, md013_with_line_length(80), false);
1497
1498        // Small misaligned table
1499        let content = "| Name | Age |\n|---|---|\n| Alice | 30 |";
1500        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1501
1502        let warnings = rule.check(&ctx).unwrap();
1503
1504        // Should generate warnings
1505        assert!(!warnings.is_empty(), "Should generate warnings");
1506
1507        // Verify it's the standard alignment message, not auto-compact
1508        assert!(warnings[0].message.contains("Table columns should be aligned"));
1509        assert!(!warnings[0].message.contains("too wide"));
1510        assert!(!warnings[0].message.contains("max-width"));
1511    }
1512
1513    // === Issue #219: Unlimited table width tests ===
1514
1515    #[test]
1516    fn test_md060_unlimited_when_md013_disabled() {
1517        // When MD013 is globally disabled, max_width should be unlimited
1518        let config = MD060Config {
1519            enabled: true,
1520            style: "aligned".to_string(),
1521            max_width: LineLength::from_const(0), // Inherit
1522        };
1523        let md013_config = MD013Config::default();
1524        let rule = MD060TableFormat::from_config_struct(config, md013_config, true /* disabled */);
1525
1526        // Very wide table that would normally exceed 80 chars
1527        let content = "| Very Long Column Header A | Very Long Column Header B | Very Long Column Header C |\n|---|---|---|\n| data | data | data |";
1528        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1529        let fixed = rule.fix(&ctx).unwrap();
1530
1531        // Should be aligned (not compacted) since MD013 is disabled
1532        let lines: Vec<&str> = fixed.lines().collect();
1533        // In aligned mode, all lines have the same length
1534        assert_eq!(
1535            lines[0].len(),
1536            lines[1].len(),
1537            "Table should be aligned when MD013 is disabled"
1538        );
1539    }
1540
1541    #[test]
1542    fn test_md060_unlimited_when_md013_tables_false() {
1543        // When MD013.tables = false, max_width should be unlimited
1544        let config = MD060Config {
1545            enabled: true,
1546            style: "aligned".to_string(),
1547            max_width: LineLength::from_const(0),
1548        };
1549        let md013_config = MD013Config {
1550            tables: false, // User doesn't care about table line length
1551            line_length: LineLength::from_const(80),
1552            ..Default::default()
1553        };
1554        let rule = MD060TableFormat::from_config_struct(config, md013_config, false);
1555
1556        // Wide table that would exceed 80 chars
1557        let content = "| Very Long Header A | Very Long Header B | Very Long Header C |\n|---|---|---|\n| x | y | z |";
1558        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1559        let fixed = rule.fix(&ctx).unwrap();
1560
1561        // Should be aligned (no auto-compact since tables=false)
1562        let lines: Vec<&str> = fixed.lines().collect();
1563        assert_eq!(
1564            lines[0].len(),
1565            lines[1].len(),
1566            "Table should be aligned when MD013.tables=false"
1567        );
1568    }
1569
1570    #[test]
1571    fn test_md060_unlimited_when_md013_line_length_zero() {
1572        // When MD013.line_length = 0, max_width should be unlimited
1573        let config = MD060Config {
1574            enabled: true,
1575            style: "aligned".to_string(),
1576            max_width: LineLength::from_const(0),
1577        };
1578        let md013_config = MD013Config {
1579            tables: true,
1580            line_length: LineLength::from_const(0), // No limit
1581            ..Default::default()
1582        };
1583        let rule = MD060TableFormat::from_config_struct(config, md013_config, false);
1584
1585        // Wide table
1586        let content = "| Very Long Header | Another Long Header | Third Long Header |\n|---|---|---|\n| x | y | z |";
1587        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1588        let fixed = rule.fix(&ctx).unwrap();
1589
1590        // Should be aligned
1591        let lines: Vec<&str> = fixed.lines().collect();
1592        assert_eq!(
1593            lines[0].len(),
1594            lines[1].len(),
1595            "Table should be aligned when MD013.line_length=0"
1596        );
1597    }
1598
1599    #[test]
1600    fn test_md060_explicit_max_width_overrides_md013_settings() {
1601        // Explicit max_width should always take precedence
1602        let config = MD060Config {
1603            enabled: true,
1604            style: "aligned".to_string(),
1605            max_width: LineLength::from_const(50), // Explicit limit
1606        };
1607        let md013_config = MD013Config {
1608            tables: false,                          // This would make it unlimited...
1609            line_length: LineLength::from_const(0), // ...and this too
1610            ..Default::default()
1611        };
1612        let rule = MD060TableFormat::from_config_struct(config, md013_config, false);
1613
1614        // Wide table that exceeds explicit 50-char limit
1615        let content = "| Very Long Column Header A | Very Long Column Header B | Very Long Column Header C |\n|---|---|---|\n| x | y | z |";
1616        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1617        let fixed = rule.fix(&ctx).unwrap();
1618
1619        // Should be compact (explicit max_width = 50 overrides MD013 settings)
1620        assert!(
1621            fixed.contains("| --- |"),
1622            "Should be compact format due to explicit max_width"
1623        );
1624    }
1625
1626    #[test]
1627    fn test_md060_inherits_md013_line_length_when_tables_enabled() {
1628        // When MD013.tables = true and MD013.line_length is set, inherit that limit
1629        let config = MD060Config {
1630            enabled: true,
1631            style: "aligned".to_string(),
1632            max_width: LineLength::from_const(0), // Inherit
1633        };
1634        let md013_config = MD013Config {
1635            tables: true,
1636            line_length: LineLength::from_const(50), // 50 char limit
1637            ..Default::default()
1638        };
1639        let rule = MD060TableFormat::from_config_struct(config, md013_config, false);
1640
1641        // Wide table that exceeds 50 chars
1642        let content = "| Very Long Column Header A | Very Long Column Header B | Very Long Column Header C |\n|---|---|---|\n| x | y | z |";
1643        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1644        let fixed = rule.fix(&ctx).unwrap();
1645
1646        // Should be compact (inherited 50-char limit from MD013)
1647        assert!(
1648            fixed.contains("| --- |"),
1649            "Should be compact format when inheriting MD013 limit"
1650        );
1651    }
1652
1653    // === Issue #311: aligned-no-space style tests ===
1654
1655    #[test]
1656    fn test_aligned_no_space_reformats_spaced_delimiter() {
1657        // Table with "aligned" style (spaces around dashes) should be reformatted
1658        // when target style is "aligned-no-space"
1659        let config = MD060Config {
1660            enabled: true,
1661            style: "aligned-no-space".to_string(),
1662            max_width: LineLength::from_const(0),
1663        };
1664        let rule = MD060TableFormat::from_config_struct(config, MD013Config::default(), false);
1665
1666        // Input: aligned table with spaces around dashes
1667        let content = "| Header 1 | Header 2 |\n| -------- | -------- |\n| Cell 1   | Cell 2   |";
1668        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1669        let fixed = rule.fix(&ctx).unwrap();
1670
1671        // Should have no spaces around dashes in delimiter row
1672        // The dashes may be longer to match column width, but should have no spaces
1673        assert!(
1674            !fixed.contains("| ----"),
1675            "Delimiter should NOT have spaces after pipe. Got:\n{fixed}"
1676        );
1677        assert!(
1678            !fixed.contains("---- |"),
1679            "Delimiter should NOT have spaces before pipe. Got:\n{fixed}"
1680        );
1681        // Verify it has the compact delimiter format (dashes touching pipes)
1682        assert!(
1683            fixed.contains("|----"),
1684            "Delimiter should have dashes touching the leading pipe. Got:\n{fixed}"
1685        );
1686    }
1687
1688    #[test]
1689    fn test_aligned_reformats_compact_delimiter() {
1690        // Table with "aligned-no-space" style (no spaces around dashes) should be reformatted
1691        // when target style is "aligned"
1692        let config = MD060Config {
1693            enabled: true,
1694            style: "aligned".to_string(),
1695            max_width: LineLength::from_const(0),
1696        };
1697        let rule = MD060TableFormat::from_config_struct(config, MD013Config::default(), false);
1698
1699        // Input: aligned-no-space table (no spaces around dashes)
1700        let content = "| Header 1 | Header 2 |\n|----------|----------|\n| Cell 1   | Cell 2   |";
1701        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1702        let fixed = rule.fix(&ctx).unwrap();
1703
1704        // Should have spaces around dashes in delimiter row
1705        assert!(
1706            fixed.contains("| -------- | -------- |") || fixed.contains("| ---------- | ---------- |"),
1707            "Delimiter should have spaces around dashes. Got:\n{fixed}"
1708        );
1709    }
1710
1711    #[test]
1712    fn test_aligned_no_space_preserves_matching_table() {
1713        // Table already in "aligned-no-space" style should be preserved
1714        let config = MD060Config {
1715            enabled: true,
1716            style: "aligned-no-space".to_string(),
1717            max_width: LineLength::from_const(0),
1718        };
1719        let rule = MD060TableFormat::from_config_struct(config, MD013Config::default(), false);
1720
1721        // Input: already in aligned-no-space style
1722        let content = "| Header 1 | Header 2 |\n|----------|----------|\n| Cell 1   | Cell 2   |";
1723        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1724        let fixed = rule.fix(&ctx).unwrap();
1725
1726        // Should be preserved as-is
1727        assert_eq!(
1728            fixed, content,
1729            "Table already in aligned-no-space style should be preserved"
1730        );
1731    }
1732
1733    #[test]
1734    fn test_aligned_preserves_matching_table() {
1735        // Table already in "aligned" style should be preserved
1736        let config = MD060Config {
1737            enabled: true,
1738            style: "aligned".to_string(),
1739            max_width: LineLength::from_const(0),
1740        };
1741        let rule = MD060TableFormat::from_config_struct(config, MD013Config::default(), false);
1742
1743        // Input: already in aligned style
1744        let content = "| Header 1 | Header 2 |\n| -------- | -------- |\n| Cell 1   | Cell 2   |";
1745        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1746        let fixed = rule.fix(&ctx).unwrap();
1747
1748        // Should be preserved as-is
1749        assert_eq!(fixed, content, "Table already in aligned style should be preserved");
1750    }
1751
1752    #[test]
1753    fn test_cjk_table_display_width_consistency() {
1754        // Test that is_table_already_aligned correctly uses display width, not byte length
1755        // CJK characters have display width of 2, but byte length of 3 in UTF-8
1756        //
1757        // This table is NOT aligned because line lengths differ
1758        // (CJK chars take 3 bytes in UTF-8 but only 2 columns in display)
1759        let table_lines = vec!["| 名前 | Age |", "|------|-----|", "| η”°δΈ­ | 25  |"];
1760
1761        // First check is raw line length equality (byte-based), which fails
1762        let is_aligned =
1763            MD060TableFormat::is_table_already_aligned(&table_lines, crate::config::MarkdownFlavor::Standard, false);
1764        assert!(
1765            !is_aligned,
1766            "Table with uneven raw line lengths should NOT be considered aligned"
1767        );
1768    }
1769
1770    #[test]
1771    fn test_cjk_width_calculation_in_aligned_check() {
1772        // calculate_cell_display_width trims content before calculating width
1773        // Verify CJK width is correctly calculated (2 per character)
1774        let cjk_width = MD060TableFormat::calculate_cell_display_width("名前");
1775        assert_eq!(cjk_width, 4, "Two CJK characters should have display width 4");
1776
1777        let ascii_width = MD060TableFormat::calculate_cell_display_width("Age");
1778        assert_eq!(ascii_width, 3, "Three ASCII characters should have display width 3");
1779
1780        // Test that spacing is trimmed before width calculation
1781        let padded_cjk = MD060TableFormat::calculate_cell_display_width(" 名前 ");
1782        assert_eq!(padded_cjk, 4, "Padded CJK should have same width after trim");
1783
1784        // Test mixed content
1785        let mixed = MD060TableFormat::calculate_cell_display_width(" ζ—₯本θͺžABC ");
1786        // 3 CJK chars (width 6) + 3 ASCII (width 3) = 9
1787        assert_eq!(mixed, 9, "Mixed CJK/ASCII content");
1788    }
1789}