mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-13 17:25:20 +00:00
Implement get_or_insert for configuration index
This commit is contained in:
parent
f7c073d441
commit
fc35618f17
1 changed files with 41 additions and 4 deletions
|
|
@ -1,6 +1,11 @@
|
||||||
|
use anyhow::anyhow;
|
||||||
use lsp_types::Url;
|
use lsp_types::Url;
|
||||||
use ruff_workspace::resolver::ConfigurationTransformer;
|
use ruff_workspace::resolver::{ConfigurationTransformer, Relativity};
|
||||||
use std::{collections::BTreeMap, path::PathBuf, sync::Arc};
|
use std::{
|
||||||
|
collections::BTreeMap,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub(crate) struct RuffConfiguration {
|
pub(crate) struct RuffConfiguration {
|
||||||
|
|
@ -16,13 +21,45 @@ pub(super) struct ConfigurationIndex {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConfigurationIndex {
|
impl ConfigurationIndex {
|
||||||
pub(super) fn get_or_insert(&mut self, path: &Url) -> Arc<RuffConfiguration> {
|
pub(super) fn get_or_insert(&mut self, document_url: &Url) -> Arc<RuffConfiguration> {
|
||||||
todo!("impl");
|
let document_path = document_url
|
||||||
|
.to_file_path()
|
||||||
|
.expect("document URL should be a valid path");
|
||||||
|
let folder = document_path
|
||||||
|
.parent()
|
||||||
|
.expect("document URL should be a file path and have a parent");
|
||||||
|
if let Some(config) = self.index.get(folder) {
|
||||||
|
return config.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
let config = Arc::new(Self::find_configuration_at_path(folder).unwrap_or_else(|err| {
|
||||||
|
tracing::error!("The following error occurred when trying to find a configuration file at `{}`:\n{err}", document_path.display());
|
||||||
|
tracing::error!("Falling back to default configuration for `{}`", document_path.display());
|
||||||
|
RuffConfiguration::default()
|
||||||
|
}));
|
||||||
|
|
||||||
|
self.index.insert(folder.to_path_buf(), config.clone());
|
||||||
|
|
||||||
|
config
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn clear(&mut self) {
|
pub(super) fn clear(&mut self) {
|
||||||
self.index.clear();
|
self.index.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn find_configuration_at_path(folder: &Path) -> crate::Result<RuffConfiguration> {
|
||||||
|
let pyproject = ruff_workspace::pyproject::find_settings_toml(folder)?
|
||||||
|
.ok_or_else(|| anyhow!("No pyproject.toml/ruff.toml/.ruff.toml file was found"))?;
|
||||||
|
let settings = ruff_workspace::resolver::resolve_root_settings(
|
||||||
|
&pyproject,
|
||||||
|
Relativity::Parent,
|
||||||
|
&LSPConfigTransformer,
|
||||||
|
)?;
|
||||||
|
Ok(RuffConfiguration {
|
||||||
|
linter: settings.linter,
|
||||||
|
formatter: settings.formatter,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct LSPConfigTransformer;
|
struct LSPConfigTransformer;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue