From afbfabec789f781d025e1d27f24eabebb5aba720 Mon Sep 17 00:00:00 2001 From: Jane Lewis Date: Mon, 22 Apr 2024 13:50:26 -0700 Subject: [PATCH] Move resolution logic to the configuration transformer --- crates/ruff_server/src/session/settings.rs | 76 ++--------------- .../src/session/workspace/ruff_settings.rs | 82 +++++++++++++------ crates/ruff_workspace/src/resolver.rs | 2 +- 3 files changed, 68 insertions(+), 92 deletions(-) diff --git a/crates/ruff_server/src/session/settings.rs b/crates/ruff_server/src/session/settings.rs index b72e7c4664c97..d762feebc6d31 100644 --- a/crates/ruff_server/src/session/settings.rs +++ b/crates/ruff_server/src/session/settings.rs @@ -1,15 +1,7 @@ -use std::{ops::Deref, path::Path, str::FromStr}; +use std::{ops::Deref, str::FromStr}; use lsp_types::Url; -use ruff_linter::{ - fs::normalize_path_to, - line_width::LineLength, - settings::types::{FilePattern, PreviewMode}, - RuleSelector, -}; -use ruff_workspace::configuration::{ - Configuration, FormatConfiguration, LintConfiguration, RuleSelection, -}; +use ruff_linter::{line_width::LineLength, RuleSelector}; use rustc_hash::FxHashMap; use serde::Deserialize; @@ -39,13 +31,13 @@ pub(crate) struct ResolvedClientSettings { #[derive(Clone, Debug, Default)] #[cfg_attr(test, derive(PartialEq, Eq))] pub(crate) struct ResolvedEditorSettings { - lint_preview: Option, - format_preview: Option, - select: Option>, - extend_select: Option>, - ignore: Option>, - exclude: Option>, - line_length: Option, + pub(super) lint_preview: Option, + pub(super) format_preview: Option, + pub(super) select: Option>, + pub(super) extend_select: Option>, + pub(super) ignore: Option>, + pub(super) exclude: Option>, + pub(super) line_length: Option, } /// This is a direct representation of the settings schema sent by the client. @@ -310,56 +302,6 @@ impl ResolvedClientSettings { } } -impl ResolvedEditorSettings { - pub(crate) fn resolve( - &self, - project_root: &Path, - project_configuration: Configuration, - ) -> crate::Result { - let Self { - format_preview, - lint_preview, - select, - extend_select, - ignore, - exclude, - line_length, - } = self.clone(); - - let editor_configuration = Configuration { - lint: LintConfiguration { - preview: lint_preview.map(PreviewMode::from), - rule_selections: vec![RuleSelection { - select, - extend_select: extend_select.unwrap_or_default(), - ignore: ignore.unwrap_or_default(), - ..Default::default() - }], - ..Default::default() - }, - format: FormatConfiguration { - preview: format_preview.map(PreviewMode::from), - ..Default::default() - }, - exclude: exclude.map(|exclude| { - exclude - .into_iter() - .map(|pattern| { - let absolute = normalize_path_to(&pattern, project_root); - FilePattern::User(pattern, absolute) - }) - .collect() - }), - line_length, - ..Default::default() - }; - - let configuration = editor_configuration.combine(project_configuration); - - configuration.into_settings(project_root) - } -} - impl Default for InitializationOptions { fn default() -> Self { Self::GlobalOnly { settings: None } diff --git a/crates/ruff_server/src/session/workspace/ruff_settings.rs b/crates/ruff_server/src/session/workspace/ruff_settings.rs index 219bfe3c6c450..d1db32b5b1a23 100644 --- a/crates/ruff_server/src/session/workspace/ruff_settings.rs +++ b/crates/ruff_server/src/session/workspace/ruff_settings.rs @@ -1,5 +1,9 @@ -use ruff_linter::display_settings; +use ruff_linter::{ + display_settings, fs::normalize_path_to, settings::types::FilePattern, + settings::types::PreviewMode, +}; use ruff_workspace::{ + configuration::{Configuration, FormatConfiguration, LintConfiguration, RuleSelection}, pyproject::settings_toml, resolver::{ConfigurationTransformer, Relativity}, }; @@ -39,18 +43,6 @@ impl std::fmt::Display for RuffSettings { } impl RuffSettings { - pub(crate) fn resolve( - project_root: &Path, - configuration: ruff_workspace::configuration::Configuration, - editor_settings: &ResolvedEditorSettings, - ) -> crate::Result { - let settings = editor_settings.resolve(project_root, configuration)?; - Ok(Self { - linter: settings.linter, - formatter: settings.formatter, - }) - } - pub(crate) fn linter(&self) -> &ruff_linter::settings::LinterSettings { &self.linter } @@ -71,18 +63,20 @@ impl RuffSettingsIndex { .map(DirEntry::into_path) { if let Some(pyproject) = settings_toml(&directory).ok().flatten() { - let Ok(configuration) = ruff_workspace::resolver::resolve_configuration( + let Ok(settings) = ruff_workspace::resolver::resolve_root_settings( &pyproject, Relativity::Parent, - &LSPConfigTransformer, + &EditorConfigurationTransformer(editor_settings, root), ) else { continue; }; - let Ok(settings) = RuffSettings::resolve(root, configuration, editor_settings) - else { - continue; - }; - index.insert(directory, Arc::new(settings)); + index.insert( + directory, + Arc::new(RuffSettings { + linter: settings.linter, + formatter: settings.formatter, + }), + ); } } @@ -108,13 +102,53 @@ impl RuffSettingsIndex { } } -struct LSPConfigTransformer; +struct EditorConfigurationTransformer<'a>(&'a ResolvedEditorSettings, &'a Path); -impl ConfigurationTransformer for LSPConfigTransformer { +impl<'a> ConfigurationTransformer for EditorConfigurationTransformer<'a> { fn transform( &self, - config: ruff_workspace::configuration::Configuration, + project_configuration: ruff_workspace::configuration::Configuration, ) -> ruff_workspace::configuration::Configuration { - config + let ResolvedEditorSettings { + format_preview, + lint_preview, + select, + extend_select, + ignore, + exclude, + line_length, + } = self.0.clone(); + + let project_root = self.1; + + let editor_configuration = Configuration { + lint: LintConfiguration { + preview: lint_preview.map(PreviewMode::from), + rule_selections: vec![RuleSelection { + select, + extend_select: extend_select.unwrap_or_default(), + ignore: ignore.unwrap_or_default(), + ..Default::default() + }], + ..Default::default() + }, + format: FormatConfiguration { + preview: format_preview.map(PreviewMode::from), + ..Default::default() + }, + exclude: exclude.map(|exclude| { + exclude + .into_iter() + .map(|pattern| { + let absolute = normalize_path_to(&pattern, project_root); + FilePattern::User(pattern, absolute) + }) + .collect() + }), + line_length, + ..Default::default() + }; + + editor_configuration.combine(project_configuration) } } diff --git a/crates/ruff_workspace/src/resolver.rs b/crates/ruff_workspace/src/resolver.rs index ff1ce41c95357..7ccd78f690c5d 100644 --- a/crates/ruff_workspace/src/resolver.rs +++ b/crates/ruff_workspace/src/resolver.rs @@ -240,7 +240,7 @@ pub trait ConfigurationTransformer: Sync { // configuration file extends another in the same path, we'll re-parse the same // file at least twice (possibly more than twice, since we'll also parse it when // resolving the "default" configuration). -pub fn resolve_configuration( +fn resolve_configuration( pyproject: &Path, relativity: Relativity, transformer: &dyn ConfigurationTransformer,