Skip to main content

rigsql_rules/layout/
lt05.rs

1use crate::rule::{CrawlType, Rule, RuleContext, RuleGroup};
2use crate::violation::LintViolation;
3
4/// LT05: Line too long.
5///
6/// Default max line length: 80 characters.
7#[derive(Debug)]
8pub struct RuleLT05 {
9    pub max_line_length: usize,
10}
11
12impl Default for RuleLT05 {
13    fn default() -> Self {
14        Self {
15            max_line_length: 80,
16        }
17    }
18}
19
20impl Rule for RuleLT05 {
21    fn code(&self) -> &'static str {
22        "LT05"
23    }
24    fn name(&self) -> &'static str {
25        "layout.long_lines"
26    }
27    fn description(&self) -> &'static str {
28        "Line too long."
29    }
30    fn explanation(&self) -> &'static str {
31        "Long lines are harder to read and review. Keep lines under the configured \
32         maximum length (default 80 characters). Break long queries across multiple lines."
33    }
34    fn groups(&self) -> &[RuleGroup] {
35        &[RuleGroup::Layout]
36    }
37    fn is_fixable(&self) -> bool {
38        false
39    }
40
41    fn configure(&mut self, settings: &std::collections::HashMap<String, String>) {
42        if let Some(val) = settings.get("max_line_length") {
43            if let Ok(n) = val.parse() {
44                self.max_line_length = n;
45            }
46        }
47    }
48
49    fn crawl_type(&self) -> CrawlType {
50        CrawlType::RootOnly
51    }
52
53    fn eval(&self, ctx: &RuleContext) -> Vec<LintViolation> {
54        let mut violations = Vec::new();
55        let source = ctx.source;
56        let mut offset = 0usize;
57
58        for line in source.lines() {
59            let line_len = line.len();
60            if line_len > self.max_line_length {
61                let span = rigsql_core::Span::new(offset as u32, (offset + line_len) as u32);
62                violations.push(LintViolation::new(
63                    self.code(),
64                    format!(
65                        "Line is too long ({} > {} characters).",
66                        line_len, self.max_line_length
67                    ),
68                    span,
69                ));
70            }
71            offset += line_len + 1; // +1 for \n
72        }
73
74        violations
75    }
76}