Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruff server: Support setting to prioritize project configuration over editor configuration #11086

Merged
merged 4 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions crates/ruff_server/src/session/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(crate) struct ResolvedClientSettings {
/// Contains the resolved values of 'editor settings' - Ruff configuration for the linter/formatter that was passed in via
/// LSP client settings. These fields are optional because we don't want to override file-based linter/formatting settings
/// if these were un-set.
#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub(crate) struct ResolvedEditorSettings {
pub(super) lint_preview: Option<bool>,
Expand All @@ -38,6 +38,22 @@ 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,
snowsignal marked this conversation as resolved.
Show resolved Hide resolved
}

/// 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)]
#[cfg_attr(test, derive(PartialEq, Eq))]
#[serde(rename_all = "camelCase")]
pub(crate) enum ConfigResolutionStrategy {
snowsignal marked this conversation as resolved.
Show resolved Hide resolved
/// The default strategy - configuration set in the editor takes priority over
/// workspace configuration set in `.toml` files.
Default,
snowsignal marked this conversation as resolved.
Show resolved Hide resolved
/// An alternative strategy - configuration set in `.toml` files takes priority
/// over configuration set in the editor.
PrioritizeWorkspace,
}

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

/// This is a direct representation of the workspace settings schema,
Expand Down Expand Up @@ -251,6 +268,11 @@ 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(
all_settings,
|settings| settings.configuration_resolution_strategy,
ConfigResolutionStrategy::Default,
),
},
}
}
Expand Down Expand Up @@ -383,6 +405,7 @@ mod tests {
),
exclude: None,
line_length: None,
configuration_resolution_strategy: None,
},
workspace_settings: [
WorkspaceSettings {
Expand Down Expand Up @@ -429,6 +452,7 @@ mod tests {
),
exclude: None,
line_length: None,
configuration_resolution_strategy: None,
},
workspace: Url {
scheme: "file",
Expand Down Expand Up @@ -488,6 +512,7 @@ mod tests {
),
exclude: None,
line_length: None,
configuration_resolution_strategy: None,
},
workspace: Url {
scheme: "file",
Expand Down Expand Up @@ -538,7 +563,8 @@ mod tests {
extend_select: None,
ignore: None,
exclude: None,
line_length: None
line_length: None,
configuration_resolution_strategy: ConfigResolutionStrategy::Default,
}
}
);
Expand Down Expand Up @@ -566,7 +592,8 @@ mod tests {
extend_select: None,
ignore: None,
exclude: None,
line_length: None
line_length: None,
configuration_resolution_strategy: ConfigResolutionStrategy::Default,
}
}
);
Expand Down Expand Up @@ -620,6 +647,7 @@ mod tests {
80,
),
),
configuration_resolution_strategy: None,
},
),
}
Expand Down Expand Up @@ -648,7 +676,8 @@ mod tests {
extend_select: None,
ignore: Some(vec![RuleSelector::from_str("RUF001").unwrap()]),
exclude: Some(vec!["third_party".into()]),
line_length: Some(LineLength::try_from(80).unwrap())
line_length: Some(LineLength::try_from(80).unwrap()),
configuration_resolution_strategy: ConfigResolutionStrategy::Default,
}
}
);
Expand Down
12 changes: 10 additions & 2 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::ResolvedEditorSettings;
use crate::session::settings::{ConfigResolutionStrategy, ResolvedEditorSettings};

#[derive(Default)]
pub(crate) struct RuffSettings {
Expand Down Expand Up @@ -117,6 +117,7 @@ impl<'a> ConfigurationTransformer for EditorConfigurationTransformer<'a> {
ignore,
exclude,
line_length,
configuration_resolution_strategy,
} = self.0.clone();

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

editor_configuration.combine(project_configuration)
match configuration_resolution_strategy {
ConfigResolutionStrategy::Default => {
editor_configuration.combine(project_configuration)
}
ConfigResolutionStrategy::PrioritizeWorkspace => {
project_configuration.combine(editor_configuration)
}
}
}
}
Loading