diff --git a/crates/ruff_server/src/session/index.rs b/crates/ruff_server/src/session/index.rs index ef810198fae31..64636103a5854 100644 --- a/crates/ruff_server/src/session/index.rs +++ b/crates/ruff_server/src/session/index.rs @@ -227,14 +227,10 @@ impl Index { })?; // O(n) complexity, which isn't ideal... but this is an uncommon operation. - self.documents.retain(|url, _| { - !url.to_file_path() - .is_ok_and(|file_path| file_path.starts_with(&workspace_path)) - }); - self.notebook_cells.retain(|_, url| { - !url.to_file_path() - .is_ok_and(|file_path| file_path.starts_with(&workspace_path)) - }); + self.documents + .retain(|url, _| !Path::new(url.path()).starts_with(&workspace_path)); + self.notebook_cells + .retain(|_, url| !Path::new(url.path()).starts_with(&workspace_path)); Ok(()) } @@ -248,7 +244,24 @@ impl Index { let document_settings = self .settings_for_url(&url) - .map(|settings| settings.ruff_settings.get(&url)) + .map(|settings| { + if let Ok(file_path) = url.to_file_path() { + settings.ruff_settings.get(&file_path) + } else { + // For a new unsaved and untitled document, use the ruff settings from the top of the workspace + // but only IF: + // * It is the only workspace + // * The ruff setting is at the top of the workspace (in the root folder) + // Otherwise, use the fallback settings. + if self.settings.len() == 1 { + let workspace_path = self.settings.keys().next().unwrap(); + settings.ruff_settings.get(&workspace_path.join("untitled")) + } else { + tracing::debug!("Use the fallback settings for the new document '{url}'."); + settings.ruff_settings.fallback() + } + } + }) .unwrap_or_else(|| { tracing::warn!( "No settings available for {} - falling back to default settings", diff --git a/crates/ruff_server/src/session/index/ruff_settings.rs b/crates/ruff_server/src/session/index/ruff_settings.rs index afa8ec624413a..1939a91aca3c0 100644 --- a/crates/ruff_server/src/session/index/ruff_settings.rs +++ b/crates/ruff_server/src/session/index/ruff_settings.rs @@ -1,5 +1,4 @@ use globset::Candidate; -use lsp_types::Url; use ruff_linter::{ display_settings, fs::normalize_path_to, settings::types::FilePattern, settings::types::PreviewMode, @@ -190,18 +189,17 @@ impl RuffSettingsIndex { Self { index, fallback } } - pub(super) fn get(&self, document_url: &Url) -> Arc { - let settings = if let Ok(document_path) = document_url.to_file_path() { - self.index - .range(..document_path.to_path_buf()) - .rfind(|(path, _)| document_path.starts_with(path)) - .map(|(_, settings)| settings) - } else { - // For a new document, use the least specific setting available or the fallback. - self.index.values().next() - }; + pub(super) fn get(&self, document_path: &Path) -> Arc { + self.index + .range(..document_path.to_path_buf()) + .rfind(|(path, _)| document_path.starts_with(path)) + .map(|(_, settings)| settings) + .unwrap_or_else(|| &self.fallback) + .clone() + } - settings.unwrap_or_else(|| &self.fallback).clone() + pub(super) fn fallback(&self) -> Arc { + self.fallback.clone() } }