Skip to content

Commit 393cbd0

Browse files
bors[bot]HKalbasi
andauthored
Merge #10841
10841: Emit moniker in lsif r=Veykril a=HKalbasi fix #10559 Co-authored-by: hkalbasi <[email protected]> Co-authored-by: HKalbasi <[email protected]>
2 parents d9b2291 + 1409781 commit 393cbd0

File tree

12 files changed

+511
-19
lines changed

12 files changed

+511
-19
lines changed

crates/base_db/src/fixture.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ use tt::Subtree;
1010
use vfs::{file_set::FileSet, VfsPath};
1111

1212
use crate::{
13-
input::CrateName, Change, CrateDisplayName, CrateGraph, CrateId, Dependency, Edition, Env,
14-
FileId, FilePosition, FileRange, ProcMacro, ProcMacroExpander, ProcMacroExpansionError,
15-
SourceDatabaseExt, SourceRoot, SourceRootId,
13+
input::{CrateName, CrateOrigin},
14+
Change, CrateDisplayName, CrateGraph, CrateId, Dependency, Edition, Env, FileId, FilePosition,
15+
FileRange, ProcMacro, ProcMacroExpander, ProcMacroExpansionError, SourceDatabaseExt,
16+
SourceRoot, SourceRootId,
1617
};
1718

1819
pub const WORKSPACE: SourceRootId = SourceRootId(0);
@@ -130,17 +131,18 @@ impl ChangeFixture {
130131
current_source_root_kind = *kind;
131132
}
132133

