-
Notifications
You must be signed in to change notification settings - Fork 893
/
ruff_settings.rs
200 lines (179 loc) · 6.44 KB
/
ruff_settings.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use ruff_linter::{
display_settings, fs::normalize_path_to, settings::types::FilePattern,
settings::types::PreviewMode,
};
use ruff_workspace::{
configuration::{Configuration, FormatConfiguration, LintConfiguration, RuleSelection},
pyproject::{find_user_settings_toml, settings_toml},
resolver::{ConfigurationTransformer, Relativity},
};
use std::{
collections::BTreeMap,
path::{Path, PathBuf},
sync::Arc,
};
use walkdir::{DirEntry, WalkDir};
use crate::session::settings::{ConfigurationPreference, ResolvedEditorSettings};
#[derive(Default)]
pub(crate) struct RuffSettings {
// settings to pass into the ruff linter
linter: ruff_linter::settings::LinterSettings,
// settings to pass into the ruff formatter
formatter: ruff_workspace::FormatterSettings,
}
pub(super) struct RuffSettingsIndex {
index: BTreeMap<PathBuf, Arc<RuffSettings>>,
fallback: Arc<RuffSettings>,
}
impl std::fmt::Display for RuffSettings {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
display_settings! {
formatter = f,
fields = [
self.linter,
self.formatter
]
}
Ok(())
}
}
impl RuffSettings {
pub(crate) fn linter(&self) -> &ruff_linter::settings::LinterSettings {
&self.linter
}
pub(crate) fn formatter(&self) -> &ruff_workspace::FormatterSettings {
&self.formatter
}
}
impl RuffSettingsIndex {
pub(super) fn new(root: &Path, editor_settings: &ResolvedEditorSettings) -> Self {
let mut index = BTreeMap::default();
for directory in WalkDir::new(root)
.into_iter()
.filter_map(Result::ok)
.filter(|entry| entry.file_type().is_dir())
.map(DirEntry::into_path)
{
if let Some(pyproject) = settings_toml(&directory).ok().flatten() {
let Ok(settings) = ruff_workspace::resolver::resolve_root_settings(
&pyproject,
Relativity::Parent,
&EditorConfigurationTransformer(editor_settings, root),
) else {
continue;
};
index.insert(
directory,
Arc::new(RuffSettings {
linter: settings.linter,
formatter: settings.formatter,
}),
);
}
}
let fallback = find_user_settings_toml()
.and_then(|user_settings| {
ruff_workspace::resolver::resolve_root_settings(
&user_settings,
Relativity::Cwd,
&EditorConfigurationTransformer(editor_settings, root),
)
.ok()
})
.unwrap_or_default();
Self {
index,
fallback: Arc::new(RuffSettings {
formatter: fallback.formatter,
linter: fallback.linter,
}),
}
}
pub(super) fn get(&self, document_path: &Path) -> Arc<RuffSettings> {
if let Some((_, settings)) = self
.index
.range(..document_path.to_path_buf())
.rev()
.find(|(path, _)| document_path.starts_with(path))
{
return settings.clone();
}
tracing::info!("No ruff settings file (pyproject.toml/ruff.toml/.ruff.toml) found for {} - falling back to default configuration", document_path.display());
self.fallback.clone()
}
}
struct EditorConfigurationTransformer<'a>(&'a ResolvedEditorSettings, &'a Path);
impl<'a> ConfigurationTransformer for EditorConfigurationTransformer<'a> {
fn transform(
&self,
filesystem_configuration: ruff_workspace::configuration::Configuration,
) -> ruff_workspace::configuration::Configuration {
let ResolvedEditorSettings {
configuration,
format_preview,
lint_preview,
select,
extend_select,
ignore,
exclude,
line_length,
configuration_preference,
} = 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()
};
// Merge in the editor-specified configuration file, if it exists.
let editor_configuration = if let Some(config_file_path) = configuration {
match open_configuration_file(&config_file_path, project_root) {
Ok(config_from_file) => editor_configuration.combine(config_from_file),
Err(err) => {
tracing::error!("Unable to find editor-specified configuration file {err}");
editor_configuration
}
}
} else {
editor_configuration
};
match configuration_preference {
ConfigurationPreference::EditorFirst => {
editor_configuration.combine(filesystem_configuration)
}
ConfigurationPreference::FilesystemFirst => {
filesystem_configuration.combine(editor_configuration)
}
ConfigurationPreference::EditorOnly => editor_configuration,
}
}
}
fn open_configuration_file(
config_path: &Path,
project_root: &Path,
) -> crate::Result<Configuration> {
let options = ruff_workspace::pyproject::load_options(config_path)?;
Configuration::from_options(options, Some(config_path), project_root)
}