Skip to content

Commit d13c5df

Browse files
Simplify examples, remove redundancy, fix string literals and unary operators
Co-authored-by: felix-andreas <[email protected]>
1 parent 63b8175 commit d13c5df

File tree

1 file changed

+49
-152
lines changed

1 file changed

+49
-152
lines changed

docs/content/formatter.md

Lines changed: 49 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -167,30 +167,44 @@ complex_call(
167167

168168
**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.
169169

170+
**Nested function calls** are formatted with hugging when appropriate:
171+
172+
```r
173+
# Before formatting
174+
result <- outer(inner(a,b),other(c,d))
175+
176+
# After formatting
177+
result <- outer(inner(a, b), other(c, d))
178+
```
179+
170180
### Function Definitions
171181

172182
Function definitions follow consistent formatting rules for parameters and body structure:
173183

174184
```r
175185
# Before formatting
176-
calculate_stats<-function(data,method="mean",trim=0){
177-
process_data(data,method,trim)
186+
calc<-function(x,y=1){x+y}
187+
stats<-function(data,method="mean"){
188+
result(data,method)
178189
}
179190

180191
# After formatting
181-
calculate_stats <- function(data, method = "mean", trim = 0) {
182-
process_data(data, method, trim)
192+
calc <- function(x, y = 1) x + y
193+
stats <- function(data, method = "mean") {
194+
result(data, method)
183195
}
184196
```
185197

198+
**Single line functions**: When the function body is a single expression, braces can be omitted if the expression starts on the same line. **Multi-line functions**: Always receive braces, even when the body starts on the same line as the function declaration.
199+
186200
**Anonymous functions** (lambda expressions) are also properly formatted:
187201

188202
```r
189203
# Before formatting
190-
apply(matrix,1,\(row)sum(row,na.rm=TRUE))
204+
lapply(data,\(x)x+1)
191205

192206
# After formatting
193-
apply(matrix, 1, \(row) sum(row, na.rm = TRUE))
207+
lapply(data, \(x) x + 1)
194208
```
195209

196210
**Body formatting**: If a function definition spans multiple lines or has a multiline condition, non-braced bodies are automatically wrapped in braces for consistency.
@@ -202,18 +216,18 @@ apply(matrix, 1, \(row) sum(row, na.rm = TRUE))
202216
```r
203217
# Before formatting
204218
if(condition){action} else{alternative}
205-
if(very_long_condition_that_spans_multiple_lines||
206-
another_condition){
207-
complex_action()
219+
if(long_condition ||
220+
other_condition){
221+
action()
208222
}
209223

210224
# After formatting
211225
if (condition) { action } else { alternative }
212226
if (
213-
very_long_condition_that_spans_multiple_lines ||
214-
another_condition
227+
long_condition ||
228+
other_condition
215229
) {
216-
complex_action()
230+
action()
217231
}
218232
```
219233

@@ -228,17 +242,11 @@ if (
228242
```r
229243
# Before formatting
230244
for(item in collection) process(item)
231-
for(i in 1:length(data)){
232-
calculate(data[i])
233-
}
234245

235246
# After formatting
236247
for (item in collection) {
237248
process(item)
238249
}
239-
for (i in 1:length(data)) {
240-
calculate(data[i])
241-
}
242250
```
243251

244252
**While loops** follow similar block enforcement rules:
@@ -274,71 +282,47 @@ Parenthesized expressions maintain their layout with smart formatting for readab
274282
```r
275283
# Before formatting
276284
(x+y*z)
277-
(very_long_expression+
278-
another_part)
285+
(long_expression+
286+
other_part)
279287

280288
# After formatting
281289
(x + y * z)
282290
(
283-
very_long_expression +
284-
another_part
291+
long_expression +
292+
other_part
285293
)
286294
```
287295

288296
**Parenthesis hugging**: If the content fits on one line, parentheses hug the content. For multiline content, proper indentation is applied.
289297

290298
### String Literals
291299

292-
String literals receive intelligent quote normalization:
300+
String literals receive intelligent quote normalization. The formatter prefers double quotes (`"`) unless the string contains unescaped double quotes:
293301

294302
```r
295303
# Before formatting
296-
message <- "Hello world"
304+
message <- 'Hello world'
297305
quoted_content <- 'Say "hello"'
298306

299307
# After formatting
300308
message <- "Hello world"
301309
quoted_content <- 'Say "hello"'
302310
```
303311

304-
**Quote selection**: The formatter prefers single quotes unless the string content contains unescaped double quotes. This helps avoid unnecessary escaping while maintaining readability.
305-
306312
### Subsetting and Member Access
307313

308314
**Bracket subsetting** follows the same formatting rules as function calls:
309315

310316
```r
311317
# Before formatting
312318
data[row_index,column_index]
313-
matrix[i=1,j=2,drop=FALSE]
314-
315-
# After formatting
316-
data[row_index, column_index]
317-
matrix[i = 1, j = 2, drop = FALSE]
318-
```
319-
320-
**Double bracket subsetting** for list/environment access:
321-
322-
```r
323-
# Before formatting
324319
environment[["variable_name"]]
325-
nested_list[[key1]][[key2]]
326-
327-
# After formatting
328-
environment[["variable_name"]]
329-
nested_list[[key1]][[key2]]
330-
```
331-
332-
**Member access operators** (`$` and `@`):
333-
334-
```r
335-
# Before formatting
336320
object$member_variable
337-
s4_object@slot_name
338321

339322
# After formatting
323+
data[row_index, column_index]
324+
environment[["variable_name"]]
340325
object$member_variable
341-
s4_object@slot_name
342326
```
343327

344328
**Namespace operators** (`::` and `:::`):
@@ -359,9 +343,9 @@ Unary operators receive appropriate spacing based on their type and context:
359343

360344
```r
361345
# Before formatting
362-
result=!condition
363-
number=-42
364-
formula=~response+predictor
346+
result = ! condition
347+
number = - 42
348+
formula = ~ response + predictor
365349

366350
# After formatting
367351
result = !condition
@@ -371,100 +355,26 @@ formula = ~ response + predictor
371355

372356
**Special spacing rule**: The `~` (formula) operator gets a space when followed by complex expressions, but not when followed by simple identifiers.
373357

374-
## Compound Expressions
375-
376-
Roughly excels at formatting complex, nested expressions while maintaining readability. Here are some examples of how compound expressions are handled:
377-
378-
### Chained Operations
379-
380-
**Pipeline chains** receive consistent indentation:
381-
382-
```r
383-
# Before formatting
384-
data %>%
385-
filter(status=="active") %>%
386-
group_by(category) %>%
387-
summarize(total=sum(amount,na.rm=TRUE)) %>%
388-
arrange(desc(total))
389-
390-
# After formatting
391-
data %>%
392-
filter(status == "active") %>%
393-
group_by(category) %>%
394-
summarize(total = sum(amount, na.rm = TRUE)) %>%
395-
arrange(desc(total))
396-
```
397-
398-
### Nested Function Calls
399-
400-
**Complex nesting** maintains proper indentation levels:
401-
402-
```r
403-
# Before formatting
404-
result<-calculate(
405-
transform(data,new_column=apply(matrix,2,function(column){
406-
mean(column,na.rm=TRUE)
407-
})),
408-
method="robust"
409-
)
410-
411-
# After formatting
412-
result <- calculate(
413-
transform(
414-
data,
415-
new_column = apply(matrix, 2, function(column) {
416-
mean(column, na.rm = TRUE)
417-
})
418-
),
419-
method = "robust"
420-
)
421-
```
422-
423-
### Mixed Expression Types
424-
425-
**Combinations of different constructs** are handled intelligently:
426-
427-
```r
428-
# Before formatting
429-
if(length(data)>0){
430-
process_results<-lapply(split(data,data$group),function(subset){
431-
if(nrow(subset)>min_size){
432-
calculate_stats(subset$values)
433-
} else {
434-
NULL
435-
}
436-
})
437-
}
438-
439-
# After formatting
440-
if (length(data) > 0) {
441-
process_results <- lapply(split(data, data$group), function(subset) {
442-
if (nrow(subset) > min_size) {
443-
calculate_stats(subset$values)
444-
} else {
445-
NULL
446-
}
447-
})
448-
}
449-
```
450-
451358
## Format Suppression
452359

453360
You can disable formatting for specific code sections using the `# fmt: skip` comment directive:
454361

455362
```r
456363
# fmt: skip
457-
data_table <- data.frame(
458-
column1=c(1,2,3),
459-
column2=c("a","b","c")
460-
)
364+
matrix(
365+
c(
366+
1, 2,
367+
3, 4
368+
),
369+
nrow=2
370+
) # This code won't be reformatted
461371

462-
processed_data <- clean_data(data_table) # This will be formatted
372+
matrix(c(1, 2,
373+
3, 4), nrow = 2) # This code will be formatted
463374

464-
results <- calculate(
465-
data=processed_data,
466-
method="custom"
467-
) # fmt: skip
375+
matrix(c(1, 2,
376+
3, 4), nrow=2) # fmt: skip
377+
# The line above won't be reformatted
468378
```
469379

470380
The `fmt: skip` directive can be placed:
@@ -510,19 +420,6 @@ Semicolon-separated expressions receive appropriate formatting:
510420
{ initialize(); process(); cleanup() }
511421
```
512422

513-
### Data Structure Access
514-
Complex subsetting patterns are handled gracefully:
515-
516-
```r
517-
# Before formatting
518-
multi_dim_array[,,index,drop=FALSE]
519-
nested_access$level1[["level2"]]@slot
520-
521-
# After formatting
522-
multi_dim_array[, , index, drop = FALSE]
523-
nested_access$level1[["level2"]]@slot
524-
```
525-
526423
### R6 Class Definitions
527424
Class definitions with empty lines between methods are preserved:
528425

0 commit comments

Comments
 (0)