Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions crates/ide/src/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use crate::{
references::{self, References, ReferencesConfig},
rename::{self, RenameConfig, RenameResult},
selection_ranges,
semantic_index::{self, ModuleCallEdge},
semantic_tokens::{self, SemaToken, SemaTokenConfig},
signature_help::{self, SignatureHelp, SignatureHelpConfig},
source_change::SourceChange,
Expand Down Expand Up @@ -184,6 +185,22 @@ impl Analysis {
self.with_db(|db| references::references(db, position, config))
}

pub fn module_incoming_calls(
&self,
file_id: FileId,
name_range: TextRange,
) -> Cancellable<Vec<ModuleCallEdge>> {
self.with_db(|db| semantic_index::incoming_module_edges(db, file_id, name_range))
}

pub fn module_outgoing_calls(
&self,
file_id: FileId,
name_range: TextRange,
) -> Cancellable<Vec<ModuleCallEdge>> {
self.with_db(|db| semantic_index::outgoing_module_edges(db, file_id, name_range))
}

pub fn prepare_rename(
&self,
position: FilePosition,
Expand Down
23 changes: 23 additions & 0 deletions crates/ide/src/db/workspace_symbol_index_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ use vfs::FileId;

use crate::{
db::root_db::RootDb,
semantic_index::{ModuleIndex, SemanticIndex},
workspace_symbols::{SymbolIndex, WorkspaceSymbol},
};

#[salsa::query_group(WorkspaceSymbolIndexDbStorage)]
pub trait WorkspaceSymbolIndexDb: SourceRootDb + HirDb {
fn file_workspace_symbols(&self, file_id: FileId) -> Arc<[WorkspaceSymbol]>;
fn source_root_symbol_index(&self, source_root_id: SourceRootId) -> Arc<SymbolIndex>;
fn source_root_module_index(&self, source_root_id: SourceRootId) -> Arc<ModuleIndex>;
}

fn file_workspace_symbols(
Expand All @@ -30,9 +32,30 @@ fn source_root_symbol_index(
Arc::new(SymbolIndex::for_source_root(db, source_root_id))
}

fn source_root_module_index(
db: &dyn WorkspaceSymbolIndexDb,
source_root_id: SourceRootId,
) -> Arc<ModuleIndex> {
Arc::new(ModuleIndex::for_source_root(db, source_root_id))
}

pub(crate) fn source_root_symbol_index_for_root(
db: &RootDb,
source_root_id: SourceRootId,
) -> Arc<SymbolIndex> {
WorkspaceSymbolIndexDb::source_root_symbol_index(db, source_root_id)
}

pub(crate) fn source_root_module_index_for_root(
db: &RootDb,
source_root_id: SourceRootId,
) -> Arc<ModuleIndex> {
WorkspaceSymbolIndexDb::source_root_module_index(db, source_root_id)
}

pub(crate) fn source_root_semantic_index_for_root(
db: &RootDb,
source_root_id: SourceRootId,
) -> Arc<SemanticIndex> {
Arc::new(SemanticIndex::for_source_root(db, source_root_id))
}
1 change: 1 addition & 0 deletions crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub mod range;
pub mod references;
pub mod rename;
pub mod selection_ranges;
pub mod semantic_index;
pub(crate) mod semantic_target;
pub mod semantic_tokens;
pub mod signature_help;
Expand Down
42 changes: 33 additions & 9 deletions crates/ide/src/module_resolution.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::cmp::Ordering;

use hir::{
base_db::{source_db::SourceRootDb, source_root::SourceRootRole},
base_db::{
source_db::{SourceDb, SourceRootDb},
source_root::SourceRootRole,
},
container::InModule,
db::HirDb,
hir_def::{
Expand All @@ -11,7 +14,7 @@ use hir::{
lower_ident_opt,
module::{ModuleId, instantiation::Instantiation},
},
scope::{ModuleEntry, ScopeResolution},
scope::ModuleEntry,
semantics::pathres::PathResolution,
};
use syntax::{
Expand All @@ -21,7 +24,7 @@ use syntax::{
use utils::get::GetRef;
use vfs::{FileId, VfsPath};

use crate::db::root_db::RootDb;
use crate::db::{root_db::RootDb, workspace_symbol_index_db::source_root_module_index_for_root};

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ModuleResolution {
Expand Down Expand Up @@ -154,13 +157,34 @@ fn resolve_module_name_with_policy(
name: &Ident,
policy: ModuleResolutionPolicy,
) -> ModuleResolution {
match db.unit_scope().resolve_module(name) {
ScopeResolution::Unique(module_id) => ModuleResolution::Unique(module_id),
ScopeResolution::Unresolved => ModuleResolution::Unresolved,
ScopeResolution::Ambiguous(candidates) => {
policy.resolve_ambiguous(db, candidates.into_vec())
}
let candidates = module_candidates(db, name);
match candidates.as_slice() {
[module_id] => ModuleResolution::Unique(*module_id),
[] => ModuleResolution::Unresolved,
_ => policy.resolve_ambiguous(db, candidates),
}
}

fn module_candidates(db: &RootDb, name: &Ident) -> Vec<ModuleId> {
let mut source_root_ids =
db.files().iter().map(|&file_id| db.source_root_id(file_id)).collect::<Vec<_>>();
source_root_ids.sort_unstable();
source_root_ids.dedup();

let mut candidates = Vec::new();
for source_root_id in source_root_ids {
let module_index = source_root_module_index_for_root(db, source_root_id);
candidates.extend(
module_index
.module_definitions(name)
.iter()
.map(|module| (module.file_id, module.name_range.start(), module.module_id)),
);
}

candidates.sort_by_key(|(file_id, name_start, _)| (file_id.0, *name_start));
candidates.dedup_by_key(|(_, _, module_id)| *module_id);
candidates.into_iter().map(|(_, _, module_id)| module_id).collect()
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
Loading
Loading