Skip to content

Commit

Permalink
Fix block comment alignment (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonadern authored Aug 30, 2024
1 parent 5af6fe3 commit ee7fed8
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 1 deletion.
31 changes: 30 additions & 1 deletion crates/uroborosql-fmt/src/cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ impl Comment {
.min()
.unwrap_or(0);

// すべての行の先頭に`*`がある
let requires_asterisk_alignment =
lines.iter().all(|line| line.trim_start().starts_with("*"));

// 開始キーワードを描画して改行
result.push_str(&format!("{start_keyword}\n"));

Expand All @@ -194,13 +198,34 @@ impl Comment {
if line.is_empty() {
// 空白行の場合そのまま描画
result.push_str(line);
} else if requires_asterisk_alignment {
// アスタリスクで揃える場合

// 先頭の空白文字とアスタリスクをトリミング
let trimmed_line = line.trim_start().trim_start_matches('*');

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

// ```
// /*
// * test
// ^スペースが必要
// ```
result.push(' ');

result.push('*');
// アスタリスクとコメントの間にスペースがなければ挿入
if !trimmed_line.starts_with(' ') {
result.push(' ');
}
result.push_str(trimmed_line);
} else if need_depth * tab_size >= min_start_space {
// タブが少なく、補完が必要な場合

// 必要なスペースの数
let need_space = need_depth * tab_size - min_start_space;

// 補完するスペースの数
// 補完するタブの数
let complement_tab = need_space / tab_size;

// 補完するスペースの数
Expand Down Expand Up @@ -256,6 +281,10 @@ impl Comment {
}

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

if requires_asterisk_alignment {
result.push(' ');
}
result.push_str(end_keyword);
} else {
// 1行コメント
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
comment
*/
select
1
;
/*
comment
comment with space
*/
select
1
;
/*
* will
not
* aligned
*/
select
1
;
-- aligned with asterisk
/*
* a
* b
*/
select
1
;
/*
* a
* b
*/
select
1
;
/*
* a
* b
*/
select
1
;
/*
* a
* b
*/
select
1
;
/*
* a
* b
* c
*/
select
1
;
-- nested
select
*
from
(
/*
* a
* b
*/
select
*
from
foo f
)
;
select
*
from
(
/*
* a
* b
*/
select
*
from
foo f
)
;
select
*
from
(
/*
* a
* b
* c
*/
select
*
from
foo f
)
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
comment
*/
select 1;

/*
comment
comment with space
*/
select 1;

/*
* will
not
* aligned
*/
select 1;

-- aligned with asterisk
/*
* a
* b
*/
select 1;

/*
* a
* b
*/
select 1;

/*
*a
*b
*/
select 1;

/*
* a
* b
*/
select 1;

/*
*a
* b
* c */
select 1;

-- nested
select *
from (
/*
* a
* b
*/
select
*
from
foo f
);

select *
from (
/*
* a
* b
*/
select
*
from
foo f
);

select *
from (
/*
* a
*b
* c
*/
select
*
from
foo f
);

0 comments on commit ee7fed8

Please sign in to comment.