Skip to content

Commit

Permalink
Move resolution logic to the configuration transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
snowsignal committed Apr 23, 2024
1 parent 59a9505 commit afbfabe
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 92 deletions.
76 changes: 9 additions & 67 deletions crates/ruff_server/src/session/settings.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<bool>,
format_preview: Option<bool>,
select: Option<Vec<RuleSelector>>,
extend_select: Option<Vec<RuleSelector>>,
ignore: Option<Vec<RuleSelector>>,
exclude: Option<Vec<String>>,
line_length: Option<LineLength>,
pub(super) lint_preview: Option<bool>,
pub(super) format_preview: Option<bool>,
pub(super) select: Option<Vec<RuleSelector>>,
pub(super) extend_select: Option<Vec<RuleSelector>>,
pub(super) ignore: Option<Vec<RuleSelector>>,
pub(super) exclude: Option<Vec<String>>,
pub(super) line_length: Option<LineLength>,
}

/// This is a direct representation of the settings schema sent by the client.
Expand Down Expand Up @@ -310,56 +302,6 @@ impl ResolvedClientSettings {
}
}

impl ResolvedEditorSettings {
pub(crate) fn resolve(
&self,
project_root: &Path,
project_configuration: Configuration,
) -> crate::Result<ruff_workspace::Settings> {
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 }
Expand Down
82 changes: 58 additions & 24 deletions crates/ruff_server/src/session/workspace/ruff_settings.rs
Original file line number Diff line number Diff line change
@@ -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},
};
Expand Down Expand Up @@ -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<Self> {
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
}
Expand All @@ -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,
}),
);
}
}

Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion crates/ruff_workspace/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit afbfabe

Please sign in to comment.