Skip to content

Commit

Permalink
Rename 'configuration resolution strategy' to 'configuration preferen…
Browse files Browse the repository at this point in the history
…ce', rename variants, and introduce a third, editor-only variant.
  • Loading branch information
snowsignal committed Apr 26, 2024
1 parent f4ad8e8 commit c8b6b56
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
41 changes: 21 additions & 20 deletions crates/ruff_server/src/session/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,23 @@ pub(crate) struct ResolvedEditorSettings {
pub(super) ignore: Option<Vec<RuleSelector>>,
pub(super) exclude: Option<Vec<String>>,
pub(super) line_length: Option<LineLength>,
pub(super) configuration_resolution_strategy: ConfigResolutionStrategy,
pub(super) configuration_preference: ConfigurationPreference,
}

/// Determines how multiple conflicting configurations should be resolved - in this
/// case, the configuration from the client settings and configuration from local
/// `.toml` files (aka 'workspace' configuration).
#[derive(Clone, Copy, Debug, Deserialize)]
#[derive(Clone, Copy, Debug, Deserialize, Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
#[serde(rename_all = "camelCase")]
pub(crate) enum ConfigResolutionStrategy {
/// The default strategy - configuration set in the editor takes priority over
/// workspace configuration set in `.toml` files.
Default,
/// An alternative strategy - configuration set in `.toml` files takes priority
/// over configuration set in the editor.
PrioritizeWorkspace,
pub(crate) enum ConfigurationPreference {
/// Configuration set in the editor takes priority over workspace configuration set in `.toml` files.
#[default]
EditorFirst,
/// Configuration set in `.toml` files takes priority over configuration set in the editor.
FilesystemFirst,
/// `.toml` files are ignored completely, and only the editor configuration is used.
EditorOnly,
}

/// This is a direct representation of the settings schema sent by the client.
Expand All @@ -68,7 +69,7 @@ pub(crate) struct ClientSettings {
code_action: Option<CodeActionOptions>,
exclude: Option<Vec<String>>,
line_length: Option<LineLength>,
configuration_resolution_strategy: Option<ConfigResolutionStrategy>,
configuration_preference: Option<ConfigurationPreference>,
}

/// This is a direct representation of the workspace settings schema,
Expand Down Expand Up @@ -268,10 +269,10 @@ impl ResolvedClientSettings {
Some(settings.exclude.as_ref()?.clone())
}),
line_length: Self::resolve_optional(all_settings, |settings| settings.line_length),
configuration_resolution_strategy: Self::resolve_or(
configuration_preference: Self::resolve_or(
all_settings,
|settings| settings.configuration_resolution_strategy,
ConfigResolutionStrategy::Default,
|settings| settings.configuration_preference,
ConfigurationPreference::EditorFirst,
),
},
}
Expand Down Expand Up @@ -405,7 +406,7 @@ mod tests {
),
exclude: None,
line_length: None,
configuration_resolution_strategy: None,
configuration_preference: None,
},
workspace_settings: [
WorkspaceSettings {
Expand Down Expand Up @@ -452,7 +453,7 @@ mod tests {
),
exclude: None,
line_length: None,
configuration_resolution_strategy: None,
configuration_preference: None,
},
workspace: Url {
scheme: "file",
Expand Down Expand Up @@ -512,7 +513,7 @@ mod tests {
),
exclude: None,
line_length: None,
configuration_resolution_strategy: None,
configuration_preference: None,
},
workspace: Url {
scheme: "file",
Expand Down Expand Up @@ -564,7 +565,7 @@ mod tests {
ignore: None,
exclude: None,
line_length: None,
configuration_resolution_strategy: ConfigResolutionStrategy::Default,
configuration_preference: ConfigurationPreference::EditorFirst,
}
}
);
Expand Down Expand Up @@ -593,7 +594,7 @@ mod tests {
ignore: None,
exclude: None,
line_length: None,
configuration_resolution_strategy: ConfigResolutionStrategy::Default,
configuration_preference: ConfigurationPreference::EditorFirst,
}
}
);
Expand Down Expand Up @@ -647,7 +648,7 @@ mod tests {
80,
),
),
configuration_resolution_strategy: None,
configuration_preference: None,
},
),
}
Expand Down Expand Up @@ -677,7 +678,7 @@ mod tests {
ignore: Some(vec![RuleSelector::from_str("RUF001").unwrap()]),
exclude: Some(vec!["third_party".into()]),
line_length: Some(LineLength::try_from(80).unwrap()),
configuration_resolution_strategy: ConfigResolutionStrategy::Default,
configuration_preference: ConfigurationPreference::EditorFirst,
}
}
);
Expand Down
17 changes: 9 additions & 8 deletions crates/ruff_server/src/session/workspace/ruff_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{
};
use walkdir::{DirEntry, WalkDir};

use crate::session::settings::{ConfigResolutionStrategy, ResolvedEditorSettings};
use crate::session::settings::{ConfigurationPreference, ResolvedEditorSettings};

#[derive(Default)]
pub(crate) struct RuffSettings {
Expand Down Expand Up @@ -107,7 +107,7 @@ struct EditorConfigurationTransformer<'a>(&'a ResolvedEditorSettings, &'a Path);
impl<'a> ConfigurationTransformer for EditorConfigurationTransformer<'a> {
fn transform(
&self,
project_configuration: ruff_workspace::configuration::Configuration,
filesystem_configuration: ruff_workspace::configuration::Configuration,
) -> ruff_workspace::configuration::Configuration {
let ResolvedEditorSettings {
format_preview,
Expand All @@ -117,7 +117,7 @@ impl<'a> ConfigurationTransformer for EditorConfigurationTransformer<'a> {
ignore,
exclude,
line_length,
configuration_resolution_strategy,
configuration_preference,
} = self.0.clone();

let project_root = self.1;
Expand Down Expand Up @@ -150,13 +150,14 @@ impl<'a> ConfigurationTransformer for EditorConfigurationTransformer<'a> {
..Default::default()
};

match configuration_resolution_strategy {
ConfigResolutionStrategy::Default => {
editor_configuration.combine(project_configuration)
match configuration_preference {
ConfigurationPreference::EditorFirst => {
editor_configuration.combine(filesystem_configuration)
}
ConfigResolutionStrategy::PrioritizeWorkspace => {
project_configuration.combine(editor_configuration)
ConfigurationPreference::FilesystemFirst => {
filesystem_configuration.combine(editor_configuration)
}
ConfigurationPreference::EditorOnly => editor_configuration,
}
}
}

0 comments on commit c8b6b56

Please sign in to comment.