Skip to content

Commit

Permalink
Support indentation with spaces (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanzaku authored Dec 6, 2024
1 parent 94a26e0 commit 53f371a
Show file tree
Hide file tree
Showing 61 changed files with 1,610 additions and 145 deletions.
3 changes: 2 additions & 1 deletion .uroborosqlfmtrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"remove_redundant_nest": true,
"complement_sql_id": true,
"convert_double_colon_cast": false,
"unify_not_equal": true
"unify_not_equal": true,
"indent_tab": true
}
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"crates/uroborosql-fmt-napi",
"crates/uroborosql-fmt-wasm"
]
resolver = "2"

[workspace.package]
authors = ["Future Corporation"]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ If there is no configuration file, the default values are used.
| [`complement_sql_id`](docs/options/complement_sql_id.md) | bool | Complement [SQL ID](https://palette-doc.rtfa.as/coding-standards/forSQL/SQL%E3%82%B3%E3%83%BC%E3%83%87%E3%82%A3%E3%83%B3%E3%82%B0%E8%A6%8F%E7%B4%84%EF%BC%88uroboroSQL%EF%BC%89.html#sql-%E8%AD%98%E5%88%A5%E5%AD%90). | false |
| [`convert_double_colon_cast`](docs/options/convert_double_colon_cast.md) | bool | Convert casts by `X::type` to the form `CAST(X AS type)`. | true |
| [`unify_not_equal`](docs/options/unify_not_equal.md) | bool | Convert comparison operator `<>` to `!=` | true |
| [`indent_tab`](docs/options/indent_tab.md) | bool | Switch the indentation style between tabs and spaces. | true |

## Structure

Expand Down
10 changes: 10 additions & 0 deletions crates/uroborosql-fmt/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ fn default_unify_not_equal() -> bool {
true
}

/// indent_tabのデフォルト値(true)
fn default_indent_tab() -> bool {
true
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub(crate) enum Case {
Expand Down Expand Up @@ -146,6 +151,9 @@ pub struct Config {
/// not_equalを!=に統一する
#[serde(default = "default_unify_not_equal")]
pub(crate) unify_not_equal: bool,
/// 空白文字ではなくタブ文字でインデントする
#[serde(default = "default_indent_tab")]
pub(crate) indent_tab: bool,
}

impl Config {
Expand Down Expand Up @@ -208,6 +216,7 @@ impl Default for Config {
complement_sql_id: default_complement_sql_id(),
convert_double_colon_cast: default_convert_double_colon_cast(),
unify_not_equal: default_unify_not_equal(),
indent_tab: default_indent_tab(),
}
}
}
Expand All @@ -234,6 +243,7 @@ pub(crate) fn load_never_complement_settings() {
remove_redundant_nest: false,
convert_double_colon_cast: false,
unify_not_equal: false,
indent_tab: true,
};

*CONFIG.write().unwrap() = config;
Expand Down
18 changes: 6 additions & 12 deletions crates/uroborosql-fmt/src/cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) use with::*;
use itertools::{repeat_n, Itertools};
use tree_sitter::{Node, Point, Range};

use crate::{config::CONFIG, error::UroboroSQLFmtError, re::RE};
use crate::{config::CONFIG, error::UroboroSQLFmtError, re::RE, util::add_indent};

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Position {
Expand Down Expand Up @@ -119,7 +119,7 @@ impl Comment {
let mut result = String::new();

// インデントの挿入
result.extend(repeat_n('\t', depth));
add_indent(&mut result, depth);

if self.is_block_comment() && !self.loc.is_single_line() {
// ブロックコメント かつ 単一行ではない (= 複数行ブロックコメント)
Expand Down Expand Up @@ -204,7 +204,7 @@ impl Comment {
// 先頭の空白文字とアスタリスクをトリミング
let trimmed_line = line.trim_start().trim_start_matches('*');

result.extend(repeat_n('\t', depth));
add_indent(&mut result, depth);

// ```
// /*
Expand All @@ -229,15 +229,9 @@ impl Comment {
let complement_tab = need_space / tab_size;

// 補完するスペースの数
let complement_space = if need_space % tab_size == 0 {
// 割り切れる場合はタブのみで補完
0
} else {
// 割り切れない場合はタブで補完した後にスペースで補完
need_space % tab_size
};
let complement_space = need_space % tab_size;

result.extend(repeat_n('\t', complement_tab));
add_indent(&mut result, complement_tab);
result.extend(repeat_n(' ', complement_space));
result.push_str(line);
} else {
Expand Down Expand Up @@ -280,7 +274,7 @@ impl Comment {
result.push('\n');
}

result.extend(repeat_n('\t', depth));
add_indent(&mut result, depth);

if requires_asterisk_alignment {
result.push(' ');
Expand Down
38 changes: 19 additions & 19 deletions crates/uroborosql-fmt/src/cst/body/insert.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use itertools::repeat_n;

use crate::{
cst::{
AlignedExpr, Clause, ColumnList, Comment, ConflictTargetColumnList, Expr, Location,
Statement,
add_indent, AlignedExpr, Clause, ColumnList, Comment, ConflictTargetColumnList, Expr,
Location, Statement,
},
error::UroboroSQLFmtError,
util::{add_single_space, add_space_by_range, tab_size},
};

use super::separeted_lines::SeparatedLines;
Expand Down Expand Up @@ -65,10 +64,10 @@ impl OnConstraint {
// ON
result.push_str(&self.keyword.0);
result.push('\n');
result.extend(repeat_n('\t', depth));
add_indent(&mut result, depth);
// CONSTRAINT
result.push_str(&self.keyword.1);
result.push('\t');
add_single_space(&mut result);
result.push_str(&self.constraint_name);
result.push('\n');

Expand Down Expand Up @@ -98,11 +97,11 @@ impl DoNothing {
pub(crate) fn render(&self, depth: usize) -> Result<String, UroboroSQLFmtError> {
let mut result = String::new();

result.extend(repeat_n('\t', depth - 1));
add_indent(&mut result, depth - 1);
// DO
result.push_str(&self.keyword.0);
result.push('\n');
result.extend(repeat_n('\t', depth));
add_indent(&mut result, depth);
// NOTHING
result.push_str(&self.keyword.1);
result.push('\n');
Expand Down Expand Up @@ -135,11 +134,11 @@ impl DoUpdate {
pub(crate) fn render(&self, depth: usize) -> Result<String, UroboroSQLFmtError> {
let mut result = String::new();

result.extend(repeat_n('\t', depth - 1));
add_indent(&mut result, depth - 1);
// DO
result.push_str(&self.keyword.0);
result.push('\n');
result.extend(repeat_n('\t', depth));
add_indent(&mut result, depth);
// UPDATE
result.push_str(&self.keyword.1);
result.push('\n');
Expand Down Expand Up @@ -186,11 +185,11 @@ impl OnConflict {
pub(crate) fn render(&self, depth: usize) -> Result<String, UroboroSQLFmtError> {
let mut result = String::new();

result.extend(repeat_n('\t', depth - 1));
add_indent(&mut result, depth - 1);
// ON
result.push_str(&self.keyword.0);
result.push('\n');
result.extend(repeat_n('\t', depth));
add_indent(&mut result, depth);
// CONFLICT
result.push_str(&self.keyword.1);

Expand All @@ -203,7 +202,7 @@ impl OnConflict {
}
ConflictTarget::SpecifyIndexColumn(specify_index_column) => {
// INDEXカラム指定の場合は改行せずに描画
result.push('\t');
add_single_space(&mut result);
result.push_str(&specify_index_column.render(depth)?);
}
}
Expand Down Expand Up @@ -248,15 +247,16 @@ impl Values {

if !is_one_row {
result.push('\n');
result.extend(repeat_n('\t', depth));
add_indent(&mut result, depth);
} else {
// "VALUES" と "(" の間の空白
result.push(' ');
}

let mut separator = String::from('\n');
separator.extend(repeat_n('\t', depth - 1));
separator.push_str(",\t");
add_indent(&mut separator, depth - 1);
separator.push(',');
add_space_by_range(&mut separator, 1, tab_size());

result.push_str(
&self
Expand Down Expand Up @@ -445,16 +445,16 @@ impl InsertBody {
let mut result = String::new();

// テーブル名
result.extend(repeat_n('\t', depth));
add_indent(&mut result, depth);
result.push_str(&self.table_name.render(depth)?);
result.push('\n');

// カラム名
if let Some(sep_lines) = &self.columns {
result.extend(repeat_n('\t', depth - 1));
add_indent(&mut result, depth - 1);
result.push_str("(\n");
result.push_str(&sep_lines.render(depth)?);
result.extend(repeat_n('\t', depth - 1));
add_indent(&mut result, depth - 1);
result.push(')');
}

Expand Down
18 changes: 9 additions & 9 deletions crates/uroborosql-fmt/src/cst/body/separeted_lines.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use itertools::{repeat_n, Itertools};
use itertools::Itertools;

use crate::{
cst::{AlignInfo, AlignedExpr, Comment, Location},
cst::{add_indent, AlignInfo, AlignedExpr, Comment, Location},
error::UroboroSQLFmtError,
util::to_tab_num,
util::{add_single_space, add_space_by_range, tab_size, to_tab_num},
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -68,7 +68,7 @@ impl SepLinesContent {
}

let mut result = String::new();
result.extend(repeat_n('\t', depth - 1));
add_indent(&mut result, depth - 1);

// separatorがある(=最初の行でない)場合はseparatorを描画
if let Some(sep) = &self.sep {
Expand All @@ -91,7 +91,7 @@ impl SepLinesContent {
result.push_str(&comment.render(depth - 1)?);
} else if is_first {
is_first = false;
result.push('\t');
add_single_space(&mut result);
result.push_str(&comment.render(0)?);
} else {
result.push_str(&comment.render(new_depth_with_sep)?);
Expand All @@ -102,16 +102,16 @@ impl SepLinesContent {

if !self.expr.is_lhs_cond() {
// コメントの挿入後に改行をしたので、タブを挿入
result.extend(repeat_n('\t', new_depth_with_sep));
add_indent(&mut result, new_depth_with_sep);
} else {
// 左辺がCASE文の場合は挿入した改行を削除
result.pop();
}
} else {
// コメントが存在しない場合はseparatorの直後にタブを挿入
let tab_num =
new_depth_with_sep - (depth - 1) - (to_tab_num(1.max(self.sep_len())) - 1);
result.extend(repeat_n('\t', tab_num));
let start_col = (depth - 1) * tab_size() + self.sep_len();
let end_col = new_depth_with_sep * tab_size();
add_space_by_range(&mut result, start_col, end_col);
}

let formatted = self.expr.render_align(new_depth_with_sep, align_info)?;
Expand Down
21 changes: 10 additions & 11 deletions crates/uroborosql-fmt/src/cst/body/with.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use itertools::repeat_n;

use crate::{
cst::{ColumnList, Comment, Location, SubExpr},
cst::{add_indent, ColumnList, Comment, Location, SubExpr},
error::UroboroSQLFmtError,
util::add_single_space,
};

/// WITH句における名前付きサブクエリ}
Expand Down Expand Up @@ -95,34 +94,34 @@ impl Cte {
let mut result = String::new();

result.push_str(&self.name);
result.push('\t');
add_single_space(&mut result);

// カラム名の指定がある場合
if let Some(column_list) = &self.column_name {
result.push_str(&column_list.render(depth)?);
result.push('\t');
add_single_space(&mut result);
}

// テーブル名の直後のコメントがある場合
if let Some(comment) = &self.name_trailing_comment {
result.push_str(comment);
result.push('\n');
result.extend(repeat_n('\t', depth));
add_indent(&mut result, depth);
}

result.push_str(&self.as_keyword);
result.push('\t');
add_single_space(&mut result);

// MATERIALIZEDの指定がある場合
if let Some(materialized) = &self.materialized_keyword {
result.push_str(materialized);
result.push('\t');
add_single_space(&mut result);
}

result.push_str(&self.sub_expr.render(depth)?);

if let Some(comment) = &self.trailing_comment {
result.push('\t');
add_single_space(&mut result);
result.push_str(comment);
}

Expand Down Expand Up @@ -197,14 +196,14 @@ impl WithBody {
let mut is_first_line = true;

for (cte, comments) in &self.contents {
result.extend(repeat_n('\t', depth - 1));
add_indent(&mut result, depth - 1);

if is_first_line {
is_first_line = false;
} else {
result.push(',')
}
result.push('\t');
add_single_space(&mut result);

let formatted = cte.render(depth)?;
result.push_str(&formatted);
Expand Down
14 changes: 8 additions & 6 deletions crates/uroborosql-fmt/src/cst/clause.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use itertools::repeat_n;
use tree_sitter::Node;

use crate::{error::UroboroSQLFmtError, util::convert_keyword_case};
use crate::{
error::UroboroSQLFmtError,
util::{add_single_space, convert_keyword_case},
};

use super::{Body, Comment, Location, SqlID};
use super::{add_indent, Body, Comment, Location, SqlID};

// 句に対応した構造体
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -65,7 +67,7 @@ impl Clause {
pub(crate) fn extend_kw_with_tab(&mut self, node: Node, src: &str) {
let loc = Location::new(node.range());
self.loc.append(loc);
self.keyword.push('\t');
add_single_space(&mut self.keyword);
self.keyword.push_str(&convert_keyword_case(
node.utf8_text(src.as_bytes()).unwrap(),
));
Expand Down Expand Up @@ -130,7 +132,7 @@ impl Clause {
// body...
let mut result = String::new();

result.extend(repeat_n('\t', depth));
add_indent(&mut result, depth);
result.push_str(&self.keyword);

if let Some(sql_id) = &self.sql_id {
Expand All @@ -147,7 +149,7 @@ impl Clause {
match &self.body {
// 句と本体を同じ行に render する
Some(Body::SingleLine(single_line)) => {
result.push('\t');
add_single_space(&mut result);
result.push_str(&single_line.render(depth)?);
}
Some(body) => {
Expand Down
Loading

0 comments on commit 53f371a

Please sign in to comment.