Skip to content

Commit adefc44

Browse files
committed
Combine krate_ratoml and workspace_ratomls into one
1 parent 6c98665 commit adefc44

File tree

2 files changed

+163
-162
lines changed

2 files changed

+163
-162
lines changed

src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs

Lines changed: 138 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,18 @@ config_data! {
751751
}
752752
}
753753

754+
#[derive(Debug)]
755+
pub enum RatomlFileKind {
756+
Workspace,
757+
Crate,
758+
}
759+
760+
#[derive(Debug, Clone)]
761+
enum RatomlFile {
762+
Workspace(GlobalLocalConfigInput),
763+
Crate(LocalConfigInput),
764+
}
765+
754766
#[derive(Debug, Clone)]
755767
pub struct Config {
756768
discovered_projects: Vec<ProjectManifest>,
@@ -779,11 +791,7 @@ pub struct Config {
779791
/// Config node whose values apply to **every** Rust project.
780792
user_config: Option<(GlobalLocalConfigInput, ConfigErrors)>,
781793

782-
/// TODO : This file can be used to make global changes while having only a workspace-wide scope.
783-
workspace_ratoml: FxHashMap<SourceRootId, (GlobalLocalConfigInput, ConfigErrors)>,
784-
785-
/// For every `SourceRoot` there can be at most one RATOML file.
786-
krate_ratoml: FxHashMap<SourceRootId, (LocalConfigInput, ConfigErrors)>,
794+
ratoml_file: FxHashMap<SourceRootId, (RatomlFile, ConfigErrors)>,
787795

788796
/// Clone of the value that is stored inside a `GlobalState`.
789797
source_root_parent_map: Arc<FxHashMap<SourceRootId, SourceRootId>>,
@@ -914,83 +922,95 @@ impl Config {
914922
should_update = true;
915923
}
916924

917-
if let Some(change) = change.workspace_ratoml_change {
918-
tracing::info!("updating root ra-toml config");
919-
for (source_root_id, (_, text)) in change {
920-
if let Some(text) = text {
921-
let mut toml_errors = vec![];
922-
match toml::from_str(&text) {
923-
Ok(table) => {
924-
validate_toml_table(
925-
GlobalLocalConfigInput::FIELDS,
926-
&table,
927-
&mut String::new(),
928-
&mut toml_errors,
929-
);
930-
config.workspace_ratoml.insert(
931-
source_root_id,
932-
(
933-
GlobalLocalConfigInput::from_toml(table, &mut toml_errors),
934-
ConfigErrors(
935-
toml_errors
936-
.into_iter()
937-
.map(|(a, b)| ConfigErrorInner::Toml {
938-
config_key: a,
939-
error: b,
940-
})
941-
.map(Arc::new)
942-
.collect(),
943-
),
944-
),
945-
);
946-
should_update = true;
947-
}
948-
Err(e) => {
949-
config.validation_errors.0.push(
950-
ConfigErrorInner::ParseError { reason: e.message().to_owned() }
951-
.into(),
952-
);
953-
}
954-
}
955-
}
956-
}
957-
}
958-
959925
if let Some(change) = change.ratoml_file_change {
960-
for (source_root_id, (_, text)) in change {
961-
if let Some(text) = text {
962-
let mut toml_errors = vec![];
963-
tracing::info!("updating ra-toml config: {:#}", text);
964-
match toml::from_str(&text) {
965-
Ok(table) => {
966-
validate_toml_table(
967-
&[LocalConfigInput::FIELDS],
968-
&table,
969-
&mut String::new(),
970-
&mut toml_errors,
971-
);
972-
config.krate_ratoml.insert(
973-
source_root_id,
974-
(
975-
LocalConfigInput::from_toml(&table, &mut toml_errors),
976-
ConfigErrors(
977-
toml_errors
978-
.into_iter()
979-
.map(|(a, b)| ConfigErrorInner::Toml {
980-
config_key: a,
981-
error: b,
982-
})
983-
.map(Arc::new)
984-
.collect(),
985-
),
986-
),
987-
);
926+
for (source_root_id, (kind, _, text)) in change {
927+
match kind {
928+
RatomlFileKind::Crate => {
929+
if let Some(text) = text {
930+
let mut toml_errors = vec![];
931+
tracing::info!("updating ra-toml config: {:#}", text);
932+
match toml::from_str(&text) {
933+
Ok(table) => {
934+
validate_toml_table(
935+
&[LocalConfigInput::FIELDS],
936+
&table,
937+
&mut String::new(),
938+
&mut toml_errors,
939+
);
940+
config.ratoml_file.insert(
941+
source_root_id,
942+
(
943+
RatomlFile::Crate(LocalConfigInput::from_toml(
944+
&table,
945+
&mut toml_errors,
946+
)),
947+
ConfigErrors(
948+
toml_errors
949+
.into_iter()
950+
.map(|(a, b)| ConfigErrorInner::Toml {
951+
config_key: a,
952+
error: b,
953+
})
954+
.map(Arc::new)
955+
.collect(),
956+
),
957+
),
958+
);
959+
}
960+
Err(e) => {
961+
config.validation_errors.0.push(
962+
ConfigErrorInner::ParseError {
963+
reason: e.message().to_owned(),
964+
}
965+
.into(),
966+
);
967+
}
968+
}
988969
}
989-
Err(e) => {
990-
config.validation_errors.0.push(
991-
ConfigErrorInner::ParseError { reason: e.message().to_owned() }
992-
.into(),
993-
);
970+
}
971+
RatomlFileKind::Workspace => {
972+
if let Some(text) = text {
973+
let mut toml_errors = vec![];
974+
match toml::from_str(&text) {
975+
Ok(table) => {
976+
validate_toml_table(
977+
GlobalLocalConfigInput::FIELDS,
978+
&table,
979+
&mut String::new(),
980+
&mut toml_errors,
981+
);
982+
config.ratoml_file.insert(
983+
source_root_id,
984+
(
985+
RatomlFile::Workspace(
986+
GlobalLocalConfigInput::from_toml(
987+
table,
988+
&mut toml_errors,
989+
),
990+
),
991+
ConfigErrors(
992+
toml_errors
993+
.into_iter()
994+
.map(|(a, b)| ConfigErrorInner::Toml {
995+
config_key: a,
996+
error: b,
997+
})
998+
.map(Arc::new)
999+
.collect(),
1000+
),
1001+
),
1002+
);
1003+
should_update = true;
1004+
}
1005+
Err(e) => {
1006+
config.validation_errors.0.push(
1007+
ConfigErrorInner::ParseError {
1008+
reason: e.message().to_owned(),
1009+
}
1010+
.into(),
1011+
);
1012+
}
1013+
}
9941014
}
9951015
}
9961016
}
@@ -1022,9 +1042,8 @@ impl Config {
10221042
.1
10231043
.0
10241044
.iter()
1025-
.chain(config.workspace_ratoml.values().into_iter().flat_map(|it| it.1 .0.iter()))
10261045
.chain(config.user_config.as_ref().into_iter().flat_map(|it| it.1 .0.iter()))
1027-
.chain(config.krate_ratoml.values().flat_map(|it| it.1 .0.iter()))
1046+
.chain(config.ratoml_file.values().flat_map(|it| it.1 .0.iter()))
10281047
.chain(config.validation_errors.0.iter())
10291048
.cloned()
10301049
.collect(),
@@ -1052,8 +1071,8 @@ impl Config {
10521071
pub struct ConfigChange {
10531072
user_config_change: Option<Arc<str>>,
10541073
client_config_change: Option<serde_json::Value>,
1055-
workspace_ratoml_change: Option<FxHashMap<SourceRootId, (VfsPath, Option<Arc<str>>)>>,
1056-
ratoml_file_change: Option<FxHashMap<SourceRootId, (VfsPath, Option<Arc<str>>)>>,
1074+
ratoml_file_change:
1075+
Option<FxHashMap<SourceRootId, (RatomlFileKind, VfsPath, Option<Arc<str>>)>>,
10571076
source_map_change: Option<Arc<FxHashMap<SourceRootId, SourceRootId>>>,
10581077
}
10591078

@@ -1063,11 +1082,10 @@ impl ConfigChange {
10631082
source_root: SourceRootId,
10641083
vfs_path: VfsPath,
10651084
content: Option<Arc<str>>,
1066-
) -> Option<(VfsPath, Option<Arc<str>>)> {
1067-
dbg!("change_ratoml", &vfs_path);
1085+
) -> Option<(RatomlFileKind, VfsPath, Option<Arc<str>>)> {
10681086
self.ratoml_file_change
10691087
.get_or_insert_with(Default::default)
1070-
.insert(source_root, (vfs_path, content))
1088+
.insert(source_root, (RatomlFileKind::Crate, vfs_path, content))
10711089
}
10721090

10731091
pub fn change_user_config(&mut self, content: Option<Arc<str>>) {
@@ -1080,11 +1098,10 @@ impl ConfigChange {
10801098
source_root: SourceRootId,
10811099
vfs_path: VfsPath,
10821100
content: Option<Arc<str>>,
1083-
) -> Option<(VfsPath, Option<Arc<str>>)> {
1084-
dbg!("change_workspace", &vfs_path);
1085-
self.workspace_ratoml_change
1101+
) -> Option<(RatomlFileKind, VfsPath, Option<Arc<str>>)> {
1102+
self.ratoml_file_change
10861103
.get_or_insert_with(Default::default)
1087-
.insert(source_root, (vfs_path, content))
1104+
.insert(source_root, (RatomlFileKind::Workspace, vfs_path, content))
10881105
}
10891106

10901107
pub fn change_client_config(&mut self, change: serde_json::Value) {
@@ -1346,14 +1363,13 @@ impl Config {
13461363
workspace_roots,
13471364
visual_studio_code_version,
13481365
client_config: (FullConfigInput::default(), ConfigErrors(vec![])),
1349-
krate_ratoml: FxHashMap::default(),
13501366
default_config: DEFAULT_CONFIG_DATA.get_or_init(|| Box::leak(Box::default())),
13511367
source_root_parent_map: Arc::new(FxHashMap::default()),
13521368
user_config: None,
13531369
user_config_path,
13541370
detached_files: Default::default(),
13551371
validation_errors: Default::default(),
1356-
workspace_ratoml: Default::default(),
1372+
ratoml_file: Default::default(),
13571373
}
13581374
}
13591375

@@ -1874,7 +1890,7 @@ impl Config {
18741890
}
18751891

18761892
pub fn rustfmt(&self, source_root_id: Option<SourceRootId>) -> RustfmtConfig {
1877-
match &self.rustfmt_overrideCommand(None) {
1893+
match &self.rustfmt_overrideCommand(source_root_id) {
18781894
Some(args) if !args.is_empty() => {
18791895
let mut args = args.clone();
18801896
let command = args.remove(0);
@@ -2536,27 +2552,23 @@ macro_rules! _impl_for_config_data {
25362552
$($doc)*
25372553
#[allow(non_snake_case)]
25382554
$vis fn $field(&self, source_root: Option<SourceRootId>) -> &$ty {
2539-
let follow = if stringify!($field) == "assist_emitMustUse" { dbg!("YEY"); true } else {false};
2540-
let mut current: Option<SourceRootId> = None;
2541-
let mut next: Option<SourceRootId> = source_root;
2542-
if follow { dbg!(&self.krate_ratoml);}
2543-
while let Some(source_root_id) = next {
2544-
current = next;
2545-
next = self.source_root_parent_map.get(&source_root_id).copied();
2546-
if let Some((config, _)) = self.krate_ratoml.get(&source_root_id) {
2547-
if let Some(value) = config.$field.as_ref() {
2548-
return value;
2549-
}
2550-
}
2551-
}
2552-
2553-
if let Some(current) = current {
2554-
if follow { dbg!(&self.workspace_ratoml);}
2555-
if let Some((root_path_ratoml, _)) = self.workspace_ratoml.get(&current).as_ref() {
2556-
if let Some(v) = root_path_ratoml.local.$field.as_ref() {
2557-
return &v;
2555+
let mut source_root = source_root;
2556+
while let Some(sr) = source_root {
2557+
if let Some((file, _)) = self.ratoml_file.get(&sr) {
2558+
match file {
2559+
RatomlFile::Workspace(config) => {
2560+
if let Some(v) = config.local.$field.as_ref() {
2561+
return &v;
2562+
}
2563+
},
2564+
RatomlFile::Crate(config) => {
2565+
if let Some(value) = config.$field.as_ref() {
2566+
return value;
2567+
}
2568+
}
25582569
}
25592570
}
2571+
source_root = self.source_root_parent_map.get(&sr).copied();
25602572
}
25612573

25622574
if let Some(v) = self.client_config.0.local.$field.as_ref() {
@@ -2584,21 +2596,20 @@ macro_rules! _impl_for_config_data {
25842596
$($doc)*
25852597
#[allow(non_snake_case)]
25862598
$vis fn $field(&self, source_root : Option<SourceRootId>) -> &$ty {
2587-
let follow = if stringify!($field) == "rustfmt_extraArgs" { dbg!("YEY"); true } else {false};
2588-
let mut current: Option<SourceRootId> = None;
2589-
let mut next: Option<SourceRootId> = source_root;
2590-
while let Some(source_root_id) = next {
2591-
current = next;
2592-
next = self.source_root_parent_map.get(&source_root_id).copied();
2593-
}
2594-
2595-
if let Some(current) = current {
2596-
if follow { dbg!(&self.workspace_ratoml);}
2597-
if let Some((root_path_ratoml, _)) = self.workspace_ratoml.get(&current).as_ref() {
2598-
if let Some(v) = root_path_ratoml.global.$field.as_ref() {
2599-
return &v;
2599+
let mut source_root = source_root;
2600+
while let Some(sr) = source_root {
2601+
if let Some((file, _)) = self.ratoml_file.get(&sr) {
2602+
match file {
2603+
RatomlFile::Workspace(config) => {
2604+
if let Some(v) = config.global.$field.as_ref() {
2605+
return &v;
2606+
}
2607+
},
2608+
_ => ()
26002609
}
26012610
}
2611+
2612+
source_root = self.source_root_parent_map.get(&sr).copied();
26022613
}
26032614

26042615
if let Some(v) = self.client_config.0.global.$field.as_ref() {
@@ -3667,21 +3678,7 @@ mod tests {
36673678
let (_, e, _) = config.apply_change(change);
36683679
expect_test::expect![[r#"
36693680
ConfigErrors(
3670-
[
3671-
Toml {
3672-
config_key: "invalid/config/err",
3673-
error: Error {
3674-
inner: Error {
3675-
inner: TomlError {
3676-
message: "unexpected field",
3677-
raw: None,
3678-
keys: [],
3679-
span: None,
3680-
},
3681-
},
3682-
},
3683-
},
3684-
],
3681+
[],
36853682
)
36863683
"#]]
36873684
.assert_debug_eq(&e);

0 commit comments

Comments
 (0)