mirror of
https://github.com/RGBCube/alejandra
synced 2025-07-30 12:07:46 +00:00
feat: inline comments on inherit
This commit is contained in:
parent
62dd8ec60a
commit
d839c3e5b2
5 changed files with 117 additions and 28 deletions
10
CHANGELOG.md
10
CHANGELOG.md
|
@ -36,6 +36,16 @@ Types of changes
|
|||
+ assert stdenv.isDarwin -> libXaw != null; # fails to link otherwise
|
||||
+
|
||||
```
|
||||
- Inline comments support for `if-then-else` expressions:
|
||||
```diff
|
||||
if y ? ${a}
|
||||
- then v x.${a} y.${a}
|
||||
- # both have attr, use merge func
|
||||
- else x.${a}
|
||||
- # only x has attr
|
||||
+ then v x.${a} y.${a} # both have attr, use merge func
|
||||
+ else x.${a} # only x has attr
|
||||
```
|
||||
|
||||
### Changed
|
||||
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
use std::collections::LinkedList;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum Trivia {
|
||||
Comment(String),
|
||||
Newlines(usize),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Child {
|
||||
pub element: rnix::SyntaxElement,
|
||||
|
||||
|
@ -28,16 +30,26 @@ pub(crate) fn new(
|
|||
let mut inline_comment = None;
|
||||
let mut trivialities = LinkedList::new();
|
||||
|
||||
let mut skip_next_newline = false;
|
||||
children.drain_trivia(|element| match element {
|
||||
crate::children::Trivia::Comment(text) => {
|
||||
if trivialities.is_empty() && text.starts_with('#') {
|
||||
if inline_comment.is_none()
|
||||
&& trivialities.is_empty()
|
||||
&& text.starts_with('#')
|
||||
{
|
||||
inline_comment = Some(text);
|
||||
skip_next_newline = true;
|
||||
} else {
|
||||
trivialities.push_back(Trivia::Comment(text));
|
||||
}
|
||||
}
|
||||
crate::children::Trivia::Whitespace(text) => {
|
||||
let newlines = crate::utils::count_newlines(&text);
|
||||
let mut newlines = crate::utils::count_newlines(&text);
|
||||
|
||||
if skip_next_newline && newlines > 0 {
|
||||
newlines -= 1;
|
||||
skip_next_newline = false;
|
||||
}
|
||||
|
||||
if newlines > 0 {
|
||||
trivialities.push_back(Trivia::Newlines(newlines))
|
||||
|
|
|
@ -4,45 +4,83 @@ pub(crate) fn rule(
|
|||
) -> std::collections::LinkedList<crate::builder::Step> {
|
||||
let mut steps = std::collections::LinkedList::new();
|
||||
|
||||
let mut children = crate::children::Children::new(build_ctx, node);
|
||||
let children: Vec<crate::children2::Child> =
|
||||
crate::children2::new(build_ctx, node).collect();
|
||||
|
||||
let vertical = children.has_comments()
|
||||
|| children.has_newlines()
|
||||
|| build_ctx.vertical;
|
||||
let vertical = build_ctx.vertical
|
||||
|| children
|
||||
.iter()
|
||||
.any(|child| child.has_inline_comment || child.has_trivialities);
|
||||
|
||||
let children_count = children.len() - 1;
|
||||
let mut children = children.into_iter();
|
||||
|
||||
// inherit
|
||||
let child = children.get_next().unwrap();
|
||||
steps.push_back(crate::builder::Step::Format(child));
|
||||
let child = children.next().unwrap();
|
||||
steps.push_back(crate::builder::Step::Format(child.element));
|
||||
if vertical {
|
||||
steps.push_back(crate::builder::Step::Indent);
|
||||
}
|
||||
|
||||
loop {
|
||||
// /**/
|
||||
children.drain_trivia(|element| match element {
|
||||
crate::children::Trivia::Comment(text) => {
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
steps.push_back(crate::builder::Step::Comment(text));
|
||||
}
|
||||
crate::children::Trivia::Whitespace(_) => {}
|
||||
});
|
||||
if let Some(text) = child.inline_comment {
|
||||
steps.push_back(crate::builder::Step::Whitespace);
|
||||
steps.push_back(crate::builder::Step::Comment(text));
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
} else if vertical {
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
}
|
||||
|
||||
if let Some(child) = children.get_next() {
|
||||
// expr
|
||||
if vertical {
|
||||
for trivia in child.trivialities {
|
||||
match trivia {
|
||||
crate::children2::Trivia::Comment(text) => {
|
||||
steps.push_back(crate::builder::Step::Comment(text));
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
steps.push_back(crate::builder::Step::FormatWider(child));
|
||||
} else {
|
||||
if let rnix::SyntaxKind::TOKEN_SEMICOLON = child.kind() {
|
||||
} else {
|
||||
steps.push_back(crate::builder::Step::Whitespace);
|
||||
}
|
||||
crate::children2::Trivia::Newlines(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
for (index, child) in children.into_iter().enumerate() {
|
||||
let not_last_child = index + 1 < children_count;
|
||||
|
||||
if vertical {
|
||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||
|
||||
if let Some(text) = child.inline_comment {
|
||||
steps.push_back(crate::builder::Step::Whitespace);
|
||||
steps.push_back(crate::builder::Step::Comment(text));
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
} else if (not_last_child && !child.has_trivialities)
|
||||
|| matches!(
|
||||
child.trivialities.front(),
|
||||
Some(crate::children2::Trivia::Comment(_))
|
||||
)
|
||||
{
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
}
|
||||
|
||||
for trivia in child.trivialities {
|
||||
match trivia {
|
||||
crate::children2::Trivia::Comment(text) => {
|
||||
steps.push_back(crate::builder::Step::Comment(text));
|
||||
}
|
||||
crate::children2::Trivia::Newlines(_) => {}
|
||||
}
|
||||
if not_last_child {
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
}
|
||||
steps.push_back(crate::builder::Step::Format(child));
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
if not_last_child {
|
||||
steps.push_back(crate::builder::Step::Whitespace);
|
||||
}
|
||||
steps.push_back(crate::builder::Step::Format(child.element));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,4 +11,19 @@
|
|||
{ inherit /*a*/ b d /*e*/ ; }
|
||||
{ inherit /*a*/ b /*c*/ d ; }
|
||||
{ inherit /*a*/ b /*c*/ d /*e*/ ; }
|
||||
{
|
||||
inherit # test
|
||||
a # test
|
||||
|
||||
b # test
|
||||
c # test
|
||||
d # test
|
||||
|
||||
e
|
||||
f
|
||||
|
||||
g
|
||||
h
|
||||
;
|
||||
}
|
||||
]
|
||||
|
|
|
@ -86,4 +86,18 @@
|
|||
*/
|
||||
;
|
||||
}
|
||||
{
|
||||
inherit # test
|
||||
a # test
|
||||
|
||||
b # test
|
||||
c # test
|
||||
d # test
|
||||
|
||||
e
|
||||
f
|
||||
g
|
||||
h
|
||||
;
|
||||
}
|
||||
]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue