Skip to content

Commit

Permalink
Correction indentation when the HAVING clause contains AND/OR (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppputtyo authored Apr 23, 2024
1 parent a13d8a0 commit f0a289a
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/uroborosql-fmt/src/cst/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use super::{ColumnList, Comment, ExistsSubquery, ExprSeq, Location, SeparatedLin
///
/// renderの際に改行とインデントをせずに描画する(※ ただし、例外的にExpr::Booleanは先頭での改行とインデントを行う)
#[derive(Debug, Clone)]
#[allow(clippy::enum_variant_names)] // TODO: この警告を解消する (https://rust-lang.github.io/rust-clippy/master/index.html#/enum_variant_names)
pub(crate) enum Expr {
/// AS句、二項比較演算、BETWEEN述語など、縦ぞろえを行う式
Aligned(Box<AlignedExpr>),
Expand Down
2 changes: 1 addition & 1 deletion crates/uroborosql-fmt/src/two_way_sql/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::error::UroboroSQLFmtError;
use super::{dag::Kind, tree::TreeNode};

/// 現在のネストのENDまで読む
fn read_before_end(src_lines: &Vec<&str>, cursor: &mut usize) -> Vec<String> {
fn read_before_end(src_lines: &[&str], cursor: &mut usize) -> Vec<String> {
let mut res_lines = vec![];
let mut nest_count = 0;

Expand Down
2 changes: 1 addition & 1 deletion crates/uroborosql-fmt/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ fn compare_token_text(
}

/// トークン列に [カンマ, 行末コメント] の並びがあれば、それを入れ替える関数。
fn swap_comma_and_trailing_comment(tokens: &mut Vec<Token>) {
fn swap_comma_and_trailing_comment(tokens: &mut [Token]) {
for idx in 0..(tokens.len() - 1) {
let fst_tok = tokens.get(idx).unwrap();

Expand Down
1 change: 1 addition & 0 deletions crates/uroborosql-fmt/src/visitor/clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod for_update;
mod frame;
mod from;
mod group_by;
mod having;
mod join;
mod limit;
mod offset;
Expand Down
4 changes: 2 additions & 2 deletions crates/uroborosql-fmt/src/visitor/clause/group_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
};

impl Visitor {
/// GROPU BY句に対応するClauseを持つVecを返す。
/// GROUP BY句に対応するClauseを持つVecを返す。
/// HAVING句がある場合は、HAVING句に対応するClauseも含む。
pub(crate) fn visit_group_by_clause(
&mut self,
Expand Down Expand Up @@ -58,7 +58,7 @@ impl Visitor {
clauses.push(clause);

if cursor.node().kind() == "having_clause" {
clauses.push(self.visit_simple_clause(cursor, src, "having_clause", "HAVING")?);
clauses.push(self.visit_having_clause(cursor, src)?);
}

cursor.goto_parent();
Expand Down
36 changes: 36 additions & 0 deletions crates/uroborosql-fmt/src/visitor/clause/having.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use tree_sitter::TreeCursor;

use crate::{
cst::*,
error::UroboroSQLFmtError,
visitor::{create_clause, ensure_kind, Visitor},
};

impl Visitor {
/// HAVING句をClauseで返す
pub(crate) fn visit_having_clause(
&mut self,
cursor: &mut TreeCursor,
src: &str,
) -> Result<Clause, UroboroSQLFmtError> {
cursor.goto_first_child();

let mut clause = create_clause(cursor, src, "HAVING")?;
cursor.goto_next_sibling();
self.consume_comment_in_clause(cursor, src, &mut clause)?;

// cursor -> _expression
let expr = self.visit_expr(cursor, src)?;

// 結果として得られた式をBodyに変換する
let body = Body::from(expr);

clause.set_body(body);

// cursorを戻す
cursor.goto_parent();
ensure_kind(cursor, "having_clause", src)?;

Ok(clause)
}
}
12 changes: 12 additions & 0 deletions crates/uroborosql-fmt/testfiles/dst/select/group_by.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
select
id as id
, sum(cnt)
from
tbl
group by
id
having
/* comment */
sum(cnt) > 1
and avg(cnt) < 10
;
10 changes: 10 additions & 0 deletions crates/uroborosql-fmt/testfiles/src/select/group_by.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
select
id, sum(cnt)
from
tbl
group by
id
having
/* comment */
sum(cnt) > 1 and avg(cnt) < 10
;

0 comments on commit f0a289a

Please sign in to comment.