Skip to main content

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 futures::future::join_all;
11use tokio::sync::{RwLock, mpsc};
12use tower_lsp::jsonrpc::Result as JsonRpcResult;
13use tower_lsp::lsp_types::*;
14use tower_lsp::{Client, LanguageServer};
15
16use crate::config::{Config, is_valid_rule_name};
17use crate::lsp::index_worker::IndexWorker;
18use crate::lsp::types::{IndexState, IndexUpdate, LspRuleSettings, RumdlLspConfig};
19use crate::rule::FixCapability;
20use crate::rules;
21use crate::workspace_index::WorkspaceIndex;
22
23/// Supported markdown file extensions (without leading dot)
24const MARKDOWN_EXTENSIONS: &[&str] = &["md", "markdown", "mdx", "mkd", "mkdn", "mdown", "mdwn", "qmd", "rmd"];
25
26/// Maximum number of rules in enable/disable lists (DoS protection)
27const MAX_RULE_LIST_SIZE: usize = 100;
28
29/// Maximum allowed line length value (DoS protection)
30const MAX_LINE_LENGTH: usize = 10_000;
31
32/// Check if a file extension is a markdown extension
33#[inline]
34fn is_markdown_extension(ext: &str) -> bool {
35    MARKDOWN_EXTENSIONS.contains(&ext.to_lowercase().as_str())
36}
37
38/// Represents a document in the LSP server's cache
39#[derive(Clone, Debug, PartialEq)]
40pub(crate) struct DocumentEntry {
41    /// The document content
42    pub(crate) content: String,
43    /// Version number from the editor (None for disk-loaded documents)
44    pub(crate) version: Option<i32>,
45    /// Whether the document was loaded from disk (true) or opened in editor (false)
46    pub(crate) from_disk: bool,
47}
48
49/// Cache entry for resolved configuration
50#[derive(Clone, Debug)]
51pub(crate) struct ConfigCacheEntry {
52    /// The resolved configuration
53    pub(crate) config: Config,
54    /// Config file path that was loaded (for invalidation)
55    pub(crate) config_file: Option<PathBuf>,
56    /// True if this entry came from the global/user fallback (no project config)
57    pub(crate) from_global_fallback: bool,
58}
59
60/// Main LSP server for rumdl
61///
62/// Following Ruff's pattern, this server provides:
63/// - Real-time diagnostics as users type
64/// - Code actions for automatic fixes
65/// - Configuration management
66/// - Multi-file support
67/// - Multi-root workspace support with per-file config resolution
68/// - Cross-file analysis with workspace indexing
69#[derive(Clone)]
70pub struct RumdlLanguageServer {
71    pub(crate) client: Client,
72    /// Configuration for the LSP server
73    pub(crate) config: Arc<RwLock<RumdlLspConfig>>,
74    /// Rumdl core configuration (fallback/default)
75    pub(crate) rumdl_config: Arc<RwLock<Config>>,
76    /// Document store for open files and cached disk files
77    pub(crate) documents: Arc<RwLock<HashMap<Url, DocumentEntry>>>,
78    /// Workspace root folders from the client
79    pub(crate) workspace_roots: Arc<RwLock<Vec<PathBuf>>>,
80    /// Configuration cache: maps directory path to resolved config
81    /// Key is the directory where config search started (file's parent dir)
82    pub(crate) config_cache: Arc<RwLock<HashMap<PathBuf, ConfigCacheEntry>>>,
83    /// Workspace index for cross-file analysis (MD051)
84    pub(crate) workspace_index: Arc<RwLock<WorkspaceIndex>>,
85    /// Current state of the workspace index (building/ready/error)
86    pub(crate) index_state: Arc<RwLock<IndexState>>,
87    /// Channel to send updates to the background index worker
88    pub(crate) update_tx: mpsc::Sender<IndexUpdate>,
89    /// Whether the client supports pull diagnostics (textDocument/diagnostic)
90    /// When true, we skip pushing diagnostics to avoid duplicates
91    pub(crate) client_supports_pull_diagnostics: Arc<RwLock<bool>>,
92}
93
94impl RumdlLanguageServer {
95    pub fn new(client: Client, cli_config_path: Option<&str>) -> Self {
96        // Initialize with CLI config path if provided (for `rumdl server --config` convenience)
97        let mut initial_config = RumdlLspConfig::default();
98        if let Some(path) = cli_config_path {
99            initial_config.config_path = Some(path.to_string());
100        }
101
102        // Create shared state for workspace indexing
103        let workspace_index = Arc::new(RwLock::new(WorkspaceIndex::new()));
104        let index_state = Arc::new(RwLock::new(IndexState::default()));
105        let workspace_roots = Arc::new(RwLock::new(Vec::new()));
106
107        // Create channels for index worker communication
108        let (update_tx, update_rx) = mpsc::channel::<IndexUpdate>(100);
109        let (relint_tx, _relint_rx) = mpsc::channel::<PathBuf>(100);
110
111        // Spawn the background index worker
112        let worker = IndexWorker::new(
113            update_rx,
114            workspace_index.clone(),
115            index_state.clone(),
116            client.clone(),
117            workspace_roots.clone(),
118            relint_tx,
119        );
120        tokio::spawn(worker.run());
121
122        Self {
123            client,
124            config: Arc::new(RwLock::new(initial_config)),
125            rumdl_config: Arc::new(RwLock::new(Config::default())),
126            documents: Arc::new(RwLock::new(HashMap::new())),
127            workspace_roots,
128            config_cache: Arc::new(RwLock::new(HashMap::new())),
129            workspace_index,
130            index_state,
131            update_tx,
132            client_supports_pull_diagnostics: Arc::new(RwLock::new(false)),
133        }
134    }
135
136    /// Get document content, either from cache or by reading from disk
137    ///
138    /// This method first checks if the document is in the cache (opened in editor).
139    /// If not found, it attempts to read the file from disk and caches it for
140    /// future requests.
141    pub(super) async fn get_document_content(&self, uri: &Url) -> Option<String> {
142        // First check the cache
143        {
144            let docs = self.documents.read().await;
145            if let Some(entry) = docs.get(uri) {
146                return Some(entry.content.clone());
147            }
148        }
149
150        // If not in cache and it's a file URI, try to read from disk
151        if let Ok(path) = uri.to_file_path() {
152            if let Ok(content) = tokio::fs::read_to_string(&path).await {
153                // Cache the document for future requests
154                let entry = DocumentEntry {
155                    content: content.clone(),
156                    version: None,
157                    from_disk: true,
158                };
159
160                let mut docs = self.documents.write().await;
161                docs.insert(uri.clone(), entry);
162
163                log::debug!("Loaded document from disk and cached: {uri}");
164                return Some(content);
165            } else {
166                log::debug!("Failed to read file from disk: {uri}");
167            }
168        }
169
170        None
171    }
172
173    /// Get document content only if the document is currently open in the editor.
174    ///
175    /// We intentionally do not read from disk here because diagnostics should be
176    /// scoped to open documents. This avoids lingering diagnostics after a file
177    /// is closed when clients use pull diagnostics.
178    async fn get_open_document_content(&self, uri: &Url) -> Option<String> {
179        let docs = self.documents.read().await;
180        docs.get(uri)
181            .and_then(|entry| (!entry.from_disk).then(|| entry.content.clone()))
182    }
183}
184
185#[tower_lsp::async_trait]
186impl LanguageServer for RumdlLanguageServer {
187    async fn initialize(&self, params: InitializeParams) -> JsonRpcResult<InitializeResult> {
188        log::info!("Initializing rumdl Language Server");
189
190        // Parse client capabilities and configuration
191        if let Some(options) = params.initialization_options
192            && let Ok(config) = serde_json::from_value::<RumdlLspConfig>(options)
193        {
194            *self.config.write().await = config;
195        }
196
197        // Detect if client supports pull diagnostics (textDocument/diagnostic)
198        // When the client supports pull, we avoid pushing to prevent duplicate diagnostics
199        let supports_pull = params
200            .capabilities
201            .text_document
202            .as_ref()
203            .and_then(|td| td.diagnostic.as_ref())
204            .is_some();
205
206        if supports_pull {
207            log::info!("Client supports pull diagnostics - disabling push to avoid duplicates");
208            *self.client_supports_pull_diagnostics.write().await = true;
209        } else {
210            log::info!("Client does not support pull diagnostics - using push model");
211        }
212
213        // Extract and store workspace roots
214        let mut roots = Vec::new();
215        if let Some(workspace_folders) = params.workspace_folders {
216            for folder in workspace_folders {
217                if let Ok(path) = folder.uri.to_file_path() {
218                    let path = path.canonicalize().unwrap_or(path);
219                    log::info!("Workspace root: {}", path.display());
220                    roots.push(path);
221                }
222            }
223        } else if let Some(root_uri) = params.root_uri
224            && let Ok(path) = root_uri.to_file_path()
225        {
226            let path = path.canonicalize().unwrap_or(path);
227            log::info!("Workspace root: {}", path.display());
228            roots.push(path);
229        }
230        *self.workspace_roots.write().await = roots;
231
232        // Load rumdl configuration with auto-discovery (fallback/default)
233        self.load_configuration(false).await;
234
235        let enable_link_navigation = self.config.read().await.enable_link_navigation;
236
237        Ok(InitializeResult {
238            capabilities: ServerCapabilities {
239                text_document_sync: Some(TextDocumentSyncCapability::Options(TextDocumentSyncOptions {
240                    open_close: Some(true),
241                    change: Some(TextDocumentSyncKind::FULL),
242                    will_save: Some(false),
243                    will_save_wait_until: Some(true),
244                    save: Some(TextDocumentSyncSaveOptions::SaveOptions(SaveOptions {
245                        include_text: Some(false),
246                    })),
247                })),
248                code_action_provider: Some(CodeActionProviderCapability::Options(CodeActionOptions {
249                    code_action_kinds: Some(vec![
250                        CodeActionKind::QUICKFIX,
251                        CodeActionKind::SOURCE_FIX_ALL,
252                        CodeActionKind::new("source.fixAll.rumdl"),
253                    ]),
254                    work_done_progress_options: WorkDoneProgressOptions::default(),
255                    resolve_provider: None,
256                })),
257                document_formatting_provider: Some(OneOf::Left(true)),
258                document_range_formatting_provider: Some(OneOf::Left(true)),
259                diagnostic_provider: Some(DiagnosticServerCapabilities::Options(DiagnosticOptions {
260                    identifier: Some("rumdl".to_string()),
261                    inter_file_dependencies: true,
262                    workspace_diagnostics: false,
263                    work_done_progress_options: WorkDoneProgressOptions::default(),
264                })),
265                completion_provider: Some(CompletionOptions {
266                    trigger_characters: Some(vec![
267                        "`".to_string(),
268                        "(".to_string(),
269                        "#".to_string(),
270                        "/".to_string(),
271                        ".".to_string(),
272                        "-".to_string(),
273                    ]),
274                    resolve_provider: Some(false),
275                    work_done_progress_options: WorkDoneProgressOptions::default(),
276                    all_commit_characters: None,
277                    completion_item: None,
278                }),
279                definition_provider: enable_link_navigation.then_some(OneOf::Left(true)),
280                references_provider: enable_link_navigation.then_some(OneOf::Left(true)),
281                hover_provider: enable_link_navigation.then_some(HoverProviderCapability::Simple(true)),
282                rename_provider: enable_link_navigation.then_some(OneOf::Right(RenameOptions {
283                    prepare_provider: Some(true),
284                    work_done_progress_options: WorkDoneProgressOptions::default(),
285                })),
286                workspace: Some(WorkspaceServerCapabilities {
287                    workspace_folders: Some(WorkspaceFoldersServerCapabilities {
288                        supported: Some(true),
289                        change_notifications: Some(OneOf::Left(true)),
290                    }),
291                    file_operations: None,
292                }),
293                ..Default::default()
294            },
295            server_info: Some(ServerInfo {
296                name: "rumdl".to_string(),
297                version: Some(env!("CARGO_PKG_VERSION").to_string()),
298            }),
299        })
300    }
301
302    async fn initialized(&self, _: InitializedParams) {
303        let version = env!("CARGO_PKG_VERSION");
304
305        // Get binary path and build time
306        let (binary_path, build_time) = std::env::current_exe()
307            .ok()
308            .map(|path| {
309                let path_str = path.to_str().unwrap_or("unknown").to_string();
310                let build_time = std::fs::metadata(&path)
311                    .ok()
312                    .and_then(|metadata| metadata.modified().ok())
313                    .and_then(|modified| modified.duration_since(std::time::UNIX_EPOCH).ok())
314                    .and_then(|duration| {
315                        let secs = duration.as_secs();
316                        chrono::DateTime::from_timestamp(secs as i64, 0)
317                            .map(|dt| dt.format("%Y-%m-%d %H:%M:%S UTC").to_string())
318                    })
319                    .unwrap_or_else(|| "unknown".to_string());
320                (path_str, build_time)
321            })
322            .unwrap_or_else(|| ("unknown".to_string(), "unknown".to_string()));
323
324        let working_dir = std::env::current_dir()
325            .ok()
326            .and_then(|p| p.to_str().map(|s| s.to_string()))
327            .unwrap_or_else(|| "unknown".to_string());
328
329        log::info!("rumdl Language Server v{version} initialized (built: {build_time}, binary: {binary_path})");
330        log::info!("Working directory: {working_dir}");
331
332        self.client
333            .log_message(MessageType::INFO, format!("rumdl v{version} Language Server started"))
334            .await;
335
336        // Trigger initial workspace indexing for cross-file analysis
337        if self.update_tx.send(IndexUpdate::FullRescan).await.is_err() {
338            log::warn!("Failed to trigger initial workspace indexing");
339        } else {
340            log::info!("Triggered initial workspace indexing for cross-file analysis");
341        }
342
343        // Register file watchers for markdown files and config files
344        let markdown_patterns = [
345            "**/*.md",
346            "**/*.markdown",
347            "**/*.mdx",
348            "**/*.mkd",
349            "**/*.mkdn",
350            "**/*.mdown",
351            "**/*.mdwn",
352            "**/*.qmd",
353            "**/*.rmd",
354        ];
355        let config_patterns = [
356            "**/.rumdl.toml",
357            "**/rumdl.toml",
358            "**/pyproject.toml",
359            "**/.markdownlint.json",
360            "**/.markdownlint-cli2.yaml",
361            "**/.markdownlint-cli2.jsonc",
362        ];
363        let watchers: Vec<_> = markdown_patterns
364            .iter()
365            .chain(config_patterns.iter())
366            .map(|pattern| FileSystemWatcher {
367                glob_pattern: GlobPattern::String((*pattern).to_string()),
368                kind: Some(WatchKind::all()),
369            })
370            .collect();
371
372        let registration = Registration {
373            id: "markdown-watcher".to_string(),
374            method: "workspace/didChangeWatchedFiles".to_string(),
375            register_options: Some(
376                serde_json::to_value(DidChangeWatchedFilesRegistrationOptions { watchers }).unwrap(),
377            ),
378        };
379
380        if self.client.register_capability(vec![registration]).await.is_err() {
381            log::debug!("Client does not support file watching capability");
382        }
383    }
384
385    async fn completion(&self, params: CompletionParams) -> JsonRpcResult<Option<CompletionResponse>> {
386        let uri = params.text_document_position.text_document.uri;
387        let position = params.text_document_position.position;
388
389        // Get document content
390        let Some(text) = self.get_document_content(&uri).await else {
391            return Ok(None);
392        };
393
394        // Code fence language completion (backtick trigger)
395        if let Some((start_col, current_text)) = Self::detect_code_fence_language_position(&text, position) {
396            log::debug!(
397                "Code fence completion triggered at {}:{}, current text: '{}'",
398                position.line,
399                position.character,
400                current_text
401            );
402            let items = self
403                .get_language_completions(&uri, &current_text, start_col, position)
404                .await;
405            if !items.is_empty() {
406                return Ok(Some(CompletionResponse::Array(items)));
407            }
408        }
409
410        // Link target completion: file paths and heading anchors
411        if self.config.read().await.enable_link_completions {
412            // For trigger characters that fire on many non-link contexts (`.`, `-`),
413            // skip the full parse when there is no `](` on the current line before
414            // the cursor.  This avoids needless work on list items and contractions.
415            let trigger = params.context.as_ref().and_then(|c| c.trigger_character.as_deref());
416            let skip_link_check = matches!(trigger, Some("." | "-")) && {
417                let line_num = position.line as usize;
418                // Scan the whole line — no byte-slicing at a UTF-16 offset needed.
419                // A line without `](` anywhere cannot contain a link target.
420                !text
421                    .lines()
422                    .nth(line_num)
423                    .map(|line| line.contains("]("))
424                    .unwrap_or(false)
425            };
426
427            if !skip_link_check && let Some(link_info) = Self::detect_link_target_position(&text, position) {
428                let items = if let Some((partial_anchor, anchor_start_col)) = link_info.anchor {
429                    log::debug!(
430                        "Anchor completion triggered at {}:{}, file: '{}', partial: '{}'",
431                        position.line,
432                        position.character,
433                        link_info.file_path,
434                        partial_anchor
435                    );
436                    self.get_anchor_completions(&uri, &link_info.file_path, &partial_anchor, anchor_start_col, position)
437                        .await
438                } else {
439                    log::debug!(
440                        "File path completion triggered at {}:{}, partial: '{}'",
441                        position.line,
442                        position.character,
443                        link_info.file_path
444                    );
445                    self.get_file_completions(&uri, &link_info.file_path, link_info.path_start_col, position)
446                        .await
447                };
448                if !items.is_empty() {
449                    return Ok(Some(CompletionResponse::Array(items)));
450                }
451            }
452        }
453
454        Ok(None)
455    }
456
457    async fn did_change_workspace_folders(&self, params: DidChangeWorkspaceFoldersParams) {
458        // Update workspace roots
459        let mut roots = self.workspace_roots.write().await;
460
461        // Remove deleted workspace folders
462        for removed in &params.event.removed {
463            if let Ok(path) = removed.uri.to_file_path() {
464                roots.retain(|r| r != &path);
465                log::info!("Removed workspace root: {}", path.display());
466            }
467        }
468
469        // Add new workspace folders
470        for added in &params.event.added {
471            if let Ok(path) = added.uri.to_file_path()
472                && !roots.contains(&path)
473            {
474                log::info!("Added workspace root: {}", path.display());
475                roots.push(path);
476            }
477        }
478        drop(roots);
479
480        // Clear config cache as workspace structure changed
481        self.config_cache.write().await.clear();
482
483        // Reload fallback configuration
484        self.reload_configuration().await;
485
486        // Trigger full workspace rescan for cross-file index
487        if self.update_tx.send(IndexUpdate::FullRescan).await.is_err() {
488            log::warn!("Failed to trigger workspace rescan after folder change");
489        }
490    }
491
492    async fn did_change_configuration(&self, params: DidChangeConfigurationParams) {
493        log::debug!("Configuration changed: {:?}", params.settings);
494
495        // Parse settings from the notification
496        // Neovim sends: { "rumdl": { "MD013": {...}, ... } }
497        // VSCode might send the full RumdlLspConfig or similar structure
498        let settings_value = params.settings;
499
500        // Try to extract "rumdl" key from settings (Neovim style)
501        let rumdl_settings = if let serde_json::Value::Object(ref obj) = settings_value {
502            obj.get("rumdl").cloned().unwrap_or(settings_value.clone())
503        } else {
504            settings_value
505        };
506
507        // Track if we successfully applied any configuration
508        let mut config_applied = false;
509        let mut warnings: Vec<String> = Vec::new();
510
511        // Try to parse as LspRuleSettings first (Neovim style with "disable", "enable", rule keys)
512        // We check this first because RumdlLspConfig with #[serde(default)] will accept any JSON
513        // and just ignore unknown fields, which would lose the Neovim-style settings
514        if let Ok(rule_settings) = serde_json::from_value::<LspRuleSettings>(rumdl_settings.clone())
515            && (rule_settings.disable.is_some()
516                || rule_settings.enable.is_some()
517                || rule_settings.line_length.is_some()
518                || (!rule_settings.rules.is_empty() && rule_settings.rules.keys().all(|k| is_valid_rule_name(k))))
519        {
520            // Validate rule names in disable/enable lists
521            if let Some(ref disable) = rule_settings.disable {
522                for rule in disable {
523                    if !is_valid_rule_name(rule) {
524                        warnings.push(format!("Unknown rule in disable list: {rule}"));
525                    }
526                }
527            }
528            if let Some(ref enable) = rule_settings.enable {
529                for rule in enable {
530                    if !is_valid_rule_name(rule) {
531                        warnings.push(format!("Unknown rule in enable list: {rule}"));
532                    }
533                }
534            }
535            // Validate rule-specific settings
536            for rule_name in rule_settings.rules.keys() {
537                if !is_valid_rule_name(rule_name) {
538                    warnings.push(format!("Unknown rule in settings: {rule_name}"));
539                }
540            }
541
542            log::info!("Applied rule settings from configuration (Neovim style)");
543            let mut config = self.config.write().await;
544            config.settings = Some(rule_settings);
545            drop(config);
546            config_applied = true;
547        } else if let Ok(full_config) = serde_json::from_value::<RumdlLspConfig>(rumdl_settings.clone())
548            && (full_config.config_path.is_some()
549                || full_config.enable_rules.is_some()
550                || full_config.disable_rules.is_some()
551                || full_config.settings.is_some()
552                || !full_config.enable_linting
553                || full_config.enable_auto_fix
554                || !full_config.enable_link_completions
555                || !full_config.enable_link_navigation)
556        {
557            // Validate rule names
558            if let Some(ref rules) = full_config.enable_rules {
559                for rule in rules {
560                    if !is_valid_rule_name(rule) {
561                        warnings.push(format!("Unknown rule in enableRules: {rule}"));
562                    }
563                }
564            }
565            if let Some(ref rules) = full_config.disable_rules {
566                for rule in rules {
567                    if !is_valid_rule_name(rule) {
568                        warnings.push(format!("Unknown rule in disableRules: {rule}"));
569                    }
570                }
571            }
572
573            log::info!("Applied full LSP configuration from settings");
574            *self.config.write().await = full_config;
575            config_applied = true;
576        } else if let serde_json::Value::Object(obj) = rumdl_settings {
577            // Otherwise, treat as per-rule settings with manual parsing
578            // Format: { "MD013": { "lineLength": 80 }, "disable": ["MD009"] }
579            let mut config = self.config.write().await;
580
581            // Manual parsing for Neovim format
582            let mut rules = std::collections::HashMap::new();
583            let mut disable = Vec::new();
584            let mut enable = Vec::new();
585            let mut line_length = None;
586
587            for (key, value) in obj {
588                match key.as_str() {
589                    "disable" => match serde_json::from_value::<Vec<String>>(value.clone()) {
590                        Ok(d) => {
591                            if d.len() > MAX_RULE_LIST_SIZE {
592                                warnings.push(format!(
593                                    "Too many rules in 'disable' ({} > {}), truncating",
594                                    d.len(),
595                                    MAX_RULE_LIST_SIZE
596                                ));
597                            }
598                            for rule in d.iter().take(MAX_RULE_LIST_SIZE) {
599                                if !is_valid_rule_name(rule) {
600                                    warnings.push(format!("Unknown rule in disable: {rule}"));
601                                }
602                            }
603                            disable = d.into_iter().take(MAX_RULE_LIST_SIZE).collect();
604                        }
605                        Err(_) => {
606                            warnings.push(format!(
607                                "Invalid 'disable' value: expected array of strings, got {value}"
608                            ));
609                        }
610                    },
611                    "enable" => match serde_json::from_value::<Vec<String>>(value.clone()) {
612                        Ok(e) => {
613                            if e.len() > MAX_RULE_LIST_SIZE {
614                                warnings.push(format!(
615                                    "Too many rules in 'enable' ({} > {}), truncating",
616                                    e.len(),
617                                    MAX_RULE_LIST_SIZE
618                                ));
619                            }
620                            for rule in e.iter().take(MAX_RULE_LIST_SIZE) {
621                                if !is_valid_rule_name(rule) {
622                                    warnings.push(format!("Unknown rule in enable: {rule}"));
623                                }
624                            }
625                            enable = e.into_iter().take(MAX_RULE_LIST_SIZE).collect();
626                        }
627                        Err(_) => {
628                            warnings.push(format!(
629                                "Invalid 'enable' value: expected array of strings, got {value}"
630                            ));
631                        }
632                    },
633                    "lineLength" | "line_length" | "line-length" => {
634                        if let Some(l) = value.as_u64() {
635                            match usize::try_from(l) {
636                                Ok(len) if len <= MAX_LINE_LENGTH => line_length = Some(len),
637                                Ok(len) => warnings.push(format!(
638                                    "Invalid 'lineLength' value: {len} exceeds maximum ({MAX_LINE_LENGTH})"
639                                )),
640                                Err(_) => warnings.push(format!("Invalid 'lineLength' value: {l} is too large")),
641                            }
642                        } else {
643                            warnings.push(format!("Invalid 'lineLength' value: expected number, got {value}"));
644                        }
645                    }
646                    // Rule-specific settings (e.g., "MD013": { "lineLength": 80 })
647                    _ if key.starts_with("MD") || key.starts_with("md") => {
648                        let normalized = key.to_uppercase();
649                        if !is_valid_rule_name(&normalized) {
650                            warnings.push(format!("Unknown rule: {key}"));
651                        }
652                        rules.insert(normalized, value);
653                    }
654                    _ => {
655                        // Unknown key - warn and ignore
656                        warnings.push(format!("Unknown configuration key: {key}"));
657                    }
658                }
659            }
660
661            let settings = LspRuleSettings {
662                line_length,
663                disable: if disable.is_empty() { None } else { Some(disable) },
664                enable: if enable.is_empty() { None } else { Some(enable) },
665                rules,
666            };
667
668            log::info!("Applied Neovim-style rule settings (manual parse)");
669            config.settings = Some(settings);
670            drop(config);
671            config_applied = true;
672        } else {
673            log::warn!("Could not parse configuration settings: {rumdl_settings:?}");
674        }
675
676        // Log warnings for invalid configuration
677        for warning in &warnings {
678            log::warn!("{warning}");
679        }
680
681        // Notify client of configuration warnings via window/logMessage
682        if !warnings.is_empty() {
683            let message = if warnings.len() == 1 {
684                format!("rumdl: {}", warnings[0])
685            } else {
686                format!("rumdl configuration warnings:\n{}", warnings.join("\n"))
687            };
688            self.client.log_message(MessageType::WARNING, message).await;
689        }
690
691        if !config_applied {
692            log::debug!("No configuration changes applied");
693        }
694
695        // Clear config cache to pick up new settings
696        self.config_cache.write().await.clear();
697
698        // Collect all open documents first (to avoid holding lock during async operations)
699        let doc_list: Vec<_> = {
700            let documents = self.documents.read().await;
701            documents
702                .iter()
703                .map(|(uri, entry)| (uri.clone(), entry.content.clone()))
704                .collect()
705        };
706
707        // Refresh diagnostics for all open documents concurrently
708        let tasks = doc_list.into_iter().map(|(uri, text)| {
709            let server = self.clone();
710            tokio::spawn(async move {
711                server.update_diagnostics(uri, text, true).await;
712            })
713        });
714
715        // Wait for all diagnostics to complete
716        let _ = join_all(tasks).await;
717    }
718
719    async fn shutdown(&self) -> JsonRpcResult<()> {
720        log::info!("Shutting down rumdl Language Server");
721
722        // Signal the index worker to shut down
723        let _ = self.update_tx.send(IndexUpdate::Shutdown).await;
724
725        Ok(())
726    }
727
728    async fn did_open(&self, params: DidOpenTextDocumentParams) {
729        let uri = params.text_document.uri;
730        let text = params.text_document.text;
731        let version = params.text_document.version;
732
733        let entry = DocumentEntry {
734            content: text.clone(),
735            version: Some(version),
736            from_disk: false,
737        };
738        self.documents.write().await.insert(uri.clone(), entry);
739
740        // Send update to index worker for cross-file analysis
741        if let Ok(path) = uri.to_file_path() {
742            let _ = self
743                .update_tx
744                .send(IndexUpdate::FileChanged {
745                    path,
746                    content: text.clone(),
747                })
748                .await;
749        }
750
751        self.update_diagnostics(uri, text, true).await;
752    }
753
754    async fn did_change(&self, params: DidChangeTextDocumentParams) {
755        let uri = params.text_document.uri;
756        let version = params.text_document.version;
757
758        if let Some(change) = params.content_changes.into_iter().next() {
759            let text = change.text;
760
761            let entry = DocumentEntry {
762                content: text.clone(),
763                version: Some(version),
764                from_disk: false,
765            };
766            self.documents.write().await.insert(uri.clone(), entry);
767
768            // Send update to index worker for cross-file analysis
769            if let Ok(path) = uri.to_file_path() {
770                let _ = self
771                    .update_tx
772                    .send(IndexUpdate::FileChanged {
773                        path,
774                        content: text.clone(),
775                    })
776                    .await;
777            }
778
779            self.update_diagnostics(uri, text, false).await;
780        }
781    }
782
783    async fn will_save_wait_until(&self, params: WillSaveTextDocumentParams) -> JsonRpcResult<Option<Vec<TextEdit>>> {
784        // Only apply fixes on manual saves (Cmd+S / Ctrl+S), not on autosave
785        // This respects VSCode's editor.formatOnSave: "explicit" setting
786        if params.reason != TextDocumentSaveReason::MANUAL {
787            return Ok(None);
788        }
789
790        let config_guard = self.config.read().await;
791        let enable_auto_fix = config_guard.enable_auto_fix;
792        drop(config_guard);
793
794        if !enable_auto_fix {
795            return Ok(None);
796        }
797
798        // Get the current document content
799        let Some(text) = self.get_document_content(&params.text_document.uri).await else {
800            return Ok(None);
801        };
802
803        // Apply all fixes
804        match self.apply_all_fixes(&params.text_document.uri, &text).await {
805            Ok(Some(fixed_text)) => {
806                // Return a single edit that replaces the entire document
807                Ok(Some(vec![TextEdit {
808                    range: Range {
809                        start: Position { line: 0, character: 0 },
810                        end: self.get_end_position(&text),
811                    },
812                    new_text: fixed_text,
813                }]))
814            }
815            Ok(None) => Ok(None),
816            Err(e) => {
817                log::error!("Failed to generate fixes in will_save_wait_until: {e}");
818                Ok(None)
819            }
820        }
821    }
822
823    async fn did_save(&self, params: DidSaveTextDocumentParams) {
824        // Re-lint the document after save
825        // Note: Auto-fixing is now handled by will_save_wait_until which runs before the save
826        if let Some(entry) = self.documents.read().await.get(&params.text_document.uri) {
827            self.update_diagnostics(params.text_document.uri, entry.content.clone(), true)
828                .await;
829        }
830    }
831
832    async fn did_close(&self, params: DidCloseTextDocumentParams) {
833        // Remove document from storage
834        self.documents.write().await.remove(&params.text_document.uri);
835
836        // Always clear diagnostics on close to ensure cleanup
837        // (Ruff does this unconditionally as a defensive measure)
838        self.client
839            .publish_diagnostics(params.text_document.uri, Vec::new(), None)
840            .await;
841    }
842
843    async fn did_change_watched_files(&self, params: DidChangeWatchedFilesParams) {
844        // Check if any of the changed files are config files
845        const CONFIG_FILES: &[&str] = &[
846            ".rumdl.toml",
847            "rumdl.toml",
848            "pyproject.toml",
849            ".markdownlint.json",
850            ".markdownlint-cli2.jsonc",
851            ".markdownlint-cli2.yaml",
852            ".markdownlint-cli2.yml",
853        ];
854
855        let mut config_changed = false;
856
857        for change in &params.changes {
858            if let Ok(path) = change.uri.to_file_path() {
859                let file_name = path.file_name().and_then(|f| f.to_str());
860                let extension = path.extension().and_then(|e| e.to_str());
861
862                // Handle config file changes
863                if let Some(name) = file_name
864                    && CONFIG_FILES.contains(&name)
865                    && !config_changed
866                {
867                    log::info!("Config file changed: {}, invalidating config cache", path.display());
868
869                    // Clear the entire config cache when any config file changes.
870                    // Fallback entries (no config_file) become stale when a new config file
871                    // is created, and directory-scoped entries may resolve differently after edits.
872                    let mut cache = self.config_cache.write().await;
873                    cache.clear();
874
875                    // Also reload the global fallback configuration
876                    drop(cache);
877                    self.reload_configuration().await;
878                    config_changed = true;
879                }
880
881                // Handle markdown file changes for workspace index
882                if let Some(ext) = extension
883                    && is_markdown_extension(ext)
884                {
885                    match change.typ {
886                        FileChangeType::CREATED | FileChangeType::CHANGED => {
887                            // Read file content and update index
888                            if let Ok(content) = tokio::fs::read_to_string(&path).await {
889                                let _ = self
890                                    .update_tx
891                                    .send(IndexUpdate::FileChanged {
892                                        path: path.clone(),
893                                        content,
894                                    })
895                                    .await;
896                            }
897                        }
898                        FileChangeType::DELETED => {
899                            let _ = self
900                                .update_tx
901                                .send(IndexUpdate::FileDeleted { path: path.clone() })
902                                .await;
903                        }
904                        _ => {}
905                    }
906                }
907            }
908        }
909
910        // Re-lint all open documents if config changed
911        if config_changed {
912            let docs_to_update: Vec<(Url, String)> = {
913                let docs = self.documents.read().await;
914                docs.iter()
915                    .filter(|(_, entry)| !entry.from_disk)
916                    .map(|(uri, entry)| (uri.clone(), entry.content.clone()))
917                    .collect()
918            };
919
920            for (uri, text) in docs_to_update {
921                self.update_diagnostics(uri, text, true).await;
922            }
923        }
924    }
925
926    async fn code_action(&self, params: CodeActionParams) -> JsonRpcResult<Option<CodeActionResponse>> {
927        let uri = params.text_document.uri;
928        let range = params.range;
929        let requested_kinds = params.context.only;
930
931        if let Some(text) = self.get_document_content(&uri).await {
932            match self.get_code_actions(&uri, &text, range).await {
933                Ok(actions) => {
934                    // Filter actions by requested kinds (if specified and non-empty)
935                    // LSP spec: "If provided with no kinds, all supported kinds are returned"
936                    // LSP code action kinds are hierarchical: source.fixAll.rumdl matches source.fixAll
937                    let filtered_actions = if let Some(ref kinds) = requested_kinds
938                        && !kinds.is_empty()
939                    {
940                        actions
941                            .into_iter()
942                            .filter(|action| {
943                                action.kind.as_ref().is_some_and(|action_kind| {
944                                    let action_kind_str = action_kind.as_str();
945                                    kinds.iter().any(|requested| {
946                                        let requested_str = requested.as_str();
947                                        // Match if action kind starts with requested kind
948                                        // e.g., "source.fixAll.rumdl" matches "source.fixAll"
949                                        action_kind_str.starts_with(requested_str)
950                                    })
951                                })
952                            })
953                            .collect()
954                    } else {
955                        actions
956                    };
957
958                    let response: Vec<CodeActionOrCommand> = filtered_actions
959                        .into_iter()
960                        .map(CodeActionOrCommand::CodeAction)
961                        .collect();
962                    Ok(Some(response))
963                }
964                Err(e) => {
965                    log::error!("Failed to get code actions: {e}");
966                    Ok(None)
967                }
968            }
969        } else {
970            Ok(None)
971        }
972    }
973
974    async fn range_formatting(&self, params: DocumentRangeFormattingParams) -> JsonRpcResult<Option<Vec<TextEdit>>> {
975        // For markdown linting, we format the entire document because:
976        // 1. Many markdown rules have document-wide implications (e.g., heading hierarchy, list consistency)
977        // 2. Fixes often need surrounding context to be applied correctly
978        // 3. This approach is common among linters (ESLint, rustfmt, etc. do similar)
979        log::debug!(
980            "Range formatting requested for {:?}, formatting entire document due to rule interdependencies",
981            params.range
982        );
983
984        let formatting_params = DocumentFormattingParams {
985            text_document: params.text_document,
986            options: params.options,
987            work_done_progress_params: params.work_done_progress_params,
988        };
989
990        self.formatting(formatting_params).await
991    }
992
993    async fn formatting(&self, params: DocumentFormattingParams) -> JsonRpcResult<Option<Vec<TextEdit>>> {
994        let uri = params.text_document.uri;
995        let options = params.options;
996
997        log::debug!("Formatting request for: {uri}");
998        log::debug!(
999            "FormattingOptions: insert_final_newline={:?}, trim_final_newlines={:?}, trim_trailing_whitespace={:?}",
1000            options.insert_final_newline,
1001            options.trim_final_newlines,
1002            options.trim_trailing_whitespace
1003        );
1004
1005        if let Some(text) = self.get_document_content(&uri).await {
1006            // Get config with LSP overrides
1007            let config_guard = self.config.read().await;
1008            let lsp_config = config_guard.clone();
1009            drop(config_guard);
1010
1011            // Resolve configuration for this specific file
1012            let file_path = uri.to_file_path().ok();
1013            let file_config = if let Some(ref path) = file_path {
1014                self.resolve_config_for_file(path).await
1015            } else {
1016                // Fallback to global config for non-file URIs
1017                self.rumdl_config.read().await.clone()
1018            };
1019
1020            // Merge LSP settings with file config based on configuration_preference
1021            let rumdl_config = self.merge_lsp_settings(file_config, &lsp_config);
1022
1023            let all_rules = rules::all_rules(&rumdl_config);
1024            let flavor = if let Some(ref path) = file_path {
1025                rumdl_config.get_flavor_for_file(path)
1026            } else {
1027                rumdl_config.markdown_flavor()
1028            };
1029
1030            // Use the standard filter_rules function which respects config's disabled rules
1031            let mut filtered_rules = rules::filter_rules(&all_rules, &rumdl_config.global);
1032
1033            // Apply LSP config overrides
1034            filtered_rules = self.apply_lsp_config_overrides(filtered_rules, &lsp_config);
1035
1036            // Phase 1: Apply lint rule fixes
1037            let mut result = text.clone();
1038            match crate::lint(
1039                &text,
1040                &filtered_rules,
1041                false,
1042                flavor,
1043                file_path.clone(),
1044                Some(&rumdl_config),
1045            ) {
1046                Ok(warnings) => {
1047                    log::debug!(
1048                        "Found {} warnings, {} with fixes",
1049                        warnings.len(),
1050                        warnings.iter().filter(|w| w.fix.is_some()).count()
1051                    );
1052
1053                    let has_fixes = warnings.iter().any(|w| w.fix.is_some());
1054                    if has_fixes {
1055                        // Only apply fixes from fixable rules during formatting
1056                        let fixable_warnings: Vec<_> = warnings
1057                            .iter()
1058                            .filter(|w| {
1059                                if let Some(rule_name) = &w.rule_name {
1060                                    filtered_rules
1061                                        .iter()
1062                                        .find(|r| r.name() == rule_name)
1063                                        .map(|r| r.fix_capability() != FixCapability::Unfixable)
1064                                        .unwrap_or(false)
1065                                } else {
1066                                    false
1067                                }
1068                            })
1069                            .cloned()
1070                            .collect();
1071
1072                        match crate::utils::fix_utils::apply_warning_fixes(&text, &fixable_warnings) {
1073                            Ok(fixed_content) => {
1074                                result = fixed_content;
1075                            }
1076                            Err(e) => {
1077                                log::error!("Failed to apply fixes: {e}");
1078                            }
1079                        }
1080                    }
1081                }
1082                Err(e) => {
1083                    log::error!("Failed to lint document: {e}");
1084                }
1085            }
1086
1087            // Phase 2: Apply FormattingOptions (standard LSP behavior)
1088            // This ensures we respect editor preferences even if lint rules don't catch everything
1089            result = Self::apply_formatting_options(result, &options);
1090
1091            // Return edit if content changed
1092            if result != text {
1093                log::debug!("Returning formatting edits");
1094                let end_position = self.get_end_position(&text);
1095                let edit = TextEdit {
1096                    range: Range {
1097                        start: Position { line: 0, character: 0 },
1098                        end: end_position,
1099                    },
1100                    new_text: result,
1101                };
1102                return Ok(Some(vec![edit]));
1103            }
1104
1105            Ok(Some(Vec::new()))
1106        } else {
1107            log::warn!("Document not found: {uri}");
1108            Ok(None)
1109        }
1110    }
1111
1112    async fn goto_definition(&self, params: GotoDefinitionParams) -> JsonRpcResult<Option<GotoDefinitionResponse>> {
1113        if !self.config.read().await.enable_link_navigation {
1114            return Ok(None);
1115        }
1116        let uri = params.text_document_position_params.text_document.uri;
1117        let position = params.text_document_position_params.position;
1118
1119        log::debug!("Go-to-definition at {uri} {}:{}", position.line, position.character);
1120
1121        Ok(self.handle_goto_definition(&uri, position).await)
1122    }
1123
1124    async fn references(&self, params: ReferenceParams) -> JsonRpcResult<Option<Vec<Location>>> {
1125        if !self.config.read().await.enable_link_navigation {
1126            return Ok(None);
1127        }
1128        let uri = params.text_document_position.text_document.uri;
1129        let position = params.text_document_position.position;
1130
1131        log::debug!("Find references at {uri} {}:{}", position.line, position.character);
1132
1133        Ok(self.handle_references(&uri, position).await)
1134    }
1135
1136    async fn hover(&self, params: HoverParams) -> JsonRpcResult<Option<Hover>> {
1137        if !self.config.read().await.enable_link_navigation {
1138            return Ok(None);
1139        }
1140        let uri = params.text_document_position_params.text_document.uri;
1141        let position = params.text_document_position_params.position;
1142
1143        log::debug!("Hover at {uri} {}:{}", position.line, position.character);
1144
1145        Ok(self.handle_hover(&uri, position).await)
1146    }
1147
1148    async fn prepare_rename(&self, params: TextDocumentPositionParams) -> JsonRpcResult<Option<PrepareRenameResponse>> {
1149        if !self.config.read().await.enable_link_navigation {
1150            return Ok(None);
1151        }
1152        let uri = params.text_document.uri;
1153        let position = params.position;
1154
1155        log::debug!("Prepare rename at {uri} {}:{}", position.line, position.character);
1156
1157        Ok(self.handle_prepare_rename(&uri, position).await)
1158    }
1159
1160    async fn rename(&self, params: RenameParams) -> JsonRpcResult<Option<WorkspaceEdit>> {
1161        if !self.config.read().await.enable_link_navigation {
1162            return Ok(None);
1163        }
1164        let uri = params.text_document_position.text_document.uri;
1165        let position = params.text_document_position.position;
1166        let new_name = params.new_name;
1167
1168        log::debug!("Rename at {uri} {}:{} → {new_name}", position.line, position.character);
1169
1170        Ok(self.handle_rename(&uri, position, &new_name).await)
1171    }
1172
1173    async fn diagnostic(&self, params: DocumentDiagnosticParams) -> JsonRpcResult<DocumentDiagnosticReportResult> {
1174        let uri = params.text_document.uri;
1175
1176        if let Some(text) = self.get_open_document_content(&uri).await {
1177            match self.lint_document(&uri, &text, true).await {
1178                Ok(diagnostics) => Ok(DocumentDiagnosticReportResult::Report(DocumentDiagnosticReport::Full(
1179                    RelatedFullDocumentDiagnosticReport {
1180                        related_documents: None,
1181                        full_document_diagnostic_report: FullDocumentDiagnosticReport {
1182                            result_id: None,
1183                            items: diagnostics,
1184                        },
1185                    },
1186                ))),
1187                Err(e) => {
1188                    log::error!("Failed to get diagnostics: {e}");
1189                    Ok(DocumentDiagnosticReportResult::Report(DocumentDiagnosticReport::Full(
1190                        RelatedFullDocumentDiagnosticReport {
1191                            related_documents: None,
1192                            full_document_diagnostic_report: FullDocumentDiagnosticReport {
1193                                result_id: None,
1194                                items: Vec::new(),
1195                            },
1196                        },
1197                    )))
1198                }
1199            }
1200        } else {
1201            Ok(DocumentDiagnosticReportResult::Report(DocumentDiagnosticReport::Full(
1202                RelatedFullDocumentDiagnosticReport {
1203                    related_documents: None,
1204                    full_document_diagnostic_report: FullDocumentDiagnosticReport {
1205                        result_id: None,
1206                        items: Vec::new(),
1207                    },
1208                },
1209            )))
1210        }
1211    }
1212}
1213
1214#[cfg(test)]
1215#[path = "tests.rs"]
1216mod tests;