Skip to content

Commit 1179c3e

Browse files
committed
Auto merge of #16639 - alibektas:13529/config_restruct, r=Veykril
internal : redesign rust-analyzer::config This PR aims to cover the infrastructural requirements for the `rust-analyzer.toml` ( #13529 ) issue. This means, that 1. We no longer have a single config base. The once single `ConfigData` has been divided into 4 : A tree of `.ratoml` files, a set of configs coming from the client ( this is what was called before the `CrateData` except that now values do not default to anything when they are not defined) , a set of configs that will reflect what the contents of a `ratoml` file defined in user's config directory ( e.g `~/.config/rust-analyzer/.rust-analyzer.toml` and finally a tree root that is populated by default values only. 2. Configs have also been divided into 3 different blocks : `global` , `local` , `client`. The current status of a config may change until #13529 got merged. Once again many thanks to `@cormacrelf` for doing all the serde work.
2 parents e64610d + 60d3a73 commit 1179c3e

15 files changed

Lines changed: 1381 additions & 922 deletions

File tree

‎Cargo.lock‎

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/base-db/src/input.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ use vfs::{file_set::FileSet, AbsPathBuf, AnchoredPath, FileId, VfsPath};
1919
// Map from crate id to the name of the crate and path of the proc-macro. If the value is `None`,
2020
// then the crate for the proc-macro hasn't been build yet as the build data is missing.
2121
pubtypeProcMacroPaths = FxHashMap<CrateId,Result<(Option<String>,AbsPathBuf),String>>;
22+
23+
#[derive(Debug,Clone,Copy,PartialEq,Eq,Hash,PartialOrd,Ord)]
24+
pubstructSourceRootId(pubu32);
25+
2226
/// Files are grouped into source roots. A source root is a directory on the
2327
/// file systems which is watched for changes. Typically it corresponds to a
2428
/// Rust crate. Source roots *might* be nested: in this case, a file belongs to
2529
/// the nearest enclosing source root. Paths to files are always relative to a
2630
/// source root, and the analyzer does not know the root path of the source root at
2731
/// all. So, a file from one source root can't refer to a file in another source
2832
/// root by path.
29-
#[derive(Debug,Clone,Copy,PartialEq,Eq,Hash,PartialOrd,Ord)]
30-
pubstructSourceRootId(pubu32);
31-
3233
#[derive(Clone,Debug,PartialEq,Eq)]
3334
pubstructSourceRoot{
3435
/// Sysroot or crates.io library.

‎crates/ide/src/expand_macro.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ fn _format(
177177
use ide_db::base_db::{FileLoader,SourceDatabase};
178178
// hack until we get hygiene working (same character amount to preserve formatting as much as possible)
179179
constDOLLAR_CRATE_REPLACE:&str = "__r_a_";
180-
let expansion = expansion.replace("$crate",DOLLAR_CRATE_REPLACE);
180+
constBUILTIN_REPLACE:&str = "builtin__POUND";
181+
let expansion =
182+
expansion.replace("$crate",DOLLAR_CRATE_REPLACE).replace("builtin #",BUILTIN_REPLACE);
181183
let(prefix, suffix) = match kind {
182184
SyntaxKind::MACRO_PAT => ("fn __(",": u32);"),
183185
SyntaxKind::MACRO_EXPR | SyntaxKind::MACRO_STMTS => ("fn __() {","}"),
@@ -206,7 +208,9 @@ fn _format(
206208
let captured_stdout = String::from_utf8(output.stdout).ok()?;
207209

208210
if output.status.success() && !captured_stdout.trim().is_empty(){
209-
let output = captured_stdout.replace(DOLLAR_CRATE_REPLACE,"$crate");
211+
let output = captured_stdout
212+
.replace(DOLLAR_CRATE_REPLACE,"$crate")
213+
.replace(BUILTIN_REPLACE,"builtin #");
210214
let output = output.trim().strip_prefix(prefix)?;
211215
let output = match kind {
212216
SyntaxKind::MACRO_PAT => {

‎crates/ide/src/lib.rs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use hir::ChangeWithProcMacros;
6666
use ide_db::{
6767
base_db::{
6868
salsa::{self,ParallelDatabase},
69-
CrateOrigin,Env,FileLoader,FileSet,SourceDatabase,VfsPath,
69+
CrateOrigin,Env,FileLoader,FileSet,SourceDatabase,SourceDatabaseExt,VfsPath,
7070
},
7171
prime_caches, symbol_index,FxHashMap,FxIndexSet,LineIndexDatabase,
7272
};
@@ -273,6 +273,10 @@ impl Analysis {
273273
self.with_db(|db| status::status(db, file_id))
274274
}
275275

276+
pubfnsource_root(&self,file_id:FileId) -> Cancellable<SourceRootId>{
277+
self.with_db(|db| db.file_source_root(file_id))
278+
}
279+
276280
pubfnparallel_prime_caches<F>(&self,num_worker_threads:u8,cb:F) -> Cancellable<()>
277281
where
278282
F:Fn(ParallelPrimeCachesProgress) + Sync + std::panic::UnwindSafe,
@@ -282,7 +286,7 @@ impl Analysis {
282286

283287
/// Gets the text of the source file.
284288
pubfnfile_text(&self,file_id:FileId) -> Cancellable<Arc<str>>{
285-
self.with_db(|db| db.file_text(file_id))
289+
self.with_db(|db| SourceDatabaseExt::file_text(db,file_id))
286290
}
287291

288292
/// Gets the syntax tree of the file.
@@ -292,7 +296,6 @@ impl Analysis {
292296

293297
/// Returns true if this file belongs to an immutable library.
294298
pubfnis_library_file(&self,file_id:FileId) -> Cancellable<bool>{
295-
use ide_db::base_db::SourceDatabaseExt;
296299
self.with_db(|db| db.source_root(db.file_source_root(file_id)).is_library)
297300
}
298301

‎crates/project-model/src/cfg_flag.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
use std::{fmt, str::FromStr};
55

66
use cfg::CfgOptions;
7+
use serde::Serialize;
78

8-
#[derive(Clone,Eq,PartialEq,Debug)]
9+
#[derive(Clone,Eq,PartialEq,Debug,Serialize)]
910
pubenumCfgFlag{
1011
Atom(String),
1112
KeyValue{key:String,value:String},

‎crates/project-model/src/project_json.rs‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
use base_db::{CrateDisplayName,CrateName};
5353
use paths::{AbsPath,AbsPathBuf,Utf8PathBuf};
5454
use rustc_hash::FxHashMap;
55-
use serde::{de,Deserialize};
55+
use serde::{de,Deserialize,Serialize};
5656
use span::Edition;
5757

5858
usecrate::cfg_flag::CfgFlag;
@@ -161,14 +161,14 @@ impl ProjectJson {
161161
}
162162
}
163163

164-
#[derive(Deserialize,Debug,Clone)]
164+
#[derive(Serialize,Deserialize,Debug,Clone)]
165165
pubstructProjectJsonData{
166166
sysroot:Option<Utf8PathBuf>,
167167
sysroot_src:Option<Utf8PathBuf>,
168168
crates:Vec<CrateData>,
169169
}
170170

171-
#[derive(Deserialize,Debug,Clone)]
171+
#[derive(Serialize,Deserialize,Debug,Clone)]
172172
structCrateData{
173173
display_name:Option<String>,
174174
root_module:Utf8PathBuf,
@@ -190,7 +190,7 @@ struct CrateData {
190190
repository:Option<String>,
191191
}
192192

193-
#[derive(Deserialize,Debug,Clone)]
193+
#[derive(Serialize,Deserialize,Debug,Clone)]
194194
#[serde(rename = "edition")]
195195
enumEditionData{
196196
#[serde(rename = "2015")]
@@ -218,20 +218,21 @@ impl From<EditionData> for Edition {
218218
///
219219
/// This will differ from `CrateId` when multiple `ProjectJson`
220220
/// workspaces are loaded.
221-
#[derive(Deserialize,Debug,Clone,Copy,Eq,PartialEq,Hash)]
221+
#[derive(Serialize,Deserialize,Debug,Clone,Copy,Eq,PartialEq,Hash)]
222222
#[serde(transparent)]
223223
pubstructCrateArrayIdx(pubusize);
224224

225-
#[derive(Deserialize,Debug,Clone,Eq,PartialEq)]
225+
#[derive(Serialize,Deserialize,Debug,Clone,Eq,PartialEq)]
226226
pub(crate)structDep{
227227
/// Identifies a crate by position in the crates array.
228228
#[serde(rename = "crate")]
229229
pub(crate)krate:CrateArrayIdx,
230+
#[serde(serialize_with = "serialize_crate_name")]
230231
#[serde(deserialize_with = "deserialize_crate_name")]
231232
pub(crate)name:CrateName,
232233
}
233234

234-
#[derive(Deserialize,Debug,Clone)]
235+
#[derive(Serialize,Deserialize,Debug,Clone)]
235236
structCrateSource{
236237
include_dirs:Vec<Utf8PathBuf>,
237238
exclude_dirs:Vec<Utf8PathBuf>,
@@ -244,3 +245,10 @@ where
244245
let name = String::deserialize(de)?;
245246
CrateName::new(&name).map_err(|err| de::Error::custom(format!("invalid crate name: {err:?}")))
246247
}
248+
249+
fnserialize_crate_name<S>(name:&CrateName,se:S) -> Result<S::Ok,S::Error>
250+
where
251+
S: serde::Serializer,
252+
{
253+
se.serialize_str(name)
254+
}

‎crates/rust-analyzer/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ tracing.workspace = true
3939
tracing-subscriber.workspace = true
4040
tracing-tree.workspace = true
4141
triomphe.workspace = true
42+
toml = "0.8.8"
4243
nohash-hasher.workspace = true
4344
always-assert = "0.2.0"
4445
walkdir = "2.3.2"
4546
semver.workspace = true
4647
memchr = "2.7.1"
48+
indexmap = { workspace = true, features = ["serde"] }
4749

4850
cfg.workspace = true
4951
flycheck.workspace = true

0 commit comments

Comments
 (0)