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
20 changes: 20 additions & 0 deletions crates/squawk_ide/src/classify.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::symbols::Name;
use squawk_syntax::{
SyntaxKind,
ast::{self, AstNode},
Expand Down Expand Up @@ -106,6 +107,7 @@ pub(crate) enum NameRefClass {
TriggerFunctionCall,
TriggerProcedureCall,
AlterEventTrigger,
OperatorFunctionRef,
}

fn is_special_fn(kind: SyntaxKind) -> bool {
Expand Down Expand Up @@ -356,6 +358,24 @@ pub(crate) fn classify_name_ref(name_ref: &ast::NameRef) -> Option<NameRefClass>
return Some(NameRefClass::SchemaQualifier);
}

// Check for function/procedure reference in CREATE OPERATOR before the type check
for ancestor in name_ref.syntax().ancestors() {
if let Some(attr_option) = ast::AttributeOption::cast(ancestor.clone())
&& let Some(name) = attr_option.name()
{
let attr_name = Name::from_node(&name);
if attr_name == Name::from_string("function")
|| attr_name == Name::from_string("procedure")
{
for outer in attr_option.syntax().ancestors() {
if ast::CreateOperator::can_cast(outer.kind()) {
return Some(NameRefClass::OperatorFunctionRef);
}
}
}
}
}

let mut in_type = false;
for ancestor in name_ref.syntax().ancestors() {
if ast::PathType::can_cast(ancestor.kind()) || ast::ExprType::can_cast(ancestor.kind()) {
Expand Down
28 changes: 28 additions & 0 deletions crates/squawk_ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7780,4 +7780,32 @@ select foo(wrong_param$0 := 5);
",
);
}

#[test]
fn goto_operator_function_ref() {
assert_snapshot!(goto("
create function pg_catalog.tsvector_concat(tsvector, tsvector) returns tsvector language internal;
create operator pg_catalog.|| (leftarg = tsvector, rightarg = tsvector, function = pg_catalog.tsvector_concat$0);
"), @r"
╭▸
2 │ create function pg_catalog.tsvector_concat(tsvector, tsvector) returns tsvector language internal;
│ ─────────────── 2. destination
3 │ create operator pg_catalog.|| (leftarg = tsvector, rightarg = tsvector, function = pg_catalog.tsvector_concat);
╰╴ ─ 1. source
");
}

#[test]
fn goto_operator_procedure_ref() {
assert_snapshot!(goto("
create function f(int, int) returns int language internal;
create operator ||| (leftarg = int, rightarg = int, procedure = f$0);
"), @r"
╭▸
2 │ create function f(int, int) returns int language internal;
│ ─ 2. destination
3 │ create operator ||| (leftarg = int, rightarg = int, procedure = f);
╰╴ ─ 1. source
");
}
}
3 changes: 2 additions & 1 deletion crates/squawk_ide/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ pub fn hover(file: &ast::SourceFile, offset: TextSize) -> Option<String> {
}
NameRefClass::DropFunction
| NameRefClass::DefaultConstraintFunctionCall
| NameRefClass::TriggerFunctionCall => {
| NameRefClass::TriggerFunctionCall
| NameRefClass::OperatorFunctionRef => {
return hover_function(root, &name_ref, &binder);
}
NameRefClass::DropAggregate => return hover_aggregate(root, &name_ref, &binder),
Expand Down
12 changes: 12 additions & 0 deletions crates/squawk_ide/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,18 @@ pub(crate) fn resolve_name_ref_ptrs(
resolve_procedure(binder, &procedure_name, &schema, None, position)
.map(|ptr| smallvec![ptr])
}
NameRefClass::OperatorFunctionRef => {
let path_type = name_ref
.syntax()
.ancestors()
.find_map(ast::PathType::cast)?;
let path = path_type.path()?;
let function_name = extract_table_name(&path)?;
let schema = extract_schema_name(&path);
let position = name_ref.syntax().text_range().start();
resolve_function(binder, &function_name, &schema, None, position)
.map(|ptr| smallvec![ptr])
}
NameRefClass::SelectFunctionCall => {
let schema = if let Some(parent_node) = name_ref.syntax().parent()
&& let Some(field_expr) = ast::FieldExpr::cast(parent_node)
Expand Down
Loading