Skip to content

Commit

Permalink
Optimise variable references search by narrowing down search scopes
Browse files Browse the repository at this point in the history
commit-id:7a9a25c2
  • Loading branch information
mkaput committed Jan 17, 2025
1 parent 39b5897 commit 9675a4f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/lang/inspect/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,31 @@ impl SymbolDef {
/// Gets the name of the symbol.
pub fn name(&self, db: &AnalysisDatabase) -> SmolStr {
match self {
SymbolDef::Item(it) => it.name(db),
SymbolDef::Variable(it) => it.name(),
SymbolDef::ExprInlineMacro(name) => name.clone(),
SymbolDef::Member(it) => it.name(db),
SymbolDef::Module(it) => it.name(db),
Self::Item(it) => it.name(db),
Self::Variable(it) => it.name(),
Self::ExprInlineMacro(name) => name.clone(),
Self::Member(it) => it.name(db),
Self::Module(it) => it.name(db),
}
}

/// Builds a search scope for finding usages of this symbol.
#[tracing::instrument(skip_all)]
pub fn search_scope(&self, db: &AnalysisDatabase) -> SearchScope {
// TODO(mkaput): Narrow down the scope as much as possible for particular symbol kinds.
SearchScope::everything(db)
match &self {
Self::Variable(var) => {
match db.first_ancestor_of_kind(var.syntax_node(db), SyntaxKind::FunctionWithBody) {
Some(owning_function) => SearchScope::file_span(
owning_function.stable_ptr().file_id(db.upcast()),
owning_function.span(db.upcast()),
),
None => SearchScope::file(var.definition_stable_ptr.file_id(db.upcast())),
}
}

// TODO(mkaput): Use visibility information to narrow down search scopes.
_ => SearchScope::everything(db),
}
}

/// Starts a find-usages search for this symbol.
Expand Down Expand Up @@ -332,6 +345,11 @@ impl VariableDef {
Some(Self { name, var, definition_stable_ptr: param.stable_ptr().untyped() })
}

/// Gets the syntax node of the variable definition.
pub fn syntax_node(&self, db: &AnalysisDatabase) -> SyntaxNode {
self.definition_stable_ptr.lookup(db.upcast())
}

/// Gets variable signature, which tries to resemble the way how it is defined in code.
pub fn signature(&self, db: &AnalysisDatabase) -> String {
let Self { name, var, .. } = self;
Expand Down
10 changes: 10 additions & 0 deletions src/lang/inspect/usages/search_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ impl SearchScope {
this
}

/// Builds a search scope spanning an entire single file.
pub fn file(file: FileId) -> Self {
Self { entries: [(file, None)].into() }
}

/// Builds a search scope spanning a slice of a single file.
pub fn file_span(file: FileId, span: TextSpan) -> Self {
Self { entries: [(file, Some(span))].into() }
}

/// Creates an iterator over all files and the optional search scope text spans.
pub fn files_and_spans(&self) -> impl Iterator<Item = (FileId, Option<TextSpan>)> + use<'_> {
self.entries.iter().map(|(&file, &span)| (file, span))
Expand Down

0 comments on commit 9675a4f

Please sign in to comment.