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
19 changes: 13 additions & 6 deletions crates/squawk_ide/src/find_references.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::binder::Binder;
use crate::builtins::{builtins_binder, parse_builtins};
use crate::classify::classify_def_node;
use crate::db::{File, bind, parse};
use crate::goto_definition::{FileId, Location};
use crate::goto_definition::{FileId, Location, LocationKind};
use crate::offsets::token_from_offset;
use crate::resolve;
use rowan::TextSize;
Expand All @@ -23,7 +24,7 @@ pub fn find_references(db: &dyn Db, file: File, offset: TextSize) -> Vec<Locatio
let builtins_tree = parse_builtins(db).tree();
let builtins_binder = builtins_binder(db);

let Some((target_file, target_defs)) = find_target_defs(
let Some((target_file, target_defs, target_kind)) = find_target_defs(
&source_file,
offset,
&current_binder,
Expand All @@ -45,6 +46,7 @@ pub fn find_references(db: &dyn Db, file: File, offset: TextSize) -> Vec<Locatio
refs.push(Location {
file: FileId::Builtins,
range: ptr.to_node(builtins_tree.syntax()).text_range(),
kind: target_kind,
});
}
}
Expand All @@ -60,6 +62,7 @@ pub fn find_references(db: &dyn Db, file: File, offset: TextSize) -> Vec<Locatio
refs.push(Location {
file: FileId::Current,
range: name_ref.syntax().text_range(),
kind: target_kind,
});
}
},
Expand All @@ -70,6 +73,7 @@ pub fn find_references(db: &dyn Db, file: File, offset: TextSize) -> Vec<Locatio
refs.push(Location {
file: FileId::Current,
range: name.syntax().text_range(),
kind: target_kind,
});
}
},
Expand All @@ -88,14 +92,17 @@ fn find_target_defs(
current_binder: &Binder,
builtins_tree: &ast::SourceFile,
builtins_binder: &Binder,
) -> Option<(FileId, SmallVec<[SyntaxNodePtr; 1]>)> {
) -> Option<(FileId, SmallVec<[SyntaxNodePtr; 1]>, LocationKind)> {
let token = token_from_offset(file, offset)?;
let parent = token.parent()?;

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

Expand All @@ -105,8 +112,8 @@ fn find_target_defs(
FileId::Current => (current_binder, file.syntax()),
FileId::Builtins => (builtins_binder, builtins_tree.syntax()),
};
if let Some(ptrs) = resolve::resolve_name_ref_ptrs(binder, root, &name_ref) {
return Some((file_id, ptrs));
if let Some((ptrs, kind)) = resolve::resolve_name_ref(binder, root, &name_ref) {
return Some((file_id, ptrs, kind));
}
}
}
Expand Down
118 changes: 110 additions & 8 deletions crates/squawk_ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::binder;
use crate::builtins::parse_builtins;
use crate::classify::{NameRefClass, classify_def_node};
use crate::db::{File, parse};
use crate::offsets::token_from_offset;
use crate::resolve;
Expand Down Expand Up @@ -31,7 +32,10 @@ pub fn goto_definition(db: &dyn Db, file: File, offset: TextSize) -> SmallVec<[L
if let Some(case_expr) = ast::CaseExpr::cast(parent)
&& let Some(case_token) = case_expr.case_token()
{
return smallvec![Location::range(case_token.text_range())];
return smallvec![Location::current(
case_token.text_range(),
LocationKind::CaseExpr
)];
}
}
}
Expand All @@ -40,26 +44,28 @@ pub fn goto_definition(db: &dyn Db, file: File, offset: TextSize) -> SmallVec<[L
if ast::Commit::can_cast(parent.kind())
&& let Some(begin_range) = find_preceding_begin(source_file, token.text_range().start())
{
return smallvec![Location::range(begin_range)];
return smallvec![Location::current(begin_range, LocationKind::CommitBegin)];
}

// goto def on ROLLBACK -> BEGIN/START TRANSACTION
if ast::Rollback::can_cast(parent.kind())
&& let Some(begin_range) = find_preceding_begin(source_file, token.text_range().start())
{
return smallvec![Location::range(begin_range)];
return smallvec![Location::current(begin_range, LocationKind::CommitBegin)];
}

// goto def on BEGIN/START TRANSACTION -> COMMIT or ROLLBACK
if ast::Begin::can_cast(parent.kind())
&& let Some(end_range) =
find_following_commit_or_rollback(source_file, token.text_range().end())
{
return smallvec![Location::range(end_range)];
return smallvec![Location::current(end_range, LocationKind::CommitEnd)];
}

if let Some(name) = ast::Name::cast(parent.clone()) {
return smallvec![Location::range(name.syntax().text_range())];
if let Some(name) = ast::Name::cast(parent.clone())
&& let Some(kind) = classify_def_node(name.syntax()).map(LocationKind::from)
{
return smallvec![Location::current(name.syntax().text_range(), kind)];
}

if let Some(name_ref) = ast::NameRef::cast(parent.clone()) {
Expand All @@ -71,13 +77,14 @@ pub fn goto_definition(db: &dyn Db, file: File, offset: TextSize) -> SmallVec<[L
// TODO: we should salsa this
let binder_output = binder::bind(file);
let root = file.syntax();
if let Some(ptrs) = resolve::resolve_name_ref_ptrs(&binder_output, root, &name_ref) {
if let Some((ptrs, kind)) = resolve::resolve_name_ref(&binder_output, root, &name_ref) {
let ranges = ptrs
.iter()
.map(|ptr| ptr.to_node(file.syntax()).text_range())
.map(|range| Location {
file: file_id,
range,
kind,
})
.collect();
return ranges;
Expand Down Expand Up @@ -106,6 +113,7 @@ pub fn goto_definition(db: &dyn Db, file: File, offset: TextSize) -> SmallVec<[L
return smallvec![Location {
file: file_id,
range: ptr.to_node(file.syntax()).text_range(),
kind: LocationKind::Type,
}];
}
}
Expand All @@ -120,17 +128,111 @@ pub enum FileId {
Builtins,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LocationKind {
Aggregate,
CaseExpr,
Channel,
Column,
CommitBegin,
CommitEnd,
Cursor,
Database,
EventTrigger,
Extension,
Function,
Index,
NamedArgParameter,
Policy,
PreparedStatement,
Procedure,
Role,
Schema,
Sequence,
Server,
Table,
Tablespace,
Trigger,
Type,
View,
Window,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Location {
pub file: FileId,
pub range: TextRange,
pub kind: LocationKind,
}

impl Location {
fn range(range: TextRange) -> Location {
fn current(range: TextRange, kind: LocationKind) -> Location {
Location {
file: FileId::Current,
range,
kind,
}
}
}

impl From<NameRefClass> for LocationKind {
fn from(class: NameRefClass) -> Self {
match class {
NameRefClass::Aggregate => LocationKind::Aggregate,
NameRefClass::Channel => LocationKind::Channel,
NameRefClass::Cursor => LocationKind::Cursor,
NameRefClass::Database => LocationKind::Database,
NameRefClass::EventTrigger => LocationKind::EventTrigger,
NameRefClass::Extension => LocationKind::Extension,
NameRefClass::Index => LocationKind::Index,
NameRefClass::NamedArgParameter => LocationKind::NamedArgParameter,
NameRefClass::Policy => LocationKind::Policy,
NameRefClass::PreparedStatement => LocationKind::PreparedStatement,
NameRefClass::Role => LocationKind::Role,
NameRefClass::Schema => LocationKind::Schema,
NameRefClass::Sequence => LocationKind::Sequence,
NameRefClass::Server => LocationKind::Server,
NameRefClass::Tablespace => LocationKind::Tablespace,
NameRefClass::Trigger => LocationKind::Trigger,
NameRefClass::Type => LocationKind::Type,
NameRefClass::View => LocationKind::View,
NameRefClass::Window => LocationKind::Window,

NameRefClass::CallProcedure | NameRefClass::Procedure | NameRefClass::ProcedureCall => {
LocationKind::Procedure
}

NameRefClass::Function
| NameRefClass::FunctionCall
| NameRefClass::FunctionName
| NameRefClass::Routine
| NameRefClass::SelectFunctionCall => LocationKind::Function,

NameRefClass::AlterColumn
| NameRefClass::CompositeTypeField
| NameRefClass::ConstraintColumn
| NameRefClass::CreateIndexColumn
| NameRefClass::DeleteColumn
| NameRefClass::ForeignKeyColumn
| NameRefClass::InsertColumn
| NameRefClass::JoinUsingColumn
| NameRefClass::MergeColumn
| NameRefClass::PolicyColumn
| NameRefClass::QualifiedColumn
| NameRefClass::SelectColumn
| NameRefClass::SelectQualifiedColumn
| NameRefClass::UpdateColumn => LocationKind::Column,

NameRefClass::DeleteQualifiedColumnTable
| NameRefClass::ForeignKeyTable
| NameRefClass::FromTable
| NameRefClass::InsertQualifiedColumnTable
| NameRefClass::LikeTable
| NameRefClass::MergeQualifiedColumnTable
| NameRefClass::PolicyQualifiedColumnTable
| NameRefClass::SelectQualifiedColumnTable
| NameRefClass::Table
| NameRefClass::UpdateQualifiedColumnTable => LocationKind::Table,
}
}
}
Expand Down
Loading
Loading