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
+87-66Lines changed: 87 additions & 66 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<<-compute()
53
53
```
54
54
55
55
**Binary operators** get spaces around them, except for range (`:`) and power (`^`) operators:
@@ -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(value)
71
71
```
72
72
73
73
### Code Blocks and Braced Expressions
@@ -114,12 +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
+
x<-1
118
+
y<-2
119
119
120
120
121
-
mean(clean_data)
122
-
}
121
+
z<-3
123
122
```
124
123
125
124
### Function Calls and Arguments
@@ -128,30 +127,62 @@ Function calls receive consistent formatting with proper spacing around argument
128
127
129
128
```r
130
129
# function_calls : compare
131
-
calculate(data=dataset,method="mean",na.rm=TRUE)
132
-
complex_call(argument1,
133
-
argument2=value,
134
-
argument3)
130
+
process(data=dataset,method="mean",na.rm=TRUE)
131
+
call(arg1,
132
+
arg2=value,
133
+
arg3)
134
+
```
135
+
136
+
**Nested function calls** can be formatted in a hugged style:
137
+
138
+
```r
139
+
# hugging_nested_function_calls : format
140
+
# Hugged format - both functions start on the same line
141
+
result<- outer(inner(
142
+
arg
143
+
))
144
+
145
+
# Expanded format - also valid
146
+
result<- outer(
147
+
inner(
148
+
arg
149
+
)
150
+
)
135
151
```
136
152
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.
153
+
**Mixed line formats** are allowed when the last argument starts on the same line:
154
+
155
+
```r
156
+
# mixed_line_format : format
157
+
# This format is preserved - last argument starts on same line
158
+
call(a=x, b=y, c= inner(
159
+
expr
160
+
))
161
+
```
138
162
139
-
**Nested function calls** are formatted with hugging when appropriate:
163
+
This behavior is particularly useful for testing frameworks and S4 method definitions:
140
164
141
165
```r
142
-
# nested_function_calls : compare
143
-
result<- outer(inner(a,b),other(c,d))
166
+
# test_that_and_s4_example: format
167
+
test_that("description", {
168
+
expect_equal(result, expected)
169
+
})
170
+
171
+
setMethod("show", "MyClass", function(object) {
172
+
cat("MyClass object\n")
173
+
})
144
174
```
145
175
176
+
146
177
### Function Definitions
147
178
148
179
Function definitions follow consistent formatting rules for parameters and body structure:
149
180
150
181
```r
151
182
# function_definitions : compare
152
-
calc<-function(x,y=1){x+y}
153
-
stats<-function(data,method="mean"){
154
-
result(data,method)
183
+
process<-function(x,y=1){x+y}
184
+
filter<-function(data,method="simple"){
185
+
select(data,method)
155
186
}
156
187
```
157
188
@@ -172,17 +203,15 @@ lapply(data,\(x)x+1)
172
203
173
204
```r
174
205
# conditional_statements : compare
175
-
if(condition){action} else{alternative}
206
+
if(condition){action()} else{action()}
176
207
177
208
if(
178
-
long_condition||
209
+
condition||
179
210
other_condition){
180
211
action()
181
212
}
182
213
```
183
214
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
215
**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
216
188
217
### Loops and Control Flow
@@ -198,31 +227,29 @@ for(item in collection) process(item)
198
227
199
228
```r
200
229
# while_loops : compare
201
-
while(condition) action()
230
+
while(condition) process()
202
231
```
203
232
204
233
**Repeat loops** also enforce braced blocks:
205
234
206
235
```r
207
236
# repeat_loops : compare
208
-
repeataction()
237
+
repeatprocess()
209
238
```
210
239
211
240
**Loop condition formatting**: Complex conditions that span multiple lines receive proper indentation within the parentheses.
212
241
213
242
### Parenthesized Expressions
214
243
215
-
Parenthesized expressions maintain their layout with smart formatting for readability:
244
+
Parenthesized expressions receive proper spacing for operators:
216
245
217
246
```r
218
247
# parenthesized_expressions : compare
219
-
(x+y*z)
220
-
(long_expression+
221
-
other_part)
248
+
(
249
+
expression+
250
+
other_part)
222
251
```
223
252
224
-
**Parenthesis hugging**: If the content fits on one line, parentheses hug the content. For multiline content, proper indentation is applied.
225
-
226
253
### String Literals
227
254
228
255
String literals receive intelligent quote normalization. The formatter prefers double quotes (`"`) unless the string contains unescaped double quotes:
@@ -259,15 +286,15 @@ Unary operators receive appropriate spacing based on their type and context:
259
286
```r
260
287
# unary_operators : compare
261
288
result=!condition
262
-
number=-42
263
-
formula=~response+predictor
289
+
value=-42
290
+
formula=~x+y
264
291
```
265
292
266
293
**Special spacing rule**: The `~` (formula) operator gets a space when followed by complex expressions, but not when followed by simple identifiers.
267
294
268
295
## Format Suppression
269
296
270
-
You can disable formatting for specific code sections using the `# fmt: skip` comment directive:
297
+
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
298
272
299
```r
273
300
# format_suppression : format
@@ -280,15 +307,19 @@ matrix(
280
307
nrow=2
281
308
) # This code won't be reformatted
282
309
310
+
# fmt: skip
283
311
matrix(c(1, 2,
284
-
3, 4), nrow=2) # fmt: skip
285
-
# The line above won't be reformatted
312
+
3, 4), nrow=2) # The line above won't be reformatted
286
313
```
287
314
315
+
Without the `fmt: skip` directive, the `matrix(...)` expression would be broken into multiple lines according to standard formatting rules.
316
+
288
317
The `fmt: skip` directive can be placed:
289
318
- Before a line to skip formatting that entire expression
290
319
- At the end of a line to skip formatting just that line
291
320
321
+
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.
322
+
292
323
## Advanced Formatting Features
293
324
294
325
The formatter intelligently handles various R idioms and special patterns:
@@ -341,7 +372,6 @@ PersonClass <- R6Class(
341
372
-**Switch statements**: Fallthrough cases (`case = ,`) are handled correctly
342
373
-**Multi-line strings**: String literal structure is preserved
343
374
-**Formula objects**: Proper spacing around `~` operator based on complexity
@@ -351,7 +381,7 @@ The formatter automatically adds braces to control flow structures when they imp
351
381
352
382
```r
353
383
# auto_bracing_function_defintions : compare
354
-
f<-function(x)
384
+
process<-function(x)
355
385
x+1
356
386
```
357
387
@@ -360,7 +390,7 @@ f <- function(x)
360
390
```r
361
391
# auto_bracing_conditional_statements : compare
362
392
if (condition)
363
-
single_statement
393
+
action()
364
394
```
365
395
366
396
**Loops**: All loop bodies are automatically braced for consistency:
@@ -373,7 +403,7 @@ for (i in 1:n)
373
403
374
404
## Hugging Behavior
375
405
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.
406
+
"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
407
378
408
**Nested function calls** can be formatted in a hugged style:
379
409
@@ -392,6 +422,20 @@ result <- outer(
392
422
)
393
423
```
394
424
425
+
**Parenthesized expressions** can also use hugging:
426
+
427
+
```r
428
+
# hugging_parenthesized : format
429
+
(expression+
430
+
other_part)
431
+
432
+
# Also allowed
433
+
(
434
+
expression+
435
+
other_part
436
+
)
437
+
```
438
+
395
439
**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:
396
440
397
441
```r
@@ -401,29 +445,6 @@ call(
401
445
b=y, c=z)
402
446
```
403
447
404
-
**Mixed line formats** are allowed when the last argument starts on the same line:
405
-
406
-
```r
407
-
# mixed_line_format : format
408
-
# This format is preserved - last argument starts on same line
409
-
call(a=x, b=y, c= inner(
410
-
expr
411
-
))
412
-
```
413
-
414
-
This behavior is particularly useful for testing frameworks and S4 method definitions:
415
-
416
-
```r
417
-
# test_that_and_s4_example: format
418
-
test_that("description", {
419
-
expect_equal(result, expected)
420
-
})
421
-
422
-
setMethod("show", "MyClass", function(object) {
423
-
cat("MyClass object\n")
424
-
})
425
-
```
426
-
427
448
## Line Endings
428
449
429
450
The formatter automatically detects and preserves the line ending style (`LF` or `CRLF`) used in the original file.
Copy file name to clipboardExpand all lines: crates/roughly/tests/snapshots/test_format__documentation_examples__auto_bracing_conditional_statements.snap
0 commit comments