Skip to content

Commit

Permalink
Adopt configuration fallback logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed May 30, 2024
1 parent a1bb16d commit edeaae6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
31 changes: 22 additions & 9 deletions crates/ruff_server/src/session/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand All @@ -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",
Expand Down
22 changes: 10 additions & 12 deletions crates/ruff_server/src/session/index/ruff_settings.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -190,18 +189,17 @@ impl RuffSettingsIndex {
Self { index, fallback }
}

pub(super) fn get(&self, document_url: &Url) -> Arc<RuffSettings> {
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<RuffSettings> {
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<RuffSettings> {
self.fallback.clone()
}
}

Expand Down

0 comments on commit edeaae6

Please sign in to comment.