From 0f49c97e93581cc85768513cfb4aa8b29680d341 Mon Sep 17 00:00:00 2001 From: hamidreza kalbasi Date: Fri, 1 Oct 2021 16:37:11 +0330 Subject: [PATCH] internal changes for mdbook --- crates/ide/src/static_index.rs | 40 +++++++++++++++++++++++----- crates/rust-analyzer/src/cli/lsif.rs | 4 +-- crates/vfs/src/vfs_path.rs | 6 +++++ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/crates/ide/src/static_index.rs b/crates/ide/src/static_index.rs index aa62e2eae5..94caa5bbb1 100644 --- a/crates/ide/src/static_index.rs +++ b/crates/ide/src/static_index.rs @@ -14,11 +14,14 @@ use syntax::{SyntaxToken, TextRange}; use crate::display::TryToNav; use crate::hover::hover_for_definition; -use crate::{Analysis, Fold, HoverConfig, HoverDocFormat, HoverResult}; +use crate::{ + Analysis, Fold, HoverConfig, HoverDocFormat, HoverResult, InlayHint, InlayHintsConfig, +}; /// A static representation of fully analyzed source code. /// /// The intended use-case is powering read-only code browsers and emitting LSIF +#[derive(Debug)] pub struct StaticIndex<'a> { pub files: Vec, pub tokens: TokenStore, @@ -27,21 +30,29 @@ pub struct StaticIndex<'a> { def_map: HashMap, } +#[derive(Debug)] pub struct ReferenceData { pub range: FileRange, pub is_definition: bool, } +#[derive(Debug)] pub struct TokenStaticData { pub hover: Option, pub definition: Option, pub references: Vec, } -#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct TokenId(usize); -#[derive(Default)] +impl TokenId { + pub fn raw(self) -> usize { + self.0 + } +} + +#[derive(Default, Debug)] pub struct TokenStore(Vec); impl TokenStore { @@ -64,9 +75,11 @@ impl TokenStore { } } +#[derive(Debug)] pub struct StaticIndexedFile { pub file_id: FileId, pub folds: Vec, + pub inlay_hints: Vec, pub tokens: Vec<(TextRange, TokenId)>, } @@ -86,6 +99,18 @@ fn all_modules(db: &dyn HirDatabase) -> Vec { impl StaticIndex<'_> { fn add_file(&mut self, file_id: FileId) { let folds = self.analysis.folding_ranges(file_id).unwrap(); + let inlay_hints = self + .analysis + .inlay_hints( + &InlayHintsConfig { + type_hints: true, + parameter_hints: true, + chaining_hints: true, + max_length: Some(25), + }, + file_id, + ) + .unwrap(); // hovers let sema = hir::Semantics::new(self.db); let tokens_or_nodes = sema.parse(file_id).syntax().clone(); @@ -99,7 +124,7 @@ impl StaticIndex<'_> { IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] | T![super] | T![crate] => true, _ => false, }); - let mut result = StaticIndexedFile { file_id, folds, tokens: vec![] }; + let mut result = StaticIndexedFile { file_id, inlay_hints, folds, tokens: vec![] }; for token in tokens { let range = token.text_range(); let node = token.parent().unwrap(); @@ -135,7 +160,8 @@ impl StaticIndex<'_> { self.files.push(result); } - pub fn compute<'a>(db: &'a RootDatabase, analysis: &'a Analysis) -> StaticIndex<'a> { + pub fn compute<'a>(analysis: &'a Analysis) -> StaticIndex<'a> { + let db = &*analysis.db; let work = all_modules(db).into_iter().filter(|module| { let file_id = module.definition_source(db).file_id.original_file(db); let source_root = db.file_source_root(file_id); @@ -183,7 +209,7 @@ mod tests { fn check_all_ranges(ra_fixture: &str) { let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture); - let s = StaticIndex::compute(&*analysis.db, &analysis); + let s = StaticIndex::compute(&analysis); let mut range_set: HashSet<_> = ranges.iter().map(|x| x.0).collect(); for f in s.files { for (range, _) in f.tokens { @@ -201,7 +227,7 @@ mod tests { fn check_definitions(ra_fixture: &str) { let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture); - let s = StaticIndex::compute(&*analysis.db, &analysis); + let s = StaticIndex::compute(&analysis); let mut range_set: HashSet<_> = ranges.iter().map(|x| x.0).collect(); for (_, t) in s.tokens.iter() { if let Some(x) = t.definition { diff --git a/crates/rust-analyzer/src/cli/lsif.rs b/crates/rust-analyzer/src/cli/lsif.rs index a235443146..1b57dd3b30 100644 --- a/crates/rust-analyzer/src/cli/lsif.rs +++ b/crates/rust-analyzer/src/cli/lsif.rs @@ -186,7 +186,7 @@ impl LsifManager<'_> { } fn add_file(&mut self, file: StaticIndexedFile) { - let StaticIndexedFile { file_id, tokens, folds } = file; + let StaticIndexedFile { file_id, tokens, folds, .. } = file; let doc_id = self.get_file_id(file_id); let text = self.analysis.file_text(file_id).unwrap(); let line_index = self.db.line_index(file_id); @@ -247,7 +247,7 @@ impl flags::Lsif { let db = host.raw_database(); let analysis = host.analysis(); - let si = StaticIndex::compute(db, &analysis); + let si = StaticIndex::compute(&analysis); let mut lsif = LsifManager::new(&analysis, db, &vfs); lsif.add_vertex(lsif::Vertex::MetaData(lsif::MetaData { diff --git a/crates/vfs/src/vfs_path.rs b/crates/vfs/src/vfs_path.rs index 0ad56e00d7..9b1a013b6d 100644 --- a/crates/vfs/src/vfs_path.rs +++ b/crates/vfs/src/vfs_path.rs @@ -25,6 +25,12 @@ impl VfsPath { VfsPath(VfsPathRepr::VirtualPath(VirtualPath(path))) } + /// Create a path from string. Input should be a string representation of + /// an absolute path inside filesystem + pub fn new_real_path(path: String) -> VfsPath { + VfsPath::from(AbsPathBuf::assert(path.into())) + } + /// Returns the `AbsPath` representation of `self` if `self` is on the file system. pub fn as_path(&self) -> Option<&AbsPath> { match &self.0 {