mirror of
https://github.com/RGBCube/alejandra
synced 2025-08-01 13:07:47 +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
|
+ 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
|
### Changed
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
use std::collections::LinkedList;
|
use std::collections::LinkedList;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub(crate) enum Trivia {
|
pub(crate) enum Trivia {
|
||||||
Comment(String),
|
Comment(String),
|
||||||
Newlines(usize),
|
Newlines(usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub(crate) struct Child {
|
pub(crate) struct Child {
|
||||||
pub element: rnix::SyntaxElement,
|
pub element: rnix::SyntaxElement,
|
||||||
|
|
||||||
|
@ -28,16 +30,26 @@ pub(crate) fn new(
|
||||||
let mut inline_comment = None;
|
let mut inline_comment = None;
|
||||||
let mut trivialities = LinkedList::new();
|
let mut trivialities = LinkedList::new();
|
||||||
|
|
||||||
|
let mut skip_next_newline = false;
|
||||||
children.drain_trivia(|element| match element {
|
children.drain_trivia(|element| match element {
|
||||||
crate::children::Trivia::Comment(text) => {
|
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);
|
inline_comment = Some(text);
|
||||||
|
skip_next_newline = true;
|
||||||
} else {
|
} else {
|
||||||
trivialities.push_back(Trivia::Comment(text));
|
trivialities.push_back(Trivia::Comment(text));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
crate::children::Trivia::Whitespace(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 {
|
if newlines > 0 {
|
||||||
trivialities.push_back(Trivia::Newlines(newlines))
|
trivialities.push_back(Trivia::Newlines(newlines))
|
||||||
|
|
|
@ -4,45 +4,83 @@ pub(crate) fn rule(
|
||||||
) -> std::collections::LinkedList<crate::builder::Step> {
|
) -> std::collections::LinkedList<crate::builder::Step> {
|
||||||
let mut steps = std::collections::LinkedList::new();
|
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()
|
let vertical = build_ctx.vertical
|
||||||
|| children.has_newlines()
|
|| children
|
||||||
|| build_ctx.vertical;
|
.iter()
|
||||||
|
.any(|child| child.has_inline_comment || child.has_trivialities);
|
||||||
|
|
||||||
|
let children_count = children.len() - 1;
|
||||||
|
let mut children = children.into_iter();
|
||||||
|
|
||||||
// inherit
|
// inherit
|
||||||
let child = children.get_next().unwrap();
|
let child = children.next().unwrap();
|
||||||
steps.push_back(crate::builder::Step::Format(child));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
if vertical {
|
if vertical {
|
||||||
steps.push_back(crate::builder::Step::Indent);
|
steps.push_back(crate::builder::Step::Indent);
|
||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
if let Some(text) = child.inline_comment {
|
||||||
// /**/
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
children.drain_trivia(|element| match element {
|
steps.push_back(crate::builder::Step::Comment(text));
|
||||||
crate::children::Trivia::Comment(text) => {
|
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);
|
} else if vertical {
|
||||||
steps.push_back(crate::builder::Step::Comment(text));
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
}
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
crate::children::Trivia::Whitespace(_) => {}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
if let Some(child) = children.get_next() {
|
for trivia in child.trivialities {
|
||||||
// expr
|
match trivia {
|
||||||
if vertical {
|
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::NewLine);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child));
|
}
|
||||||
} else {
|
crate::children2::Trivia::Newlines(_) => {}
|
||||||
if let rnix::SyntaxKind::TOKEN_SEMICOLON = child.kind() {
|
}
|
||||||
} else {
|
}
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
|
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 {
|
} 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 d /*e*/ ; }
|
||||||
{ inherit /*a*/ b /*c*/ d ; }
|
{ inherit /*a*/ b /*c*/ d ; }
|
||||||
{ inherit /*a*/ b /*c*/ d /*e*/ ; }
|
{ 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