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
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jsonwebtoken = "9.3.1"
lazy_static = "1.5.0"
log = "0.4.25"
reqwest = { version = "0.11.27", features = ["native-tls-vendored", "blocking", "json"] }
la-arena = "0.3.1"
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0"
serde_plain = "1.0"
Expand Down
31 changes: 25 additions & 6 deletions PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,26 @@ FROM (
WHERE total_amount > 1000;
```

### Rule: aggregate free having condition

```sql
select a from t group by a having a > 10;
-- ^^^^^^

-- quick fix to:
select a from t where a > 10 group by a;
```

### Rule: order direction is redundent

```sql
select * from t order by a asc;
-- ^^^ order direction is redundent. asc is the default.

-- quick fix to:
select * from t order by a;
```

### Rule: sum(boolean) to case stmt

```sql
Expand Down Expand Up @@ -1076,18 +1096,19 @@ also show lex command
### Snippets

- [datagrip live templates](https://blog.jetbrains.com/datagrip/2019/03/11/top-9-sql-features-of-datagrip-you-have-to-know/#live_templates)
- insert
- select
- create table
- [postgresql-snippets](https://github.com/Manuel7806/postgresql-snippets/blob/main/snippets/snippets.code-snippets)

### Quick Fix: alias query

```sql
select * from bar
-- ^$ action:rename-alias
```

becomes after filling in alias name with `b`
-- becomes after filling in alias name with `b`

```sql
select b.* from bar b
```

Expand All @@ -1096,11 +1117,9 @@ another example:
```sql
select name, email from bar
-- ^$ action:rename-alias
```

becomes after filling in alias name with `b`
-- becomes after filling in alias name with `b`

```sql
select b.name, b.email from bar
```

Expand Down
2 changes: 2 additions & 0 deletions crates/squawk_ide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ rowan.workspace = true
line-index.workspace = true
annotate-snippets.workspace = true
log.workspace = true
smol_str.workspace = true
la-arena.workspace = true

[dev-dependencies]
insta.workspace = true
Expand Down
115 changes: 115 additions & 0 deletions crates/squawk_ide/src/binder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/// Loosely based on TypeScript's binder
/// see: typescript-go/internal/binder/binder.go
use la_arena::Arena;
use squawk_syntax::{SyntaxNodePtr, ast, ast::AstNode};

use crate::scope::{Scope, ScopeId};
use crate::symbols::{Name, Schema, Symbol, SymbolKind};

pub(crate) struct Binder {
pub(crate) scopes: Arena<Scope>,
pub(crate) symbols: Arena<Symbol>,
}

impl Binder {
fn new() -> Self {
let mut scopes = Arena::new();
let _root_scope = scopes.alloc(Scope::with_parent(None));
Binder {
scopes,
symbols: Arena::new(),
}
}

pub(crate) fn root_scope(&self) -> ScopeId {
self.scopes
.iter()
.next()
.map(|(id, _)| id)
.expect("root scope must exist")
}
}

pub(crate) fn bind(file: &ast::SourceFile) -> Binder {
let mut binder = Binder::new();

bind_file(&mut binder, file);

binder
}

fn bind_file(b: &mut Binder, file: &ast::SourceFile) {
for stmt in file.stmts() {
bind_stmt(b, stmt);
}
}

fn bind_stmt(b: &mut Binder, stmt: ast::Stmt) {
if let ast::Stmt::CreateTable(create_table) = stmt {
bind_create_table(b, create_table)
}
}

fn bind_create_table(b: &mut Binder, create_table: ast::CreateTable) {
let Some(path) = create_table.path() else {
return;
};
let Some(table_name) = item_name(&path) else {
return;
};
let name_ptr = path_to_ptr(&path);
let schema = schema_name(&path);

let table_id = b.symbols.alloc(Symbol {
kind: SymbolKind::Table,
ptr: name_ptr,
schema,
});

let root = b.root_scope();
b.scopes[root].insert(table_name, table_id);
}

fn item_name(path: &ast::Path) -> Option<Name> {
let segment = path.segment()?;

if let Some(name) = segment.name() {
return Some(Name::new(name.syntax().text().to_string()));
}
if let Some(name) = segment.name_ref() {
return Some(Name::new(name.syntax().text().to_string()));
}

None
}

fn path_to_ptr(path: &ast::Path) -> SyntaxNodePtr {
if let Some(segment) = path.segment() {
if let Some(name) = segment.name() {
return SyntaxNodePtr::new(name.syntax());
}
if let Some(name_ref) = segment.name_ref() {
return SyntaxNodePtr::new(name_ref.syntax());
}
}
SyntaxNodePtr::new(path.syntax())
}

fn schema_name(path: &ast::Path) -> Schema {
let Some(qualifier) = path.qualifier() else {
return Schema::Public;
};
let Some(segment) = qualifier.segment() else {
return Schema::Public;
};

let schema_name = if let Some(name) = segment.name() {
Name::new(name.syntax().text().to_string())
} else if let Some(name_ref) = segment.name_ref() {
Name::new(name_ref.syntax().text().to_string())
} else {
return Schema::Public;
};

Schema::from_name(schema_name)
}
73 changes: 73 additions & 0 deletions crates/squawk_ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::binder;
use crate::offsets::token_from_offset;
use crate::resolve;
use rowan::{TextRange, TextSize};
use squawk_syntax::{
SyntaxKind,
Expand Down Expand Up @@ -45,6 +47,14 @@ pub fn goto_definition(file: ast::SourceFile, offset: TextSize) -> Option<TextRa
}
}

if let Some(name_ref) = ast::NameRef::cast(parent.clone()) {
let binder_output = binder::bind(&file);
if let Some(ptr) = resolve::resolve_name_ref(&binder_output, &name_ref) {
let node = ptr.to_node(file.syntax());
return Some(node.text_range());
}
}

return None;
}

Expand Down Expand Up @@ -204,6 +214,69 @@ rollback$0;
");
}

#[test]
fn goto_drop_table() {
assert_snapshot!(goto("
create table t();
drop table t$0;
"), @r"
╭▸
2 │ create table t();
│ ─ 2. destination
3 │ drop table t;
╰╴ ─ 1. source
");
}

#[test]
fn goto_drop_table_with_schema() {
assert_snapshot!(goto("
create table public.t();
drop table t$0;
"), @r"
╭▸
2 │ create table public.t();
│ ─ 2. destination
3 │ drop table t;
╰╴ ─ 1. source
");

assert_snapshot!(goto("
create table foo.t();
drop table foo.t$0;
"), @r"
╭▸
2 │ create table foo.t();
│ ─ 2. destination
3 │ drop table foo.t;
╰╴ ─ 1. source
");

goto_not_found(
"
-- defaults to public schema
create table t();
drop table foo.t$0;
",
);

// todo: temp tables
}

#[test]
fn goto_drop_table_defined_after() {
assert_snapshot!(goto("
drop table t$0;
create table t();
"), @r"
╭▸
2 │ drop table t;
│ ─ 1. source
3 │ create table t();
╰╴ ─ 2. destination
");
}

#[test]
fn begin_to_rollback() {
assert_snapshot!(goto(
Expand Down
4 changes: 4 additions & 0 deletions crates/squawk_ide/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
mod binder;
pub mod code_actions;
pub mod column_name;
pub mod expand_selection;
mod generated;
pub mod goto_definition;
mod offsets;
mod resolve;
mod scope;
mod symbols;
#[cfg(test)]
pub mod test_utils;
Loading
Loading