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
58 changes: 53 additions & 5 deletions crates/squawk_ide/src/document_symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ use rowan::TextRange;
use squawk_syntax::ast::{self, AstNode};

use crate::binder::{self, extract_string_literal};
use crate::resolve::{resolve_function_info, resolve_table_info, resolve_type_info};
use crate::resolve::{
resolve_function_info, resolve_table_info, resolve_type_info, resolve_view_info,
};

#[derive(Debug)]
pub enum DocumentSymbolKind {
Table,
View,
Function,
Type,
Enum,
Column,
Variant,
}
Expand Down Expand Up @@ -47,6 +51,11 @@ pub fn document_symbols(file: &ast::SourceFile) -> Vec<DocumentSymbol> {
symbols.push(symbol);
}
}
ast::Stmt::CreateView(create_view) => {
if let Some(symbol) = create_view_symbol(&binder, create_view) {
symbols.push(symbol);
}
}
ast::Stmt::Select(select) => {
symbols.extend(cte_table_symbols(select));
}
Expand Down Expand Up @@ -142,6 +151,39 @@ fn create_table_symbol(
})
}

fn create_view_symbol(
binder: &binder::Binder,
create_view: ast::CreateView,
) -> Option<DocumentSymbol> {
let path = create_view.path()?;
let segment = path.segment()?;
let name_node = segment.name()?;

let (schema, view_name) = resolve_view_info(binder, &path)?;
let name = format!("{}.{}", schema.0, view_name);

let full_range = create_view.syntax().text_range();
let focus_range = name_node.syntax().text_range();

let mut children = vec![];
if let Some(column_list) = create_view.column_list() {
for column in column_list.columns() {
if let Some(column_symbol) = create_column_symbol(column) {
children.push(column_symbol);
}
}
}

Some(DocumentSymbol {
name,
detail: None,
kind: DocumentSymbolKind::View,
full_range,
focus_range,
children,
})
}

fn create_function_symbol(
binder: &binder::Binder,
create_function: ast::CreateFunction,
Expand Down Expand Up @@ -198,7 +240,11 @@ fn create_type_symbol(
Some(DocumentSymbol {
name,
detail: None,
kind: DocumentSymbolKind::Type,
kind: if create_type.variant_list().is_some() {
DocumentSymbolKind::Enum
} else {
DocumentSymbolKind::Type
},
full_range,
focus_range,
children,
Expand Down Expand Up @@ -280,8 +326,10 @@ mod tests {
fn symbol_to_group<'a>(symbol: &DocumentSymbol, sql: &'a str) -> Group<'a> {
let kind = match symbol.kind {
DocumentSymbolKind::Table => "table",
DocumentSymbolKind::View => "view",
DocumentSymbolKind::Function => "function",
DocumentSymbolKind::Type => "type",
DocumentSymbolKind::Enum => "enum",
DocumentSymbolKind::Column => "column",
DocumentSymbolKind::Variant => "variant",
};
Expand Down Expand Up @@ -477,7 +525,7 @@ create function my_schema.hello() returns void as $$ select 1; $$ language sql;
assert_snapshot!(
symbols("create type status as enum ('active', 'inactive');"),
@r"
info: type: public.status
info: enum: public.status
╭▸
1 │ create type status as enum ('active', 'inactive');
│ ┬───────────┯━━━━━───────────────────────────────
Expand Down Expand Up @@ -554,7 +602,7 @@ create function my_schema.hello() returns void as $$ select 1; $$ language sql;
assert_snapshot!(
symbols("create type myschema.status as enum ('active', 'inactive');"),
@r"
info: type: myschema.status
info: enum: myschema.status
╭▸
1 │ create type myschema.status as enum ('active', 'inactive');
│ ┬────────────────────┯━━━━━───────────────────────────────
Expand All @@ -579,7 +627,7 @@ create function my_schema.hello() returns void as $$ select 1; $$ language sql;
assert_snapshot!(
symbols("create type priority as enum ('low', 'medium', 'high', 'urgent');"),
@r"
info: type: public.priority
info: enum: public.priority
╭▸
1 │ create type priority as enum ('low', 'medium', 'high', 'urgent');
│ ┬───────────┯━━━━━━━────────────────────────────────────────────
Expand Down
4 changes: 4 additions & 0 deletions crates/squawk_ide/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,10 @@ pub(crate) fn resolve_type_info(binder: &Binder, path: &ast::Path) -> Option<(Sc
resolve_symbol_info(binder, path, SymbolKind::Type)
}

pub(crate) fn resolve_view_info(binder: &Binder, path: &ast::Path) -> Option<(Schema, String)> {
resolve_symbol_info(binder, path, SymbolKind::View)
}

fn resolve_symbol_info(
binder: &Binder,
path: &ast::Path,
Expand Down
2 changes: 2 additions & 0 deletions crates/squawk_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,10 @@ fn handle_document_symbol(
detail: sym.detail,
kind: match sym.kind {
DocumentSymbolKind::Table => SymbolKind::STRUCT,
DocumentSymbolKind::View => SymbolKind::STRUCT,
DocumentSymbolKind::Function => SymbolKind::FUNCTION,
DocumentSymbolKind::Type => SymbolKind::CLASS,
DocumentSymbolKind::Enum => SymbolKind::ENUM,
DocumentSymbolKind::Column => SymbolKind::FIELD,
DocumentSymbolKind::Variant => SymbolKind::ENUM_MEMBER,
},
Expand Down
2 changes: 2 additions & 0 deletions crates/squawk_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,10 @@ fn convert_document_symbol(
detail: symbol.detail,
kind: match symbol.kind {
squawk_ide::document_symbols::DocumentSymbolKind::Table => "table",
squawk_ide::document_symbols::DocumentSymbolKind::View => "view",
squawk_ide::document_symbols::DocumentSymbolKind::Function => "function",
squawk_ide::document_symbols::DocumentSymbolKind::Type => "type",
squawk_ide::document_symbols::DocumentSymbolKind::Enum => "enum",
squawk_ide::document_symbols::DocumentSymbolKind::Column => "column",
squawk_ide::document_symbols::DocumentSymbolKind::Variant => "variant",
}
Expand Down
Loading