Skip to content

Commit f185d1c

Browse files
bors[bot]HKalbasi
andauthored
Merge #10423
10423: Internal: refactor for mdbook plugin r=Veykril a=HKalbasi This PR is for upstreaming changes that I made for mdbook plugin. Changes are adding inlay hints to `StaticIndex` and changing some functions for working around privacy of crates. Aside this, is it okay if I bring the plugin in tree? It is a simple binary crate. I feel it will better maintained here and become resistant to api changes. Co-authored-by: hamidreza kalbasi <[email protected]>
2 parents a871da3 + 0f49c97 commit f185d1c

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

crates/ide/src/static_index.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ use syntax::{SyntaxToken, TextRange};
1414

1515
use crate::display::TryToNav;
1616
use crate::hover::hover_for_definition;
17-
use crate::{Analysis, Fold, HoverConfig, HoverDocFormat, HoverResult};
17+
use crate::{
18+
Analysis, Fold, HoverConfig, HoverDocFormat, HoverResult, InlayHint, InlayHintsConfig,
19+
};
1820

1921
/// A static representation of fully analyzed source code.
2022
///
2123
/// The intended use-case is powering read-only code browsers and emitting LSIF
24+
#[derive(Debug)]
2225
pub struct StaticIndex<'a> {
2326
pub files: Vec<StaticIndexedFile>,
2427
pub tokens: TokenStore,
@@ -27,21 +30,29 @@ pub struct StaticIndex<'a> {
2730
def_map: HashMap<Definition, TokenId>,
2831
}
2932

33+
#[derive(Debug)]
3034
pub struct ReferenceData {
3135
pub range: FileRange,
3236
pub is_definition: bool,
3337
}
3438

39+
#[derive(Debug)]
3540
pub struct TokenStaticData {
3641
pub hover: Option<HoverResult>,
3742
pub definition: Option<FileRange>,
3843
pub references: Vec<ReferenceData>,
3944
}
4045

41-
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
46+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4247
pub struct TokenId(usize);
4348

44-
#[derive(Default)]
49+
impl TokenId {
50+
pub fn raw(self) -> usize {
51+
self.0
52+
}
53+
}
54+
55+
#[derive(Default, Debug)]
4556
pub struct TokenStore(Vec<TokenStaticData>);
4657

4758
impl TokenStore {
@@ -64,9 +75,11 @@ impl TokenStore {
6475
}
6576
}
6677

78+
#[derive(Debug)]
6779
pub struct StaticIndexedFile {
6880
pub file_id: FileId,
6981
pub folds: Vec<Fold>,
82+
pub inlay_hints: Vec<InlayHint>,
7083
pub tokens: Vec<(TextRange, TokenId)>,
7184
}
7285

@@ -86,6 +99,18 @@ fn all_modules(db: &dyn HirDatabase) -> Vec<Module> {
8699
impl StaticIndex<'_> {
87100
fn add_file(&mut self, file_id: FileId) {
88101
let folds = self.analysis.folding_ranges(file_id).unwrap();
102+
let inlay_hints = self
103+
.analysis
104+
.inlay_hints(
105+
&InlayHintsConfig {
106+
type_hints: true,
107+
parameter_hints: true,
108+
chaining_hints: true,
109+
max_length: Some(25),
110+
},
111+
file_id,
112+
)
113+
.unwrap();
89114
// hovers
90115
let sema = hir::Semantics::new(self.db);
91116
let tokens_or_nodes = sema.parse(file_id).syntax().clone();
@@ -99,7 +124,7 @@ impl StaticIndex<'_> {
99124
IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] | T![super] | T![crate] => true,
100125
_ => false,
101126
});
102-
let mut result = StaticIndexedFile { file_id, folds, tokens: vec![] };
127+
let mut result = StaticIndexedFile { file_id, inlay_hints, folds, tokens: vec![] };
103128
for token in tokens {
104129
let range = token.text_range();
105130
let node = token.parent().unwrap();
@@ -133,7 +158,8 @@ impl StaticIndex<'_> {
133158
self.files.push(result);
134159
}
135160

136-
pub fn compute<'a>(db: &'a RootDatabase, analysis: &'a Analysis) -> StaticIndex<'a> {
161+
pub fn compute<'a>(analysis: &'a Analysis) -> StaticIndex<'a> {
162+
let db = &*analysis.db;
137163
let work = all_modules(db).into_iter().filter(|module| {
138164
let file_id = module.definition_source(db).file_id.original_file(db);
139165
let source_root = db.file_source_root(file_id);
@@ -181,7 +207,7 @@ mod tests {
181207

182208
fn check_all_ranges(ra_fixture: &str) {
183209
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
184-
let s = StaticIndex::compute(&*analysis.db, &analysis);
210+
let s = StaticIndex::compute(&analysis);
185211
let mut range_set: HashSet<_> = ranges.iter().map(|x| x.0).collect();
186212
for f in s.files {
187213
for (range, _) in f.tokens {
@@ -199,7 +225,7 @@ mod tests {
199225

200226
fn check_definitions(ra_fixture: &str) {
201227
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
202-
let s = StaticIndex::compute(&*analysis.db, &analysis);
228+
let s = StaticIndex::compute(&analysis);
203229
let mut range_set: HashSet<_> = ranges.iter().map(|x| x.0).collect();
204230
for (_, t) in s.tokens.iter() {
205231
if let Some(x) = t.definition {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl LsifManager<'_> {
186186
}
187187

188188
fn add_file(&mut self, file: StaticIndexedFile) {
189-
let StaticIndexedFile { file_id, tokens, folds } = file;
189+
let StaticIndexedFile { file_id, tokens, folds, .. } = file;
190190
let doc_id = self.get_file_id(file_id);
191191
let text = self.analysis.file_text(file_id).unwrap();
192192
let line_index = self.db.line_index(file_id);
@@ -247,7 +247,7 @@ impl flags::Lsif {
247247
let db = host.raw_database();
248248
let analysis = host.analysis();
249249

250-
let si = StaticIndex::compute(db, &analysis);
250+
let si = StaticIndex::compute(&analysis);
251251

252252
let mut lsif = LsifManager::new(&analysis, db, &vfs);
253253
lsif.add_vertex(lsif::Vertex::MetaData(lsif::MetaData {

crates/vfs/src/vfs_path.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ impl VfsPath {
2525
VfsPath(VfsPathRepr::VirtualPath(VirtualPath(path)))
2626
}
2727

28+
/// Create a path from string. Input should be a string representation of
29+
/// an absolute path inside filesystem
30+
pub fn new_real_path(path: String) -> VfsPath {
31+
VfsPath::from(AbsPathBuf::assert(path.into()))
32+
}
33+
2834
/// Returns the `AbsPath` representation of `self` if `self` is on the file system.
2935
pub fn as_path(&self) -> Option<&AbsPath> {
3036
match &self.0 {

0 commit comments

Comments
 (0)