Skip to content

Commit 9f33b63

Browse files
committed
refactor: remove unnecessary code
1 parent 5c3d7ab commit 9f33b63

19 files changed

+133
-173
lines changed

src/alejandra_engine/src/children.rs

+21-44
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
#[derive(Clone)]
2-
pub(crate) struct Child {
3-
pub element: rnix::SyntaxElement,
4-
#[allow(dead_code)]
5-
pub pos: crate::position::Position,
6-
}
7-
81
pub(crate) struct Children {
9-
children: Vec<Child>,
2+
children: Vec<rnix::SyntaxElement>,
103
current_index: usize,
114
}
125

@@ -20,44 +13,34 @@ impl Children {
2013
build_ctx: &crate::builder::BuildCtx,
2114
node: &rnix::SyntaxNode,
2215
) -> Children {
23-
let mut children = Vec::new();
16+
let mut children: Vec<rnix::SyntaxElement> = Vec::new();
2417

2518
let mut pos = build_ctx.pos_old.clone();
2619

2720
for child in node.children_with_tokens() {
2821
match child {
2922
rnix::SyntaxElement::Node(node) => {
30-
children.push(Child {
31-
element: node.clone().into(),
32-
pos: pos.clone(),
33-
});
23+
children.push(node.clone().into());
3424
pos.update(&node.text().to_string());
3525
}
3626
rnix::SyntaxElement::Token(token) => {
3727
match token.kind() {
3828
rnix::SyntaxKind::TOKEN_COMMENT => {
39-
children.push(Child {
40-
element: crate::builder::make_isolated_token(
29+
children.push(
30+
crate::builder::make_isolated_token(
4131
rnix::SyntaxKind::TOKEN_COMMENT,
4232
&dedent_comment(&pos, token.text()),
4333
)
4434
.into(),
45-
pos: pos.clone(),
46-
});
35+
);
4736
}
4837
rnix::SyntaxKind::TOKEN_WHITESPACE => {
4938
if crate::utils::count_newlines(token.text()) > 0 {
50-
children.push(Child {
51-
element: token.clone().into(),
52-
pos: pos.clone(),
53-
});
39+
children.push(token.clone().into());
5440
}
5541
}
5642
_ => {
57-
children.push(Child {
58-
element: token.clone().into(),
59-
pos: pos.clone(),
60-
});
43+
children.push(token.clone().into());
6144
}
6245
}
6346

@@ -69,21 +52,21 @@ impl Children {
6952
Children { children, current_index: 0 }
7053
}
7154

72-
pub fn get(&mut self, index: usize) -> Option<Child> {
55+
pub fn get(&mut self, index: usize) -> Option<rnix::SyntaxElement> {
7356
if index + 1 > self.children.len() {
7457
None
7558
} else {
7659
Some(self.children[index].clone())
7760
}
7861
}
7962

80-
pub fn get_next(&mut self) -> Option<Child> {
63+
pub fn get_next(&mut self) -> Option<rnix::SyntaxElement> {
8164
let child = self.get(self.current_index);
8265
self.move_next();
8366
child
8467
}
8568

86-
pub fn get_remaining(&mut self) -> Vec<Child> {
69+
pub fn get_remaining(&mut self) -> Vec<rnix::SyntaxElement> {
8770
let remaining = &self.children[self.current_index..self.children.len()];
8871
self.current_index = self.children.len();
8972
remaining.to_vec()
@@ -93,11 +76,11 @@ impl Children {
9376
self.current_index < self.children.len()
9477
}
9578

96-
pub fn peek_next(&mut self) -> Option<Child> {
79+
pub fn peek_next(&mut self) -> Option<rnix::SyntaxElement> {
9780
self.get(self.current_index)
9881
}
9982

100-
pub fn peek_prev(&mut self) -> Option<Child> {
83+
pub fn peek_prev(&mut self) -> Option<rnix::SyntaxElement> {
10184
self.get(self.current_index - 1)
10285
}
10386

@@ -110,38 +93,32 @@ impl Children {
11093
}
11194

11295
pub fn has_comments(&self) -> bool {
113-
self.children.iter().any(|child| {
114-
child.element.kind() == rnix::SyntaxKind::TOKEN_COMMENT
115-
})
96+
self.children
97+
.iter()
98+
.any(|child| child.kind() == rnix::SyntaxKind::TOKEN_COMMENT)
11699
}
117100

118101
pub fn has_newlines(&self) -> bool {
119102
self.children.iter().any(|child| {
120-
child.element.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE
103+
child.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE
121104
&& crate::utils::has_newlines(
122-
child.element.as_token().as_ref().unwrap().text(),
105+
child.as_token().as_ref().unwrap().text(),
123106
)
124107
})
125108
}
126109

127110
pub fn drain_trivia<F: FnMut(Trivia)>(&mut self, mut callback: F) {
128111
while let Some(child) = self.peek_next() {
129-
match child.element.kind() {
112+
match child.kind() {
130113
rnix::SyntaxKind::TOKEN_COMMENT => {
131114
callback(Trivia::Comment(
132-
child.element.into_token().unwrap().text().to_string(),
115+
child.into_token().unwrap().text().to_string(),
133116
));
134117
self.move_next();
135118
}
136119
rnix::SyntaxKind::TOKEN_WHITESPACE => {
137120
callback(Trivia::Whitespace(
138-
child
139-
.element
140-
.as_token()
141-
.as_ref()
142-
.unwrap()
143-
.text()
144-
.to_string(),
121+
child.as_token().as_ref().unwrap().text().to_string(),
145122
));
146123
self.move_next();
147124
}

src/alejandra_engine/src/parsers/if_else.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(crate) fn parse(
3232
});
3333

3434
// expr
35-
if_else.if_expr = Some(children.get_next().unwrap().element);
35+
if_else.if_expr = Some(children.get_next().unwrap());
3636

3737
// /**/
3838
children.drain_trivia(|element| match element {
@@ -54,7 +54,7 @@ pub(crate) fn parse(
5454
});
5555

5656
// expr
57-
if_else.then_expr = Some(children.get_next().unwrap().element);
57+
if_else.then_expr = Some(children.get_next().unwrap());
5858

5959
// /**/
6060
children.drain_trivia(|element| match element {
@@ -76,7 +76,7 @@ pub(crate) fn parse(
7676
});
7777

7878
// expr
79-
if_else.else_expr = Some(children.get_next().unwrap().element);
79+
if_else.else_expr = Some(children.get_next().unwrap());
8080

8181
if_else
8282
}

src/alejandra_engine/src/parsers/pattern.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ pub(crate) fn parse(
2727

2828
// x @
2929
let child = children.peek_next().unwrap();
30-
if let rnix::SyntaxKind::NODE_PAT_BIND = child.element.kind() {
31-
pattern.initial_at = Some(child.element);
30+
if let rnix::SyntaxKind::NODE_PAT_BIND = child.kind() {
31+
pattern.initial_at = Some(child);
3232
children.move_next();
3333
}
3434

@@ -50,9 +50,9 @@ pub(crate) fn parse(
5050
// Before an item we can have: comma, comments, whitespace
5151
loop {
5252
let child = children.peek_next().unwrap();
53-
// eprintln!("before item {:?}", child.element.kind());
53+
// eprintln!("before item {:?}", child.kind());
5454

55-
match child.element.kind() {
55+
match child.kind() {
5656
rnix::SyntaxKind::NODE_PAT_ENTRY
5757
| rnix::SyntaxKind::TOKEN_CURLY_B_CLOSE
5858
| rnix::SyntaxKind::TOKEN_ELLIPSIS => {
@@ -62,8 +62,7 @@ pub(crate) fn parse(
6262
children.move_next();
6363
}
6464
rnix::SyntaxKind::TOKEN_COMMENT => {
65-
let content =
66-
child.element.into_token().unwrap().to_string();
65+
let content = child.into_token().unwrap().to_string();
6766

6867
argument.comments_before.push_back(content);
6968
children.move_next();
@@ -77,16 +76,16 @@ pub(crate) fn parse(
7776

7877
// item
7978
let child = children.peek_next().unwrap();
80-
// eprintln!("item {:?}", child.element.kind());
81-
match child.element.kind() {
79+
// eprintln!("item {:?}", child.kind());
80+
match child.kind() {
8281
rnix::SyntaxKind::TOKEN_CURLY_B_CLOSE => {
8382
pattern.comments_before_curly_b_close =
8483
argument.comments_before;
8584
break;
8685
}
8786
rnix::SyntaxKind::TOKEN_ELLIPSIS
8887
| rnix::SyntaxKind::NODE_PAT_ENTRY => {
89-
argument.item = Some(child.element);
88+
argument.item = Some(child);
9089
children.move_next();
9190
}
9291
_ => {}
@@ -95,9 +94,9 @@ pub(crate) fn parse(
9594
// After an item we can have: comma, comments, whitespace
9695
loop {
9796
let child = children.peek_next().unwrap();
98-
// eprintln!("after item {:?}", child.element.kind());
97+
// eprintln!("after item {:?}", child.kind());
9998

100-
match child.element.kind() {
99+
match child.kind() {
101100
rnix::SyntaxKind::NODE_PAT_ENTRY
102101
| rnix::SyntaxKind::TOKEN_ELLIPSIS
103102
| rnix::SyntaxKind::TOKEN_CURLY_B_CLOSE => {
@@ -107,16 +106,14 @@ pub(crate) fn parse(
107106
children.move_next();
108107
}
109108
rnix::SyntaxKind::TOKEN_COMMENT => {
110-
let content =
111-
child.element.into_token().unwrap().to_string();
109+
let content = child.into_token().unwrap().to_string();
112110

113111
children.move_next();
114112
argument.comment_after = Some(content);
115113
break;
116114
}
117115
rnix::SyntaxKind::TOKEN_WHITESPACE => {
118-
let content =
119-
child.element.into_token().unwrap().to_string();
116+
let content = child.into_token().unwrap().to_string();
120117

121118
children.move_next();
122119
if crate::utils::count_newlines(&content) > 0 {
@@ -143,8 +140,8 @@ pub(crate) fn parse(
143140

144141
// @ x
145142
if let Some(child) = children.peek_next() {
146-
if let rnix::SyntaxKind::NODE_PAT_BIND = child.element.kind() {
147-
pattern.end_at = Some(child.element);
143+
if let rnix::SyntaxKind::NODE_PAT_BIND = child.kind() {
144+
pattern.end_at = Some(child);
148145
}
149146
}
150147

src/alejandra_engine/src/rules/apply.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ pub(crate) fn rule(
1414
let child = children.get_next().unwrap();
1515

1616
if vertical {
17-
steps.push_back(crate::builder::Step::FormatWider(child.element));
17+
steps.push_back(crate::builder::Step::FormatWider(child));
1818
} else {
19-
steps.push_back(crate::builder::Step::Format(child.element));
19+
steps.push_back(crate::builder::Step::Format(child));
2020
}
2121

2222
// /**/
@@ -35,24 +35,24 @@ pub(crate) fn rule(
3535
let child = children.get_next().unwrap();
3636
if vertical {
3737
if let rnix::SyntaxKind::TOKEN_COMMENT
38-
| rnix::SyntaxKind::TOKEN_WHITESPACE = child_prev.element.kind()
38+
| rnix::SyntaxKind::TOKEN_WHITESPACE = child_prev.kind()
3939
{
4040
steps.push_back(crate::builder::Step::NewLine);
4141
steps.push_back(crate::builder::Step::Pad);
4242
} else if let rnix::SyntaxKind::NODE_ATTR_SET
4343
| rnix::SyntaxKind::NODE_LIST
4444
| rnix::SyntaxKind::NODE_PAREN
45-
| rnix::SyntaxKind::NODE_STRING = child.element.kind()
45+
| rnix::SyntaxKind::NODE_STRING = child.kind()
4646
{
4747
steps.push_back(crate::builder::Step::Whitespace);
4848
} else {
4949
steps.push_back(crate::builder::Step::NewLine);
5050
steps.push_back(crate::builder::Step::Pad);
5151
};
52-
steps.push_back(crate::builder::Step::FormatWider(child.element));
52+
steps.push_back(crate::builder::Step::FormatWider(child));
5353
} else {
5454
steps.push_back(crate::builder::Step::Whitespace);
55-
steps.push_back(crate::builder::Step::Format(child.element));
55+
steps.push_back(crate::builder::Step::Format(child));
5656
}
5757

5858
steps

src/alejandra_engine/src/rules/assert_and_with.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub(crate) fn rule(
1212

1313
// with
1414
let child = children.get_next().unwrap();
15-
steps.push_back(crate::builder::Step::Format(child.element));
15+
steps.push_back(crate::builder::Step::Format(child));
1616

1717
// /**/
1818
let mut comment = false;
@@ -36,14 +36,14 @@ pub(crate) fn rule(
3636
// expr
3737
let child = children.get_next().unwrap();
3838
if vertical {
39-
steps.push_back(crate::builder::Step::FormatWider(child.element));
39+
steps.push_back(crate::builder::Step::FormatWider(child));
4040
} else {
41-
steps.push_back(crate::builder::Step::Format(child.element));
41+
steps.push_back(crate::builder::Step::Format(child));
4242
}
4343

4444
// ;
4545
let child = children.get_next().unwrap();
46-
steps.push_back(crate::builder::Step::Format(child.element));
46+
steps.push_back(crate::builder::Step::Format(child));
4747

4848
// /**/
4949
let mut comment: bool = false;
@@ -62,16 +62,16 @@ pub(crate) fn rule(
6262
if vertical {
6363
if {
6464
matches!(
65-
child.element.kind(),
65+
child.kind(),
6666
rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH
6767
)
6868
} {
6969
steps.push_back(crate::builder::Step::NewLine);
7070
steps.push_back(crate::builder::Step::Pad);
71-
steps.push_back(crate::builder::Step::FormatWider(child.element));
71+
steps.push_back(crate::builder::Step::FormatWider(child));
7272
} else if comment
7373
|| !matches!(
74-
child.element.kind(),
74+
child.kind(),
7575
rnix::SyntaxKind::NODE_ATTR_SET
7676
| rnix::SyntaxKind::NODE_IDENT
7777
| rnix::SyntaxKind::NODE_PAREN
@@ -84,15 +84,15 @@ pub(crate) fn rule(
8484
steps.push_back(crate::builder::Step::Indent);
8585
steps.push_back(crate::builder::Step::NewLine);
8686
steps.push_back(crate::builder::Step::Pad);
87-
steps.push_back(crate::builder::Step::FormatWider(child.element));
87+
steps.push_back(crate::builder::Step::FormatWider(child));
8888
steps.push_back(crate::builder::Step::Dedent);
8989
} else {
9090
steps.push_back(crate::builder::Step::Whitespace);
91-
steps.push_back(crate::builder::Step::FormatWider(child.element));
91+
steps.push_back(crate::builder::Step::FormatWider(child));
9292
}
9393
} else {
9494
steps.push_back(crate::builder::Step::Whitespace);
95-
steps.push_back(crate::builder::Step::Format(child.element));
95+
steps.push_back(crate::builder::Step::Format(child));
9696
}
9797

9898
steps

0 commit comments

Comments
 (0)