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
2 changes: 2 additions & 0 deletions crates/squawk_ide/src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ impl Binder {
) -> Option<SyntaxNodePtr> {
let symbols = self.scope.get(name)?;

// TODO: we should just require Schema, rather than doing the lookup in
// here. Having to thread position is annoying.
let search_paths = match schema {
Some(s) => std::slice::from_ref(s),
None => self.search_path_at(position),
Expand Down
7 changes: 3 additions & 4 deletions crates/squawk_ide/src/code_actions/add_explicit_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ use squawk_linter::Edit;
use squawk_syntax::ast::{self, AstNode};
use squawk_syntax::quote::quote_column_alias;

use crate::{column_name::ColumnName, db::File, offsets::token_from_offset};
use crate::{column_name::ColumnName, file::InFile, offsets::token_from_offset};

use super::{ActionKind, CodeAction};

pub(super) fn add_explicit_alias(
db: &dyn Db,
file: File,
position: InFile<TextSize>,
actions: &mut Vec<CodeAction>,
offset: TextSize,
) -> Option<()> {
let token = token_from_offset(db, file, offset)?;
let token = token_from_offset(db, position)?;
let target = token.parent_ancestors().find_map(ast::Target::cast)?;

if target.as_name().is_some() {
Expand Down
21 changes: 11 additions & 10 deletions crates/squawk_ide/src/code_actions/add_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ use squawk_linter::Edit;
use squawk_syntax::ast::{self, AstNode};

use super::{ActionKind, CodeAction};
use crate::{
db::{File, bind},
offsets::token_from_offset,
};
use crate::{db::bind, file::InFile, offsets::token_from_offset};

pub(super) fn add_schema(
db: &dyn Db,
file: File,
position: InFile<TextSize>,
actions: &mut Vec<CodeAction>,
offset: TextSize,
) -> Option<()> {
let token = token_from_offset(db, file, offset)?;
let file = position.file_id;
let offset = position.value;
let token = token_from_offset(db, position)?;
let range = token.parent_ancestors().find_map(|node| {
if let Some(path) = ast::Path::cast(node.clone()) {
if path.qualifier().is_some() {
Expand All @@ -40,13 +38,16 @@ pub(super) fn add_schema(
return None;
}

let position = token.text_range().start();
let schema = bind(db, file).search_path_at(position).first()?.to_string();
let token_start = token.text_range().start();
let schema = bind(db, file)
.search_path_at(token_start)
.first()?
.to_string();
let replacement = format!("{}.", schema);

actions.push(CodeAction {
title: "Add schema".to_owned(),
edits: vec![Edit::insert(replacement, position)],
edits: vec![Edit::insert(replacement, token_start)],
kind: ActionKind::RefactorRewrite,
});

Expand Down
47 changes: 23 additions & 24 deletions crates/squawk_ide/src/code_actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rowan::TextSize;
use salsa::Database as Db;
use squawk_linter::Edit;

use crate::db::File;
use crate::file::InFile;

mod add_explicit_alias;
mod add_schema;
Expand Down Expand Up @@ -64,29 +64,28 @@ pub struct CodeAction {
pub kind: ActionKind,
}

#[salsa::tracked]
pub fn code_actions(db: &dyn Db, file: File, offset: TextSize) -> Option<Vec<CodeAction>> {
pub fn code_actions(db: &dyn Db, position: InFile<TextSize>) -> Option<Vec<CodeAction>> {
let mut actions = vec![];
rewrite_as_regular_string(db, file, &mut actions, offset);
rewrite_as_dollar_quoted_string(db, file, &mut actions, offset);
remove_else_clause(db, file, &mut actions, offset);
rewrite_table_as_select(db, file, &mut actions, offset);
rewrite_select_as_table(db, file, &mut actions, offset);
rewrite_from(db, file, &mut actions, offset);
rewrite_leading_from(db, file, &mut actions, offset);
rewrite_values_as_select(db, file, &mut actions, offset);
rewrite_select_as_values(db, file, &mut actions, offset);
rewrite_select_into_as_create_table_as(db, file, &mut actions, offset);
rewrite_create_table_as_as_select_into(db, file, &mut actions, offset);
add_schema(db, file, &mut actions, offset);
quote_identifier(db, file, &mut actions, offset);
unquote_identifier(db, file, &mut actions, offset);
add_explicit_alias(db, file, &mut actions, offset);
remove_redundant_alias(db, file, &mut actions, offset);
rewrite_cast_to_double_colon(db, file, &mut actions, offset);
rewrite_double_colon_to_cast(db, file, &mut actions, offset);
rewrite_between_as_binary_expression(db, file, &mut actions, offset);
rewrite_not_equals_operator(db, file, &mut actions, offset);
rewrite_timestamp_type(db, file, &mut actions, offset);
rewrite_as_regular_string(db, position, &mut actions);
rewrite_as_dollar_quoted_string(db, position, &mut actions);
remove_else_clause(db, position, &mut actions);
rewrite_table_as_select(db, position, &mut actions);
rewrite_select_as_table(db, position, &mut actions);
rewrite_from(db, position, &mut actions);
rewrite_leading_from(db, position, &mut actions);
rewrite_values_as_select(db, position, &mut actions);
rewrite_select_as_values(db, position, &mut actions);
rewrite_select_into_as_create_table_as(db, position, &mut actions);
rewrite_create_table_as_as_select_into(db, position, &mut actions);
add_schema(db, position, &mut actions);
quote_identifier(db, position, &mut actions);
unquote_identifier(db, position, &mut actions);
add_explicit_alias(db, position, &mut actions);
remove_redundant_alias(db, position, &mut actions);
rewrite_cast_to_double_colon(db, position, &mut actions);
rewrite_double_colon_to_cast(db, position, &mut actions);
rewrite_between_as_binary_expression(db, position, &mut actions);
rewrite_not_equals_operator(db, position, &mut actions);
rewrite_timestamp_type(db, position, &mut actions);
Some(actions)
}
7 changes: 3 additions & 4 deletions crates/squawk_ide/src/code_actions/quote_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ use salsa::Database as Db;
use squawk_linter::Edit;
use squawk_syntax::ast::{self, AstNode};

use crate::{db::File, offsets::token_from_offset};
use crate::{file::InFile, offsets::token_from_offset};

use super::{ActionKind, CodeAction};

pub(super) fn quote_identifier(
db: &dyn Db,
file: File,
position: InFile<TextSize>,
actions: &mut Vec<CodeAction>,
offset: TextSize,
) -> Option<()> {
let token = token_from_offset(db, file, offset)?;
let token = token_from_offset(db, position)?;
let parent = token.parent()?;

let (is_quoted, text, text_range) = if let Some(name) = ast::Name::cast(parent.clone()) {
Expand Down
8 changes: 5 additions & 3 deletions crates/squawk_ide/src/code_actions/remove_else_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ use squawk_syntax::{
ast::{self, AstNode},
};

use crate::db::{File, parse};
use crate::db::parse;
use crate::file::InFile;

use super::{ActionKind, CodeAction};

pub(super) fn remove_else_clause(
db: &dyn Db,
file: File,
position: InFile<TextSize>,
actions: &mut Vec<CodeAction>,
offset: TextSize,
) -> Option<()> {
let file = position.file_id;
let offset = position.value;
let source_file = parse(db, file).tree();

let else_token = source_file
Expand Down
7 changes: 3 additions & 4 deletions crates/squawk_ide/src/code_actions/remove_redundant_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ use salsa::Database as Db;
use squawk_linter::Edit;
use squawk_syntax::ast::{self, AstNode};

use crate::{column_name::ColumnName, db::File, offsets::token_from_offset, symbols::Name};
use crate::{column_name::ColumnName, file::InFile, offsets::token_from_offset, symbols::Name};

use super::{ActionKind, CodeAction};

pub(super) fn remove_redundant_alias(
db: &dyn Db,
file: File,
position: InFile<TextSize>,
actions: &mut Vec<CodeAction>,
offset: TextSize,
) -> Option<()> {
let token = token_from_offset(db, file, offset)?;
let token = token_from_offset(db, position)?;
let target = token.parent_ancestors().find_map(ast::Target::cast)?;

let as_name = target.as_name()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ use salsa::Database as Db;
use squawk_linter::Edit;
use squawk_syntax::{SyntaxKind, ast::AstNode};

use crate::db::{File, parse};
use crate::db::parse;
use crate::file::InFile;

use super::{ActionKind, CodeAction};

pub(super) fn rewrite_as_dollar_quoted_string(
db: &dyn Db,
file: File,
position: InFile<TextSize>,
actions: &mut Vec<CodeAction>,
offset: TextSize,
) -> Option<()> {
let file = position.file_id;
let offset = position.value;
let string = parse(db, file)
.tree()
.syntax()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ use salsa::Database as Db;
use squawk_linter::Edit;
use squawk_syntax::{SyntaxKind, ast::AstNode};

use crate::db::{File, parse};
use crate::db::parse;
use crate::file::InFile;

use super::{ActionKind, CodeAction};

pub(super) fn rewrite_as_regular_string(
db: &dyn Db,
file: File,
position: InFile<TextSize>,
actions: &mut Vec<CodeAction>,
offset: TextSize,
) -> Option<()> {
let file = position.file_id;
let offset = position.value;
let dollar_string = parse(db, file)
.tree()
.syntax()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ use salsa::Database as Db;
use squawk_linter::Edit;
use squawk_syntax::ast::AstNode;

use crate::{db::File, offsets::token_from_offset};
use crate::{file::InFile, offsets::token_from_offset};

use super::{ActionKind, CodeAction};

pub(super) fn rewrite_between_as_binary_expression(
db: &dyn Db,
file: File,
position: InFile<TextSize>,
actions: &mut Vec<CodeAction>,
offset: TextSize,
) -> Option<()> {
use squawk_syntax::ast;

let token = token_from_offset(db, file, offset)?;
let token = token_from_offset(db, position)?;
let between_expr = token.parent_ancestors().find_map(ast::BetweenExpr::cast)?;

let target = between_expr.target()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ use salsa::Database as Db;
use squawk_linter::Edit;
use squawk_syntax::ast::{self, AstNode};

use crate::{db::File, offsets::token_from_offset};
use crate::{file::InFile, offsets::token_from_offset};

use super::{ActionKind, CodeAction};

pub(super) fn rewrite_cast_to_double_colon(
db: &dyn Db,
file: File,
position: InFile<TextSize>,
actions: &mut Vec<CodeAction>,
offset: TextSize,
) -> Option<()> {
let token = token_from_offset(db, file, offset)?;
let token = token_from_offset(db, position)?;
let cast_expr = token.parent_ancestors().find_map(ast::CastExpr::cast)?;

if cast_expr.colon_colon().is_some() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ use squawk_syntax::{
ast::{self, AstNode},
};

use crate::{db::File, offsets::token_from_offset};
use crate::{file::InFile, offsets::token_from_offset};

use super::{ActionKind, CodeAction};

pub(super) fn rewrite_create_table_as_as_select_into(
db: &dyn Db,
file: File,
position: InFile<TextSize>,
actions: &mut Vec<CodeAction>,
offset: TextSize,
) -> Option<()> {
let token = token_from_offset(db, file, offset)?;
let token = token_from_offset(db, position)?;
let create_table_as = token
.parent_ancestors()
.find_map(ast::CreateTableAs::cast)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ use salsa::Database as Db;
use squawk_linter::Edit;
use squawk_syntax::ast::{self, AstNode};

use crate::{db::File, offsets::token_from_offset};
use crate::{file::InFile, offsets::token_from_offset};

use super::{ActionKind, CodeAction};

pub(super) fn rewrite_double_colon_to_cast(
db: &dyn Db,
file: File,
position: InFile<TextSize>,
actions: &mut Vec<CodeAction>,
offset: TextSize,
) -> Option<()> {
let token = token_from_offset(db, file, offset)?;
let token = token_from_offset(db, position)?;
let cast_expr = token.parent_ancestors().find_map(ast::CastExpr::cast)?;

if cast_expr.cast_token().is_some() {
Expand Down
7 changes: 3 additions & 4 deletions crates/squawk_ide/src/code_actions/rewrite_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ use salsa::Database as Db;
use squawk_linter::Edit;
use squawk_syntax::ast::{self, AstNode};

use crate::{db::File, offsets::token_from_offset};
use crate::{file::InFile, offsets::token_from_offset};

use super::{ActionKind, CodeAction};

pub(super) fn rewrite_from(
db: &dyn Db,
file: File,
position: InFile<TextSize>,
actions: &mut Vec<CodeAction>,
offset: TextSize,
) -> Option<()> {
let token = token_from_offset(db, file, offset)?;
let token = token_from_offset(db, position)?;
let select = token.parent_ancestors().find_map(ast::Select::cast)?;

if select.select_clause().is_some() {
Expand Down
7 changes: 3 additions & 4 deletions crates/squawk_ide/src/code_actions/rewrite_leading_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ use squawk_syntax::{
ast::{self, AstNode},
};

use crate::{db::File, offsets::token_from_offset};
use crate::{file::InFile, offsets::token_from_offset};

use super::{ActionKind, CodeAction};

pub(super) fn rewrite_leading_from(
db: &dyn Db,
file: File,
position: InFile<TextSize>,
actions: &mut Vec<CodeAction>,
offset: TextSize,
) -> Option<()> {
let token = token_from_offset(db, file, offset)?;
let token = token_from_offset(db, position)?;
let select = token.parent_ancestors().find_map(ast::Select::cast)?;

let from_clause = select.from_clause()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ use salsa::Database as Db;
use squawk_linter::Edit;
use squawk_syntax::ast::{self, AstNode};

use crate::{db::File, offsets::token_from_offset};
use crate::{file::InFile, offsets::token_from_offset};

use super::{ActionKind, CodeAction};

pub(super) fn rewrite_not_equals_operator(
db: &dyn Db,
file: File,
position: InFile<TextSize>,
actions: &mut Vec<CodeAction>,
offset: TextSize,
) -> Option<()> {
let token = token_from_offset(db, file, offset)?;
let token = token_from_offset(db, position)?;
let bin_expr = token.parent_ancestors().find_map(ast::BinExpr::cast)?;

let (op_token, replacement, title) = match bin_expr.op()? {
Expand Down
Loading
Loading