Skip to content

Commit c442ea1

Browse files
committed
Store empty input as None
1 parent 9c506a9 commit c442ea1

File tree

1 file changed

+30
-22
lines changed
  • crates/rust-analyzer/src/config

1 file changed

+30
-22
lines changed

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

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ pub enum ConfigTreeError {
2424
/// Some rust-analyzer.toml files have changed, and/or the LSP client sent a new configuration.
2525
pub struct ConfigChanges {
2626
ra_toml_changes: Vec<vfs::ChangedFile>,
27-
xdg_config_change: Option<Arc<ConfigInput>>,
28-
client_change: Option<Arc<ConfigInput>>,
27+
/// - `None` => no change
28+
/// - `Some(None)` => the XDG_CONFIG_HOME rust-analyzer.toml file was deleted
29+
/// - `Some(Some(...))` => the XDG_CONFIG_HOME rust-analyzer.toml file was updated
30+
xdg_config_change: Option<Option<Arc<ConfigInput>>>,
31+
/// - `None` => no change
32+
/// - `Some(None)` => the client config was removed / reset or something
33+
/// - `Some(Some(...))` => the client config was updated
34+
client_change: Option<Option<Arc<ConfigInput>>>,
2935
parent_changes: Vec<ConfigParentChange>,
3036
}
3137

@@ -96,14 +102,13 @@ slotmap::new_key_type! {
96102

97103
struct ConfigNode {
98104
src: ConfigSource,
99-
// TODO: make option
100-
input: Arc<ConfigInput>,
105+
input: Option<Arc<ConfigInput>>,
101106
computed: ComputedIdx,
102107
}
103108

104109
struct ConfigTree {
105110
tree: indextree::Arena<ConfigNode>,
106-
client_config: Arc<ConfigInput>,
111+
client_config: Option<Arc<ConfigInput>>,
107112
xdg_config_node_id: NodeId,
108113
ra_file_id_map: FxHashMap<FileId, NodeId>,
109114
computed: SlotMap<ComputedIdx, Option<Arc<LocalConfigData>>>,
@@ -117,6 +122,9 @@ fn parse_toml(
117122
) -> Option<Arc<ConfigInput>> {
118123
let content = vfs.file_contents(file_id);
119124
let path = vfs.file_path(file_id);
125+
if content.is_empty() {
126+
return None;
127+
}
120128
let content_str = match std::str::from_utf8(content) {
121129
Err(e) => {
122130
tracing::error!("non-UTF8 TOML content for {path}: {e}");
@@ -146,18 +154,12 @@ impl ConfigTree {
146154
let mut ra_file_id_map = FxHashMap::default();
147155
let xdg_config = tree.new_node(ConfigNode {
148156
src: ConfigSource::RaToml(xdg_config_file_id),
149-
input: Arc::new(ConfigInput::default()),
157+
input: None,
150158
computed: computed.insert(Option::<Arc<LocalConfigData>>::None),
151159
});
152160
ra_file_id_map.insert(xdg_config_file_id, xdg_config);
153161

154-
Self {
155-
client_config: Arc::new(Default::default()),
156-
xdg_config_node_id: xdg_config,
157-
ra_file_id_map,
158-
tree,
159-
computed,
160-
}
162+
Self { client_config: None, xdg_config_node_id: xdg_config, ra_file_id_map, tree, computed }
161163
}
162164

163165
fn read_only(&self, file_id: FileId) -> Result<Option<Arc<LocalConfigData>>, ConfigTreeError> {
@@ -190,12 +192,20 @@ impl ConfigTree {
190192
{
191193
let self_input = node.input.clone();
192194
let parent_computed = self.compute_inner(parent)?;
193-
Arc::new(parent_computed.clone_with_overrides(self_input.local.clone()))
195+
if let Some(input) = self_input.as_deref() {
196+
Arc::new(parent_computed.clone_with_overrides(input.local.clone()))
197+
} else {
198+
parent_computed
199+
}
194200
} else {
195201
// We have hit a root node
196202
let self_input = node.input.clone();
197-
let root_local = RootLocalConfigData::from_root_input(self_input.local.clone());
198-
Arc::new(root_local.0)
203+
if let Some(input) = self_input.as_deref() {
204+
let root_local = RootLocalConfigData::from_root_input(input.local.clone());
205+
Arc::new(root_local.0)
206+
} else {
207+
Arc::new(LocalConfigData::default())
208+
}
199209
};
200210
// Get a new &mut slot because self.compute(parent) also gets mut access
201211
let slot = &mut self.computed[idx];
@@ -204,7 +214,7 @@ impl ConfigTree {
204214
}
205215
}
206216

207-
fn insert_toml(&mut self, file_id: FileId, input: Arc<ConfigInput>) -> NodeId {
217+
fn insert_toml(&mut self, file_id: FileId, input: Option<Arc<ConfigInput>>) -> NodeId {
208218
let computed = self.computed.insert(None);
209219
let node =
210220
self.tree.new_node(ConfigNode { src: ConfigSource::RaToml(file_id), input, computed });
@@ -215,7 +225,7 @@ impl ConfigTree {
215225
fn update_toml(
216226
&mut self,
217227
file_id: FileId,
218-
input: Arc<ConfigInput>,
228+
input: Option<Arc<ConfigInput>>,
219229
) -> Result<(), ConfigTreeError> {
220230
let Some(node_id) = self.ra_file_id_map.get(&file_id).cloned() else {
221231
return Err(ConfigTreeError::NonExistent);
@@ -281,13 +291,11 @@ impl ConfigTree {
281291
// turn and face the strain
282292
match change.change_kind {
283293
vfs::ChangeKind::Create => {
284-
let input = parse_toml(change.file_id, vfs, &mut scratch_errors, errors)
285-
.unwrap_or_default();
294+
let input = parse_toml(change.file_id, vfs, &mut scratch_errors, errors);
286295
let _new_node = self.insert_toml(change.file_id, input);
287296
}
288297
vfs::ChangeKind::Modify => {
289-
let input = parse_toml(change.file_id, vfs, &mut scratch_errors, errors)
290-
.unwrap_or_default();
298+
let input = parse_toml(change.file_id, vfs, &mut scratch_errors, errors);
291299
if let Err(e) = self.update_toml(change.file_id, input) {
292300
errors.push(e);
293301
}

0 commit comments

Comments
 (0)