rumdl_lib/lsp/
server.rs

1//! Main Language Server Protocol server implementation for rumdl
2//!
3//! This module implements the core LSP server following Ruff's architecture.
4//! It provides real-time markdown linting, diagnostics, and code actions.
5
6use std::collections::HashMap;
7use std::path::PathBuf;
8use std::sync::Arc;
9
10use anyhow::Result;
11use tokio::sync::RwLock;
12use tower_lsp::jsonrpc::Result as JsonRpcResult;
13use tower_lsp::lsp_types::*;
14use tower_lsp::{Client, LanguageServer};
15
16use crate::config::Config;
17use crate::lint;
18use crate::lsp::types::{RumdlLspConfig, warning_to_code_actions, warning_to_diagnostic};
19use crate::rule::Rule;
20use crate::rules;
21
22/// Represents a document in the LSP server's cache
23#[derive(Clone, Debug, PartialEq)]
24struct DocumentEntry {
25    /// The document content
26    content: String,
27    /// Version number from the editor (None for disk-loaded documents)
28    version: Option<i32>,
29    /// Whether the document was loaded from disk (true) or opened in editor (false)
30    from_disk: bool,
31}
32
33/// Cache entry for resolved configuration
34#[derive(Clone, Debug)]
35pub(crate) struct ConfigCacheEntry {
36    /// The resolved configuration
37    pub(crate) config: Config,
38    /// Config file path that was loaded (for invalidation)
39    pub(crate) config_file: Option<PathBuf>,
40    /// True if this entry came from the global/user fallback (no project config)
41    pub(crate) from_global_fallback: bool,
42}
43
44/// Main LSP server for rumdl
45///
46/// Following Ruff's pattern, this server provides:
47/// - Real-time diagnostics as users type
48/// - Code actions for automatic fixes
49/// - Configuration management
50/// - Multi-file support
51/// - Multi-root workspace support with per-file config resolution
52#[derive(Clone)]
53pub struct RumdlLanguageServer {
54    client: Client,
55    /// Configuration for the LSP server
56    config: Arc<RwLock<RumdlLspConfig>>,
57    /// Rumdl core configuration (fallback/default)
58    #[cfg_attr(test, allow(dead_code))]
59    pub(crate) rumdl_config: Arc<RwLock<Config>>,
60    /// Document store for open files and cached disk files
61    documents: Arc<RwLock<HashMap<Url, DocumentEntry>>>,
62    /// Workspace root folders from the client
63    #[cfg_attr(test, allow(dead_code))]
64    pub(crate) workspace_roots: Arc<RwLock<Vec<PathBuf>>>,
65    /// Configuration cache: maps directory path to resolved config
66    /// Key is the directory where config search started (file's parent dir)
67    #[cfg_attr(test, allow(dead_code))]
68    pub(crate) config_cache: Arc<RwLock<HashMap<PathBuf, ConfigCacheEntry>>>,
69}
70
71impl RumdlLanguageServer {
72    pub fn new(client: Client) -> Self {
73        Self {
74            client,
75            config: Arc::new(RwLock::new(RumdlLspConfig::default())),
76            rumdl_config: Arc::new(RwLock::new(Config::default())),
77            documents: Arc::new(RwLock::new(HashMap::new())),
78            workspace_roots: Arc::new(RwLock::new(Vec::new())),
79            config_cache: Arc::new(RwLock::new(HashMap::new())),
80        }
81    }
82
83    /// Get document content, either from cache or by reading from disk
84    ///
85    /// This method first checks if the document is in the cache (opened in editor).
86    /// If not found, it attempts to read the file from disk and caches it for
87    /// future requests.
88    async fn get_document_content(&self, uri: &Url) -> Option<String> {
89        // First check the cache
90        {
91            let docs = self.documents.read().await;
92            if let Some(entry) = docs.get(uri) {
93                return Some(entry.content.clone());
94            }
95        }
96
97        // If not in cache and it's a file URI, try to read from disk
98        if let Ok(path) = uri.to_file_path() {
99            if let Ok(content) = tokio::fs::read_to_string(&path).await {
100                // Cache the document for future requests
101                let entry = DocumentEntry {
102                    content: content.clone(),
103                    version: None,
104                    from_disk: true,
105                };
106
107                let mut docs = self.documents.write().await;
108                docs.insert(uri.clone(), entry);
109
110                log::debug!("Loaded document from disk and cached: {uri}");
111                return Some(content);
112            } else {
113                log::debug!("Failed to read file from disk: {uri}");
114            }
115        }
116
117        None
118    }
119
120    /// Apply LSP config overrides to the filtered rules
121    fn apply_lsp_config_overrides(
122        &self,
123        mut filtered_rules: Vec<Box<dyn Rule>>,
124        lsp_config: &RumdlLspConfig,
125    ) -> Vec<Box<dyn Rule>> {
126        // Apply enable_rules override from LSP config (if specified, only these rules are active)
127        if let Some(enable) = &lsp_config.enable_rules
128            && !enable.is_empty()
129        {
130            let enable_set: std::collections::HashSet<String> = enable.iter().cloned().collect();
131            filtered_rules.retain(|rule| enable_set.contains(rule.name()));
132        }
133
134        // Apply disable_rules override from LSP config
135        if let Some(disable) = &lsp_config.disable_rules
136            && !disable.is_empty()
137        {
138            let disable_set: std::collections::HashSet<String> = disable.iter().cloned().collect();
139            filtered_rules.retain(|rule| !disable_set.contains(rule.name()));
140        }
141
142        filtered_rules
143    }
144
145    /// Check if a file URI should be excluded based on exclude patterns
146    async fn should_exclude_uri(&self, uri: &Url) -> bool {
147        // Try to convert URI to file path
148        let file_path = match uri.to_file_path() {
149            Ok(path) => path,
150            Err(_) => return false, // If we can't get a path, don't exclude
151        };
152
153        // Resolve configuration for this specific file to get its exclude patterns
154        let rumdl_config = self.resolve_config_for_file(&file_path).await;
155        let exclude_patterns = &rumdl_config.global.exclude;
156
157        // If no exclude patterns, don't exclude
158        if exclude_patterns.is_empty() {
159            return false;
160        }
161
162        // Convert path to relative path for pattern matching
163        // This matches the CLI behavior in find_markdown_files
164        let path_to_check = if file_path.is_absolute() {
165            // Try to make it relative to the current directory
166            if let Ok(cwd) = std::env::current_dir() {
167                // Canonicalize both paths to handle symlinks
168                if let (Ok(canonical_cwd), Ok(canonical_path)) = (cwd.canonicalize(), file_path.canonicalize()) {
169                    if let Ok(relative) = canonical_path.strip_prefix(&canonical_cwd) {
170                        relative.to_string_lossy().to_string()
171                    } else {
172                        // Path is absolute but not under cwd
173                        file_path.to_string_lossy().to_string()
174                    }
175                } else {
176                    // Canonicalization failed
177                    file_path.to_string_lossy().to_string()
178                }
179            } else {
180                file_path.to_string_lossy().to_string()
181            }
182        } else {
183            // Already relative
184            file_path.to_string_lossy().to_string()
185        };
186
187        // Check if path matches any exclude pattern
188        for pattern in exclude_patterns {
189            if let Ok(glob) = globset::Glob::new(pattern) {
190                let matcher = glob.compile_matcher();
191                if matcher.is_match(&path_to_check) {
192                    log::debug!("Excluding file from LSP linting: {path_to_check}");
193                    return true;
194                }
195            }
196        }
197
198        false
199    }
200
201    /// Lint a document and return diagnostics
202    pub(crate) async fn lint_document(&self, uri: &Url, text: &str) -> Result<Vec<Diagnostic>> {
203        let config_guard = self.config.read().await;
204
205        // Skip linting if disabled
206        if !config_guard.enable_linting {
207            return Ok(Vec::new());
208        }
209
210        let lsp_config = config_guard.clone();
211        drop(config_guard); // Release config lock early
212
213        // Check if file should be excluded based on exclude patterns
214        if self.should_exclude_uri(uri).await {
215            return Ok(Vec::new());
216        }
217
218        // Resolve configuration for this specific file
219        let rumdl_config = if let Ok(file_path) = uri.to_file_path() {
220            self.resolve_config_for_file(&file_path).await
221        } else {
222            // Fallback to global config for non-file URIs
223            (*self.rumdl_config.read().await).clone()
224        };
225
226        let all_rules = rules::all_rules(&rumdl_config);
227        let flavor = rumdl_config.markdown_flavor();
228
229        // Use the standard filter_rules function which respects config's disabled rules
230        let mut filtered_rules = rules::filter_rules(&all_rules, &rumdl_config.global);
231
232        // Apply LSP config overrides (select_rules, ignore_rules from VSCode settings)
233        filtered_rules = self.apply_lsp_config_overrides(filtered_rules, &lsp_config);
234
235        // Run rumdl linting with the configured flavor
236        match crate::lint(text, &filtered_rules, false, flavor) {
237            Ok(warnings) => {
238                let diagnostics = warnings.iter().map(warning_to_diagnostic).collect();
239                Ok(diagnostics)
240            }
241            Err(e) => {
242                log::error!("Failed to lint document {uri}: {e}");
243                Ok(Vec::new())
244            }
245        }
246    }
247
248    /// Update diagnostics for a document
249    async fn update_diagnostics(&self, uri: Url, text: String) {
250        // Get the document version if available
251        let version = {
252            let docs = self.documents.read().await;
253            docs.get(&uri).and_then(|entry| entry.version)
254        };
255
256        match self.lint_document(&uri, &text).await {
257            Ok(diagnostics) => {
258                self.client.publish_diagnostics(uri, diagnostics, version).await;
259            }
260            Err(e) => {
261                log::error!("Failed to update diagnostics: {e}");
262            }
263        }
264    }
265
266    /// Apply all available fixes to a document
267    async fn apply_all_fixes(&self, uri: &Url, text: &str) -> Result<Option<String>> {
268        // Check if file should be excluded based on exclude patterns
269        if self.should_exclude_uri(uri).await {
270            return Ok(None);
271        }
272
273        let config_guard = self.config.read().await;
274        let lsp_config = config_guard.clone();
275        drop(config_guard);
276
277        // Resolve configuration for this specific file
278        let rumdl_config = if let Ok(file_path) = uri.to_file_path() {
279            self.resolve_config_for_file(&file_path).await
280        } else {
281            // Fallback to global config for non-file URIs
282            (*self.rumdl_config.read().await).clone()
283        };
284
285        let all_rules = rules::all_rules(&rumdl_config);
286        let flavor = rumdl_config.markdown_flavor();
287
288        // Use the standard filter_rules function which respects config's disabled rules
289        let mut filtered_rules = rules::filter_rules(&all_rules, &rumdl_config.global);
290
291        // Apply LSP config overrides (select_rules, ignore_rules from VSCode settings)
292        filtered_rules = self.apply_lsp_config_overrides(filtered_rules, &lsp_config);
293
294        // First, run lint to get active warnings (respecting ignore comments)
295        // This tells us which rules actually have unfixed issues
296        let mut rules_with_warnings = std::collections::HashSet::new();
297        let mut fixed_text = text.to_string();
298
299        match lint(&fixed_text, &filtered_rules, false, flavor) {
300            Ok(warnings) => {
301                for warning in warnings {
302                    if let Some(rule_name) = &warning.rule_name {
303                        rules_with_warnings.insert(rule_name.clone());
304                    }
305                }
306            }
307            Err(e) => {
308                log::warn!("Failed to lint document for auto-fix: {e}");
309                return Ok(None);
310            }
311        }
312
313        // Early return if no warnings to fix
314        if rules_with_warnings.is_empty() {
315            return Ok(None);
316        }
317
318        // Only apply fixes for rules that have active warnings
319        let mut any_changes = false;
320
321        for rule in &filtered_rules {
322            // Skip rules that don't have any active warnings
323            if !rules_with_warnings.contains(rule.name()) {
324                continue;
325            }
326
327            let ctx = crate::lint_context::LintContext::new(&fixed_text, flavor);
328            match rule.fix(&ctx) {
329                Ok(new_text) => {
330                    if new_text != fixed_text {
331                        fixed_text = new_text;
332                        any_changes = true;
333                    }
334                }
335                Err(e) => {
336                    // Only log if it's an actual error, not just "rule doesn't support auto-fix"
337                    let msg = e.to_string();
338                    if !msg.contains("does not support automatic fixing") {
339                        log::warn!("Failed to apply fix for rule {}: {}", rule.name(), e);
340                    }
341                }
342            }
343        }
344
345        if any_changes { Ok(Some(fixed_text)) } else { Ok(None) }
346    }
347
348    /// Get the end position of a document
349    fn get_end_position(&self, text: &str) -> Position {
350        let mut line = 0u32;
351        let mut character = 0u32;
352
353        for ch in text.chars() {
354            if ch == '\n' {
355                line += 1;
356                character = 0;
357            } else {
358                character += 1;
359            }
360        }
361
362        Position { line, character }
363    }
364
365    /// Get code actions for diagnostics at a position
366    async fn get_code_actions(&self, uri: &Url, text: &str, range: Range) -> Result<Vec<CodeAction>> {
367        let config_guard = self.config.read().await;
368        let lsp_config = config_guard.clone();
369        drop(config_guard);
370
371        // Resolve configuration for this specific file
372        let rumdl_config = if let Ok(file_path) = uri.to_file_path() {
373            self.resolve_config_for_file(&file_path).await
374        } else {
375            // Fallback to global config for non-file URIs
376            (*self.rumdl_config.read().await).clone()
377        };
378
379        let all_rules = rules::all_rules(&rumdl_config);
380        let flavor = rumdl_config.markdown_flavor();
381
382        // Use the standard filter_rules function which respects config's disabled rules
383        let mut filtered_rules = rules::filter_rules(&all_rules, &rumdl_config.global);
384
385        // Apply LSP config overrides (select_rules, ignore_rules from VSCode settings)
386        filtered_rules = self.apply_lsp_config_overrides(filtered_rules, &lsp_config);
387
388        match crate::lint(text, &filtered_rules, false, flavor) {
389            Ok(warnings) => {
390                let mut actions = Vec::new();
391                let mut fixable_count = 0;
392
393                for warning in &warnings {
394                    // Check if warning is within the requested range
395                    let warning_line = (warning.line.saturating_sub(1)) as u32;
396                    if warning_line >= range.start.line && warning_line <= range.end.line {
397                        // Get all code actions for this warning (fix + ignore actions)
398                        let mut warning_actions = warning_to_code_actions(warning, uri, text);
399                        actions.append(&mut warning_actions);
400
401                        if warning.fix.is_some() {
402                            fixable_count += 1;
403                        }
404                    }
405                }
406
407                // Add "Fix all" action if there are multiple fixable issues in range
408                if fixable_count > 1 {
409                    // Count total fixable issues in the document
410                    let total_fixable = warnings.iter().filter(|w| w.fix.is_some()).count();
411
412                    if let Ok(fixed_content) = crate::utils::fix_utils::apply_warning_fixes(text, &warnings)
413                        && fixed_content != text
414                    {
415                        // Calculate proper end position
416                        let mut line = 0u32;
417                        let mut character = 0u32;
418                        for ch in text.chars() {
419                            if ch == '\n' {
420                                line += 1;
421                                character = 0;
422                            } else {
423                                character += 1;
424                            }
425                        }
426
427                        let fix_all_action = CodeAction {
428                            title: format!("Fix all rumdl issues ({total_fixable} fixable)"),
429                            kind: Some(CodeActionKind::QUICKFIX),
430                            diagnostics: Some(Vec::new()),
431                            edit: Some(WorkspaceEdit {
432                                changes: Some(
433                                    [(
434                                        uri.clone(),
435                                        vec![TextEdit {
436                                            range: Range {
437                                                start: Position { line: 0, character: 0 },
438                                                end: Position { line, character },
439                                            },
440                                            new_text: fixed_content,
441                                        }],
442                                    )]
443                                    .into_iter()
444                                    .collect(),
445                                ),
446                                ..Default::default()
447                            }),
448                            command: None,
449                            is_preferred: Some(true),
450                            disabled: None,
451                            data: None,
452                        };
453
454                        // Insert at the beginning to make it prominent
455                        actions.insert(0, fix_all_action);
456                    }
457                }
458
459                Ok(actions)
460            }
461            Err(e) => {
462                log::error!("Failed to get code actions: {e}");
463                Ok(Vec::new())
464            }
465        }
466    }
467
468    /// Load or reload rumdl configuration from files
469    async fn load_configuration(&self, notify_client: bool) {
470        let config_guard = self.config.read().await;
471        let explicit_config_path = config_guard.config_path.clone();
472        drop(config_guard);
473
474        // Use the same discovery logic as CLI but with LSP-specific error handling
475        match Self::load_config_for_lsp(explicit_config_path.as_deref()) {
476            Ok(sourced_config) => {
477                let loaded_files = sourced_config.loaded_files.clone();
478                *self.rumdl_config.write().await = sourced_config.into();
479
480                if !loaded_files.is_empty() {
481                    let message = format!("Loaded rumdl config from: {}", loaded_files.join(", "));
482                    log::info!("{message}");
483                    if notify_client {
484                        self.client.log_message(MessageType::INFO, &message).await;
485                    }
486                } else {
487                    log::info!("Using default rumdl configuration (no config files found)");
488                }
489            }
490            Err(e) => {
491                let message = format!("Failed to load rumdl config: {e}");
492                log::warn!("{message}");
493                if notify_client {
494                    self.client.log_message(MessageType::WARNING, &message).await;
495                }
496                // Use default configuration
497                *self.rumdl_config.write().await = crate::config::Config::default();
498            }
499        }
500    }
501
502    /// Reload rumdl configuration from files (with client notification)
503    async fn reload_configuration(&self) {
504        self.load_configuration(true).await;
505    }
506
507    /// Load configuration for LSP - similar to CLI loading but returns Result
508    fn load_config_for_lsp(
509        config_path: Option<&str>,
510    ) -> Result<crate::config::SourcedConfig, crate::config::ConfigError> {
511        // Use the same configuration loading as the CLI
512        crate::config::SourcedConfig::load_with_discovery(config_path, None, false)
513    }
514
515    /// Resolve configuration for a specific file
516    ///
517    /// This method searches for a configuration file starting from the file's directory
518    /// and walking up the directory tree until a workspace root is hit or a config is found.
519    ///
520    /// Results are cached to avoid repeated filesystem access.
521    pub(crate) async fn resolve_config_for_file(&self, file_path: &std::path::Path) -> Config {
522        // Get the directory to start searching from
523        let search_dir = file_path.parent().unwrap_or(file_path).to_path_buf();
524
525        // Check cache first
526        {
527            let cache = self.config_cache.read().await;
528            if let Some(entry) = cache.get(&search_dir) {
529                let source_owned: String; // ensure owned storage for logging
530                let source: &str = if entry.from_global_fallback {
531                    "global/user fallback"
532                } else if let Some(path) = &entry.config_file {
533                    source_owned = path.to_string_lossy().to_string();
534                    &source_owned
535                } else {
536                    "<unknown>"
537                };
538                log::debug!(
539                    "Config cache hit for directory: {} (loaded from: {})",
540                    search_dir.display(),
541                    source
542                );
543                return entry.config.clone();
544            }
545        }
546
547        // Cache miss - need to search for config
548        log::debug!(
549            "Config cache miss for directory: {}, searching for config...",
550            search_dir.display()
551        );
552
553        // Try to find workspace root for this file
554        let workspace_root = {
555            let workspace_roots = self.workspace_roots.read().await;
556            workspace_roots
557                .iter()
558                .find(|root| search_dir.starts_with(root))
559                .map(|p| p.to_path_buf())
560        };
561
562        // Search upward from the file's directory
563        let mut current_dir = search_dir.clone();
564        let mut found_config: Option<(Config, Option<PathBuf>)> = None;
565
566        loop {
567            // Try to find a config file in the current directory
568            const CONFIG_FILES: &[&str] = &[".rumdl.toml", "rumdl.toml", "pyproject.toml", ".markdownlint.json"];
569
570            for config_file_name in CONFIG_FILES {
571                let config_path = current_dir.join(config_file_name);
572                if config_path.exists() {
573                    log::debug!("Found config file: {}", config_path.display());
574
575                    // Load the config
576                    if let Some(config_path_str) = config_path.to_str() {
577                        if let Ok(sourced) = Self::load_config_for_lsp(Some(config_path_str)) {
578                            found_config = Some((sourced.into(), Some(config_path)));
579                            break;
580                        }
581                    } else {
582                        log::warn!("Skipping config file with non-UTF-8 path: {}", config_path.display());
583                    }
584                }
585            }
586
587            if found_config.is_some() {
588                break;
589            }
590
591            // Check if we've hit a workspace root
592            if let Some(ref root) = workspace_root
593                && &current_dir == root
594            {
595                log::debug!("Hit workspace root without finding config: {}", root.display());
596                break;
597            }
598
599            // Move up to parent directory
600            if let Some(parent) = current_dir.parent() {
601                current_dir = parent.to_path_buf();
602            } else {
603                // Hit filesystem root
604                break;
605            }
606        }
607
608        // Use found config or fall back to global/user config loaded at initialization
609        let (config, config_file) = if let Some((cfg, path)) = found_config {
610            (cfg, path)
611        } else {
612            log::debug!("No project config found; using global/user fallback config");
613            let fallback = self.rumdl_config.read().await.clone();
614            (fallback, None)
615        };
616
617        // Cache the result
618        let from_global = config_file.is_none();
619        let entry = ConfigCacheEntry {
620            config: config.clone(),
621            config_file,
622            from_global_fallback: from_global,
623        };
624
625        self.config_cache.write().await.insert(search_dir, entry);
626
627        config
628    }
629}
630
631#[tower_lsp::async_trait]
632impl LanguageServer for RumdlLanguageServer {
633    async fn initialize(&self, params: InitializeParams) -> JsonRpcResult<InitializeResult> {
634        log::info!("Initializing rumdl Language Server");
635
636        // Parse client capabilities and configuration
637        if let Some(options) = params.initialization_options
638            && let Ok(config) = serde_json::from_value::<RumdlLspConfig>(options)
639        {
640            *self.config.write().await = config;
641        }
642
643        // Extract and store workspace roots
644        let mut roots = Vec::new();
645        if let Some(workspace_folders) = params.workspace_folders {
646            for folder in workspace_folders {
647                if let Ok(path) = folder.uri.to_file_path() {
648                    log::info!("Workspace root: {}", path.display());
649                    roots.push(path);
650                }
651            }
652        } else if let Some(root_uri) = params.root_uri
653            && let Ok(path) = root_uri.to_file_path()
654        {
655            log::info!("Workspace root: {}", path.display());
656            roots.push(path);
657        }
658        *self.workspace_roots.write().await = roots;
659
660        // Load rumdl configuration with auto-discovery (fallback/default)
661        self.load_configuration(false).await;
662
663        Ok(InitializeResult {
664            capabilities: ServerCapabilities {
665                text_document_sync: Some(TextDocumentSyncCapability::Options(TextDocumentSyncOptions {
666                    open_close: Some(true),
667                    change: Some(TextDocumentSyncKind::FULL),
668                    will_save: Some(false),
669                    will_save_wait_until: Some(true),
670                    save: Some(TextDocumentSyncSaveOptions::SaveOptions(SaveOptions {
671                        include_text: Some(false),
672                    })),
673                })),
674                code_action_provider: Some(CodeActionProviderCapability::Simple(true)),
675                document_formatting_provider: Some(OneOf::Left(true)),
676                document_range_formatting_provider: Some(OneOf::Left(true)),
677                diagnostic_provider: Some(DiagnosticServerCapabilities::Options(DiagnosticOptions {
678                    identifier: Some("rumdl".to_string()),
679                    inter_file_dependencies: false,
680                    workspace_diagnostics: false,
681                    work_done_progress_options: WorkDoneProgressOptions::default(),
682                })),
683                workspace: Some(WorkspaceServerCapabilities {
684                    workspace_folders: Some(WorkspaceFoldersServerCapabilities {
685                        supported: Some(true),
686                        change_notifications: Some(OneOf::Left(true)),
687                    }),
688                    file_operations: None,
689                }),
690                ..Default::default()
691            },
692            server_info: Some(ServerInfo {
693                name: "rumdl".to_string(),
694                version: Some(env!("CARGO_PKG_VERSION").to_string()),
695            }),
696        })
697    }
698
699    async fn initialized(&self, _: InitializedParams) {
700        let version = env!("CARGO_PKG_VERSION");
701
702        // Get binary path and build time
703        let (binary_path, build_time) = std::env::current_exe()
704            .ok()
705            .map(|path| {
706                let path_str = path.to_str().unwrap_or("unknown").to_string();
707                let build_time = std::fs::metadata(&path)
708                    .ok()
709                    .and_then(|metadata| metadata.modified().ok())
710                    .and_then(|modified| modified.duration_since(std::time::UNIX_EPOCH).ok())
711                    .and_then(|duration| {
712                        let secs = duration.as_secs();
713                        chrono::DateTime::from_timestamp(secs as i64, 0)
714                            .map(|dt| dt.format("%Y-%m-%d %H:%M:%S UTC").to_string())
715                    })
716                    .unwrap_or_else(|| "unknown".to_string());
717                (path_str, build_time)
718            })
719            .unwrap_or_else(|| ("unknown".to_string(), "unknown".to_string()));
720
721        let working_dir = std::env::current_dir()
722            .ok()
723            .and_then(|p| p.to_str().map(|s| s.to_string()))
724            .unwrap_or_else(|| "unknown".to_string());
725
726        log::info!("rumdl Language Server v{version} initialized (built: {build_time}, binary: {binary_path})");
727        log::info!("Working directory: {working_dir}");
728
729        self.client
730            .log_message(MessageType::INFO, format!("rumdl v{version} Language Server started"))
731            .await;
732    }
733
734    async fn did_change_workspace_folders(&self, params: DidChangeWorkspaceFoldersParams) {
735        // Update workspace roots
736        let mut roots = self.workspace_roots.write().await;
737
738        // Remove deleted workspace folders
739        for removed in &params.event.removed {
740            if let Ok(path) = removed.uri.to_file_path() {
741                roots.retain(|r| r != &path);
742                log::info!("Removed workspace root: {}", path.display());
743            }
744        }
745
746        // Add new workspace folders
747        for added in &params.event.added {
748            if let Ok(path) = added.uri.to_file_path()
749                && !roots.contains(&path)
750            {
751                log::info!("Added workspace root: {}", path.display());
752                roots.push(path);
753            }
754        }
755        drop(roots);
756
757        // Clear config cache as workspace structure changed
758        self.config_cache.write().await.clear();
759
760        // Reload fallback configuration
761        self.reload_configuration().await;
762    }
763
764    async fn shutdown(&self) -> JsonRpcResult<()> {
765        log::info!("Shutting down rumdl Language Server");
766        Ok(())
767    }
768
769    async fn did_open(&self, params: DidOpenTextDocumentParams) {
770        let uri = params.text_document.uri;
771        let text = params.text_document.text;
772        let version = params.text_document.version;
773
774        let entry = DocumentEntry {
775            content: text.clone(),
776            version: Some(version),
777            from_disk: false,
778        };
779        self.documents.write().await.insert(uri.clone(), entry);
780
781        self.update_diagnostics(uri, text).await;
782    }
783
784    async fn did_change(&self, params: DidChangeTextDocumentParams) {
785        let uri = params.text_document.uri;
786        let version = params.text_document.version;
787
788        if let Some(change) = params.content_changes.into_iter().next() {
789            let text = change.text;
790
791            let entry = DocumentEntry {
792                content: text.clone(),
793                version: Some(version),
794                from_disk: false,
795            };
796            self.documents.write().await.insert(uri.clone(), entry);
797
798            self.update_diagnostics(uri, text).await;
799        }
800    }
801
802    async fn will_save_wait_until(&self, params: WillSaveTextDocumentParams) -> JsonRpcResult<Option<Vec<TextEdit>>> {
803        let config_guard = self.config.read().await;
804        let enable_auto_fix = config_guard.enable_auto_fix;
805        drop(config_guard);
806
807        if !enable_auto_fix {
808            return Ok(None);
809        }
810
811        // Get the current document content
812        let Some(text) = self.get_document_content(&params.text_document.uri).await else {
813            return Ok(None);
814        };
815
816        // Apply all fixes
817        match self.apply_all_fixes(&params.text_document.uri, &text).await {
818            Ok(Some(fixed_text)) => {
819                // Return a single edit that replaces the entire document
820                Ok(Some(vec![TextEdit {
821                    range: Range {
822                        start: Position { line: 0, character: 0 },
823                        end: self.get_end_position(&text),
824                    },
825                    new_text: fixed_text,
826                }]))
827            }
828            Ok(None) => Ok(None),
829            Err(e) => {
830                log::error!("Failed to generate fixes in will_save_wait_until: {e}");
831                Ok(None)
832            }
833        }
834    }
835
836    async fn did_save(&self, params: DidSaveTextDocumentParams) {
837        // Re-lint the document after save
838        // Note: Auto-fixing is now handled by will_save_wait_until which runs before the save
839        if let Some(entry) = self.documents.read().await.get(&params.text_document.uri) {
840            self.update_diagnostics(params.text_document.uri, entry.content.clone())
841                .await;
842        }
843    }
844
845    async fn did_close(&self, params: DidCloseTextDocumentParams) {
846        // Remove document from storage
847        self.documents.write().await.remove(&params.text_document.uri);
848
849        // Clear diagnostics
850        self.client
851            .publish_diagnostics(params.text_document.uri, Vec::new(), None)
852            .await;
853    }
854
855    async fn did_change_watched_files(&self, params: DidChangeWatchedFilesParams) {
856        // Check if any of the changed files are config files
857        const CONFIG_FILES: &[&str] = &[".rumdl.toml", "rumdl.toml", "pyproject.toml", ".markdownlint.json"];
858
859        for change in &params.changes {
860            if let Ok(path) = change.uri.to_file_path()
861                && let Some(file_name) = path.file_name().and_then(|f| f.to_str())
862                && CONFIG_FILES.contains(&file_name)
863            {
864                log::info!("Config file changed: {}, invalidating config cache", path.display());
865
866                // Invalidate all cache entries that were loaded from this config file
867                let mut cache = self.config_cache.write().await;
868                cache.retain(|_, entry| {
869                    if let Some(config_file) = &entry.config_file {
870                        config_file != &path
871                    } else {
872                        true
873                    }
874                });
875
876                // Also reload the global fallback configuration
877                drop(cache);
878                self.reload_configuration().await;
879
880                // Re-lint all open documents
881                // First collect URIs and content to avoid holding lock during async operations
882                let docs_to_update: Vec<(Url, String)> = {
883                    let docs = self.documents.read().await;
884                    docs.iter()
885                        .filter(|(_, entry)| !entry.from_disk)
886                        .map(|(uri, entry)| (uri.clone(), entry.content.clone()))
887                        .collect()
888                };
889
890                // Now update diagnostics without holding the lock
891                for (uri, text) in docs_to_update {
892                    self.update_diagnostics(uri, text).await;
893                }
894
895                break;
896            }
897        }
898    }
899
900    async fn code_action(&self, params: CodeActionParams) -> JsonRpcResult<Option<CodeActionResponse>> {
901        let uri = params.text_document.uri;
902        let range = params.range;
903
904        if let Some(text) = self.get_document_content(&uri).await {
905            match self.get_code_actions(&uri, &text, range).await {
906                Ok(actions) => {
907                    let response: Vec<CodeActionOrCommand> =
908                        actions.into_iter().map(CodeActionOrCommand::CodeAction).collect();
909                    Ok(Some(response))
910                }
911                Err(e) => {
912                    log::error!("Failed to get code actions: {e}");
913                    Ok(None)
914                }
915            }
916        } else {
917            Ok(None)
918        }
919    }
920
921    async fn range_formatting(&self, params: DocumentRangeFormattingParams) -> JsonRpcResult<Option<Vec<TextEdit>>> {
922        // For markdown linting, we format the entire document because:
923        // 1. Many markdown rules have document-wide implications (e.g., heading hierarchy, list consistency)
924        // 2. Fixes often need surrounding context to be applied correctly
925        // 3. This approach is common among linters (ESLint, rustfmt, etc. do similar)
926        log::debug!(
927            "Range formatting requested for {:?}, formatting entire document due to rule interdependencies",
928            params.range
929        );
930
931        let formatting_params = DocumentFormattingParams {
932            text_document: params.text_document,
933            options: params.options,
934            work_done_progress_params: params.work_done_progress_params,
935        };
936
937        self.formatting(formatting_params).await
938    }
939
940    async fn formatting(&self, params: DocumentFormattingParams) -> JsonRpcResult<Option<Vec<TextEdit>>> {
941        let uri = params.text_document.uri;
942
943        log::debug!("Formatting request for: {uri}");
944
945        if let Some(text) = self.get_document_content(&uri).await {
946            // Get config with LSP overrides
947            let config_guard = self.config.read().await;
948            let lsp_config = config_guard.clone();
949            drop(config_guard);
950
951            // Resolve configuration for this specific file
952            let rumdl_config = if let Ok(file_path) = uri.to_file_path() {
953                self.resolve_config_for_file(&file_path).await
954            } else {
955                // Fallback to global config for non-file URIs
956                self.rumdl_config.read().await.clone()
957            };
958
959            let all_rules = rules::all_rules(&rumdl_config);
960            let flavor = rumdl_config.markdown_flavor();
961
962            // Use the standard filter_rules function which respects config's disabled rules
963            let mut filtered_rules = rules::filter_rules(&all_rules, &rumdl_config.global);
964
965            // Apply LSP config overrides
966            filtered_rules = self.apply_lsp_config_overrides(filtered_rules, &lsp_config);
967
968            // Use warning fixes for all rules
969            match crate::lint(&text, &filtered_rules, false, flavor) {
970                Ok(warnings) => {
971                    log::debug!(
972                        "Found {} warnings, {} with fixes",
973                        warnings.len(),
974                        warnings.iter().filter(|w| w.fix.is_some()).count()
975                    );
976
977                    let has_fixes = warnings.iter().any(|w| w.fix.is_some());
978                    if has_fixes {
979                        match crate::utils::fix_utils::apply_warning_fixes(&text, &warnings) {
980                            Ok(fixed_content) => {
981                                if fixed_content != text {
982                                    log::debug!("Returning formatting edits");
983                                    let end_position = self.get_end_position(&text);
984                                    let edit = TextEdit {
985                                        range: Range {
986                                            start: Position { line: 0, character: 0 },
987                                            end: end_position,
988                                        },
989                                        new_text: fixed_content,
990                                    };
991                                    return Ok(Some(vec![edit]));
992                                }
993                            }
994                            Err(e) => {
995                                log::error!("Failed to apply fixes: {e}");
996                            }
997                        }
998                    }
999                    Ok(Some(Vec::new()))
1000                }
1001                Err(e) => {
1002                    log::error!("Failed to format document: {e}");
1003                    Ok(Some(Vec::new()))
1004                }
1005            }
1006        } else {
1007            log::warn!("Document not found: {uri}");
1008            Ok(None)
1009        }
1010    }
1011
1012    async fn diagnostic(&self, params: DocumentDiagnosticParams) -> JsonRpcResult<DocumentDiagnosticReportResult> {
1013        let uri = params.text_document.uri;
1014
1015        if let Some(text) = self.get_document_content(&uri).await {
1016            match self.lint_document(&uri, &text).await {
1017                Ok(diagnostics) => Ok(DocumentDiagnosticReportResult::Report(DocumentDiagnosticReport::Full(
1018                    RelatedFullDocumentDiagnosticReport {
1019                        related_documents: None,
1020                        full_document_diagnostic_report: FullDocumentDiagnosticReport {
1021                            result_id: None,
1022                            items: diagnostics,
1023                        },
1024                    },
1025                ))),
1026                Err(e) => {
1027                    log::error!("Failed to get diagnostics: {e}");
1028                    Ok(DocumentDiagnosticReportResult::Report(DocumentDiagnosticReport::Full(
1029                        RelatedFullDocumentDiagnosticReport {
1030                            related_documents: None,
1031                            full_document_diagnostic_report: FullDocumentDiagnosticReport {
1032                                result_id: None,
1033                                items: Vec::new(),
1034                            },
1035                        },
1036                    )))
1037                }
1038            }
1039        } else {
1040            Ok(DocumentDiagnosticReportResult::Report(DocumentDiagnosticReport::Full(
1041                RelatedFullDocumentDiagnosticReport {
1042                    related_documents: None,
1043                    full_document_diagnostic_report: FullDocumentDiagnosticReport {
1044                        result_id: None,
1045                        items: Vec::new(),
1046                    },
1047                },
1048            )))
1049        }
1050    }
1051}
1052
1053#[cfg(test)]
1054mod tests {
1055    use super::*;
1056    use crate::rule::LintWarning;
1057    use tower_lsp::LspService;
1058
1059    fn create_test_server() -> RumdlLanguageServer {
1060        let (service, _socket) = LspService::new(RumdlLanguageServer::new);
1061        service.inner().clone()
1062    }
1063
1064    #[tokio::test]
1065    async fn test_server_creation() {
1066        let server = create_test_server();
1067
1068        // Verify default configuration
1069        let config = server.config.read().await;
1070        assert!(config.enable_linting);
1071        assert!(!config.enable_auto_fix);
1072    }
1073
1074    #[tokio::test]
1075    async fn test_lint_document() {
1076        let server = create_test_server();
1077
1078        // Test linting with a simple markdown document
1079        let uri = Url::parse("file:///test.md").unwrap();
1080        let text = "# Test\n\nThis is a test  \nWith trailing spaces  ";
1081
1082        let diagnostics = server.lint_document(&uri, text).await.unwrap();
1083
1084        // Should find trailing spaces violations
1085        assert!(!diagnostics.is_empty());
1086        assert!(diagnostics.iter().any(|d| d.message.contains("trailing")));
1087    }
1088
1089    #[tokio::test]
1090    async fn test_lint_document_disabled() {
1091        let server = create_test_server();
1092
1093        // Disable linting
1094        server.config.write().await.enable_linting = false;
1095
1096        let uri = Url::parse("file:///test.md").unwrap();
1097        let text = "# Test\n\nThis is a test  \nWith trailing spaces  ";
1098
1099        let diagnostics = server.lint_document(&uri, text).await.unwrap();
1100
1101        // Should return empty diagnostics when disabled
1102        assert!(diagnostics.is_empty());
1103    }
1104
1105    #[tokio::test]
1106    async fn test_get_code_actions() {
1107        let server = create_test_server();
1108
1109        let uri = Url::parse("file:///test.md").unwrap();
1110        let text = "# Test\n\nThis is a test  \nWith trailing spaces  ";
1111
1112        // Create a range covering the whole document
1113        let range = Range {
1114            start: Position { line: 0, character: 0 },
1115            end: Position { line: 3, character: 21 },
1116        };
1117
1118        let actions = server.get_code_actions(&uri, text, range).await.unwrap();
1119
1120        // Should have code actions for fixing trailing spaces
1121        assert!(!actions.is_empty());
1122        assert!(actions.iter().any(|a| a.title.contains("trailing")));
1123    }
1124
1125    #[tokio::test]
1126    async fn test_get_code_actions_outside_range() {
1127        let server = create_test_server();
1128
1129        let uri = Url::parse("file:///test.md").unwrap();
1130        let text = "# Test\n\nThis is a test  \nWith trailing spaces  ";
1131
1132        // Create a range that doesn't cover the violations
1133        let range = Range {
1134            start: Position { line: 0, character: 0 },
1135            end: Position { line: 0, character: 6 },
1136        };
1137
1138        let actions = server.get_code_actions(&uri, text, range).await.unwrap();
1139
1140        // Should have no code actions for this range
1141        assert!(actions.is_empty());
1142    }
1143
1144    #[tokio::test]
1145    async fn test_document_storage() {
1146        let server = create_test_server();
1147
1148        let uri = Url::parse("file:///test.md").unwrap();
1149        let text = "# Test Document";
1150
1151        // Store document
1152        let entry = DocumentEntry {
1153            content: text.to_string(),
1154            version: Some(1),
1155            from_disk: false,
1156        };
1157        server.documents.write().await.insert(uri.clone(), entry);
1158
1159        // Verify storage
1160        let stored = server.documents.read().await.get(&uri).map(|e| e.content.clone());
1161        assert_eq!(stored, Some(text.to_string()));
1162
1163        // Remove document
1164        server.documents.write().await.remove(&uri);
1165
1166        // Verify removal
1167        let stored = server.documents.read().await.get(&uri).cloned();
1168        assert_eq!(stored, None);
1169    }
1170
1171    #[tokio::test]
1172    async fn test_configuration_loading() {
1173        let server = create_test_server();
1174
1175        // Load configuration with auto-discovery
1176        server.load_configuration(false).await;
1177
1178        // Verify configuration was loaded successfully
1179        // The config could be from: .rumdl.toml, pyproject.toml, .markdownlint.json, or default
1180        let rumdl_config = server.rumdl_config.read().await;
1181        // The loaded config is valid regardless of source
1182        drop(rumdl_config); // Just verify we can access it without panic
1183    }
1184
1185    #[tokio::test]
1186    async fn test_load_config_for_lsp() {
1187        // Test with no config file
1188        let result = RumdlLanguageServer::load_config_for_lsp(None);
1189        assert!(result.is_ok());
1190
1191        // Test with non-existent config file
1192        let result = RumdlLanguageServer::load_config_for_lsp(Some("/nonexistent/config.toml"));
1193        assert!(result.is_err());
1194    }
1195
1196    #[tokio::test]
1197    async fn test_warning_conversion() {
1198        let warning = LintWarning {
1199            message: "Test warning".to_string(),
1200            line: 1,
1201            column: 1,
1202            end_line: 1,
1203            end_column: 10,
1204            severity: crate::rule::Severity::Warning,
1205            fix: None,
1206            rule_name: Some("MD001".to_string()),
1207        };
1208
1209        // Test diagnostic conversion
1210        let diagnostic = warning_to_diagnostic(&warning);
1211        assert_eq!(diagnostic.message, "Test warning");
1212        assert_eq!(diagnostic.severity, Some(DiagnosticSeverity::WARNING));
1213        assert_eq!(diagnostic.code, Some(NumberOrString::String("MD001".to_string())));
1214
1215        // Test code action conversion (no fix, but should have ignore action)
1216        let uri = Url::parse("file:///test.md").unwrap();
1217        let actions = warning_to_code_actions(&warning, &uri, "Test content");
1218        // Should have 1 action: ignore-line (no fix available)
1219        assert_eq!(actions.len(), 1);
1220        assert_eq!(actions[0].title, "Ignore MD001 for this line");
1221    }
1222
1223    #[tokio::test]
1224    async fn test_multiple_documents() {
1225        let server = create_test_server();
1226
1227        let uri1 = Url::parse("file:///test1.md").unwrap();
1228        let uri2 = Url::parse("file:///test2.md").unwrap();
1229        let text1 = "# Document 1";
1230        let text2 = "# Document 2";
1231
1232        // Store multiple documents
1233        {
1234            let mut docs = server.documents.write().await;
1235            let entry1 = DocumentEntry {
1236                content: text1.to_string(),
1237                version: Some(1),
1238                from_disk: false,
1239            };
1240            let entry2 = DocumentEntry {
1241                content: text2.to_string(),
1242                version: Some(1),
1243                from_disk: false,
1244            };
1245            docs.insert(uri1.clone(), entry1);
1246            docs.insert(uri2.clone(), entry2);
1247        }
1248
1249        // Verify both are stored
1250        let docs = server.documents.read().await;
1251        assert_eq!(docs.len(), 2);
1252        assert_eq!(docs.get(&uri1).map(|s| s.content.as_str()), Some(text1));
1253        assert_eq!(docs.get(&uri2).map(|s| s.content.as_str()), Some(text2));
1254    }
1255
1256    #[tokio::test]
1257    async fn test_auto_fix_on_save() {
1258        let server = create_test_server();
1259
1260        // Enable auto-fix
1261        {
1262            let mut config = server.config.write().await;
1263            config.enable_auto_fix = true;
1264        }
1265
1266        let uri = Url::parse("file:///test.md").unwrap();
1267        let text = "#Heading without space"; // MD018 violation
1268
1269        // Store document
1270        let entry = DocumentEntry {
1271            content: text.to_string(),
1272            version: Some(1),
1273            from_disk: false,
1274        };
1275        server.documents.write().await.insert(uri.clone(), entry);
1276
1277        // Test apply_all_fixes
1278        let fixed = server.apply_all_fixes(&uri, text).await.unwrap();
1279        assert!(fixed.is_some());
1280        // MD018 adds space, MD047 adds trailing newline
1281        assert_eq!(fixed.unwrap(), "# Heading without space\n");
1282    }
1283
1284    #[tokio::test]
1285    async fn test_get_end_position() {
1286        let server = create_test_server();
1287
1288        // Single line
1289        let pos = server.get_end_position("Hello");
1290        assert_eq!(pos.line, 0);
1291        assert_eq!(pos.character, 5);
1292
1293        // Multiple lines
1294        let pos = server.get_end_position("Hello\nWorld\nTest");
1295        assert_eq!(pos.line, 2);
1296        assert_eq!(pos.character, 4);
1297
1298        // Empty string
1299        let pos = server.get_end_position("");
1300        assert_eq!(pos.line, 0);
1301        assert_eq!(pos.character, 0);
1302
1303        // Ends with newline - position should be at start of next line
1304        let pos = server.get_end_position("Hello\n");
1305        assert_eq!(pos.line, 1);
1306        assert_eq!(pos.character, 0);
1307    }
1308
1309    #[tokio::test]
1310    async fn test_empty_document_handling() {
1311        let server = create_test_server();
1312
1313        let uri = Url::parse("file:///empty.md").unwrap();
1314        let text = "";
1315
1316        // Test linting empty document
1317        let diagnostics = server.lint_document(&uri, text).await.unwrap();
1318        assert!(diagnostics.is_empty());
1319
1320        // Test code actions on empty document
1321        let range = Range {
1322            start: Position { line: 0, character: 0 },
1323            end: Position { line: 0, character: 0 },
1324        };
1325        let actions = server.get_code_actions(&uri, text, range).await.unwrap();
1326        assert!(actions.is_empty());
1327    }
1328
1329    #[tokio::test]
1330    async fn test_config_update() {
1331        let server = create_test_server();
1332
1333        // Update config
1334        {
1335            let mut config = server.config.write().await;
1336            config.enable_auto_fix = true;
1337            config.config_path = Some("/custom/path.toml".to_string());
1338        }
1339
1340        // Verify update
1341        let config = server.config.read().await;
1342        assert!(config.enable_auto_fix);
1343        assert_eq!(config.config_path, Some("/custom/path.toml".to_string()));
1344    }
1345
1346    #[tokio::test]
1347    async fn test_document_formatting() {
1348        let server = create_test_server();
1349        let uri = Url::parse("file:///test.md").unwrap();
1350        let text = "# Test\n\nThis is a test  \nWith trailing spaces  ";
1351
1352        // Store document
1353        let entry = DocumentEntry {
1354            content: text.to_string(),
1355            version: Some(1),
1356            from_disk: false,
1357        };
1358        server.documents.write().await.insert(uri.clone(), entry);
1359
1360        // Create formatting params
1361        let params = DocumentFormattingParams {
1362            text_document: TextDocumentIdentifier { uri: uri.clone() },
1363            options: FormattingOptions {
1364                tab_size: 4,
1365                insert_spaces: true,
1366                properties: HashMap::new(),
1367                trim_trailing_whitespace: Some(true),
1368                insert_final_newline: Some(true),
1369                trim_final_newlines: Some(true),
1370            },
1371            work_done_progress_params: WorkDoneProgressParams::default(),
1372        };
1373
1374        // Call formatting
1375        let result = server.formatting(params).await.unwrap();
1376
1377        // Should return text edits that fix the trailing spaces
1378        assert!(result.is_some());
1379        let edits = result.unwrap();
1380        assert!(!edits.is_empty());
1381
1382        // The new text should have trailing spaces removed
1383        let edit = &edits[0];
1384        // The formatted text should have the trailing spaces removed from the middle line
1385        // and a final newline added
1386        let expected = "# Test\n\nThis is a test  \nWith trailing spaces\n";
1387        assert_eq!(edit.new_text, expected);
1388    }
1389
1390    /// Test that resolve_config_for_file() finds the correct config in multi-root workspace
1391    #[tokio::test]
1392    async fn test_resolve_config_for_file_multi_root() {
1393        use std::fs;
1394        use tempfile::tempdir;
1395
1396        let temp_dir = tempdir().unwrap();
1397        let temp_path = temp_dir.path();
1398
1399        // Setup project A with line_length=60
1400        let project_a = temp_path.join("project_a");
1401        let project_a_docs = project_a.join("docs");
1402        fs::create_dir_all(&project_a_docs).unwrap();
1403
1404        let config_a = project_a.join(".rumdl.toml");
1405        fs::write(
1406            &config_a,
1407            r#"
1408[global]
1409
1410[MD013]
1411line_length = 60
1412"#,
1413        )
1414        .unwrap();
1415
1416        // Setup project B with line_length=120
1417        let project_b = temp_path.join("project_b");
1418        fs::create_dir(&project_b).unwrap();
1419
1420        let config_b = project_b.join(".rumdl.toml");
1421        fs::write(
1422            &config_b,
1423            r#"
1424[global]
1425
1426[MD013]
1427line_length = 120
1428"#,
1429        )
1430        .unwrap();
1431
1432        // Create LSP server and initialize with workspace roots
1433        let server = create_test_server();
1434
1435        // Set workspace roots
1436        {
1437            let mut roots = server.workspace_roots.write().await;
1438            roots.push(project_a.clone());
1439            roots.push(project_b.clone());
1440        }
1441
1442        // Test file in project A
1443        let file_a = project_a_docs.join("test.md");
1444        fs::write(&file_a, "# Test A\n").unwrap();
1445
1446        let config_for_a = server.resolve_config_for_file(&file_a).await;
1447        let line_length_a = crate::config::get_rule_config_value::<usize>(&config_for_a, "MD013", "line_length");
1448        assert_eq!(line_length_a, Some(60), "File in project_a should get line_length=60");
1449
1450        // Test file in project B
1451        let file_b = project_b.join("test.md");
1452        fs::write(&file_b, "# Test B\n").unwrap();
1453
1454        let config_for_b = server.resolve_config_for_file(&file_b).await;
1455        let line_length_b = crate::config::get_rule_config_value::<usize>(&config_for_b, "MD013", "line_length");
1456        assert_eq!(line_length_b, Some(120), "File in project_b should get line_length=120");
1457    }
1458
1459    /// Test that config resolution respects workspace root boundaries
1460    #[tokio::test]
1461    async fn test_config_resolution_respects_workspace_boundaries() {
1462        use std::fs;
1463        use tempfile::tempdir;
1464
1465        let temp_dir = tempdir().unwrap();
1466        let temp_path = temp_dir.path();
1467
1468        // Create parent config that should NOT be used
1469        let parent_config = temp_path.join(".rumdl.toml");
1470        fs::write(
1471            &parent_config,
1472            r#"
1473[global]
1474
1475[MD013]
1476line_length = 80
1477"#,
1478        )
1479        .unwrap();
1480
1481        // Create workspace root with its own config
1482        let workspace_root = temp_path.join("workspace");
1483        let workspace_subdir = workspace_root.join("subdir");
1484        fs::create_dir_all(&workspace_subdir).unwrap();
1485
1486        let workspace_config = workspace_root.join(".rumdl.toml");
1487        fs::write(
1488            &workspace_config,
1489            r#"
1490[global]
1491
1492[MD013]
1493line_length = 100
1494"#,
1495        )
1496        .unwrap();
1497
1498        let server = create_test_server();
1499
1500        // Register workspace_root as a workspace root
1501        {
1502            let mut roots = server.workspace_roots.write().await;
1503            roots.push(workspace_root.clone());
1504        }
1505
1506        // Test file deep in subdirectory
1507        let test_file = workspace_subdir.join("deep").join("test.md");
1508        fs::create_dir_all(test_file.parent().unwrap()).unwrap();
1509        fs::write(&test_file, "# Test\n").unwrap();
1510
1511        let config = server.resolve_config_for_file(&test_file).await;
1512        let line_length = crate::config::get_rule_config_value::<usize>(&config, "MD013", "line_length");
1513
1514        // Should find workspace_root/.rumdl.toml (100), NOT parent config (80)
1515        assert_eq!(
1516            line_length,
1517            Some(100),
1518            "Should find workspace config, not parent config outside workspace"
1519        );
1520    }
1521
1522    /// Test that config cache works (cache hit scenario)
1523    #[tokio::test]
1524    async fn test_config_cache_hit() {
1525        use std::fs;
1526        use tempfile::tempdir;
1527
1528        let temp_dir = tempdir().unwrap();
1529        let temp_path = temp_dir.path();
1530
1531        let project = temp_path.join("project");
1532        fs::create_dir(&project).unwrap();
1533
1534        let config_file = project.join(".rumdl.toml");
1535        fs::write(
1536            &config_file,
1537            r#"
1538[global]
1539
1540[MD013]
1541line_length = 75
1542"#,
1543        )
1544        .unwrap();
1545
1546        let server = create_test_server();
1547        {
1548            let mut roots = server.workspace_roots.write().await;
1549            roots.push(project.clone());
1550        }
1551
1552        let test_file = project.join("test.md");
1553        fs::write(&test_file, "# Test\n").unwrap();
1554
1555        // First call - cache miss
1556        let config1 = server.resolve_config_for_file(&test_file).await;
1557        let line_length1 = crate::config::get_rule_config_value::<usize>(&config1, "MD013", "line_length");
1558        assert_eq!(line_length1, Some(75));
1559
1560        // Verify cache was populated
1561        {
1562            let cache = server.config_cache.read().await;
1563            let search_dir = test_file.parent().unwrap();
1564            assert!(
1565                cache.contains_key(search_dir),
1566                "Cache should be populated after first call"
1567            );
1568        }
1569
1570        // Second call - cache hit (should return same config without filesystem access)
1571        let config2 = server.resolve_config_for_file(&test_file).await;
1572        let line_length2 = crate::config::get_rule_config_value::<usize>(&config2, "MD013", "line_length");
1573        assert_eq!(line_length2, Some(75));
1574    }
1575
1576    /// Test nested directory config search (file searches upward)
1577    #[tokio::test]
1578    async fn test_nested_directory_config_search() {
1579        use std::fs;
1580        use tempfile::tempdir;
1581
1582        let temp_dir = tempdir().unwrap();
1583        let temp_path = temp_dir.path();
1584
1585        let project = temp_path.join("project");
1586        fs::create_dir(&project).unwrap();
1587
1588        // Config at project root
1589        let config = project.join(".rumdl.toml");
1590        fs::write(
1591            &config,
1592            r#"
1593[global]
1594
1595[MD013]
1596line_length = 110
1597"#,
1598        )
1599        .unwrap();
1600
1601        // File deep in nested structure
1602        let deep_dir = project.join("src").join("docs").join("guides");
1603        fs::create_dir_all(&deep_dir).unwrap();
1604        let deep_file = deep_dir.join("test.md");
1605        fs::write(&deep_file, "# Test\n").unwrap();
1606
1607        let server = create_test_server();
1608        {
1609            let mut roots = server.workspace_roots.write().await;
1610            roots.push(project.clone());
1611        }
1612
1613        let resolved_config = server.resolve_config_for_file(&deep_file).await;
1614        let line_length = crate::config::get_rule_config_value::<usize>(&resolved_config, "MD013", "line_length");
1615
1616        assert_eq!(
1617            line_length,
1618            Some(110),
1619            "Should find config by searching upward from deep directory"
1620        );
1621    }
1622
1623    /// Test fallback to default config when no config file found
1624    #[tokio::test]
1625    async fn test_fallback_to_default_config() {
1626        use std::fs;
1627        use tempfile::tempdir;
1628
1629        let temp_dir = tempdir().unwrap();
1630        let temp_path = temp_dir.path();
1631
1632        let project = temp_path.join("project");
1633        fs::create_dir(&project).unwrap();
1634
1635        // No config file created!
1636
1637        let test_file = project.join("test.md");
1638        fs::write(&test_file, "# Test\n").unwrap();
1639
1640        let server = create_test_server();
1641        {
1642            let mut roots = server.workspace_roots.write().await;
1643            roots.push(project.clone());
1644        }
1645
1646        let config = server.resolve_config_for_file(&test_file).await;
1647
1648        // Default global line_length is 80
1649        assert_eq!(
1650            config.global.line_length, 80,
1651            "Should fall back to default config when no config file found"
1652        );
1653    }
1654
1655    /// Test config priority: closer config wins over parent config
1656    #[tokio::test]
1657    async fn test_config_priority_closer_wins() {
1658        use std::fs;
1659        use tempfile::tempdir;
1660
1661        let temp_dir = tempdir().unwrap();
1662        let temp_path = temp_dir.path();
1663
1664        let project = temp_path.join("project");
1665        fs::create_dir(&project).unwrap();
1666
1667        // Parent config
1668        let parent_config = project.join(".rumdl.toml");
1669        fs::write(
1670            &parent_config,
1671            r#"
1672[global]
1673
1674[MD013]
1675line_length = 100
1676"#,
1677        )
1678        .unwrap();
1679
1680        // Subdirectory with its own config (should override parent)
1681        let subdir = project.join("subdir");
1682        fs::create_dir(&subdir).unwrap();
1683
1684        let subdir_config = subdir.join(".rumdl.toml");
1685        fs::write(
1686            &subdir_config,
1687            r#"
1688[global]
1689
1690[MD013]
1691line_length = 50
1692"#,
1693        )
1694        .unwrap();
1695
1696        let server = create_test_server();
1697        {
1698            let mut roots = server.workspace_roots.write().await;
1699            roots.push(project.clone());
1700        }
1701
1702        // File in subdirectory
1703        let test_file = subdir.join("test.md");
1704        fs::write(&test_file, "# Test\n").unwrap();
1705
1706        let config = server.resolve_config_for_file(&test_file).await;
1707        let line_length = crate::config::get_rule_config_value::<usize>(&config, "MD013", "line_length");
1708
1709        assert_eq!(
1710            line_length,
1711            Some(50),
1712            "Closer config (subdir) should override parent config"
1713        );
1714    }
1715}