Skip to content

Commit 7420375

Browse files
committed
fix: attribute generator does not append new line
Signed-off-by: azjezz <azjezz@protonmail.com>
1 parent a66b6c4 commit 7420375

16 files changed

+260
-242
lines changed

examples/complete.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function format(
6262
*
6363
* @immutable
6464
*/
65-
abstract class Example
65+
abstract class Example extends Foo\Bar\Baz implements Foo\Bar\BazInterface
6666
{
6767
final const A = "Hello World!";
6868

@@ -119,7 +119,13 @@ public final function helloWorld(): void {
119119
}
120120
}
121121

122-
interface Formatter
122+
/**
123+
* This is a simple formatter interface.
124+
*
125+
* @immutable
126+
*/
127+
#[Foo(foo: 1, bar: 2), Bar(foo: 1, bar: 2)]
128+
interface Formatter extends Qux
123129
{
124130
public function format(
125131
string $template,

examples/complete.rs

Lines changed: 180 additions & 164 deletions
Large diffs are not rendered by default.

src/attribute.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,20 @@ impl Generator for AttributeGroup {
3434
if let Some(arguments) = arguments {
3535
format!("{}({})", name, arguments)
3636
} else {
37-
format!("{}", name)
37+
name.to_string()
3838
}
3939
})
4040
.collect::<Vec<String>>()
4141
.join(", "),
4242
);
43-
result.push_str("]");
43+
result.push_str("]\n");
4444

4545
result
4646
}
4747
}
48+
49+
impl Default for AttributeGroup {
50+
fn default() -> Self {
51+
Self::new()
52+
}
53+
}

src/body.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,28 @@ impl Generator for Body {
4646
let mut code = String::new();
4747

4848
code.push_str(" {");
49-
code.push_str("\n");
49+
code.push('\n');
5050
code.push_str(&factory(indentation, level + 1));
51-
code.push_str("\n");
51+
code.push('\n');
5252
code.push_str(&indentation.indent("}", level));
53-
code.push_str("\n");
53+
code.push('\n');
5454

5555
code
5656
}
5757
None => {
5858
let mut code = String::new();
5959

6060
if self.semicolon_for_empty {
61-
code.push_str(";");
61+
code.push(';');
6262
} else {
6363
code.push_str(" {");
64-
code.push_str("\n");
64+
code.push('\n');
6565
code.push_str(&indentation.indent("// empty body", level + 1));
66-
code.push_str("\n");
66+
code.push('\n');
6767
code.push_str(&indentation.indent("}", level));
6868
}
6969

70-
code.push_str("\n");
70+
code.push('\n');
7171

7272
code
7373
}

src/class.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ impl Class {
5353
self
5454
}
5555

