Skip to content

Commit b3b9b53

Browse files
committed
Auto merge of rust-lang#16706 - Veykril:load-cargo-ide-db, r=Veykril
internal: Remove load-cargo dependency on ide This lightens up the dep tree for projects using r-a as a library that do not need the ide crate itself.
2 parents ecda464 + 4ee0dbd commit b3b9b53

18 files changed

+69
-60
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ide-db/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ doctest = false
1313

1414
[dependencies]
1515
cov-mark = "2.0.0-pre.1"
16+
crossbeam-channel = "0.5.5"
1617
tracing.workspace = true
1718
rayon.workspace = true
1819
fst = { version = "0.4.7", default-features = false }
@@ -52,4 +53,4 @@ test-fixture.workspace = true
5253
sourcegen.workspace = true
5354

5455
[lints]
55-
workspace = true
56+
workspace = true

crates/ide-db/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub mod helpers;
1515
pub mod items_locator;
1616
pub mod label;
1717
pub mod path_transform;
18+
pub mod prime_caches;
1819
pub mod rename;
1920
pub mod rust_doc;
2021
pub mod search;

crates/ide/src/prime_caches.rs renamed to crates/ide-db/src/prime_caches.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ mod topologic_sort;
77
use std::time::Duration;
88

99
use hir::db::DefDatabase;
10-
use ide_db::{
10+
11+
use crate::{
1112
base_db::{
1213
salsa::{Database, ParallelDatabase, Snapshot},
1314
Cancelled, CrateGraph, CrateId, SourceDatabase, SourceDatabaseExt,
1415
},
15-
FxHashSet, FxIndexMap,
16+
FxHashSet, FxIndexMap, RootDatabase,
1617
};
1718

18-
use crate::RootDatabase;
19-
2019
/// We're indexing many crates.
2120
#[derive(Debug)]
2221
pub struct ParallelPrimeCachesProgress {
@@ -28,7 +27,7 @@ pub struct ParallelPrimeCachesProgress {
2827
pub crates_done: usize,
2928
}
3029

31-
pub(crate) fn parallel_prime_caches(
30+
pub fn parallel_prime_caches(
3231
db: &RootDatabase,
3332
num_worker_threads: u8,
3433
cb: &(dyn Fn(ParallelPrimeCachesProgress) + Sync),

crates/ide/src/prime_caches/topologic_sort.rs renamed to crates/ide-db/src/prime_caches/topologic_sort.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! helper data structure to schedule work for parallel prime caches.
22
use std::{collections::VecDeque, hash::Hash};
33

4-
use ide_db::FxHashMap;
4+
use crate::FxHashMap;
55

66
pub(crate) struct TopologicSortIterBuilder<T> {
77
nodes: FxHashMap<T, Entry<T>>,

crates/ide/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ doctest = false
1313

1414
[dependencies]
1515
cov-mark = "2.0.0-pre.1"
16-
crossbeam-channel = "0.5.5"
1716
arrayvec.workspace = true
1817
either.workspace = true
1918
itertools.workspace = true
@@ -56,4 +55,4 @@ test-fixture.workspace = true
5655
in-rust-tree = ["ide-assists/in-rust-tree", "ide-diagnostics/in-rust-tree"]
5756

5857
[lints]
59-
workspace = true
58+
workspace = true

crates/ide/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ mod fixture;
1717

1818
mod markup;
1919
mod navigation_target;
20-
mod prime_caches;
2120

2221
mod annotations;
2322
mod call_hierarchy;
@@ -68,7 +67,7 @@ use ide_db::{
6867
salsa::{self, ParallelDatabase},
6968
CrateOrigin, Env, FileLoader, FileSet, SourceDatabase, VfsPath,
7069
},
71-
symbol_index, FxHashMap, FxIndexSet, LineIndexDatabase,
70+
prime_caches, symbol_index, FxHashMap, FxIndexSet, LineIndexDatabase,
7271
};
7372
use syntax::SourceFile;
7473
use triomphe::Arc;
@@ -100,7 +99,6 @@ pub use crate::{
10099
},
101100
move_item::Direction,
102101
navigation_target::{NavigationTarget, TryToNav, UpmappingResult},
103-
prime_caches::ParallelPrimeCachesProgress,
104102
references::ReferenceSearchResult,
105103
rename::RenameError,
106104
runnables::{Runnable, RunnableKind, TestId},
@@ -127,6 +125,7 @@ pub use ide_db::{
127125
documentation::Documentation,
128126
label::Label,
129127
line_index::{LineCol, LineIndex},
128+
prime_caches::ParallelPrimeCachesProgress,
130129
search::{ReferenceCategory, SearchScope},
131130
source_change::{FileSystemEdit, SnippetEdit, SourceChange},
132131
symbol_index::Query,
@@ -165,6 +164,10 @@ impl AnalysisHost {
165164
AnalysisHost { db: RootDatabase::new(lru_capacity) }
166165
}
167166

167+
pub fn with_database(db: RootDatabase) -> AnalysisHost {
168+
AnalysisHost { db }
169+
}
170+
168171
pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
169172
self.db.update_base_query_lru_capacities(lru_capacity);
170173
}

crates/ide/src/moniker.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! This module generates [moniker](https://microsoft.github.io/language-server-protocol/specifications/lsif/0.6.0/specification/#exportsImports)
22
//! for LSIF and LSP.
33
4+
use core::fmt;
5+
46
use hir::{Adt, AsAssocItem, AssocItemContainer, Crate, DescendPreference, MacroKind, Semantics};
57
use ide_db::{
68
base_db::{CrateOrigin, FilePosition, LangCrateOrigin},
@@ -93,9 +95,10 @@ pub struct MonikerIdentifier {
9395
pub description: Vec<MonikerDescriptor>,
9496
}
9597

96-
impl ToString for MonikerIdentifier {
97-
fn to_string(&self) -> String {
98-
format!("{}::{}", self.crate_name, self.description.iter().map(|x| &x.name).join("::"))
98+
impl fmt::Display for MonikerIdentifier {
99+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100+
f.write_str(&self.crate_name)?;
101+
f.write_fmt(format_args!("::{}", self.description.iter().map(|x| &x.name).join("::")))
99102
}
100103
}
101104

crates/load-cargo/Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ crossbeam-channel.workspace = true
1616
itertools.workspace = true
1717
tracing.workspace = true
1818

19-
ide.workspace = true
19+
# workspace deps
20+
21+
hir-expand.workspace = true
2022
ide-db.workspace = true
2123
proc-macro-api.workspace = true
2224
project-model.workspace = true
25+
span.workspace = true
2326
tt.workspace = true
24-
vfs.workspace = true
2527
vfs-notify.workspace = true
26-
span.workspace = true
27-
28-
hir-expand.workspace = true
28+
vfs.workspace = true
2929

3030
[lints]
31-
workspace = true
31+
workspace = true

crates/load-cargo/src/lib.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ use hir_expand::proc_macro::{
99
ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind, ProcMacroLoadResult,
1010
ProcMacros,
1111
};
12-
use ide::{AnalysisHost, SourceRoot};
1312
use ide_db::{
14-
base_db::{CrateGraph, Env},
15-
Change, FxHashMap,
13+
base_db::{CrateGraph, Env, SourceRoot},
14+
prime_caches, Change, FxHashMap, RootDatabase,
1615
};
1716
use itertools::Itertools;
1817
use proc_macro_api::{MacroDylib, ProcMacroServer};
@@ -38,7 +37,7 @@ pub fn load_workspace_at(
3837
cargo_config: &CargoConfig,
3938
load_config: &LoadCargoConfig,
4039
progress: &dyn Fn(String),
41-
) -> anyhow::Result<(AnalysisHost, vfs::Vfs, Option<ProcMacroServer>)> {
40+
) -> anyhow::Result<(RootDatabase, vfs::Vfs, Option<ProcMacroServer>)> {
4241
let root = AbsPathBuf::assert(std::env::current_dir()?.join(root));
4342
let root = ProjectManifest::discover_single(&root)?;
4443
let mut workspace = ProjectWorkspace::load(root, cargo_config, progress)?;
@@ -55,7 +54,7 @@ pub fn load_workspace(
5554
ws: ProjectWorkspace,
5655
extra_env: &FxHashMap<String, String>,
5756
load_config: &LoadCargoConfig,
58-
) -> anyhow::Result<(AnalysisHost, vfs::Vfs, Option<ProcMacroServer>)> {
57+
) -> anyhow::Result<(RootDatabase, vfs::Vfs, Option<ProcMacroServer>)> {
5958
let (sender, receiver) = unbounded();
6059
let mut vfs = vfs::Vfs::default();
6160
let mut loader = {
@@ -113,7 +112,7 @@ pub fn load_workspace(
113112
version: 0,
114113
});
115114

116-
let host = load_crate_graph(
115+
let db = load_crate_graph(
117116
&ws,
118117
crate_graph,
119118
proc_macros,
@@ -123,9 +122,9 @@ pub fn load_workspace(
123122
);
124123

125124
if load_config.prefill_caches {
126-
host.analysis().parallel_prime_caches(1, |_| {})?;
125+
prime_caches::parallel_prime_caches(&db, 1, &|_| ());
127126
}
128-
Ok((host, vfs, proc_macro_server.ok()))
127+
Ok((db, vfs, proc_macro_server.ok()))
129128
}
130129

131130
#[derive(Default)]
@@ -308,16 +307,16 @@ fn load_crate_graph(
308307
source_root_config: SourceRootConfig,
309308
vfs: &mut vfs::Vfs,
310309
receiver: &Receiver<vfs::loader::Message>,
311-
) -> AnalysisHost {
310+
) -> RootDatabase {
312311
let (ProjectWorkspace::Cargo { toolchain, target_layout, .. }
313312
| ProjectWorkspace::Json { toolchain, target_layout, .. }
314313
| ProjectWorkspace::DetachedFiles { toolchain, target_layout, .. }) = ws;
315314

316315
let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
317-
let mut host = AnalysisHost::new(lru_cap);
316+
let mut db = RootDatabase::new(lru_cap);
318317
let mut analysis_change = Change::new();
319318

320-
host.raw_database_mut().enable_proc_attr_macros();
319+
db.enable_proc_attr_macros();
321320

322321
// wait until Vfs has loaded all roots
323322
for task in receiver {
@@ -352,8 +351,8 @@ fn load_crate_graph(
352351
.set_target_data_layouts(iter::repeat(target_layout.clone()).take(num_crates).collect());
353352
analysis_change.set_toolchains(iter::repeat(toolchain.clone()).take(num_crates).collect());
354353

355-
host.apply_change(analysis_change);
356-
host
354+
db.apply_change(analysis_change);
355+
db
357356
}
358357

359358
fn expander_to_proc_macro(
@@ -407,10 +406,10 @@ mod tests {
407406
with_proc_macro_server: ProcMacroServerChoice::None,
408407
prefill_caches: false,
409408
};
410-
let (host, _vfs, _proc_macro) =
409+
let (db, _vfs, _proc_macro) =
411410
load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {}).unwrap();
412411

413-
let n_crates = host.raw_database().crate_graph().iter().count();
412+
let n_crates = db.crate_graph().iter().count();
414413
// RA has quite a few crates, but the exact count doesn't matter
415414
assert!(n_crates > 20);
416415
}

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use hir_def::{
1616
};
1717
use hir_ty::{Interner, Substitution, TyExt, TypeFlags};
1818
use ide::{
19-
Analysis, AnnotationConfig, DiagnosticsConfig, InlayFieldsToResolve, InlayHintsConfig, LineCol,
20-
RootDatabase,
19+
Analysis, AnalysisHost, AnnotationConfig, DiagnosticsConfig, InlayFieldsToResolve,
20+
InlayHintsConfig, LineCol, RootDatabase,
2121
};
2222
use ide_db::{
2323
base_db::{
@@ -90,16 +90,18 @@ impl flags::AnalysisStats {
9090
Some(build_scripts_sw.elapsed())
9191
};
9292

93-
let (host, vfs, _proc_macro) =
93+
let (db, vfs, _proc_macro) =
9494
load_workspace(workspace.clone(), &cargo_config.extra_env, &load_cargo_config)?;
95-
let db = host.raw_database();
9695
eprint!("{:<20} {}", "Database loaded:", db_load_sw.elapsed());
9796
eprint!(" (metadata {metadata_time}");
9897
if let Some(build_scripts_time) = build_scripts_time {
9998
eprint!("; build {build_scripts_time}");
10099
}
101100
eprintln!(")");
102101

102+
let host = AnalysisHost::with_database(db);
103+
let db = host.raw_database();
104+
103105
let mut analysis_sw = self.stop_watch();
104106

105107
let mut krates = Crate::all(db);

crates/rust-analyzer/src/cli/diagnostics.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use project_model::{CargoConfig, RustLibSource};
55
use rustc_hash::FxHashSet;
66

77
use hir::{db::HirDatabase, Crate, HirFileIdExt, Module};
8-
use ide::{AssistResolveStrategy, DiagnosticsConfig, Severity};
8+
use ide::{AnalysisHost, AssistResolveStrategy, DiagnosticsConfig, Severity};
99
use ide_db::base_db::SourceDatabaseExt;
1010
use load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice};
1111

@@ -26,8 +26,9 @@ impl flags::Diagnostics {
2626
with_proc_macro_server,
2727
prefill_caches: false,
2828
};
29-
let (host, _vfs, _proc_macro) =
29+
let (db, _vfs, _proc_macro) =
3030
load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;
31+
let host = AnalysisHost::with_database(db);
3132
let db = host.raw_database();
3233
let analysis = host.analysis();
3334

crates/rust-analyzer/src/cli/lsif.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::env;
44
use std::time::Instant;
55

66
use ide::{
7-
Analysis, FileId, FileRange, MonikerKind, PackageInformation, RootDatabase, StaticIndex,
8-
StaticIndexedFile, TokenId, TokenStaticData,
7+
Analysis, AnalysisHost, FileId, FileRange, MonikerKind, PackageInformation, RootDatabase,
8+
StaticIndex, StaticIndexedFile, TokenId, TokenStaticData,
99
};
1010
use ide_db::{
1111
base_db::salsa::{self, ParallelDatabase},
@@ -300,8 +300,9 @@ impl flags::Lsif {
300300

301301
let workspace = ProjectWorkspace::load(manifest, &cargo_config, no_progress)?;
302302

303-
let (host, vfs, _proc_macro) =
303+
let (db, vfs, _proc_macro) =
304304
load_workspace(workspace, &cargo_config.extra_env, &load_cargo_config)?;
305+
let host = AnalysisHost::with_database(db);
305306
let db = host.raw_database();
306307
let analysis = host.analysis();
307308

crates/rust-analyzer/src/cli/run_tests.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ impl flags::RunTests {
2020
with_proc_macro_server: ProcMacroServerChoice::Sysroot,
2121
prefill_caches: false,
2222
};
23-
let (host, _vfs, _proc_macro) =
23+
let (ref db, _vfs, _proc_macro) =
2424
load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;
25-
let db = host.raw_database();
2625

2726
let tests = all_modules(db)
2827
.into_iter()

crates/rust-analyzer/src/cli/rustc_tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ impl Tester {
8787
with_proc_macro_server: ProcMacroServerChoice::Sysroot,
8888
prefill_caches: false,
8989
};
90-
let (host, _vfs, _proc_macro) =
90+
let (db, _vfs, _proc_macro) =
9191
load_workspace(workspace, &cargo_config.extra_env, &load_cargo_config)?;
92+
let host = AnalysisHost::with_database(db);
9293
let db = host.raw_database();
9394
let krates = Crate::all(db);
9495
let root_crate = krates.iter().cloned().find(|krate| krate.origin(db).is_local()).unwrap();

0 commit comments

Comments
 (0)