aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis')
-rw-r--r--crates/ra_analysis/src/db.rs20
-rw-r--r--crates/ra_analysis/src/descriptors/function/imp.rs4
-rw-r--r--crates/ra_analysis/src/descriptors/function/mod.rs13
-rw-r--r--crates/ra_analysis/src/descriptors/mod.rs3
-rw-r--r--crates/ra_analysis/src/imp.rs2
-rw-r--r--crates/ra_analysis/src/loc2id.rs50
6 files changed, 70 insertions, 22 deletions
diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs
index baf6d915a..d78b6afb9 100644
--- a/crates/ra_analysis/src/db.rs
+++ b/crates/ra_analysis/src/db.rs
@@ -1,7 +1,5 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use parking_lot::Mutex;
4
5use ra_editor::LineIndex; 3use ra_editor::LineIndex;
6use ra_syntax::{SourceFileNode, SyntaxNode}; 4use ra_syntax::{SourceFileNode, SyntaxNode};
7use salsa::{self, Database}; 5use salsa::{self, Database};
@@ -11,19 +9,17 @@ use crate::{
11 descriptors::{ 9 descriptors::{
12 DescriptorDatabase, FnScopesQuery, FnSyntaxQuery, ModuleScopeQuery, ModuleTreeQuery, 10 DescriptorDatabase, FnScopesQuery, FnSyntaxQuery, ModuleScopeQuery, ModuleTreeQuery,
13 SubmodulesQuery, 11 SubmodulesQuery,
14 module::{ModuleSource, ModuleId},
15 }, 12 },
16 input::SourceRootId,
17 symbol_index::SymbolIndex, 13 symbol_index::SymbolIndex,
18 syntax_ptr::SyntaxPtr, 14 syntax_ptr::SyntaxPtr,
19 loc2id::Loc2IdMap, 15 loc2id::{IdMaps, IdDatabase},
20 Cancelable, Canceled, FileId, 16 Cancelable, Canceled, FileId,
21}; 17};
22 18
23#[derive(Debug)] 19#[derive(Debug)]
24pub(crate) struct RootDatabase { 20pub(crate) struct RootDatabase {
25 runtime: salsa::Runtime<RootDatabase>, 21 runtime: salsa::Runtime<RootDatabase>,
26 loc2id: Arc<Mutex<Loc2IdMap<(SourceRootId, ModuleSource), ModuleId>>>, 22 id_maps: IdMaps,
27} 23}
28 24
29impl salsa::Database for RootDatabase { 25impl salsa::Database for RootDatabase {
@@ -35,8 +31,8 @@ impl salsa::Database for RootDatabase {
35impl Default for RootDatabase { 31impl Default for RootDatabase {
36 fn default() -> RootDatabase { 32 fn default() -> RootDatabase {
37 let mut db = RootDatabase { 33 let mut db = RootDatabase {
38 runtime: Default::default(), 34 runtime: salsa::Runtime::default(),
39 loc2id: Default::default(), 35 id_maps: IdMaps::default(),
40 }; 36 };
41 db.query_mut(crate::input::SourceRootQuery) 37 db.query_mut(crate::input::SourceRootQuery)
42 .set(crate::input::WORKSPACE, Default::default()); 38 .set(crate::input::WORKSPACE, Default::default());
@@ -60,11 +56,17 @@ impl salsa::ParallelDatabase for RootDatabase {
60 fn snapshot(&self) -> salsa::Snapshot<RootDatabase> { 56 fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
61 salsa::Snapshot::new(RootDatabase { 57 salsa::Snapshot::new(RootDatabase {
62 runtime: self.runtime.snapshot(self), 58 runtime: self.runtime.snapshot(self),
63 loc2id: Arc::clone(&self.loc2id), 59 id_maps: self.id_maps.clone(),
64 }) 60 })
65 } 61 }
66} 62}
67 63
64impl IdDatabase for RootDatabase {
65 fn id_maps(&self) -> &IdMaps {
66 &self.id_maps
67 }
68}
69
68salsa::database_storage! { 70salsa::database_storage! {
69 pub(crate) struct RootDatabaseStorage for RootDatabase { 71 pub(crate) struct RootDatabaseStorage for RootDatabase {
70 impl crate::input::FilesDatabase { 72 impl crate::input::FilesDatabase {
diff --git a/crates/ra_analysis/src/descriptors/function/imp.rs b/crates/ra_analysis/src/descriptors/function/imp.rs
index a989a04cd..a7257acf9 100644
--- a/crates/ra_analysis/src/descriptors/function/imp.rs
+++ b/crates/ra_analysis/src/descriptors/function/imp.rs
@@ -8,9 +8,9 @@ use crate::descriptors::{
8}; 8};
9 9
10/// Resolve `FnId` to the corresponding `SyntaxNode` 10/// Resolve `FnId` to the corresponding `SyntaxNode`
11/// TODO: this should return something more type-safe then `SyntaxNode`
12pub(crate) fn fn_syntax(db: &impl DescriptorDatabase, fn_id: FnId) -> FnDefNode { 11pub(crate) fn fn_syntax(db: &impl DescriptorDatabase, fn_id: FnId) -> FnDefNode {
13 let syntax = db.resolve_syntax_ptr(fn_id.0); 12 let ptr = db.id_maps().fn_ptr(fn_id);
13 let syntax = db.resolve_syntax_ptr(ptr);
14 FnDef::cast(syntax.borrowed()).unwrap().owned() 14 FnDef::cast(syntax.borrowed()).unwrap().owned()
15} 15}
16 16
diff --git a/crates/ra_analysis/src/descriptors/function/mod.rs b/crates/ra_analysis/src/descriptors/function/mod.rs
index d5db28a64..86eee5e93 100644
--- a/crates/ra_analysis/src/descriptors/function/mod.rs
+++ b/crates/ra_analysis/src/descriptors/function/mod.rs
@@ -8,17 +8,18 @@ use ra_syntax::{
8 TextRange, TextUnit, 8 TextRange, TextUnit,
9}; 9};
10 10
11use crate::{syntax_ptr::SyntaxPtr, FileId}; 11use crate::{
12 syntax_ptr::SyntaxPtr, FileId,
13 loc2id::IdDatabase,
14};
12 15
13pub(crate) use self::scope::{resolve_local_name, FnScopes}; 16pub(crate) use self::scope::{resolve_local_name, FnScopes};
14 17pub(crate) use crate::loc2id::FnId;
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16pub(crate) struct FnId(SyntaxPtr);
17 18
18impl FnId { 19impl FnId {
19 pub(crate) fn new(file_id: FileId, fn_def: ast::FnDef) -> FnId { 20 pub(crate) fn get(db: &impl IdDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId {
20 let ptr = SyntaxPtr::new(file_id, fn_def.syntax()); 21 let ptr = SyntaxPtr::new(file_id, fn_def.syntax());
21 FnId(ptr) 22 db.id_maps().fn_id(ptr)
22 } 23 }
23} 24}
24 25
diff --git a/crates/ra_analysis/src/descriptors/mod.rs b/crates/ra_analysis/src/descriptors/mod.rs
index 56bde3849..d602c4e04 100644
--- a/crates/ra_analysis/src/descriptors/mod.rs
+++ b/crates/ra_analysis/src/descriptors/mod.rs
@@ -13,12 +13,13 @@ use crate::{
13 descriptors::function::{resolve_local_name, FnId, FnScopes}, 13 descriptors::function::{resolve_local_name, FnId, FnScopes},
14 descriptors::module::{ModuleId, ModuleScope, ModuleTree, ModuleSource}, 14 descriptors::module::{ModuleId, ModuleScope, ModuleTree, ModuleSource},
15 input::SourceRootId, 15 input::SourceRootId,
16 loc2id::IdDatabase,
16 syntax_ptr::LocalSyntaxPtr, 17 syntax_ptr::LocalSyntaxPtr,
17 Cancelable, 18 Cancelable,
18}; 19};
19 20
20salsa::query_group! { 21salsa::query_group! {
21 pub(crate) trait DescriptorDatabase: SyntaxDatabase { 22 pub(crate) trait DescriptorDatabase: SyntaxDatabase + IdDatabase {
22 fn module_tree(source_root_id: SourceRootId) -> Cancelable<Arc<ModuleTree>> { 23 fn module_tree(source_root_id: SourceRootId) -> Cancelable<Arc<ModuleTree>> {
23 type ModuleTreeQuery; 24 type ModuleTreeQuery;
24 use fn module::imp::module_tree; 25 use fn module::imp::module_tree;
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs
index 74c248a96..812fed32d 100644
--- a/crates/ra_analysis/src/imp.rs
+++ b/crates/ra_analysis/src/imp.rs
@@ -621,7 +621,7 @@ fn resolve_local_name(
621 name_ref: ast::NameRef, 621 name_ref: ast::NameRef,
622) -> Option<(SmolStr, TextRange)> { 622) -> Option<(SmolStr, TextRange)> {
623 let fn_def = name_ref.syntax().ancestors().find_map(ast::FnDef::cast)?; 623 let fn_def = name_ref.syntax().ancestors().find_map(ast::FnDef::cast)?;
624 let fn_id = FnId::new(file_id, fn_def); 624 let fn_id = FnId::get(db, file_id, fn_def);
625 let scopes = db.fn_scopes(fn_id); 625 let scopes = db.fn_scopes(fn_id);
626 let scope_entry = crate::descriptors::function::resolve_local_name(name_ref, &scopes)?; 626 let scope_entry = crate::descriptors::function::resolve_local_name(name_ref, &scopes)?;
627 let syntax = db.resolve_syntax_ptr(scope_entry.ptr().into_global(file_id)); 627 let syntax = db.resolve_syntax_ptr(scope_entry.ptr().into_global(file_id));
diff --git a/crates/ra_analysis/src/loc2id.rs b/crates/ra_analysis/src/loc2id.rs
index a53ce8348..8eaa24997 100644
--- a/crates/ra_analysis/src/loc2id.rs
+++ b/crates/ra_analysis/src/loc2id.rs
@@ -1,7 +1,16 @@
1use std::hash::Hash; 1use parking_lot::Mutex;
2
3use std::{
4 hash::Hash,
5 sync::Arc,
6};
2 7
3use rustc_hash::FxHashMap; 8use rustc_hash::FxHashMap;
4 9
10use crate::{
11 syntax_ptr::SyntaxPtr,
12};
13
5/// There are two principle ways to refer to things: 14/// There are two principle ways to refer to things:
6/// - by their locatinon (module in foo/bar/baz.rs at line 42) 15/// - by their locatinon (module in foo/bar/baz.rs at line 42)
7/// - by their numeric id (module `ModuleId(42)`) 16/// - by their numeric id (module `ModuleId(42)`)
@@ -53,8 +62,8 @@ where
53 id 62 id
54 } 63 }
55 64
56 pub fn id2loc(&self, id: &ID) -> L { 65 pub fn id2loc(&self, id: ID) -> L {
57 self.id2loc[id].clone() 66 self.id2loc[&id].clone()
58 } 67 }
59} 68}
60 69
@@ -62,3 +71,38 @@ pub(crate) trait NumericId: Clone + Eq + Hash {
62 fn from_u32(id: u32) -> Self; 71 fn from_u32(id: u32) -> Self;
63 fn to_u32(self) -> u32; 72 fn to_u32(self) -> u32;
64} 73}
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
76pub(crate) struct FnId(u32);
77
78impl NumericId for FnId {
79 fn from_u32(id: u32) -> FnId {
80 FnId(id)
81 }
82 fn to_u32(self) -> u32 {
83 self.0
84 }
85}
86
87pub(crate) trait IdDatabase: salsa::Database {
88 fn id_maps(&self) -> &IdMaps;
89}
90
91#[derive(Debug, Default, Clone)]
92pub(crate) struct IdMaps {
93 inner: Arc<IdMapsInner>,
94}
95
96impl IdMaps {
97 pub(crate) fn fn_id(&self, ptr: SyntaxPtr) -> FnId {
98 self.inner.fns.lock().loc2id(&ptr)
99 }
100 pub(crate) fn fn_ptr(&self, fn_id: FnId) -> SyntaxPtr {
101 self.inner.fns.lock().id2loc(fn_id)
102 }
103}
104
105#[derive(Debug, Default)]
106struct IdMapsInner {
107 fns: Mutex<Loc2IdMap<SyntaxPtr, FnId>>,
108}