56-
pub fn extend<T: ToString>(mut self, extends: T) -> Self {
56+
pub fn extends<T: ToString>(mut self, extends: T) -> Self {
5757
self.extends = Some(extends.to_string());
5858

5959
self
6060
}
6161

62-
pub fn implement<T: ToString>(mut self, implements: T) -> Self {
62+
pub fn implements<T: ToString>(mut self, implements: T) -> Self {
6363
self.implements.push(implements.to_string());
6464

6565
self
@@ -129,7 +129,7 @@ impl Generator for Class {
129129
.join("\n"),
130130
);
131131

132-
code.push_str("\n");
132+
code.push('\n');
133133
}
134134

135135
if !self.properties.is_empty() {
@@ -142,7 +142,7 @@ impl Generator for Class {
142142
.join("\n"),
143143
);
144144

145-
code.push_str("\n");
145+
code.push('\n');
146146
}
147147

148148
if !self.methods.is_empty() {

src/comment.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl Document {
1818
Self { elements: vec![] }
1919
}
2020

21-
pub fn add(mut self, element: Element) -> Self {
21+
pub fn add_element(mut self, element: Element) -> Self {
2222
self.elements.push(element);
2323

2424
self
@@ -46,7 +46,7 @@ impl Document {
4646

4747
pub fn text<T: ToString>(mut self, text: T) -> Self {
4848
self.elements
49-
.push(Element::Text(format!("{}\n", text.to_string()).to_owned()));
49+
.push(Element::Text(format!("{}\n", text.to_string())));
5050

5151
self
5252
}
@@ -90,3 +90,9 @@ impl Generator for Element {
9090
}
9191
}
9292
}
93+
94+
impl Default for Document {
95+
fn default() -> Self {
96+
Self::new()
97+
}
98+
}

src/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl Generator for ClassConstant {
152152
.join("\n"),
153153
);
154154

155-
code.push_str("\n");
155+
code.push('\n');
156156
}
157157

158158
if let Some(visibility) = &self.visibility {

src/data_type.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ pub enum DataType {
2727
}
2828

2929
impl Generator for DataType {
30-
fn generate(&self, indentation: Indentation, level: usize) -> String {
30+
fn generate(&self, _indentation: Indentation, _level: usize) -> String {
3131
match self {
3232
DataType::Named(name) => name.to_string(),
3333
DataType::Nullable(inner) => {
34-
format!("?{}", inner.generate(indentation, level))
34+
format!("?{}", inner.generate(_indentation, _level))
3535
}
3636
DataType::Union(inner) => inner
3737
.iter()
38-
.map(|t| t.generate(indentation, level))
38+
.map(|t| t.generate(_indentation, _level))
3939
.collect::<Vec<String>>()
4040
.join("|"),
4141
DataType::Intersection(inner) => inner
4242
.iter()
43-
.map(|t| t.generate(indentation, level))
43+
.map(|t| t.generate(_indentation, _level))
4444
.collect::<Vec<String>>()
4545
.join("&"),
4646
DataType::Null => "null".to_string(),

src/file.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ impl File {
7979
}
8080

8181
pub fn class(mut self, class: Class) -> Self {
82-
self.classes.push(class.into());
82+
self.classes.push(class);
8383

8484
self
8585
}
8686

8787
pub fn interface(mut self, interface: Interface) -> Self {
88-
self.interfaces.push(interface.into());
88+
self.interfaces.push(interface);
8989

9090
self
9191
}
@@ -116,7 +116,7 @@ impl Generator for File {
116116
code.push_str(&format!("use {};\n", r#use));
117117
}
118118

119-
code.push_str("\n");
119+
code.push('\n');
120120
}
121121

122122
if !self.function_uses.is_empty() {
@@ -125,7 +125,7 @@ impl Generator for File {
125125
code.push_str(&format!("use function {};\n", function));
126126
}
127127

128-
code.push_str("\n");
128+
code.push('\n');
129129
}
130130

131131
if !self.constant_uses.is_empty() {
@@ -134,34 +134,34 @@ impl Generator for File {
134134
code.push_str(&format!("use const {};\n", constant));
135135
}
136136

137-
code.push_str("\n");
137+
code.push('\n');
138138
}
139139

140140
if used {
141-
code.push_str("\n");
141+
code.push('\n');
142142
}
143143

144144
if !self.constants.is_empty() {
145145
for constant in &self.constants {
146146
code.push_str(&constant.generate(indentation, level));
147147
}
148148

149-
code.push_str("\n");
149+
code.push('\n');
150150
}
151151

152152
for function in &self.functions {
153153
code.push_str(&function.generate(indentation, level));
154-
code.push_str("\n");
154+
code.push('\n');
155155
}
156156

157157
for class in &self.classes {
158158
code.push_str(&class.generate(indentation, level));
159-
code.push_str("\n");
159+
code.push('\n');
160160
}
161161

162162
for interface in &self.interfaces {
163163
code.push_str(&interface.generate(indentation, level));
164-
code.push_str("\n");
164+
code.push('\n');
165165
}
166166

167167
code
@@ -170,6 +170,12 @@ impl Generator for File {
170170

171171
impl Display for File {
172172
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
173-
write!(f, "{}", self.generate(Indentation::Spaces(4), 0))
173+
write!(f, "{}", self.generate(Indentation::default(), 0))
174+
}
175+
}
176+
177+
impl Default for File {
178+
fn default() -> Self {
179+
Self::new()
174180
}
175181
}

src/function.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,8 @@ impl Generator for Function {
6868
code.push_str(&document.generate(indentation, level));
6969
}
7070

71-
if !self.attributes.is_empty() {
72-
code.push_str(
73-
&self
74-
.attributes
75-
.iter()
76-
.map(|attributes| attributes.generate(indentation, level))
77-
.collect::<Vec<String>>()
78-
.join("\n"),
79-
);
80-
81-
code.push_str("\n");
71+
for attribute in &self.attributes {
72+
code.push_str(&attribute.generate(indentation, level));
8273
}
8374

8475
code.push_str(format!("function {}", self.name).as_str());
@@ -97,7 +88,7 @@ impl Generator for Function {
9788
);
9889

9990
code.push_str(",\n");
100-
code.push_str(")");
91+
code.push(')');
10192
}
10293

10394
if let Some(return_type) = &self.return_type {

0 commit comments

Comments
 (0)