use crate::config as rumdl_config;
use crate::lint_context::LintContext;
use crate::rule::{LintWarning, Rule};
use crate::rules::md013_line_length::MD013LineLength;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DocCommentKind {
Outer,
Inner,
}
#[derive(Debug, Clone)]
pub struct DocCommentLineInfo {
pub leading_whitespace: String,
pub prefix: String,
}
#[derive(Debug, Clone)]
pub struct DocCommentBlock {
pub kind: DocCommentKind,
pub start_line: usize,
pub end_line: usize,
pub byte_start: usize,
pub byte_end: usize,
pub markdown: String,
pub line_metadata: Vec<DocCommentLineInfo>,
pub prefix_byte_lengths: Vec<usize>,
}
fn classify_doc_comment_line(line: &str) -> Option<(DocCommentKind, String, String)> {
let trimmed = line.trim_start();
let leading_ws = &line[..line.len() - trimmed.len()];
if trimmed.starts_with("////") {
return None;
}
if let Some(after) = trimmed.strip_prefix("///") {
let prefix = if after.starts_with(' ') || after.starts_with('\t') {
format!("///{}", &after[..1])
} else {
"///".to_string()
};
Some((DocCommentKind::Outer, leading_ws.to_string(), prefix))
} else if let Some(after) = trimmed.strip_prefix("//!") {
let prefix = if after.starts_with(' ') || after.starts_with('\t') {
format!("//!{}", &after[..1])
} else {
"//!".to_string()
};
Some((DocCommentKind::Inner, leading_ws.to_string(), prefix))
} else {
None
}
}
fn extract_markdown_from_line(trimmed: &str, kind: DocCommentKind) -> &str {
let prefix = match kind {
DocCommentKind::Outer => "///",
DocCommentKind::Inner => "//!",
};
let after_prefix = &trimmed[prefix.len()..];
if let Some(stripped) = after_prefix.strip_prefix(' ') {
stripped
} else {
after_prefix
}
}
pub fn extract_doc_comment_blocks(content: &str) -> Vec<DocCommentBlock> {
let mut blocks = Vec::new();
let mut current_block: Option<DocCommentBlock> = None;
let mut byte_offset = 0;
let lines: Vec<&str> = content.split('\n').collect();
let num_lines = lines.len();
for (line_idx, line) in lines.iter().enumerate() {
let line_byte_start = byte_offset;
let has_newline = line_idx < num_lines - 1 || content.ends_with('\n');
let line_byte_end = byte_offset + line.len() + if has_newline { 1 } else { 0 };
if let Some((kind, leading_ws, prefix)) = classify_doc_comment_line(line) {
let trimmed = line.trim_start();
let md_content = extract_markdown_from_line(trimmed, kind);
let prefix_byte_len = leading_ws.len() + prefix.len();
let line_info = DocCommentLineInfo {
leading_whitespace: leading_ws,
prefix,
};
match current_block.as_mut() {
Some(block) if block.kind == kind => {
block.end_line = line_idx;
block.byte_end = line_byte_end;
block.markdown.push('\n');
block.markdown.push_str(md_content);
block.line_metadata.push(line_info);
block.prefix_byte_lengths.push(prefix_byte_len);
}
_ => {
if let Some(block) = current_block.take() {
blocks.push(block);
}
current_block = Some(DocCommentBlock {
kind,
start_line: line_idx,
end_line: line_idx,
byte_start: line_byte_start,
byte_end: line_byte_end,
markdown: md_content.to_string(),
line_metadata: vec![line_info],
prefix_byte_lengths: vec![prefix_byte_len],
});
}
}
} else {
if let Some(block) = current_block.take() {
blocks.push(block);
}
}
byte_offset = line_byte_end;
}
if let Some(block) = current_block.take() {
blocks.push(block);
}
blocks
}
pub const SKIPPED_RULES: &[&str] = &["MD025", "MD033", "MD040", "MD041", "MD047", "MD051", "MD052", "MD054"];
pub fn check_doc_comment_blocks(
content: &str,
rules: &[Box<dyn Rule>],
config: &rumdl_config::Config,
) -> Vec<LintWarning> {
let blocks = extract_doc_comment_blocks(content);
let mut all_warnings = Vec::new();
for block in &blocks {
if block.markdown.trim().is_empty() {
continue;
}
let ctx = LintContext::new(&block.markdown, config.markdown_flavor(), None);
for rule in rules {
if SKIPPED_RULES.contains(&rule.name()) {
continue;
}
let doc_rule: Box<dyn Rule>;
let effective_rule: &dyn Rule = if rule.name() == "MD013" {
if let Some(md013) = rule.as_any().downcast_ref::<MD013LineLength>() {
doc_rule = Box::new(md013.with_code_blocks_disabled());
doc_rule.as_ref()
} else {
rule.as_ref()
}
} else {
rule.as_ref()
};
if let Ok(rule_warnings) = effective_rule.check(&ctx) {
for warning in rule_warnings {
let file_line = warning.line + block.start_line;
let file_end_line = warning.end_line + block.start_line;
let block_line_idx = warning.line.saturating_sub(1);
let col_offset = block.prefix_byte_lengths.get(block_line_idx).copied().unwrap_or(0);
let file_column = warning.column + col_offset;
let block_end_line_idx = warning.end_line.saturating_sub(1);
let end_col_offset = block.prefix_byte_lengths.get(block_end_line_idx).copied().unwrap_or(0);
let file_end_column = warning.end_column + end_col_offset;
all_warnings.push(LintWarning {
line: file_line,
end_line: file_end_line,
column: file_column,
end_column: file_end_column,
fix: None,
..warning
});
}
}
}
}
all_warnings
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_classify_outer_doc_comment() {
let (kind, ws, prefix) = classify_doc_comment_line("/// Hello").unwrap();
assert_eq!(kind, DocCommentKind::Outer);
assert_eq!(ws, "");
assert_eq!(prefix, "/// ");
}
#[test]
fn test_classify_inner_doc_comment() {
let (kind, ws, prefix) = classify_doc_comment_line("//! Module doc").unwrap();
assert_eq!(kind, DocCommentKind::Inner);
assert_eq!(ws, "");
assert_eq!(prefix, "//! ");
}
#[test]
fn test_classify_empty_outer() {
let (kind, ws, prefix) = classify_doc_comment_line("///").unwrap();
assert_eq!(kind, DocCommentKind::Outer);
assert_eq!(ws, "");
assert_eq!(prefix, "///");
}
#[test]
fn test_classify_empty_inner() {
let (kind, ws, prefix) = classify_doc_comment_line("//!").unwrap();
assert_eq!(kind, DocCommentKind::Inner);
assert_eq!(ws, "");
assert_eq!(prefix, "//!");
}
#[test]
fn test_classify_indented() {
let (kind, ws, prefix) = classify_doc_comment_line(" /// Indented").unwrap();
assert_eq!(kind, DocCommentKind::Outer);
assert_eq!(ws, " ");
assert_eq!(prefix, "/// ");
}
#[test]
fn test_classify_no_space_after_prefix() {
let (kind, ws, prefix) = classify_doc_comment_line("///content").unwrap();
assert_eq!(kind, DocCommentKind::Outer);
assert_eq!(ws, "");
assert_eq!(prefix, "///");
}
#[test]
fn test_classify_tab_after_prefix() {
let (kind, ws, prefix) = classify_doc_comment_line("///\tcontent").unwrap();
assert_eq!(kind, DocCommentKind::Outer);
assert_eq!(ws, "");
assert_eq!(prefix, "///\t");
}
#[test]
fn test_classify_inner_no_space() {
let (kind, _, prefix) = classify_doc_comment_line("//!content").unwrap();
assert_eq!(kind, DocCommentKind::Inner);
assert_eq!(prefix, "//!");
}
#[test]
fn test_classify_four_slashes_is_not_doc() {
assert!(classify_doc_comment_line("//// Not a doc comment").is_none());
}
#[test]
fn test_classify_regular_comment() {
assert!(classify_doc_comment_line("// Regular comment").is_none());
}
#[test]
fn test_classify_code_line() {
assert!(classify_doc_comment_line("let x = 3;").is_none());
}
#[test]
fn test_extract_no_space_content() {
let content = "///no space here\n";
let blocks = extract_doc_comment_blocks(content);
assert_eq!(blocks.len(), 1);
assert_eq!(blocks[0].markdown, "no space here");
}
#[test]
fn test_extract_basic_outer_block() {
let content = "/// First line\n/// Second line\nfn foo() {}\n";
let blocks = extract_doc_comment_blocks(content);
assert_eq!(blocks.len(), 1);
assert_eq!(blocks[0].kind, DocCommentKind::Outer);
assert_eq!(blocks[0].start_line, 0);
assert_eq!(blocks[0].end_line, 1);
assert_eq!(blocks[0].markdown, "First line\nSecond line");
assert_eq!(blocks[0].line_metadata.len(), 2);
}
#[test]
fn test_extract_basic_inner_block() {
let content = "//! Module doc\n//! More info\n\nuse std::io;\n";
let blocks = extract_doc_comment_blocks(content);
assert_eq!(blocks.len(), 1);
assert_eq!(blocks[0].kind, DocCommentKind::Inner);
assert_eq!(blocks[0].markdown, "Module doc\nMore info");
}
#[test]
fn test_extract_multiple_blocks() {
let content = "/// Block 1\nfn foo() {}\n/// Block 2\nfn bar() {}\n";
let blocks = extract_doc_comment_blocks(content);
assert_eq!(blocks.len(), 2);
assert_eq!(blocks[0].markdown, "Block 1");
assert_eq!(blocks[0].start_line, 0);
assert_eq!(blocks[1].markdown, "Block 2");
assert_eq!(blocks[1].start_line, 2);
}
#[test]
fn test_extract_mixed_kinds_separate_blocks() {
let content = "//! Inner\n/// Outer\n";
let blocks = extract_doc_comment_blocks(content);
assert_eq!(blocks.len(), 2);
assert_eq!(blocks[0].kind, DocCommentKind::Inner);
assert_eq!(blocks[1].kind, DocCommentKind::Outer);
}
#[test]
fn test_extract_empty_doc_line() {
let content = "/// First\n///\n/// Third\n";
let blocks = extract_doc_comment_blocks(content);
assert_eq!(blocks.len(), 1);
assert_eq!(blocks[0].markdown, "First\n\nThird");
}
#[test]
fn test_extract_preserves_extra_space() {
let content = "/// Two spaces\n";
let blocks = extract_doc_comment_blocks(content);
assert_eq!(blocks.len(), 1);
assert_eq!(blocks[0].markdown, " Two spaces");
}
#[test]
fn test_extract_indented_doc_comments() {
let content = " /// Indented\n /// More\n";
let blocks = extract_doc_comment_blocks(content);
assert_eq!(blocks.len(), 1);
assert_eq!(blocks[0].markdown, "Indented\nMore");
assert_eq!(blocks[0].line_metadata[0].leading_whitespace, " ");
}
#[test]
fn test_no_doc_comments() {
let content = "fn main() {\n let x = 3;\n}\n";
let blocks = extract_doc_comment_blocks(content);
assert!(blocks.is_empty());
}
#[test]
fn test_byte_offsets() {
let content = "/// Hello\nfn foo() {}\n/// World\n";
let blocks = extract_doc_comment_blocks(content);
assert_eq!(blocks.len(), 2);
assert_eq!(blocks[0].byte_start, 0);
assert_eq!(blocks[0].byte_end, 10);
assert_eq!(blocks[1].byte_start, 22);
assert_eq!(blocks[1].byte_end, 32);
}
#[test]
fn test_byte_offsets_no_trailing_newline() {
let content = "/// Hello";
let blocks = extract_doc_comment_blocks(content);
assert_eq!(blocks.len(), 1);
assert_eq!(blocks[0].byte_start, 0);
assert_eq!(blocks[0].byte_end, content.len());
}
#[test]
fn test_prefix_byte_lengths() {
let content = " /// Indented\n/// Top-level\n";
let blocks = extract_doc_comment_blocks(content);
assert_eq!(blocks.len(), 1);
assert_eq!(blocks[0].prefix_byte_lengths[0], 8);
assert_eq!(blocks[0].prefix_byte_lengths[1], 4);
}
}