mirror of
https://github.com/RGBCube/alejandra
synced 2025-07-30 12:07:46 +00:00
feat: handle multiline comments
This commit is contained in:
parent
04ac8d46b1
commit
00ce27baac
21 changed files with 1041 additions and 305 deletions
|
@ -92,8 +92,6 @@ Let's get Alejandra on our systems:
|
|||
|
||||
Yet there are a few improvements to implement like:
|
||||
- Multiline strings indentation is missing `'' ... ''`.
|
||||
- Multiline comments indentation is bugged `/* ... */`.
|
||||
- And many more as community feedback drives.
|
||||
|
||||
Style is negotiable at this moment.
|
||||
|
||||
|
|
|
@ -71,11 +71,31 @@ fn build_step(
|
|||
|
||||
match step {
|
||||
crate::builder::Step::Comment(text) => {
|
||||
let mut lines: Vec<String> =
|
||||
text.lines().map(|line| line.trim_end().to_string()).collect();
|
||||
|
||||
lines = lines
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, line)| {
|
||||
if index == 0 {
|
||||
line.to_string()
|
||||
} else {
|
||||
format!(
|
||||
"{0:<1$} {2}",
|
||||
"",
|
||||
2 * build_ctx.indentation,
|
||||
line,
|
||||
)
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
add_token(
|
||||
builder,
|
||||
build_ctx,
|
||||
rnix::SyntaxKind::TOKEN_COMMENT,
|
||||
text,
|
||||
&lines.join("\n"),
|
||||
);
|
||||
}
|
||||
crate::builder::Step::Dedent => {
|
||||
|
|
103
src/children.rs
103
src/children.rs
|
@ -132,21 +132,108 @@ fn dedent_comment(pos: &crate::position::Position, text: &str) -> String {
|
|||
if text.starts_with("#") {
|
||||
text.to_string()
|
||||
} else {
|
||||
let text = text[2..text.len() - 2]
|
||||
let mut lines: Vec<String> = text[2..text.len() - 2]
|
||||
.lines()
|
||||
.map(|line| line.to_string())
|
||||
.collect();
|
||||
|
||||
// If all lines are whitespace just return a compact comment
|
||||
if lines.iter().all(|line| line.trim().len() == 0) {
|
||||
return "/**/".to_string();
|
||||
}
|
||||
|
||||
// println!("{:?}", lines);
|
||||
// println!("0\n{0:<1$}/*{2}*/\n", "", pos.column, lines.join("\n"));
|
||||
|
||||
if lines.len() == 1 {
|
||||
lines.insert(0, "".to_string());
|
||||
lines[1] = format!("{0:<1$}{2}", "", pos.column + 2, lines[1]);
|
||||
} else if lines[0].trim().len() == 0 {
|
||||
lines[0] = "".to_string();
|
||||
} else {
|
||||
lines.insert(0, format!("{0:<1$}", "", pos.column + 1));
|
||||
lines[1] = format!("{0:<1$}{2}", "", pos.column + 2, lines[1]);
|
||||
}
|
||||
|
||||
// println!("{:?}", lines);
|
||||
// println!("1\n{0:<1$}/*{2}*/\n", "", pos.column, lines.join("\n"));
|
||||
|
||||
let len = lines.len();
|
||||
if len == 2 {
|
||||
lines.push(format!("{0:<1$}", "", pos.column + 1));
|
||||
} else if lines[len - 1].trim().len() == 0 {
|
||||
lines[len - 1] = format!("{0:<1$}", "", pos.column + 1)
|
||||
} else {
|
||||
// lines[len - 1] =
|
||||
// format!("{0:<1$}{2}", "", pos.column + 2, lines[len - 1]);
|
||||
lines.push(format!("{0:<1$}", "", pos.column + 1));
|
||||
}
|
||||
|
||||
// println!("{:?}", lines);
|
||||
// println!("2\n{0:<1$}/*{2}*/\n", "", pos.column, lines.join("\n"));
|
||||
|
||||
let mut indentation: usize = usize::MAX;
|
||||
for (index, line) in lines.iter().enumerate() {
|
||||
if index != 0 && index + 1 != lines.len() {
|
||||
let line = line.trim_end();
|
||||
|
||||
if line.len() > 0 {
|
||||
indentation = usize::min(
|
||||
indentation,
|
||||
line.len() - line.trim_start().len(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
if indentation == usize::MAX {
|
||||
indentation = pos.column;
|
||||
};
|
||||
|
||||
lines = lines
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, line)| {
|
||||
if index > 0 {
|
||||
line.chars()
|
||||
.skip(if pos.column >= 1 { pos.column - 1 } else { 0 })
|
||||
.collect::<String>()
|
||||
if index == 0 || index + 1 == lines.len() {
|
||||
line.to_string()
|
||||
} else {
|
||||
if pos.column >= indentation {
|
||||
format!(
|
||||
"{0:<1$}{2}",
|
||||
"",
|
||||
pos.column - indentation + 1,
|
||||
line,
|
||||
)
|
||||
} else if line.len() >= indentation - pos.column {
|
||||
line[indentation - pos.column - 1..line.len()]
|
||||
.to_string()
|
||||
} else {
|
||||
line.to_string()
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
// println!("{:?}", lines);
|
||||
// println!("3\n{0:<1$}/*{2}*/\n", "", pos.column, lines.join("\n"));
|
||||
// println!("indentation={} pos.column{}", indentation, pos.column);
|
||||
|
||||
lines = lines
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, line)| {
|
||||
if index == 0 {
|
||||
line.to_string()
|
||||
} else if line.len() >= pos.column + 1 {
|
||||
line[pos.column + 1..line.len()].to_string()
|
||||
} else {
|
||||
line.to_string()
|
||||
}
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n");
|
||||
.collect();
|
||||
|
||||
format!("/*{}*/", text)
|
||||
// println!("{:?}", lines);
|
||||
// println!("4\n{0:<1$}/*{2}*/\n", "", pos.column, lines.join("\n"));
|
||||
|
||||
format!("/*{}*/", lines.join("\n"))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,21 +51,30 @@ pub fn rule(
|
|||
| rnix::SyntaxKind::TOKEN_CURLY_B_OPEN = prev_kind
|
||||
{
|
||||
steps.push_back(crate::builder::Step::Whitespace);
|
||||
steps.push_back(crate::builder::Step::Indent);
|
||||
}
|
||||
|
||||
if let rnix::SyntaxKind::TOKEN_COMMENT
|
||||
| rnix::SyntaxKind::TOKEN_ELLIPSIS
|
||||
| rnix::SyntaxKind::NODE_PAT_ENTRY = prev_kind
|
||||
{
|
||||
steps.push_back(crate::builder::Step::Indent);
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
steps.push_back(crate::builder::Step::Whitespace);
|
||||
steps.push_back(crate::builder::Step::Whitespace);
|
||||
}
|
||||
|
||||
children.drain_comment(|text| {
|
||||
steps.push_back(crate::builder::Step::Comment(text));
|
||||
});
|
||||
|
||||
if let rnix::SyntaxKind::TOKEN_COMMA
|
||||
| rnix::SyntaxKind::TOKEN_CURLY_B_OPEN
|
||||
| rnix::SyntaxKind::TOKEN_COMMENT
|
||||
| rnix::SyntaxKind::TOKEN_ELLIPSIS
|
||||
| rnix::SyntaxKind::NODE_PAT_ENTRY = prev_kind
|
||||
{
|
||||
steps.push_back(crate::builder::Step::Dedent);
|
||||
}
|
||||
}
|
||||
// item
|
||||
rnix::SyntaxKind::TOKEN_ELLIPSIS
|
||||
|
|
|
@ -2,13 +2,21 @@
|
|||
( a b )
|
||||
(
|
||||
a
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
c
|
||||
)
|
||||
(
|
||||
/*a*/
|
||||
/*
|
||||
a
|
||||
*/
|
||||
b
|
||||
/*c*/
|
||||
/*
|
||||
c
|
||||
*/
|
||||
d
|
||||
/*e*/
|
||||
/*
|
||||
e
|
||||
*/
|
||||
)
|
||||
|
|
|
@ -2,20 +2,28 @@
|
|||
( assert b; c )
|
||||
(
|
||||
assert b;
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
c
|
||||
)
|
||||
(
|
||||
assert
|
||||
/*a*/
|
||||
/*
|
||||
a
|
||||
*/
|
||||
b;
|
||||
c
|
||||
)
|
||||
(
|
||||
assert
|
||||
/*a*/
|
||||
/*
|
||||
a
|
||||
*/
|
||||
b;
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
c
|
||||
)
|
||||
(
|
||||
|
|
|
@ -1,13 +1,19 @@
|
|||
[
|
||||
{ }
|
||||
{
|
||||
/*a*/
|
||||
/*
|
||||
a
|
||||
*/
|
||||
}
|
||||
{ a = 1; }
|
||||
{
|
||||
/*a*/
|
||||
/*
|
||||
a
|
||||
*/
|
||||
b = 1;
|
||||
/*c*/
|
||||
/*
|
||||
c
|
||||
*/
|
||||
}
|
||||
{
|
||||
a = {
|
||||
|
|
44
tests/cases/comments/in
Normal file
44
tests/cases/comments/in
Normal file
|
@ -0,0 +1,44 @@
|
|||
[
|
||||
/*
|
||||
*/
|
||||
/*
|
||||
*/
|
||||
|
||||
/*
|
||||
*/
|
||||
|
||||
/*
|
||||
*/
|
||||
|
||||
/*
|
||||
*/
|
||||
|
||||
/*
|
||||
*/
|
||||
|
||||
/*@*/
|
||||
|
||||
/**
|
||||
@
|
||||
**/
|
||||
|
||||
/*@
|
||||
@
|
||||
@*/
|
||||
|
||||
/*@
|
||||
@
|
||||
@*/
|
||||
|
||||
/*@
|
||||
@
|
||||
@*/
|
||||
|
||||
/*@
|
||||
@
|
||||
@*/
|
||||
|
||||
/* test
|
||||
* test
|
||||
*/
|
||||
]
|
40
tests/cases/comments/out
Normal file
40
tests/cases/comments/out
Normal file
|
@ -0,0 +1,40 @@
|
|||
[
|
||||
/**/
|
||||
/**/
|
||||
/**/
|
||||
/**/
|
||||
/**/
|
||||
/**/
|
||||
/*
|
||||
@
|
||||
*/
|
||||
/*
|
||||
*
|
||||
@
|
||||
*
|
||||
*/
|
||||
/*
|
||||
@
|
||||
@
|
||||
@
|
||||
*/
|
||||
/*
|
||||
@
|
||||
@
|
||||
@
|
||||
*/
|
||||
/*
|
||||
@
|
||||
@
|
||||
@
|
||||
*/
|
||||
/*
|
||||
@
|
||||
@
|
||||
@
|
||||
*/
|
||||
/*
|
||||
test
|
||||
* test
|
||||
*/
|
||||
]
|
|
@ -1,10 +1,16 @@
|
|||
a
|
||||
.${
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
c
|
||||
.${
|
||||
/*d*/
|
||||
/*
|
||||
d
|
||||
*/
|
||||
e.${ f }
|
||||
}
|
||||
/*g*/
|
||||
/*
|
||||
g
|
||||
*/
|
||||
}
|
||||
|
|
|
@ -11,51 +11,75 @@
|
|||
inherit
|
||||
b
|
||||
d
|
||||
/*e*/
|
||||
/*
|
||||
e
|
||||
*/
|
||||
;
|
||||
}
|
||||
{
|
||||
inherit
|
||||
b
|
||||
/*c*/
|
||||
/*
|
||||
c
|
||||
*/
|
||||
d;
|
||||
}
|
||||
{
|
||||
inherit
|
||||
b
|
||||
/*c*/
|
||||
/*
|
||||
c
|
||||
*/
|
||||
d
|
||||
/*e*/
|
||||
/*
|
||||
e
|
||||
*/
|
||||
;
|
||||
}
|
||||
{
|
||||
inherit
|
||||
/*a*/
|
||||
/*
|
||||
a
|
||||
*/
|
||||
b
|
||||
d;
|
||||
}
|
||||
{
|
||||
inherit
|
||||
/*a*/
|
||||
/*
|
||||
a
|
||||
*/
|
||||
b
|
||||
d
|
||||
/*e*/
|
||||
/*
|
||||
e
|
||||
*/
|
||||
;
|
||||
}
|
||||
{
|
||||
inherit
|
||||
/*a*/
|
||||
/*
|
||||
a
|
||||
*/
|
||||
b
|
||||
/*c*/
|
||||
/*
|
||||
c
|
||||
*/
|
||||
d;
|
||||
}
|
||||
{
|
||||
inherit
|
||||
/*a*/
|
||||
/*
|
||||
a
|
||||
*/
|
||||
b
|
||||
/*c*/
|
||||
/*
|
||||
c
|
||||
*/
|
||||
d
|
||||
/*e*/
|
||||
/*
|
||||
e
|
||||
*/
|
||||
;
|
||||
}
|
||||
]
|
||||
|
|
|
@ -4,14 +4,18 @@
|
|||
inherit
|
||||
(
|
||||
b
|
||||
/*c*/
|
||||
/*
|
||||
c
|
||||
*/
|
||||
)
|
||||
d;
|
||||
}
|
||||
{
|
||||
inherit
|
||||
(
|
||||
/*a*/
|
||||
/*
|
||||
a
|
||||
*/
|
||||
b
|
||||
)
|
||||
d;
|
||||
|
@ -19,9 +23,13 @@
|
|||
{
|
||||
inherit
|
||||
(
|
||||
/*a*/
|
||||
/*
|
||||
a
|
||||
*/
|
||||
b
|
||||
/*c*/
|
||||
/*
|
||||
c
|
||||
*/
|
||||
)
|
||||
d;
|
||||
}
|
||||
|
|
|
@ -3,49 +3,73 @@
|
|||
b = {
|
||||
a =
|
||||
1
|
||||
/*d*/
|
||||
/*
|
||||
d
|
||||
*/
|
||||
;
|
||||
};
|
||||
c = {
|
||||
a =
|
||||
/*c*/
|
||||
/*
|
||||
c
|
||||
*/
|
||||
1;
|
||||
};
|
||||
d = {
|
||||
a =
|
||||
/*c*/
|
||||
/*
|
||||
c
|
||||
*/
|
||||
1
|
||||
/*d*/
|
||||
/*
|
||||
d
|
||||
*/
|
||||
;
|
||||
};
|
||||
e = {
|
||||
a
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
=
|
||||
1;
|
||||
};
|
||||
f = {
|
||||
a
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
=
|
||||
1
|
||||
/*d*/
|
||||
/*
|
||||
d
|
||||
*/
|
||||
;
|
||||
};
|
||||
h = {
|
||||
a
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
=
|
||||
/*c*/
|
||||
/*
|
||||
c
|
||||
*/
|
||||
1;
|
||||
};
|
||||
i = {
|
||||
a
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
=
|
||||
/*c*/
|
||||
/*
|
||||
c
|
||||
*/
|
||||
1
|
||||
/*d*/
|
||||
/*
|
||||
d
|
||||
*/
|
||||
;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -2,20 +2,28 @@
|
|||
( a: d )
|
||||
(
|
||||
a:
|
||||
/*c*/
|
||||
/*
|
||||
c
|
||||
*/
|
||||
d
|
||||
)
|
||||
(
|
||||
a
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
:
|
||||
d
|
||||
)
|
||||
(
|
||||
a
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
:
|
||||
/*c*/
|
||||
/*
|
||||
c
|
||||
*/
|
||||
d
|
||||
)
|
||||
(
|
||||
|
|
|
@ -6,54 +6,78 @@ let
|
|||
let
|
||||
c = 1;
|
||||
in
|
||||
/*e*/
|
||||
/*
|
||||
e
|
||||
*/
|
||||
f;
|
||||
/**/
|
||||
a =
|
||||
let
|
||||
c = 1;
|
||||
/*d*/
|
||||
/*
|
||||
d
|
||||
*/
|
||||
in
|
||||
f;
|
||||
/**/
|
||||
a =
|
||||
let
|
||||
c = 1;
|
||||
/*d*/
|
||||
/*
|
||||
d
|
||||
*/
|
||||
in
|
||||
/*e*/
|
||||
/*
|
||||
e
|
||||
*/
|
||||
f;
|
||||
/**/
|
||||
a =
|
||||
let
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
c = 1;
|
||||
in
|
||||
f;
|
||||
/**/
|
||||
a =
|
||||
let
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
c = 1;
|
||||
in
|
||||
/*e*/
|
||||
/*
|
||||
e
|
||||
*/
|
||||
f;
|
||||
/**/
|
||||
a =
|
||||
let
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
c = 1;
|
||||
/*d*/
|
||||
/*
|
||||
d
|
||||
*/
|
||||
in
|
||||
f;
|
||||
/**/
|
||||
a =
|
||||
let
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
c = 1;
|
||||
/*d*/
|
||||
/*
|
||||
d
|
||||
*/
|
||||
in
|
||||
/*e*/
|
||||
/*
|
||||
e
|
||||
*/
|
||||
f;
|
||||
/**/
|
||||
in
|
||||
|
|
|
@ -1,8 +1,14 @@
|
|||
(
|
||||
/*a*/
|
||||
/*
|
||||
a
|
||||
*/
|
||||
(
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
( c )
|
||||
)
|
||||
/*d*/
|
||||
/*
|
||||
d
|
||||
*/
|
||||
)
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,14 +1,10 @@
|
|||
# a
|
||||
/* test
|
||||
* test
|
||||
*/
|
||||
/* Some functions f
|
||||
name attribute.
|
||||
*/
|
||||
/* Add to or over
|
||||
derivation.
|
||||
|
||||
# a
|
||||
|
||||
"test"
|
||||
|
||||
# b
|
||||
/* c */
|
||||
|
||||
/* c */
|
||||
# c
|
||||
Example:
|
||||
addMetaAttrs {des
|
||||
*/
|
||||
1
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
# a
|
||||
/* test
|
||||
* test
|
||||
/*
|
||||
Some functions f
|
||||
name attribute.
|
||||
*/
|
||||
# a
|
||||
"test"
|
||||
# b
|
||||
/* c */
|
||||
/* c */
|
||||
# c
|
||||
/*
|
||||
Add to or over
|
||||
derivation.
|
||||
|
||||
Example:
|
||||
addMetaAttrs {des
|
||||
*/
|
||||
1
|
||||
|
|
|
@ -1,8 +1,14 @@
|
|||
"${
|
||||
/*a*/
|
||||
/*
|
||||
a
|
||||
*/
|
||||
"${
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
"${ c }"
|
||||
}"
|
||||
/*d*/
|
||||
/*
|
||||
d
|
||||
*/
|
||||
}"
|
||||
|
|
|
@ -2,20 +2,28 @@
|
|||
( with b; c )
|
||||
(
|
||||
with b;
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
c
|
||||
)
|
||||
(
|
||||
with
|
||||
/*a*/
|
||||
/*
|
||||
a
|
||||
*/
|
||||
b;
|
||||
c
|
||||
)
|
||||
(
|
||||
with
|
||||
/*a*/
|
||||
/*
|
||||
a
|
||||
*/
|
||||
b;
|
||||
/*b*/
|
||||
/*
|
||||
b
|
||||
*/
|
||||
c
|
||||
)
|
||||
(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue