Skip to content

Commit

Permalink
feat: add alignComments option (fix #13)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Aug 25, 2024
1 parent c7922ac commit 8dd9fac
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [omitNumberLeadingZero](./config/omit-number-leading-zero.md)
- [trailingComma](./config/trailing-comma.md)
- [formatComments](./config/format-comments.md)
- [alignComments](./config/align-comments.md)
- [linebreakInPseudoParens](./config/linebreak-in-pseudo-parens.md)
- [declarationOrder](./config/declaration-order.md)
- [singleLineBlockThreshold](./config/single-line-block-threshold.md)
Expand Down
61 changes: 61 additions & 0 deletions docs/src/config/align-comments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# `alignComments`

Control whether to tweak multi-line comments indentation.

Default option value is `true`.

## Example for `false`

[Playground](https://malva-play.vercel.app/?code=H4sIAAAAAAAAA0tUqOZSAAJ9LYXk%2FNzc1LwShZzMvFQFQ7AoEKCIGmEVNQaLaumDqZLUihLdlNTk%2FKLEksz8PCuFvPy8VGuuWgAzUTX%2BaQAAAA%3D%3D&config=H4sIAAAAAAAAA6vmUlBQSszJTM9zzs%2FNTc0rKVayUkhLzClO5aoFAIntfVEcAAAA&syntax=css)

When formatting the 4-space indented CSS into 2-space indented CSS:

```css
a {
/* comment line 1
comment line 2
comment line 3
*/
text-decoration: none;
}
```

will be formatted as:

```css
a {
/* comment line 1
comment line 2
comment line 3
*/
text-decoration: none;
}
```

## Example for `true`

[Playground](https://malva-play.vercel.app/?code=H4sIAAAAAAAAA0tUqOZSAAJ9LYXk%2FNzc1LwShZzMvFQFQ7AoEKCIGmEVNQaLaumDqZLUihLdlNTk%2FKLEksz8PCuFvPy8VGuuWgAzUTX%2BaQAAAA%3D%3D&config=H4sIAAAAAAAAA6vmUlBQSszJTM9zzs%2FNTc0rKVayUigpKk3lqgUAiNln3BsAAAA%3D&syntax=css)

When formatting the 4-space indented CSS into 2-space indented CSS:

```css
a {
/* comment line 1
comment line 2
comment line 3
*/
text-decoration: none;
}
```

will be formatted as:

```css
a {
/* comment line 1
comment line 2
comment line 3
*/
text-decoration: none;
}
```
5 changes: 5 additions & 0 deletions dprint_plugin/deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@
"type": "boolean",
"default": false
},
"alignComments": {
"description": "Control whether to tweak multi-line comments indentation.",
"type": "boolean",
"default": true
},
"linebreakInPseudoParens": {
"description": "Control whether line break should be inserted in pseudo class/element parens or not if current line is too long.",
"type": "boolean",
Expand Down
1 change: 1 addition & 0 deletions dprint_plugin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ pub(crate) fn resolve_config(
trailing_comma: get_value(&mut config, "trailingComma", false, &mut diagnostics),
format_comments: get_nullable_value(&mut config, "formatComments", &mut diagnostics)
.unwrap_or_else(|| get_value(&mut config, "padComments", false, &mut diagnostics)),
align_comments: get_value(&mut config, "alignComments", true, &mut diagnostics),
linebreak_in_pseudo_parens: get_value(
&mut config,
"linebreakInPseudoParens",
Expand Down
4 changes: 4 additions & 0 deletions malva/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ pub struct LanguageOptions {
/// See [`formatComments`](https://malva.netlify.app/config/format-comments.html)
pub format_comments: bool,

#[cfg_attr(feature = "config_serde", serde(alias = "alignComments"))]
pub align_comments: bool,

#[cfg_attr(
feature = "config_serde",
serde(alias = "linebreakInPseudoParens", alias = "lineBreakInPseudoParens")
Expand Down Expand Up @@ -266,6 +269,7 @@ impl Default for LanguageOptions {
omit_number_leading_zero: false,
trailing_comma: false,
format_comments: false,
align_comments: true,
linebreak_in_pseudo_parens: false,
declaration_order: None,
single_line_block_threshold: None,
Expand Down
29 changes: 29 additions & 0 deletions malva/src/doc_gen/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub(crate) fn format_comment<'s>(comment: &Comment<'s>, ctx: &Ctx<'_, 's>) -> Do
docs.extend(
lines.map(|line| Doc::hard_line().append(Doc::text(line.trim_start()))),
);
} else if ctx.options.align_comments {
docs.append(&mut reflow(comment, ctx));
} else {
docs.extend(itertools::intersperse(
lines.map(Doc::text),
Expand Down Expand Up @@ -76,3 +78,30 @@ pub(crate) fn format_comment<'s>(comment: &Comment<'s>, ctx: &Ctx<'_, 's>) -> Do
}
}
}

pub(super) fn reflow<'s>(comment: &Comment<'s>, ctx: &Ctx<'_, 's>) -> Vec<Doc<'s>> {
let col = ctx
.line_bounds
.get_line_col(comment.span.start)
.1
.saturating_sub(1);
let mut docs = Vec::with_capacity(2);
let mut lines = comment.content.split('\n').enumerate().peekable();
while let Some((i, line)) = lines.next() {
let s = line.strip_suffix('\r').unwrap_or(line);
let s = if s.starts_with([' ', '\t']) && i > 0 {
s.get(col..).unwrap_or(s)
} else {
s
};
if i > 0 {
if s.trim().is_empty() && lines.peek().is_some() {
docs.push(Doc::empty_line());
} else {
docs.push(Doc::hard_line());
}
}
docs.push(Doc::text(s));
}
docs
}
2 changes: 1 addition & 1 deletion malva/src/line_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl LineBounds {

pub(crate) fn get_line_col(&self, pos: usize) -> (usize, usize) {
let line = self.get_line_at(pos);
(line, pos - self.0[line - 1])
(line, pos - self.0[line.saturating_sub(1)])
}

fn get_line_at(&self, pos: usize) -> usize {
Expand Down
8 changes: 8 additions & 0 deletions malva/tests/fmt/css/align-comments/disabled.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*cfg alignComments = false */
a {
/* comment line 1
comment line 2
comment line 3
*/
text-decoration: none;
}
11 changes: 11 additions & 0 deletions malva/tests/fmt/css/align-comments/disabled.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: malva/tests/fmt.rs
---
/*cfg alignComments = false */
a {
/* comment line 1
comment line 2
comment line 3
*/
text-decoration: none;
}
7 changes: 7 additions & 0 deletions malva/tests/fmt/css/align-comments/enabled.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
a {
/* comment line 1
comment line 2
comment line 3
*/
text-decoration: none;
}
10 changes: 10 additions & 0 deletions malva/tests/fmt/css/align-comments/enabled.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: malva/tests/fmt.rs
---
a {
/* comment line 1
comment line 2
comment line 3
*/
text-decoration: none;
}

0 comments on commit 8dd9fac

Please sign in to comment.