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
9 changes: 6 additions & 3 deletions crates/squawk_ide/src/ast_nav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ pub(crate) fn select_from_variant(select_variant: ast::SelectVariant) -> Option<
}
}

#[derive(Debug)]
pub(crate) enum ParentSouce {
Alias(ast::Alias),
CreateTable(ast::CreateTableLike),
Expand All @@ -135,9 +136,11 @@ pub(crate) fn parent_source(node: &SyntaxNode) -> Option<ParentSouce> {
}

for ancestor in node.ancestors() {
if let Some(alias) = ast::Alias::cast(ancestor.clone())
&& alias.column_list().is_some()
{
if let Some(paren_select) = ast::ParenSelect::cast(ancestor.clone()) {
return Some(ParentSouce::ParenSelect(paren_select));
}

if let Some(alias) = ast::Alias::cast(ancestor.clone()) {
return Some(ParentSouce::Alias(alias));
}

Expand Down
83 changes: 22 additions & 61 deletions crates/squawk_ide/src/find_references.rs
Original file line number Diff line number Diff line change
@@ -1,80 +1,41 @@
use crate::builtins::builtins_file;
use crate::db::{File, parse};
use crate::location::{Location, LocationKind};
use crate::offsets::token_from_offset;
use crate::resolve;
use crate::goto_definition;
use crate::location::Location;
use rowan::TextSize;
use salsa::Database as Db;
use smallvec::{SmallVec, smallvec};
use squawk_syntax::{
SyntaxNodePtr,
ast::{self, AstNode},
};
use squawk_syntax::ast::{self, AstNode};

#[salsa::tracked]
pub fn find_references(db: &dyn Db, file: File, offset: TextSize) -> Vec<Location> {
let Some((target_file, target_defs, target_kind)) = find_target_defs(db, offset, file) else {
let targets = goto_definition::goto_definition(db, file, offset);
let Some(first) = targets.first() else {
return vec![];
};

let mut refs: Vec<Location> = vec![];
let mut refs = targets.to_vec();

for ptr in &target_defs {
refs.push(Location {
file: target_file,
range: ptr.text_range(),
kind: target_kind,
});
}

for node in parse(db, file).tree().syntax().descendants() {
if let Some(name_ref) = ast::NameRef::cast(node) {
// Check if the ref matches one of the defs
if let Some(found_defs) = resolve::resolve_name_ref_ptrs(db, target_file, &name_ref)
&& found_defs.iter().any(|def| target_defs.contains(def))
{
refs.push(Location {
file,
range: name_ref.syntax().text_range(),
kind: target_kind,
});
}
for node in parse(db, file)
.tree()
.syntax()
.descendants()
.filter(|x| ast::NameRef::can_cast(x.kind()))
{
let range = node.text_range();
let matches = goto_definition::goto_definition(db, file, range.start())
.into_iter()
.any(|location| targets.contains(&location));
if matches {
refs.push(Location {
file,
range,
kind: first.kind,
});
}
}

refs.sort_by_key(|loc| (loc.file != file, loc.range.start()));
refs
}

fn find_target_defs(
db: &dyn Db,
offset: TextSize,
current_file: File,
) -> Option<(File, SmallVec<[SyntaxNodePtr; 1]>, LocationKind)> {
let token = token_from_offset(db, current_file, offset)?;
let parent = token.parent()?;

if let Some(name) = ast::Name::cast(parent.clone())
&& let Some(kind) = LocationKind::from_node(name.syntax())
{
return Some((
current_file,
smallvec![SyntaxNodePtr::new(name.syntax())],
kind,
));
}

if let Some(name_ref) = ast::NameRef::cast(parent.clone()) {
for target_file in [current_file, builtins_file(db)] {
if let Some((ptrs, kind)) = resolve::resolve_name_ref(db, target_file, &name_ref) {
return Some((target_file, ptrs, kind));
}
}
}

None
}

#[cfg(test)]
mod test {
use crate::builtins::builtins_file;
Expand Down
Loading
Loading