You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: crates/roughly/tests/format/formatter.template.md
+59-51Lines changed: 59 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,7 @@ Roughly applies specific formatting rules to different R code constructs. The fo
49
49
```r
50
50
# assignment_operators : compare
51
51
x<-1
52
-
result<<-calculate()
52
+
data<<-process()
53
53
```
54
54
55
55
**Binary operators** get spaces around them, except for range (`:`) and power (`^`) operators:
@@ -58,7 +58,7 @@ result<<-calculate()
58
58
# binary_operators : compare
59
59
result=x+y*z
60
60
power=base^exponent
61
-
sequence=1:10
61
+
data=1:10
62
62
```
63
63
64
64
**Pipeline operators** maintain proper indentation when expressions span multiple lines:
@@ -67,7 +67,7 @@ sequence=1:10
67
67
# pipeline_operators : compare
68
68
data %>%
69
69
filter(condition) %>%
70
-
summarize(mean_value=mean(value))
70
+
select(mean_value=mean(value))
71
71
```
72
72
73
73
### Code Blocks and Braced Expressions
@@ -114,11 +114,11 @@ The formatter normalizes line spacing between expressions, allowing at most one
114
114
115
115
```r
116
116
# line_spacing : compare
117
-
calculate_mean<-function(data) {
118
-
clean_data<-data[!is.na(data)]
117
+
process<-function(data) {
118
+
result<-filter(data)
119
119
120
120
121
-
mean(clean_data)
121
+
result
122
122
}
123
123
```
124
124
@@ -128,15 +128,13 @@ Function calls receive consistent formatting with proper spacing around argument
128
128
129
129
```r
130
130
# function_calls : compare
131
-
calculate(data=dataset,method="mean",na.rm=TRUE)
132
-
complex_call(argument1,
133
-
argument2=value,
134
-
argument3)
131
+
process(data=dataset,method="mean",na.rm=TRUE)
132
+
call(arg1,
133
+
arg2=value,
134
+
arg3)
135
135
```
136
136
137
-
**Argument hugging**: When function arguments can fit on a single line and the last argument's value starts on the same line as the opening parenthesis, the formatter keeps a compact format. Otherwise, it expands to multiple lines with proper indentation.
138
-
139
-
**Nested function calls** are formatted with hugging when appropriate:
137
+
**Nested function calls** are formatted with proper spacing:
140
138
141
139
```r
142
140
# nested_function_calls : compare
@@ -149,9 +147,9 @@ Function definitions follow consistent formatting rules for parameters and body
149
147
150
148
```r
151
149
# function_definitions : compare
152
-
calc<-function(x,y=1){x+y}
153
-
stats<-function(data,method="mean"){
154
-
result(data,method)
150
+
process<-function(x,y=1){x+y}
151
+
filter<-function(data,method="simple"){
152
+
select(data,method)
155
153
}
156
154
```
157
155
@@ -172,17 +170,15 @@ lapply(data,\(x)x+1)
172
170
173
171
```r
174
172
# conditional_statements : compare
175
-
if(condition){action} else{alternative}
173
+
if(condition){process()} else{filter()}
176
174
177
175
if(
178
-
long_condition||
176
+
condition||
179
177
other_condition){
180
-
action()
178
+
process()
181
179
}
182
180
```
183
181
184
-
**Condition hugging**: When conditions fit on a single line without comments, the formatter keeps them compact. For multiline conditions, proper indentation is applied.
185
-
186
182
**Block enforcement**: If an if-statement has a multiline condition, the formatter ensures the body is wrapped in braces even if it's a single expression.
187
183
188
184
### Loops and Control Flow
@@ -191,38 +187,36 @@ if(
191
187
192
188
```r
193
189
# for_loops : compare
194
-
for(itemincollection) process(item)
190
+
for(itemindata) process(item)
195
191
```
196
192
197
193
**While loops** follow similar block enforcement rules:
198
194
199
195
```r
200
196
# while_loops : compare
201
-
while(condition) action()
197
+
while(condition) process()
202
198
```
203
199
204
200
**Repeat loops** also enforce braced blocks:
205
201
206
202
```r
207
203
# repeat_loops : compare
208
-
repeataction()
204
+
repeatprocess()
209
205
```
210
206
211
207
**Loop condition formatting**: Complex conditions that span multiple lines receive proper indentation within the parentheses.
212
208
213
209
### Parenthesized Expressions
214
210
215
-
Parenthesized expressions maintain their layout with smart formatting for readability:
211
+
Parenthesized expressions receive proper spacing for operators:
216
212
217
213
```r
218
214
# parenthesized_expressions : compare
219
-
(x+y*z)
220
-
(long_expression+
221
-
other_part)
215
+
(
216
+
expression+
217
+
other_part)
222
218
```
223
219
224
-
**Parenthesis hugging**: If the content fits on one line, parentheses hug the content. For multiline content, proper indentation is applied.
225
-
226
220
### String Literals
227
221
228
222
String literals receive intelligent quote normalization. The formatter prefers double quotes (`"`) unless the string contains unescaped double quotes:
@@ -259,36 +253,37 @@ Unary operators receive appropriate spacing based on their type and context:
259
253
```r
260
254
# unary_operators : compare
261
255
result=!condition
262
-
number=-42
263
-
formula=~response+predictor
256
+
value=-42
257
+
formula=~x+y
264
258
```
265
259
266
260
**Special spacing rule**: The `~` (formula) operator gets a space when followed by complex expressions, but not when followed by simple identifiers.
267
261
268
262
## Format Suppression
269
263
270
-
You can disable formatting for specific code sections using the `# fmt: skip` comment directive:
264
+
You can disable formatting for specific code sections using the `# fmt: skip` comment directive. This is useful when you want to preserve specific formatting for readability, such as aligned data structures.
271
265
272
266
```r
273
267
# format_suppression : format
274
268
# fmt: skip
275
-
matrix(
276
-
c(
277
-
1, 2,
278
-
3, 4
279
-
),
280
-
nrow=2
269
+
c(
270
+
1, 2,
271
+
3, 4
281
272
) # This code won't be reformatted
282
273
283
-
matrix(c(1, 2,
284
-
3, 4), nrow=2) # fmt: skip
285
-
# The line above won't be reformatted
274
+
# fmt: skip
275
+
c(1, 2,
276
+
3, 4) # The line above won't be reformatted
286
277
```
287
278
279
+
Without the `fmt: skip` directive, the `c(...)` expression would be formatted according to standard rules.
280
+
288
281
The `fmt: skip` directive can be placed:
289
282
- Before a line to skip formatting that entire expression
290
283
- At the end of a line to skip formatting just that line
291
284
285
+
You can also skip formatting for an entire file by placing `# fmt: skip-file` at the top of the file. This directive must be placed at the very beginning of the file to take effect.
286
+
292
287
## Advanced Formatting Features
293
288
294
289
The formatter intelligently handles various R idioms and special patterns:
@@ -341,7 +336,6 @@ PersonClass <- R6Class(
341
336
-**Switch statements**: Fallthrough cases (`case = ,`) are handled correctly
342
337
-**Multi-line strings**: String literal structure is preserved
343
338
-**Formula objects**: Proper spacing around `~` operator based on complexity
@@ -351,7 +345,7 @@ The formatter automatically adds braces to control flow structures when they imp
351
345
352
346
```r
353
347
# auto_bracing_function_defintions : compare
354
-
f<-function(x)
348
+
process<-function(x)
355
349
x+1
356
350
```
357
351
@@ -360,7 +354,7 @@ f <- function(x)
360
354
```r
361
355
# auto_bracing_conditional_statements : compare
362
356
if (condition)
363
-
single_statement
357
+
process()
364
358
```
365
359
366
360
**Loops**: All loop bodies are automatically braced for consistency:
@@ -373,7 +367,7 @@ for (i in 1:n)
373
367
374
368
## Hugging Behavior
375
369
376
-
"Hugging" refers to how nested function calls are formatted - keeping them compact by allowing the inner calls to start on the same line as the outer call's opening parenthesis. This is part of roughly's non-invasive approach: both hugged and expanded formats are allowed.
370
+
"Hugging" refers to how nested expressions are formatted in multiline contexts - keeping them compact by allowing inner expressions to start on the same line as the outer expression's opening delimiter. This is part of roughly's non-invasive approach: both hugged and expanded formats are allowed, but hugging only applies to multiline expressions.
377
371
378
372
**Nested function calls** can be formatted in a hugged style:
379
373
@@ -392,6 +386,20 @@ result <- outer(
392
386
)
393
387
```
394
388
389
+
**Parenthesized expressions** can also use hugging:
390
+
391
+
```r
392
+
# hugging_parenthesized : format
393
+
(expression+
394
+
other_part)
395
+
396
+
# Also allowed
397
+
(
398
+
expression+
399
+
other_part
400
+
)
401
+
```
402
+
395
403
**Non-invasive multi-line formatting**: When expressions are already multi-line, roughly only adds necessary spacing but preserves the overall structure. However, if all arguments don't fit on their separate lines, they will be properly separated:
Copy file name to clipboardExpand all lines: crates/roughly/tests/snapshots/test_format__documentation_examples__auto_bracing_conditional_statements.snap
0 commit comments