aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/query_definitions.rs
blob: 07415386260808fdb202eda79c7c0855bfdead7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::{
    sync::Arc,
    time::Instant,
};

use rustc_hash::FxHashMap;
use ra_syntax::{
    AstNode, SyntaxNode, TreeArc,
};
use ra_db::SourceRootId;

use crate::{
    SourceFileItems, SourceItemId, DefId, HirFileId,
    FnScopes,
    db::HirDatabase,
    nameres::{ItemMap, Resolver},
};

pub(super) fn fn_scopes(db: &impl HirDatabase, def_id: DefId) -> Arc<FnScopes> {
    let body = db.body_hir(def_id);
    let res = FnScopes::new(body);
    Arc::new(res)
}

pub(super) fn file_items(db: &impl HirDatabase, file_id: HirFileId) -> Arc<SourceFileItems> {
    let source_file = db.hir_source_file(file_id);
    let res = SourceFileItems::new(file_id, &source_file);
    Arc::new(res)
}

pub(super) fn file_item(
    db: &impl HirDatabase,
    source_item_id: SourceItemId,
) -> TreeArc<SyntaxNode> {
    match source_item_id.item_id {
        Some(id) => db.file_items(source_item_id.file_id)[id].to_owned(),
        None => db
            .hir_source_file(source_item_id.file_id)
            .syntax()
            .to_owned(),
    }
}

pub(super) fn item_map(db: &impl HirDatabase, source_root: SourceRootId) -> Arc<ItemMap> {
    let start = Instant::now();
    let module_tree = db.module_tree(source_root);
    let input = module_tree
        .modules()
        .map(|id| (id, db.lower_module_module(source_root, id)))
        .collect::<FxHashMap<_, _>>();

    let resolver = Resolver::new(db, &input, source_root, module_tree);
    let res = resolver.resolve();
    let elapsed = start.elapsed();
    log::info!("item_map: {:?}", elapsed);
    Arc::new(res)
}