mdbook-lint-core 0.14.3

Core linting engine for mdbook-lint - library for markdown linting with mdBook support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
//! Rule provider system and lint engine.

use crate::config::Config;
use crate::error::Result;
use crate::registry::RuleRegistry;
use serde_json::Value;

/// Trait for rule providers to register rules with the engine
pub trait RuleProvider: Send + Sync {
    /// Unique identifier for this rule provider
    fn provider_id(&self) -> &'static str;

    /// Human-readable description of this rule provider
    fn description(&self) -> &'static str;

    /// Version of this rule provider
    fn version(&self) -> &'static str;

    /// Register all rules from this provider with the registry
    fn register_rules(&self, registry: &mut RuleRegistry);

    /// Provider-specific configuration schema
    fn config_schema(&self) -> Option<Value> {
        None
    }

    /// List of rule IDs that this provider registers
    fn rule_ids(&self) -> Vec<&'static str> {
        Vec::new()
    }

    /// Provider initialization hook
    fn initialize(&self) -> Result<()> {
        Ok(())
    }

    /// Register all rules from this provider with the registry, using configuration
    /// This method allows rules to be configured at registration time.
    /// The default implementation calls the legacy register_rules method for backward compatibility.
    fn register_rules_with_config(&self, registry: &mut RuleRegistry, _config: Option<&Config>) {
        // Default implementation calls the old method for backward compatibility
        self.register_rules(registry);
    }
}

/// Registry for managing rule providers and creating engines
#[derive(Default)]
pub struct PluginRegistry {
    providers: Vec<Box<dyn RuleProvider>>,
}

impl PluginRegistry {
    /// Create a new empty plugin registry
    pub fn new() -> Self {
        Self {
            providers: Vec::new(),
        }
    }

    /// Register a rule provider
    pub fn register_provider(&mut self, provider: Box<dyn RuleProvider>) -> Result<()> {
        // Initialize the provider
        provider.initialize()?;

        // Check for duplicate provider IDs
        let provider_id = provider.provider_id();
        if self
            .providers
            .iter()
            .any(|p| p.provider_id() == provider_id)
        {
            return Err(crate::error::MdBookLintError::plugin_error(format!(
                "Provider with ID '{provider_id}' is already registered"
            )));
        }

        self.providers.push(provider);
        Ok(())
    }

    /// Get all registered providers
    pub fn providers(&self) -> &[Box<dyn RuleProvider>] {
        &self.providers
    }

    /// Get a provider by ID
    pub fn get_provider(&self, id: &str) -> Option<&dyn RuleProvider> {
        self.providers
            .iter()
            .find(|p| p.provider_id() == id)
            .map(|p| p.as_ref())
    }

    /// Create a rule registry with all registered providers
    pub fn create_rule_registry(&self) -> Result<RuleRegistry> {
        self.create_rule_registry_with_config(None)
    }

    /// Create a rule registry with all registered providers, using configuration
    pub fn create_rule_registry_with_config(
        &self,
        config: Option<&Config>,
    ) -> Result<RuleRegistry> {
        let mut registry = RuleRegistry::new();

        for provider in &self.providers {
            provider.register_rules_with_config(&mut registry, config);
        }

        Ok(registry)
    }

    /// Create a lint engine with all registered providers
    pub fn create_engine(&self) -> Result<LintEngine> {
        self.create_engine_with_config(None)
    }

    /// Create a lint engine with all registered providers, using configuration
    pub fn create_engine_with_config(&self, config: Option<&Config>) -> Result<LintEngine> {
        let registry = self.create_rule_registry_with_config(config)?;
        Ok(LintEngine::with_registry(registry))
    }

    /// List all available rule IDs from all providers
    pub fn available_rule_ids(&self) -> Vec<String> {
        let mut rule_ids = Vec::new();

        for provider in &self.providers {
            for rule_id in provider.rule_ids() {
                rule_ids.push(rule_id.to_string());
            }
        }

        rule_ids.sort();
        rule_ids.dedup();
        rule_ids
    }

    /// Get provider information for debugging/introspection
    pub fn provider_info(&self) -> Vec<ProviderInfo> {
        self.providers
            .iter()
            .map(|p| ProviderInfo {
                id: p.provider_id().to_string(),
                description: p.description().to_string(),
                version: p.version().to_string(),
                rule_count: p.rule_ids().len(),
            })
            .collect()
    }
}

/// Information about a registered provider (for debugging/introspection)
#[derive(Debug, Clone)]
pub struct ProviderInfo {
    pub id: String,
    pub description: String,
    pub version: String,
    pub rule_count: usize,
}

/// Markdown linting engine
pub struct LintEngine {
    registry: RuleRegistry,
}

impl LintEngine {
    /// Create a new lint engine with no rules
    pub fn new() -> Self {
        Self {
            registry: RuleRegistry::new(),
        }
    }

    /// Create a lint engine with an existing rule registry
    pub fn with_registry(registry: RuleRegistry) -> Self {
        Self { registry }
    }

    /// Get the underlying rule registry
    pub fn registry(&self) -> &RuleRegistry {
        &self.registry
    }

    /// Get a mutable reference to the rule registry
    pub fn registry_mut(&mut self) -> &mut RuleRegistry {
        &mut self.registry
    }

    /// Lint a document with all registered rules
    pub fn lint_document(&self, document: &crate::Document) -> Result<Vec<crate::Violation>> {
        self.registry.check_document_optimized(document)
    }

    /// Lint a document with specific configuration
    pub fn lint_document_with_config(
        &self,
        document: &crate::Document,
        config: &crate::Config,
    ) -> Result<Vec<crate::Violation>> {
        self.registry
            .check_document_optimized_with_config(document, config)
    }

    /// Lint content string directly (convenience method)
    ///
    /// # Arguments
    ///
    /// * `content` - The markdown content to lint
    /// * `source_label` - A label for error messages (e.g., filename). This is NOT read from disk.
    pub fn lint_content(&self, content: &str, source_label: &str) -> Result<Vec<crate::Violation>> {
        let document =
            crate::Document::new(content.to_string(), std::path::PathBuf::from(source_label))?;
        self.lint_document(&document)
    }

    /// Apply a single fix to content
    ///
    /// Returns `Some(fixed_content)` if the fix was applied, `None` if the violation
    /// has no fix or the fix couldn't be applied.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let violations = engine.lint_content("# Test\n\n\n\n", "test.md")?;
    /// if let Some(v) = violations.first() {
    ///     if let Some(fixed) = engine.apply_fix("# Test\n\n\n\n", v) {
    ///         println!("Fixed: {}", fixed);
    ///     }
    /// }
    /// ```
    pub fn apply_fix(&self, content: &str, violation: &crate::Violation) -> Option<String> {
        let fix = violation.fix.as_ref()?;

        let start_offset = position_to_offset(content, &fix.start)?;
        let mut end_offset = position_to_offset(content, &fix.end)?;

        // Handle newline duplication: if the replacement ends with a newline and
        // the original content has a newline at the end position, skip it to avoid
        // double newlines. This is a common pattern when rules create fixes that
        // replace entire lines including their trailing newline.
        let replacement = fix.replacement.as_deref().unwrap_or("");
        if replacement.ends_with('\n') && content.as_bytes().get(end_offset) == Some(&b'\n') {
            end_offset += 1;
        }

        if start_offset <= end_offset && end_offset <= content.len() {
            let mut result = content.to_string();
            result.replace_range(start_offset..end_offset, replacement);
            Some(result)
        } else {
            None
        }
    }

    /// Apply all available fixes to content
    ///
    /// Applies fixes from violations that have them, processing in reverse position
    /// order to avoid offset issues. Returns the fixed content and a list of
    /// violations that could not be fixed.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let violations = engine.lint_content(content, "test.md")?;
    /// let (fixed_content, unfixed) = engine.apply_fixes(content, &violations);
    /// if fixed_content != content {
    ///     println!("Applied {} fixes", violations.len() - unfixed.len());
    /// }
    /// ```
    pub fn apply_fixes(
        &self,
        content: &str,
        violations: &[crate::Violation],
    ) -> (String, Vec<crate::Violation>) {
        use std::cmp::Ordering;

        if violations.is_empty() {
            return (content.to_string(), Vec::new());
        }

        // Collect violations with fixes, along with their index for tracking unfixed ones
        let mut fixable: Vec<(usize, &crate::Violation)> = violations
            .iter()
            .enumerate()
            .filter(|(_, v)| v.fix.is_some())
            .collect();

        if fixable.is_empty() {
            return (content.to_string(), violations.to_vec());
        }

        // Sort by position (descending) to avoid offset issues when applying
        fixable.sort_by(|a, b| {
            let fix_a = a.1.fix.as_ref().unwrap();
            let fix_b = b.1.fix.as_ref().unwrap();
            match fix_b.start.line.cmp(&fix_a.start.line) {
                Ordering::Equal => fix_b.start.column.cmp(&fix_a.start.column),
                other => other,
            }
        });

        let mut result = content.to_string();
        let mut applied_indices = std::collections::HashSet::new();

        for (idx, violation) in &fixable {
            let fix = violation.fix.as_ref().unwrap();

            let start = position_to_offset(&result, &fix.start);
            let mut end = position_to_offset(&result, &fix.end);

            // Handle newline duplication (see apply_fix for details)
            let replacement = fix.replacement.as_deref().unwrap_or("");
            if let Some(end_offset) = end
                && replacement.ends_with('\n')
                && result.as_bytes().get(end_offset) == Some(&b'\n')
            {
                end = Some(end_offset + 1);
            }

            if let (Some(start), Some(end)) = (start, end)
                && start <= end
                && end <= result.len()
            {
                result.replace_range(start..end, replacement);
                applied_indices.insert(*idx);
            }
        }

        // Collect violations that weren't fixed
        let unfixed: Vec<crate::Violation> = violations
            .iter()
            .enumerate()
            .filter(|(idx, v)| v.fix.is_none() || !applied_indices.contains(idx))
            .map(|(_, v)| v.clone())
            .collect();

        (result, unfixed)
    }

    /// Get all available rule IDs
    pub fn available_rules(&self) -> Vec<&'static str> {
        self.registry.rule_ids()
    }

    /// Get enabled rules based on configuration
    pub fn enabled_rules(&self, config: &crate::Config) -> Vec<&dyn crate::rule::Rule> {
        self.registry.get_enabled_rules(config)
    }

    /// Lint a collection of documents with collection rules
    ///
    /// Collection rules analyze multiple documents together for cross-document validation.
    /// This method runs all registered collection rules against the provided documents.
    pub fn lint_collection(&self, documents: &[crate::Document]) -> Result<Vec<crate::Violation>> {
        self.registry.check_collection(documents)
    }

    /// Lint a collection of documents with specific configuration
    pub fn lint_collection_with_config(
        &self,
        documents: &[crate::Document],
        config: &crate::Config,
    ) -> Result<Vec<crate::Violation>> {
        self.registry
            .check_collection_with_config(documents, config)
    }

    /// Get all available collection rule IDs
    pub fn available_collection_rules(&self) -> Vec<&'static str> {
        self.registry.collection_rule_ids()
    }

    /// Check if there are any collection rules registered
    pub fn has_collection_rules(&self) -> bool {
        self.registry.has_collection_rules()
    }
}

/// Convert a line/column position to a byte offset in text
fn position_to_offset(text: &str, pos: &crate::violation::Position) -> Option<usize> {
    let mut current_line = 1;
    let mut current_col = 1;

    for (offset, ch) in text.char_indices() {
        if current_line == pos.line && current_col == pos.column {
            return Some(offset);
        }

        if ch == '\n' {
            current_line += 1;
            current_col = 1;
        } else {
            current_col += 1;
        }
    }

    // Handle position at end of content
    if current_line == pos.line && current_col == pos.column {
        Some(text.len())
    } else {
        None
    }
}

impl Default for LintEngine {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rule::{Rule, RuleCategory, RuleMetadata};
    use std::path::PathBuf;

    // Test rule for plugin system testing
    struct TestRule;

