1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-08-01 21:17:45 +00:00

feat: handle multiline comments

This commit is contained in:
Kevin Amado 2022-01-26 21:54:27 -05:00
parent 04ac8d46b1
commit 00ce27baac
No known key found for this signature in database
GPG key ID: FFF341057F503148
21 changed files with 1041 additions and 305 deletions

View file

@ -92,8 +92,6 @@ Let's get Alejandra on our systems:
Yet there are a few improvements to implement like: Yet there are a few improvements to implement like:
- Multiline strings indentation is missing `'' ... ''`. - Multiline strings indentation is missing `'' ... ''`.
- Multiline comments indentation is bugged `/* ... */`.
- And many more as community feedback drives.
Style is negotiable at this moment. Style is negotiable at this moment.

View file

@ -71,11 +71,31 @@ fn build_step(
match step { match step {
crate::builder::Step::Comment(text) => { 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( add_token(
builder, builder,
build_ctx, build_ctx,
rnix::SyntaxKind::TOKEN_COMMENT, rnix::SyntaxKind::TOKEN_COMMENT,
text, &lines.join("\n"),
); );
} }
crate::builder::Step::Dedent => { crate::builder::Step::Dedent => {

View file

@ -132,21 +132,108 @@ fn dedent_comment(pos: &crate::position::Position, text: &str) -> String {
if text.starts_with("#") { if text.starts_with("#") {
text.to_string() text.to_string()
} else { } else {
let text = text[2..text.len() - 2] let mut lines: Vec<String> = text[2..text.len() - 2]
.lines() .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() .enumerate()
.map(|(index, line)| { .map(|(index, line)| {
if index > 0 { if index == 0 || index + 1 == lines.len() {
line.chars() line.to_string()
.skip(if pos.column >= 1 { pos.column - 1 } else { 0 }) } else {
.collect::<String>() 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 { } else {
line.to_string() line.to_string()
} }
}) })
.collect::<Vec<String>>() .collect();
.join("\n");
format!("/*{}*/", text) // println!("{:?}", lines);
// println!("4\n{0:<1$}/*{2}*/\n", "", pos.column, lines.join("\n"));
format!("/*{}*/", lines.join("\n"))
} }
} }

View file

@ -51,21 +51,30 @@ pub fn rule(
| rnix::SyntaxKind::TOKEN_CURLY_B_OPEN = prev_kind | rnix::SyntaxKind::TOKEN_CURLY_B_OPEN = prev_kind
{ {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::Indent);
} }
if let rnix::SyntaxKind::TOKEN_COMMENT if let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_ELLIPSIS | rnix::SyntaxKind::TOKEN_ELLIPSIS
| rnix::SyntaxKind::NODE_PAT_ENTRY = prev_kind | 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::NewLine);
steps.push_back(crate::builder::Step::Pad); 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| { children.drain_comment(|text| {
steps.push_back(crate::builder::Step::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 // item
rnix::SyntaxKind::TOKEN_ELLIPSIS rnix::SyntaxKind::TOKEN_ELLIPSIS

View file

@ -2,13 +2,21 @@
( a b ) ( a b )
( (
a a
/*b*/ /*
b
*/
c c
) )
( (
/*a*/ /*
a
*/
b b
/*c*/ /*
c
*/
d d
/*e*/ /*
e
*/
) )

View file

@ -2,20 +2,28 @@
( assert b; c ) ( assert b; c )
( (
assert b; assert b;
/*b*/ /*
b
*/
c c
) )
( (
assert assert
/*a*/ /*
a
*/
b; b;
c c
) )
( (
assert assert
/*a*/ /*
a
*/
b; b;
/*b*/ /*
b
*/
c c
) )
( (

View file

@ -1,13 +1,19 @@
[ [
{ } { }
{ {
/*a*/ /*
a
*/
} }
{ a = 1; } { a = 1; }
{ {
/*a*/ /*
a
*/
b = 1; b = 1;
/*c*/ /*
c
*/
} }
{ {
a = { a = {

44
tests/cases/comments/in Normal file
View file

@ -0,0 +1,44 @@
[
/*
*/
/*
*/
/*
*/
/*
*/
/*
*/
/*
*/
/*@*/
/**
@
**/
/*@
@
@*/
/*@
@
@*/
/*@
@
@*/
/*@
@
@*/
/* test
* test
*/
]

40
tests/cases/comments/out Normal file
View file

@ -0,0 +1,40 @@
[
/**/
/**/
/**/
/**/
/**/
/**/
/*
@
*/
/*
*
@
*
*/
/*
@
@
@
*/
/*
@
@
@
*/
/*
@
@
@
*/
/*
@
@
@
*/
/*
test
* test
*/
]

View file

@ -1,10 +1,16 @@
a a
.${ .${
/*b*/ /*
b
*/
c c
.${ .${
/*d*/ /*
d
*/
e.${ f } e.${ f }
} }
/*g*/ /*
g
*/
} }

View file

@ -11,51 +11,75 @@
inherit inherit
b b
d d
/*e*/ /*
e
*/
; ;
} }
{ {
inherit inherit
b b
/*c*/ /*
c
*/
d; d;
} }
{ {
inherit inherit
b b
/*c*/ /*
c
*/
d d
/*e*/ /*
e
*/
; ;
} }
{ {
inherit inherit
/*a*/ /*
a
*/
b b
d; d;
} }
{ {
inherit inherit
/*a*/ /*
a
*/
b b
d d
/*e*/ /*
e
*/
; ;
} }
{ {
inherit inherit
/*a*/ /*
a
*/
b b
/*c*/ /*
c
*/
d; d;
} }
{ {
inherit inherit
/*a*/ /*
a
*/
b b
/*c*/ /*
c
*/
d d
/*e*/ /*
e
*/
; ;
} }
] ]

View file

@ -4,14 +4,18 @@
inherit inherit
( (
b b
/*c*/ /*
c
*/
) )
d; d;
} }
{ {
inherit inherit
( (
/*a*/ /*
a
*/
b b
) )
d; d;
@ -19,9 +23,13 @@
{ {
inherit inherit
( (
/*a*/ /*
a
*/
b b
/*c*/ /*
c
*/
) )
d; d;
} }

View file

@ -3,49 +3,73 @@
b = { b = {
a = a =
1 1
/*d*/ /*
d
*/
; ;
}; };
c = { c = {
a = a =
/*c*/ /*
c
*/
1; 1;
}; };
d = { d = {
a = a =
/*c*/ /*
c
*/
1 1
/*d*/ /*
d
*/
; ;
}; };
e = { e = {
a a
/*b*/ /*
b
*/
= =
1; 1;
}; };
f = { f = {
a a
/*b*/ /*
b
*/
= =
1 1
/*d*/ /*
d
*/
; ;
}; };
h = { h = {
a a
/*b*/ /*
b
*/
= =
/*c*/ /*
c
*/
1; 1;
}; };
i = { i = {
a a
/*b*/ /*
b
*/
= =
/*c*/ /*
c
*/
1 1
/*d*/ /*
d
*/
; ;
}; };
} }

View file

@ -2,20 +2,28 @@
( a: d ) ( a: d )
( (
a: a:
/*c*/ /*
c
*/
d d
) )
( (
a a
/*b*/ /*
b
*/
: :
d d
) )
( (
a a
/*b*/ /*
b
*/
: :
/*c*/ /*
c
*/
d d
) )
( (

View file

@ -6,54 +6,78 @@ let
let let
c = 1; c = 1;
in in
/*e*/ /*
e
*/
f; f;
/**/ /**/
a = a =
let let
c = 1; c = 1;
/*d*/ /*
d
*/
in in
f; f;
/**/ /**/
a = a =
let let
c = 1; c = 1;
/*d*/ /*
d
*/
in in
/*e*/ /*
e
*/
f; f;
/**/ /**/
a = a =
let let
/*b*/ /*
b
*/
c = 1; c = 1;
in in
f; f;
/**/ /**/
a = a =
let let
/*b*/ /*
b
*/
c = 1; c = 1;
in in
/*e*/ /*
e
*/
f; f;
/**/ /**/
a = a =
let let
/*b*/ /*
b
*/
c = 1; c = 1;
/*d*/ /*
d
*/
in in
f; f;
/**/ /**/
a = a =
let let
/*b*/ /*
b
*/
c = 1; c = 1;
/*d*/ /*
d
*/
in in
/*e*/ /*
e
*/
f; f;
/**/ /**/
in in

View file

@ -1,8 +1,14 @@
( (
/*a*/ /*
a
*/
( (
/*b*/ /*
b
*/
( c ) ( c )
) )
/*d*/ /*
d
*/
) )

File diff suppressed because it is too large Load diff

View file

@ -1,14 +1,10 @@
# a /* Some functions f
/* test name attribute.
* test */
*/ /* Add to or over
derivation.
# a Example:
addMetaAttrs {des
"test" */
1
# b
/* c */
/* c */
# c

View file

@ -1,10 +1,12 @@
# a /*
/* test Some functions f
* test name attribute.
*/ */
# a /*
"test" Add to or over
# b derivation.
/* c */
/* c */ Example:
# c addMetaAttrs {des
*/
1

View file

@ -1,8 +1,14 @@
"${ "${
/*a*/ /*
a
*/
"${ "${
/*b*/ /*
b
*/
"${ c }" "${ c }"
}" }"
/*d*/ /*
d
*/
}" }"

View file

@ -2,20 +2,28 @@
( with b; c ) ( with b; c )
( (
with b; with b;
/*b*/ /*
b
*/
c c
) )
( (
with with
/*a*/ /*
a
*/
b; b;
c c
) )
( (
with with
/*a*/ /*
a
*/
b; b;
/*b*/ /*
b
*/
c c
) )
( (