133-
if let Some(krate) = meta.krate {
134+
if let Some((krate, origin, version)) = meta.krate {
134135
let crate_name = CrateName::normalize_dashes(&krate);
135136
let crate_id = crate_graph.add_crate_root(
136137
file_id,
137138
meta.edition,
138139
Some(crate_name.clone().into()),
139-
None,
140+
version,
140141
meta.cfg.clone(),
141142
meta.cfg,
142143
meta.env,
143144
Default::default(),
145+
origin,
144146
);
145147
let prev = crates.insert(crate_name.clone(), crate_id);
146148
assert!(prev.is_none());
@@ -174,6 +176,7 @@ impl ChangeFixture {
174176
default_cfg,
175177
Env::default(),
176178
Default::default(),
179+
Default::default(),
177180
);
178181
} else {
179182
for (from, to, prelude) in crate_deps {
@@ -209,6 +212,7 @@ impl ChangeFixture {
209212
CfgOptions::default(),
210213
Env::default(),
211214
Vec::new(),
215+
CrateOrigin::Lang,
212216
);
213217

214218
for krate in all_crates {
@@ -243,6 +247,7 @@ impl ChangeFixture {
243247
CfgOptions::default(),
244248
Env::default(),
245249
proc_macro,
250+
CrateOrigin::Lang,
246251
);
247252

248253
for krate in all_crates {
@@ -324,7 +329,7 @@ enum SourceRootKind {
324329
#[derive(Debug)]
325330
struct FileMeta {
326331
path: String,
327-
krate: Option<String>,
332+
krate: Option<(String, CrateOrigin, Option<String>)>,
328333
deps: Vec<String>,
329334
extern_prelude: Vec<String>,
330335
cfg: CfgOptions,
@@ -333,16 +338,32 @@ struct FileMeta {
333338
introduce_new_source_root: Option<SourceRootKind>,
334339
}
335340

341+
fn parse_crate(crate_str: String) -> (String, CrateOrigin, Option<String>) {
342+
if let Some((a, b)) = crate_str.split_once("@") {
343+
let (version, origin) = match b.split_once(":") {
344+
Some(("CratesIo", data)) => match data.split_once(",") {
345+
Some((version, url)) => {
346+
(version, CrateOrigin::CratesIo { repo: Some(url.to_owned()) })
347+
}
348+
_ => panic!("Bad crates.io parameter: {}", data),
349+
},
350+
_ => panic!("Bad string for crate origin: {}", b),
351+
};
352+
(a.to_owned(), origin, Some(version.to_string()))
353+
} else {
354+
(crate_str, CrateOrigin::Unknown, None)
355+
}
356+
}
357+
336358
impl From<Fixture> for FileMeta {
337359
fn from(f: Fixture) -> FileMeta {
338360
let mut cfg = CfgOptions::default();
339361
f.cfg_atoms.iter().for_each(|it| cfg.insert_atom(it.into()));
340362
f.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into()));
341-
342363
let deps = f.deps;
343364
FileMeta {
344365
path: f.path,
345-
krate: f.krate,
366+
krate: f.krate.map(parse_crate),
346367
extern_prelude: f.extern_prelude.unwrap_or_else(|| deps.clone()),
347368
deps,
348369
cfg,

crates/base_db/src/input.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ impl ops::Deref for CrateName {
112112
}
113113
}
114114

115+
/// Origin of the crates. It is used in emitting monikers.
116+
#[derive(Debug, Clone)]
117+
pub enum CrateOrigin {
118+
/// Crates that are from crates.io official registry,
119+
CratesIo { repo: Option<String> },
120+
/// Crates that are provided by the language, like std, core, proc-macro, ...
121+
Lang,
122+
/// Crates that we don't know their origin.
123+
// Idealy this enum should cover all cases, and then we remove this variant.
124+
Unknown,
125+
}
126+
127+
impl Default for CrateOrigin {
128+
fn default() -> Self {
129+
Self::Unknown
130+
}
131+
}
132+
115133
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
116134
pub struct CrateDisplayName {
117135
// The name we use to display various paths (with `_`).
@@ -205,6 +223,7 @@ pub struct CrateData {
205223
pub env: Env,
206224
pub dependencies: Vec<Dependency>,
207225
pub proc_macro: Vec<ProcMacro>,
226+
pub origin: CrateOrigin,
208227
}
209228

210229
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -256,6 +275,7 @@ impl CrateGraph {
256275
potential_cfg_options: CfgOptions,
257276
env: Env,
258277
proc_macro: Vec<ProcMacro>,
278+
origin: CrateOrigin,
259279
) -> CrateId {
260280
let data = CrateData {
261281
root_file_id: file_id,
@@ -267,6 +287,7 @@ impl CrateGraph {
267287
env,
268288
proc_macro,
269289
dependencies: Vec::new(),
290+
origin,
270291
};
271292
let crate_id = CrateId(self.arena.len() as u32);
272293
let prev = self.arena.insert(crate_id, data);
@@ -571,6 +592,7 @@ mod tests {
571592
CfgOptions::default(),
572593
Env::default(),
573594
Default::default(),
595+
Default::default(),
574596
);
575597
let crate2 = graph.add_crate_root(
576598
FileId(2u32),
@@ -581,6 +603,7 @@ mod tests {
581603
CfgOptions::default(),
582604
Env::default(),
583605
Default::default(),
606+
Default::default(),
584607
);
585608
let crate3 = graph.add_crate_root(
586609
FileId(3u32),
@@ -591,6 +614,7 @@ mod tests {
591614
CfgOptions::default(),
592615
Env::default(),
593616
Default::default(),
617+
Default::default(),
594618
);
595619
assert!(graph
596620
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@@ -615,6 +639,7 @@ mod tests {
615639
CfgOptions::default(),
616640
Env::default(),
617641
Default::default(),
642+
Default::default(),
618643
);
619644
let crate2 = graph.add_crate_root(
620645
FileId(2u32),
@@ -625,6 +650,7 @@ mod tests {
625650
CfgOptions::default(),
626651
Env::default(),
627652
Default::default(),
653+
Default::default(),
628654
);
629655
assert!(graph
630656
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@@ -646,6 +672,7 @@ mod tests {
646672
CfgOptions::default(),
647673
Env::default(),
648674
Default::default(),
675+
Default::default(),
649676
);
650677
let crate2 = graph.add_crate_root(
651678
FileId(2u32),
@@ -656,6 +683,7 @@ mod tests {
656683
CfgOptions::default(),
657684
Env::default(),
658685
Default::default(),
686+
Default::default(),
659687
);
660688
let crate3 = graph.add_crate_root(
661689
FileId(3u32),
@@ -666,6 +694,7 @@ mod tests {
666694
CfgOptions::default(),
667695
Env::default(),
668696
Default::default(),
697+
Default::default(),
669698
);
670699
assert!(graph
671700
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@@ -687,6 +716,7 @@ mod tests {
687716
CfgOptions::default(),
688717
Env::default(),
689718
Default::default(),
719+
Default::default(),
690720
);
691721
let crate2 = graph.add_crate_root(
692722
FileId(2u32),
@@ -697,6 +727,7 @@ mod tests {
697727
CfgOptions::default(),
698728
Env::default(),
699729
Default::default(),
730+
Default::default(),
700731
);
701732
assert!(graph
702733
.add_dep(

crates/base_db/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use syntax::{ast, Parse, SourceFile, TextRange, TextSize};
1111
pub use crate::{
1212
change::Change,
1313
input::{
14-
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, Dependency, Edition, Env,
15-
ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroId, ProcMacroKind,
16-
SourceRoot, SourceRootId,
14+
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency,
15+
Edition, Env, ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroId,
16+
ProcMacroKind, SourceRoot, SourceRootId,
1717
},
1818
};
1919
pub use salsa::{self, Cancelled};

crates/hir/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ mod display;
3434
use std::{iter, ops::ControlFlow, sync::Arc};
3535

3636
use arrayvec::ArrayVec;
37-
use base_db::{CrateDisplayName, CrateId, Edition, FileId};
37+
use base_db::{CrateDisplayName, CrateId, CrateOrigin, Edition, FileId};
3838
use either::Either;
3939
use hir_def::{
4040
adt::{ReprKind, VariantData},
@@ -144,6 +144,10 @@ pub struct CrateDependency {
144144
}
145145

146146
impl Crate {
147+
pub fn origin(self, db: &dyn HirDatabase) -> CrateOrigin {
148+
db.crate_graph()[self.id].origin.clone()
149+
}
150+
147151
pub fn dependencies(self, db: &dyn HirDatabase) -> Vec<CrateDependency> {
148152
db.crate_graph()[self.id]
149153
.dependencies

crates/ide/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mod inlay_hints;
4141
mod join_lines;
4242
mod markdown_remove;
4343
mod matching_brace;
44+
mod moniker;
4445
mod move_item;
4546
mod parent_module;
4647
mod references;
@@ -83,6 +84,7 @@ pub use crate::{
8384
inlay_hints::{InlayHint, InlayHintsConfig, InlayKind},
8485
join_lines::JoinLinesConfig,
8586
markup::Markup,
87+
moniker::{MonikerKind, MonikerResult, PackageInformation},
8688
move_item::Direction,
8789
navigation_target::NavigationTarget,
8890
prime_caches::PrimeCachesProgress,
@@ -225,6 +227,7 @@ impl Analysis {
225227
cfg_options,
226228
Env::default(),
227229
Default::default(),
230+
Default::default(),
228231
);
229232
change.change_file(file_id, Some(Arc::new(text)));
230233
change.set_crate_graph(crate_graph);
@@ -425,6 +428,14 @@ impl Analysis {
425428
self.with_db(|db| hover::hover(db, range, config))
426429
}
427430

431+
/// Returns moniker of symbol at position.
432+
pub fn moniker(
433+
&self,
434+
position: FilePosition,
435+
) -> Cancellable<Option<RangeInfo<Vec<moniker::MonikerResult>>>> {
436+
self.with_db(|db| moniker::moniker(db, position))
437+
}
438+
428439
/// Return URL(s) for the documentation of the symbol under the cursor.
429440
pub fn external_docs(
430441
&self,

0 commit comments

Comments
 (0)