Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimise variable references search by narrowing down search scopes #178

Merged
merged 1 commit into from
Jan 17, 2025
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
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) {
mkaput marked this conversation as resolved.
Show resolved Hide resolved
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(#195): 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
Loading