1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-31 12:37:45 +00:00

feat: rewrite comments in-place

This commit is contained in:
Kevin Amado 2022-02-22 20:40:01 -05:00
parent 0badf84d57
commit eb6ff57bee
2 changed files with 37 additions and 12 deletions

View file

@ -301,3 +301,21 @@ pub fn fits_in_single_line(
None => false, None => false,
} }
} }
pub fn make_isolated_token(
kind: rnix::SyntaxKind,
text: &str,
) -> rnix::SyntaxToken {
use rowan::Language;
let mut builder = rowan::GreenNodeBuilder::new();
builder.start_node(rnix::NixLanguage::kind_to_raw(
rnix::SyntaxKind::NODE_ROOT,
));
builder.token(rnix::NixLanguage::kind_to_raw(kind), text);
builder.finish_node();
let node = builder.finish();
rnix::SyntaxNode::new_root(node).first_token().unwrap()
}

View file

@ -35,6 +35,16 @@ impl Children {
} }
rnix::SyntaxElement::Token(token) => { rnix::SyntaxElement::Token(token) => {
match token.kind() { match token.kind() {
rnix::SyntaxKind::TOKEN_COMMENT => {
children.push(Child {
element: crate::builder::make_isolated_token(
rnix::SyntaxKind::TOKEN_COMMENT,
&dedent_comment(&pos, token.text()),
)
.into(),
pos: pos.clone(),
});
}
rnix::SyntaxKind::TOKEN_WHITESPACE => { rnix::SyntaxKind::TOKEN_WHITESPACE => {
if with_newlines if with_newlines
&& crate::utils::count_newlines(token.text()) && crate::utils::count_newlines(token.text())
@ -127,10 +137,9 @@ impl Children {
pub fn drain_comment<F: FnMut(String)>(&mut self, mut callback: F) { pub fn drain_comment<F: FnMut(String)>(&mut self, mut callback: F) {
if let Some(child) = self.peek_next() { if let Some(child) = self.peek_next() {
if let rnix::SyntaxKind::TOKEN_COMMENT = child.element.kind() { if let rnix::SyntaxKind::TOKEN_COMMENT = child.element.kind() {
callback(dedent_comment( callback(
&child.pos, child.element.into_token().unwrap().text().to_string(),
child.element.into_token().unwrap().text(), );
));
self.move_next(); self.move_next();
} }
} }
@ -140,10 +149,9 @@ impl Children {
while let Some(child) = self.peek_next() { while let Some(child) = self.peek_next() {
match child.element.kind() { match child.element.kind() {
rnix::SyntaxKind::TOKEN_COMMENT => { rnix::SyntaxKind::TOKEN_COMMENT => {
callback(dedent_comment( callback(
&child.pos, child.element.into_token().unwrap().text().to_string(),
child.element.into_token().unwrap().text(), );
));
self.move_next(); self.move_next();
} }
_ => { _ => {
@ -160,10 +168,9 @@ impl Children {
while let Some(child) = self.peek_next() { while let Some(child) = self.peek_next() {
match child.element.kind() { match child.element.kind() {
rnix::SyntaxKind::TOKEN_COMMENT => { rnix::SyntaxKind::TOKEN_COMMENT => {
callback(DrainCommentOrNewline::Comment(dedent_comment( callback(DrainCommentOrNewline::Comment(
&child.pos, child.element.into_token().unwrap().text().to_string(),
child.element.into_token().unwrap().text(), ));
)));
self.move_next(); self.move_next();
} }
rnix::SyntaxKind::TOKEN_WHITESPACE => { rnix::SyntaxKind::TOKEN_WHITESPACE => {