use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
use crate::rule_config_serde::RuleConfig;
use crate::utils::regex_cache::ORDERED_LIST_MARKER_REGEX;
use std::collections::HashMap;
use toml;
mod md029_config;
pub use md029_config::{ListStyle, MD029Config};
type ListItemGroup<'a> = (
usize,
Vec<(
usize,
&'a crate::lint_context::LineInfo,
&'a crate::lint_context::ListItemInfo,
)>,
);
#[derive(Debug, Clone, Default)]
pub struct MD029OrderedListPrefix {
config: MD029Config,
}
impl MD029OrderedListPrefix {
pub fn new(style: ListStyle) -> Self {
Self {
config: MD029Config { style },
}
}
pub fn from_config_struct(config: MD029Config) -> Self {
Self { config }
}
#[inline]
fn parse_marker_number(marker: &str) -> Option<usize> {
let num_part = if let Some(stripped) = marker.strip_suffix('.') {
stripped
} else {
marker
};
num_part.parse::<usize>().ok()
}
#[inline]
fn get_expected_number(&self, index: usize, detected_style: Option<ListStyle>, start_value: u64) -> usize {
let style = match self.config.style {
ListStyle::OneOrOrdered | ListStyle::Consistent => detected_style.unwrap_or(ListStyle::OneOne),
_ => self.config.style.clone(),
};
match style {
ListStyle::One | ListStyle::OneOne => 1,
ListStyle::Ordered => (start_value as usize) + index,
ListStyle::Ordered0 => index,
ListStyle::OneOrOrdered | ListStyle::Consistent => {
1
}
}
}
fn detect_list_style(
items: &[(
usize,
&crate::lint_context::LineInfo,
&crate::lint_context::ListItemInfo,
)],
start_value: u64,
) -> ListStyle {
if items.len() < 2 {
let first_num = Self::parse_marker_number(&items[0].2.marker);
if first_num == Some(start_value as usize) {
return ListStyle::Ordered;
}
return ListStyle::OneOne;
}
let first_num = Self::parse_marker_number(&items[0].2.marker);
let second_num = Self::parse_marker_number(&items[1].2.marker);
if matches!((first_num, second_num), (Some(0), Some(1))) {
return ListStyle::Ordered0;
}
if first_num != Some(1) || second_num != Some(1) {
return ListStyle::Ordered;
}
let all_ones = items
.iter()
.all(|(_, _, item)| Self::parse_marker_number(&item.marker) == Some(1));
if all_ones {
ListStyle::OneOne
} else {
ListStyle::Ordered
}
}
fn group_items_by_commonmark_list<'a>(
ctx: &'a crate::lint_context::LintContext,
line_to_list: &std::collections::HashMap<usize, usize>,
) -> Vec<ListItemGroup<'a>> {
let mut items_with_list_id: Vec<(
usize,
usize,
&crate::lint_context::LineInfo,
&crate::lint_context::ListItemInfo,
)> = Vec::new();
for line_num in 1..=ctx.lines.len() {
if let Some(line_info) = ctx.line_info(line_num)
&& let Some(list_item) = line_info.list_item.as_deref()
&& list_item.is_ordered
{
if let Some(&list_id) = line_to_list.get(&line_num) {
items_with_list_id.push((list_id, line_num, line_info, list_item));
}
}
}
let mut groups: std::collections::HashMap<
usize,
Vec<(
usize,
&crate::lint_context::LineInfo,
&crate::lint_context::ListItemInfo,
)>,
> = std::collections::HashMap::new();
for (list_id, line_num, line_info, list_item) in items_with_list_id {
groups
.entry(list_id)
.or_default()
.push((line_num, line_info, list_item));
}
let mut result: Vec<_> = groups.into_iter().collect();
for (_, items) in &mut result {
items.sort_by_key(|(line_num, _, _)| *line_num);
}
result.sort_by_key(|(_, items)| items.first().map(|(ln, _, _)| *ln).unwrap_or(0));
result
}
fn check_commonmark_list_group(
&self,
_ctx: &crate::lint_context::LintContext,
group: &[(
usize,
&crate::lint_context::LineInfo,
&crate::lint_context::ListItemInfo,
)],
warnings: &mut Vec<LintWarning>,
document_wide_style: Option<ListStyle>,
start_value: u64,
) {
if group.is_empty() {
return;
}
type LevelGroups<'a> = HashMap<
usize,
Vec<(
usize,
&'a crate::lint_context::LineInfo,
&'a crate::lint_context::ListItemInfo,
)>,
>;
let mut level_groups: LevelGroups = HashMap::new();
for (line_num, line_info, list_item) in group {
level_groups
.entry(list_item.marker_column)
.or_default()
.push((*line_num, *line_info, *list_item));
}
let mut sorted_levels: Vec<_> = level_groups.into_iter().collect();
sorted_levels.sort_by_key(|(indent, _)| *indent);
for (_indent, mut items) in sorted_levels {
items.sort_by_key(|(line_num, _, _)| *line_num);
if items.is_empty() {
continue;
}
let detected_style = if let Some(doc_style) = document_wide_style.clone() {
Some(doc_style)
} else if self.config.style == ListStyle::OneOrOrdered {
Some(Self::detect_list_style(&items, start_value))
} else {
None
};
for (idx, (line_num, line_info, list_item)) in items.iter().enumerate() {
if let Some(actual_num) = Self::parse_marker_number(&list_item.marker) {
let expected_num = self.get_expected_number(idx, detected_style.clone(), start_value);
if actual_num != expected_num {
let marker_start = line_info.byte_offset + list_item.marker_column;
let number_len = if let Some(dot_pos) = list_item.marker.find('.') {
dot_pos
} else if let Some(paren_pos) = list_item.marker.find(')') {
paren_pos
} else {
list_item.marker.len()
};
let style_name = match detected_style.as_ref().unwrap_or(&ListStyle::Ordered) {
ListStyle::OneOne => "one",
ListStyle::Ordered => "ordered",
ListStyle::Ordered0 => "ordered0",
_ => "ordered",
};
let style_context = match self.config.style {
ListStyle::Consistent => format!("document style '{style_name}'"),
ListStyle::OneOrOrdered => format!("list style '{style_name}'"),
ListStyle::One | ListStyle::OneOne => "configured style 'one'".to_string(),
ListStyle::Ordered => "configured style 'ordered'".to_string(),
ListStyle::Ordered0 => "configured style 'ordered0'".to_string(),
};
let should_provide_fix =
start_value == 1 || matches!(self.config.style, ListStyle::One | ListStyle::OneOne);
warnings.push(LintWarning {
rule_name: Some(self.name().to_string()),
message: format!(
"Ordered list item number {actual_num} does not match {style_context} (expected {expected_num})"
),
line: *line_num,
column: list_item.marker_column + 1,
end_line: *line_num,
end_column: list_item.marker_column + number_len + 1,
severity: Severity::Warning,
fix: if should_provide_fix {
Some(Fix {
range: marker_start..marker_start + number_len,
replacement: expected_num.to_string(),
})
} else {
None
},
});
}
}
}
}
}
}
impl Rule for MD029OrderedListPrefix {
fn name(&self) -> &'static str {
"MD029"
}
fn description(&self) -> &'static str {
"Ordered list marker value"
}
fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
if ctx.content.is_empty() {
return Ok(Vec::new());
}
if (!ctx.content.contains('.') && !ctx.content.contains(')'))
|| !ctx.content.lines().any(|line| ORDERED_LIST_MARKER_REGEX.is_match(line))
{
return Ok(Vec::new());
}
let mut warnings = Vec::new();
let list_groups = Self::group_items_by_commonmark_list(ctx, &ctx.line_to_list);
if list_groups.is_empty() {
return Ok(Vec::new());
}
let document_wide_style = if self.config.style == ListStyle::Consistent {
let mut all_document_items = Vec::new();
for (_, items) in &list_groups {
for (line_num, line_info, list_item) in items {
all_document_items.push((*line_num, *line_info, *list_item));
}
}
if !all_document_items.is_empty() {
Some(Self::detect_list_style(&all_document_items, 1))
} else {
None
}
} else {
None
};
for (list_id, items) in list_groups {
let start_value = ctx.list_start_values.get(&list_id).copied().unwrap_or(1);
self.check_commonmark_list_group(ctx, &items, &mut warnings, document_wide_style.clone(), start_value);
}
warnings.sort_by_key(|w| (w.line, w.column));
Ok(warnings)
}
fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
let warnings = self.check(ctx)?;
let warnings =
crate::utils::fix_utils::filter_warnings_by_inline_config(warnings, ctx.inline_config(), self.name());
if warnings.is_empty() {
return Ok(ctx.content.to_string());
}
let mut fixes: Vec<&Fix> = Vec::new();
for warning in &warnings {
if let Some(ref fix) = warning.fix {
fixes.push(fix);
}
}
fixes.sort_by_key(|f| f.range.start);
let mut result = String::new();
let mut last_pos = 0;
let content_bytes = ctx.content.as_bytes();
for fix in fixes {
if last_pos < fix.range.start {
let chunk = &content_bytes[last_pos..fix.range.start];
result.push_str(
std::str::from_utf8(chunk).map_err(|_| LintError::InvalidInput("Invalid UTF-8".to_string()))?,
);
}
result.push_str(&fix.replacement);
last_pos = fix.range.end;
}
if last_pos < content_bytes.len() {
let chunk = &content_bytes[last_pos..];
result.push_str(
std::str::from_utf8(chunk).map_err(|_| LintError::InvalidInput("Invalid UTF-8".to_string()))?,
);
}
Ok(result)
}
fn category(&self) -> RuleCategory {
RuleCategory::List
}
fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
ctx.content.is_empty() || !ctx.likely_has_lists()
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn default_config_section(&self) -> Option<(String, toml::Value)> {
let default_config = MD029Config::default();
let json_value = serde_json::to_value(&default_config).ok()?;
let toml_value = crate::rule_config_serde::json_to_toml_value(&json_value)?;
if let toml::Value::Table(table) = toml_value {
if !table.is_empty() {
Some((MD029Config::RULE_NAME.to_string(), toml::Value::Table(table)))
} else {
None
}
} else {
None
}
}
fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
where
Self: Sized,
{
let rule_config = crate::rule_config_serde::load_rule_config::<MD029Config>(config);
Box::new(MD029OrderedListPrefix::from_config_struct(rule_config))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_basic_functionality() {
let rule = MD029OrderedListPrefix::default();
let content = "1. First item\n2. Second item\n3. Third item";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty());
let content = "1. First item\n3. Third item\n5. Fifth item";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2);
let rule = MD029OrderedListPrefix::new(ListStyle::OneOne);
let content = "1. First item\n2. Second item\n3. Third item";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2);
let rule = MD029OrderedListPrefix::new(ListStyle::Ordered0);
let content = "0. First item\n1. Second item\n2. Third item";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty());
}
#[test]
fn test_redundant_computation_fix() {
let rule = MD029OrderedListPrefix::default();
let content = "1. First item\n3. Wrong number\n2. Another wrong number";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2);
assert!(result[0].message.contains("3") && result[0].message.contains("expected 2"));
assert!(result[1].message.contains("2") && result[1].message.contains("expected 3"));
}
#[test]
fn test_performance_improvement() {
let rule = MD029OrderedListPrefix::default();
let mut content = String::from("1. Item 1\n"); for i in 2..=100 {
content.push_str(&format!("{}. Item {}\n", i * 5 - 5, i)); }
let ctx = crate::lint_context::LintContext::new(&content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 99, "Should have warnings for items 2-100 (99 items)");
assert!(result[0].message.contains("5") && result[0].message.contains("expected 2"));
}
#[test]
fn test_one_or_ordered_with_all_ones() {
let rule = MD029OrderedListPrefix::new(ListStyle::OneOrOrdered);
let content = "1. First item\n1. Second item\n1. Third item";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty(), "All ones should be valid in OneOrOrdered mode");
}
#[test]
fn test_one_or_ordered_with_sequential() {
let rule = MD029OrderedListPrefix::new(ListStyle::OneOrOrdered);
let content = "1. First item\n2. Second item\n3. Third item";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Sequential numbering should be valid in OneOrOrdered mode"
);
}
#[test]
fn test_one_or_ordered_with_mixed_style() {
let rule = MD029OrderedListPrefix::new(ListStyle::OneOrOrdered);
let content = "1. First item\n2. Second item\n1. Third item";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Mixed style should produce one warning");
assert!(result[0].message.contains("1") && result[0].message.contains("expected 3"));
}
#[test]
fn test_one_or_ordered_separate_lists() {
let rule = MD029OrderedListPrefix::new(ListStyle::OneOrOrdered);
let content = "# First list\n\n1. Item A\n1. Item B\n\n# Second list\n\n1. Item X\n2. Item Y\n3. Item Z";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Separate lists can use different styles in OneOrOrdered mode"
);
}
}