    impl Rule for TestRule {
        fn id(&self) -> &'static str {
            "TEST001"
        }
        fn name(&self) -> &'static str {
            "test-rule"
        }
        fn description(&self) -> &'static str {
            "A test rule"
        }
        fn metadata(&self) -> RuleMetadata {
            RuleMetadata::stable(RuleCategory::Structure)
        }
        fn check_with_ast<'a>(
            &self,
            _document: &crate::Document,
            _ast: Option<&'a comrak::nodes::AstNode<'a>>,
        ) -> Result<Vec<crate::Violation>> {
            Ok(vec![])
        }
    }

    // Test provider
    struct TestProvider;

    impl RuleProvider for TestProvider {
        fn provider_id(&self) -> &'static str {
            "test-provider"
        }
        fn description(&self) -> &'static str {
            "Test provider"
        }
        fn version(&self) -> &'static str {
            "0.1.0"
        }

        fn register_rules(&self, registry: &mut RuleRegistry) {
            registry.register(Box::new(TestRule));
        }

        fn rule_ids(&self) -> Vec<&'static str> {
            vec!["TEST001"]
        }
    }

    #[test]
    fn test_plugin_registry_basic() {
        let mut registry = PluginRegistry::new();
        assert_eq!(registry.providers().len(), 0);

        registry.register_provider(Box::new(TestProvider)).unwrap();
        assert_eq!(registry.providers().len(), 1);

        let provider = registry.get_provider("test-provider").unwrap();
        assert_eq!(provider.provider_id(), "test-provider");
        assert_eq!(provider.description(), "Test provider");
    }

    #[test]
    fn test_plugin_registry_duplicate_id() {
        let mut registry = PluginRegistry::new();
        registry.register_provider(Box::new(TestProvider)).unwrap();

        // Should fail with duplicate ID
        let result = registry.register_provider(Box::new(TestProvider));
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("already registered")
        );
    }

    #[test]
    fn test_create_engine_from_registry() {
        let mut registry = PluginRegistry::new();
        registry.register_provider(Box::new(TestProvider)).unwrap();

        let engine = registry.create_engine().unwrap();
        let rule_ids = engine.available_rules();
        assert!(rule_ids.contains(&"TEST001"));
    }

    #[test]
    fn test_available_rule_ids() {
        let mut registry = PluginRegistry::new();
        registry.register_provider(Box::new(TestProvider)).unwrap();

        let rule_ids = registry.available_rule_ids();
        assert_eq!(rule_ids, vec!["TEST001"]);
    }

    #[test]
    fn test_provider_info() {
        let mut registry = PluginRegistry::new();
        registry.register_provider(Box::new(TestProvider)).unwrap();

        let info = registry.provider_info();
        assert_eq!(info.len(), 1);
        assert_eq!(info[0].id, "test-provider");
        assert_eq!(info[0].description, "Test provider");
        assert_eq!(info[0].version, "0.1.0");
        assert_eq!(info[0].rule_count, 1);
    }

    #[test]
    fn test_get_provider_not_found() {
        let registry = PluginRegistry::new();
        assert!(registry.get_provider("nonexistent").is_none());
    }

    #[test]
    fn test_create_rule_registry() {
        let mut registry = PluginRegistry::new();
        registry.register_provider(Box::new(TestProvider)).unwrap();

        let rule_registry = registry.create_rule_registry().unwrap();
        assert!(!rule_registry.is_empty());
    }

    // Test provider with initialization failure
    struct FailingProvider;

    impl RuleProvider for FailingProvider {
        fn provider_id(&self) -> &'static str {
            "failing-provider"
        }
        fn description(&self) -> &'static str {
            "Failing test provider"
        }
        fn version(&self) -> &'static str {
            "0.1.0"
        }
        fn register_rules(&self, _registry: &mut RuleRegistry) {}
        fn initialize(&self) -> Result<()> {
            Err(crate::error::MdBookLintError::plugin_error(
                "Initialization failed",
            ))
        }
    }

    #[test]
    fn test_provider_initialization_failure() {
        let mut registry = PluginRegistry::new();
        let result = registry.register_provider(Box::new(FailingProvider));
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Initialization failed")
        );
    }

    // Test provider with config schema
    struct ConfigurableProvider;

    impl RuleProvider for ConfigurableProvider {
        fn provider_id(&self) -> &'static str {
            "configurable-provider"
        }
        fn description(&self) -> &'static str {
            "Configurable test provider"
        }
        fn version(&self) -> &'static str {
            "0.1.0"
        }
        fn register_rules(&self, _registry: &mut RuleRegistry) {}
        fn config_schema(&self) -> Option<Value> {
            Some(serde_json::json!({
                "type": "object",
                "properties": {
                    "enabled": {"type": "boolean"}
                }
            }))
        }
    }

    #[test]
    fn test_provider_with_config_schema() {
        let provider = ConfigurableProvider;
        let schema = provider.config_schema();
        assert!(schema.is_some());
        let schema = schema.unwrap();
        assert_eq!(schema["type"], "object");
    }

    #[test]
    fn test_lint_engine_with_registry() {
        let mut rule_registry = RuleRegistry::new();
        rule_registry.register(Box::new(TestRule));

        let engine = LintEngine::with_registry(rule_registry);
        let rules = engine.available_rules();
        assert!(rules.contains(&"TEST001"));
    }

    #[test]
    fn test_lint_engine_api() {
        let mut registry = PluginRegistry::new();
        registry.register_provider(Box::new(TestProvider)).unwrap();
        let engine = registry.create_engine().unwrap();

        // Test basic content linting
        let _violations = engine.lint_content("# Test\n", "test.md").unwrap();

        // Test document linting
        let document =
            crate::Document::new("# Test".to_string(), PathBuf::from("test.md")).unwrap();
        let _violations = engine.lint_document(&document).unwrap();
    }

    #[test]
    fn test_position_to_offset() {
        let text = "line1\nline2\nline3";

        // Line 1, column 1 = offset 0
        assert_eq!(
            super::position_to_offset(text, &crate::violation::Position { line: 1, column: 1 }),
            Some(0)
        );

        // Line 1, column 3 = offset 2 ('n' in 'line1')
        assert_eq!(
            super::position_to_offset(text, &crate::violation::Position { line: 1, column: 3 }),
            Some(2)
        );

        // Line 2, column 1 = offset 6 (after 'line1\n')
        assert_eq!(
            super::position_to_offset(text, &crate::violation::Position { line: 2, column: 1 }),
            Some(6)
        );

        // Line 3, column 1 = offset 12
        assert_eq!(
            super::position_to_offset(text, &crate::violation::Position { line: 3, column: 1 }),
            Some(12)
        );

        // Invalid position
        assert_eq!(
            super::position_to_offset(
                text,
                &crate::violation::Position {
                    line: 10,
                    column: 1
                }
            ),
            None
        );
    }

    #[test]
    fn test_apply_fix_simple() {
        let engine = LintEngine::new();
        let content = "hello world";

        // Create a violation with a fix to replace "world" with "rust"
        let violation = crate::Violation {
            rule_id: "TEST".to_string(),
            rule_name: "test".to_string(),
            message: "test".to_string(),
            line: 1,
            column: 7,
            severity: crate::Severity::Warning,
            fix: Some(crate::violation::Fix {
                description: "Replace world with rust".to_string(),
                replacement: Some("rust".to_string()),
                start: crate::violation::Position { line: 1, column: 7 },
                end: crate::violation::Position {
                    line: 1,
                    column: 12,
                },
            }),
        };

        let result = engine.apply_fix(content, &violation);
        assert_eq!(result, Some("hello rust".to_string()));
    }

    #[test]
    fn test_apply_fix_no_fix() {
        let engine = LintEngine::new();
        let content = "hello world";

        let violation = crate::Violation {
            rule_id: "TEST".to_string(),
            rule_name: "test".to_string(),
            message: "test".to_string(),
            line: 1,
            column: 1,
            severity: crate::Severity::Warning,
            fix: None,
        };

        let result = engine.apply_fix(content, &violation);
        assert_eq!(result, None);
    }

    #[test]
    fn test_apply_fixes_multiple() {
        let engine = LintEngine::new();
        let content = "aaa bbb ccc";

        let violations = vec![
            crate::Violation {
                rule_id: "TEST".to_string(),
                rule_name: "test".to_string(),
                message: "test".to_string(),
                line: 1,
                column: 1,
                severity: crate::Severity::Warning,
                fix: Some(crate::violation::Fix {
                    description: "Replace aaa with AAA".to_string(),
                    replacement: Some("AAA".to_string()),
                    start: crate::violation::Position { line: 1, column: 1 },
                    end: crate::violation::Position { line: 1, column: 4 },
                }),
            },
            crate::Violation {
                rule_id: "TEST".to_string(),
                rule_name: "test".to_string(),
                message: "test".to_string(),
                line: 1,
                column: 9,
                severity: crate::Severity::Warning,
                fix: Some(crate::violation::Fix {
                    description: "Replace ccc with CCC".to_string(),
                    replacement: Some("CCC".to_string()),
                    start: crate::violation::Position { line: 1, column: 9 },
                    end: crate::violation::Position {
                        line: 1,
                        column: 12,
                    },
                }),
            },
        ];

        let (fixed, unfixed) = engine.apply_fixes(content, &violations);
        assert_eq!(fixed, "AAA bbb CCC");
        assert!(unfixed.is_empty());
    }

    #[test]
    fn test_apply_fixes_mixed() {
        let engine = LintEngine::new();
        let content = "hello world";

        let violations = vec![
            crate::Violation {
                rule_id: "TEST1".to_string(),
                rule_name: "test".to_string(),
                message: "has fix".to_string(),
                line: 1,
                column: 7,
                severity: crate::Severity::Warning,
                fix: Some(crate::violation::Fix {
                    description: "Replace".to_string(),
                    replacement: Some("rust".to_string()),
                    start: crate::violation::Position { line: 1, column: 7 },
                    end: crate::violation::Position {
                        line: 1,
                        column: 12,
                    },
                }),
            },
            crate::Violation {
                rule_id: "TEST2".to_string(),
                rule_name: "test".to_string(),
                message: "no fix".to_string(),
                line: 1,
                column: 1,
                severity: crate::Severity::Warning,
                fix: None,
            },
        ];

        let (fixed, unfixed) = engine.apply_fixes(content, &violations);
        assert_eq!(fixed, "hello rust");
        assert_eq!(unfixed.len(), 1);
        assert_eq!(unfixed[0].rule_id, "TEST2");
    }

    #[test]
    fn test_apply_fix_newline_handling() {
        // Test that fixes ending with newline don't cause double newlines
        let engine = LintEngine::new();

        // Content has a line that will be replaced, followed by another line
        let content = "# Old Heading\nNext line\n";

        // Fix replaces the heading line (including its newline) with a new heading
        let violation = crate::Violation {
            rule_id: "TEST".to_string(),
            rule_name: "test".to_string(),
            message: "Replace heading".to_string(),
            line: 1,
            column: 1,
            severity: crate::Severity::Warning,
            fix: Some(crate::violation::Fix {
                description: "Replace heading".to_string(),
                start: crate::violation::Position { line: 1, column: 1 },
                end: crate::violation::Position {
                    line: 1,
                    column: 14,
                }, // Points to the newline position
                replacement: Some("# New Heading\n".to_string()),
            }),
        };

        let result = engine.apply_fix(content, &violation);
        assert!(result.is_some());
        let fixed = result.unwrap();

        // Should have exactly one newline between the heading and next line, not two
        assert_eq!(fixed, "# New Heading\nNext line\n");
        assert!(!fixed.contains("\n\n"), "Should not have double newlines");
    }

    #[test]
    fn test_apply_fix_no_newline_no_adjustment() {
        // Test that fixes NOT ending with newline work normally
        let engine = LintEngine::new();
        let content = "hello world";

        let violation = crate::Violation {
            rule_id: "TEST".to_string(),
            rule_name: "test".to_string(),
            message: "Replace word".to_string(),
            line: 1,
            column: 7,
            severity: crate::Severity::Warning,
            fix: Some(crate::violation::Fix {
                description: "Replace word".to_string(),
                start: crate::violation::Position { line: 1, column: 7 },
                end: crate::violation::Position {
                    line: 1,
                    column: 12,
                },
                replacement: Some("rust".to_string()),
            }),
        };

        let result = engine.apply_fix(content, &violation);
        assert!(result.is_some());
        assert_eq!(result.unwrap(), "hello rust");
    }
}