mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 07:04:22 +00:00
Add Loc2IdMap
This commit is contained in:
parent
f1d8558cd5
commit
cb22a799d6
6 changed files with 84 additions and 0 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -600,6 +600,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"fst 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ra_editor 0.1.0",
|
||||
"ra_syntax 0.1.0",
|
||||
"rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
|
@ -11,6 +11,7 @@ rayon = "1.0.2"
|
|||
fst = "0.3.1"
|
||||
salsa = "0.8.0"
|
||||
rustc-hash = "1.0"
|
||||
parking_lot = "0.6.4"
|
||||
ra_syntax = { path = "../ra_syntax" }
|
||||
ra_editor = { path = "../ra_editor" }
|
||||
test_utils = { path = "../test_utils" }
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use parking_lot::Mutex;
|
||||
|
||||
use ra_editor::LineIndex;
|
||||
use ra_syntax::{SourceFileNode, SyntaxNode};
|
||||
use salsa::{self, Database};
|
||||
|
@ -9,15 +11,19 @@ use crate::{
|
|||
descriptors::{
|
||||
DescriptorDatabase, FnScopesQuery, FnSyntaxQuery, ModuleScopeQuery, ModuleTreeQuery,
|
||||
SubmodulesQuery,
|
||||
module::{ModuleSource, ModuleId},
|
||||
},
|
||||
input::SourceRootId,
|
||||
symbol_index::SymbolIndex,
|
||||
syntax_ptr::SyntaxPtr,
|
||||
loc2id::Loc2IdMap,
|
||||
Cancelable, Canceled, FileId,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct RootDatabase {
|
||||
runtime: salsa::Runtime<RootDatabase>,
|
||||
loc2id: Arc<Mutex<Loc2IdMap<(SourceRootId, ModuleSource), ModuleId>>>,
|
||||
}
|
||||
|
||||
impl salsa::Database for RootDatabase {
|
||||
|
@ -30,6 +36,7 @@ impl Default for RootDatabase {
|
|||
fn default() -> RootDatabase {
|
||||
let mut db = RootDatabase {
|
||||
runtime: Default::default(),
|
||||
loc2id: Default::default(),
|
||||
};
|
||||
db.query_mut(crate::input::SourceRootQuery)
|
||||
.set(crate::input::WORKSPACE, Default::default());
|
||||
|
@ -53,6 +60,7 @@ impl salsa::ParallelDatabase for RootDatabase {
|
|||
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
|
||||
salsa::Snapshot::new(RootDatabase {
|
||||
runtime: self.runtime.snapshot(self),
|
||||
loc2id: Arc::clone(&self.loc2id),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,6 +58,15 @@ enum ModuleSourceNode {
|
|||
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
||||
pub(crate) struct ModuleId(u32);
|
||||
|
||||
impl crate::loc2id::NumericId for ModuleId {
|
||||
fn from_u32(id: u32) -> Self {
|
||||
ModuleId(id)
|
||||
}
|
||||
fn to_u32(self) -> u32 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
||||
pub(crate) struct LinkId(u32);
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ extern crate rustc_hash;
|
|||
extern crate salsa;
|
||||
|
||||
mod db;
|
||||
mod loc2id;
|
||||
mod input;
|
||||
mod imp;
|
||||
mod completion;
|
||||
|
|
64
crates/ra_analysis/src/loc2id.rs
Normal file
64
crates/ra_analysis/src/loc2id.rs
Normal file
|
@ -0,0 +1,64 @@
|
|||
use std::hash::Hash;
|
||||
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
/// There are two principle ways to refer to things:
|
||||
/// - by their locatinon (module in foo/bar/baz.rs at line 42)
|
||||
/// - by their numeric id (module `ModuleId(42)`)
|
||||
///
|
||||
/// The first one is more powerful (you can actually find the thing in question
|
||||
/// by id), but the second one is so much more compact.
|
||||
///
|
||||
/// `Loc2IdMap` allows us to have a cake an eat it as well: by maintaining a
|
||||
/// bidirectional mapping between positional and numeric ids, we can use compact
|
||||
/// representation wich still allows us to get the actual item
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Loc2IdMap<L, ID>
|
||||
where
|
||||
ID: NumericId,
|
||||
L: Clone + Eq + Hash,
|
||||
{
|
||||
loc2id: FxHashMap<L, ID>,
|
||||
id2loc: FxHashMap<ID, L>,
|
||||
}
|
||||
|
||||
impl<L, ID> Default for Loc2IdMap<L, ID>
|
||||
where
|
||||
ID: NumericId,
|
||||
L: Clone + Eq + Hash,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Loc2IdMap {
|
||||
loc2id: FxHashMap::default(),
|
||||
id2loc: FxHashMap::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<L, ID> Loc2IdMap<L, ID>
|
||||
where
|
||||
ID: NumericId,
|
||||
L: Clone + Eq + Hash,
|
||||
{
|
||||
pub fn loc2id(&mut self, loc: &L) -> ID {
|
||||
match self.loc2id.get(loc) {
|
||||
Some(id) => return id.clone(),
|
||||
None => (),
|
||||
}
|
||||
let id = self.loc2id.len();
|
||||
assert!(id < u32::max_value() as usize);
|
||||
let id = ID::from_u32(id as u32);
|
||||
self.loc2id.insert(loc.clone(), id.clone());
|
||||
self.id2loc.insert(id.clone(), loc.clone());
|
||||
id
|
||||
}
|
||||
|
||||
pub fn id2loc(&self, id: &ID) -> L {
|
||||
self.id2loc[id].clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait NumericId: Clone + Eq + Hash {
|
||||
fn from_u32(id: u32) -> Self;
|
||||
fn to_u32(self) -> u32;
|
||||
}
|
Loading…
Reference in a new issue