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
+66-1Lines changed: 66 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,13 +30,78 @@ The formatter follows these key principles:
30
30
31
31
R is an expression-based language with a strong focus on numerical computing and data analysis. Unlike many other programming languages, R code is often written interactively and exploratively, where preserving the original intent and structure of expressions is crucial for readability and debugging.
32
32
33
-
The non-invasive approach means roughly respects your existing line breaks and won't arbitrarily split expressions that you've chosen to keep on one line. This is particularly important in R because:
33
+
The non-invasive approach means roughly respects your existing line breaks and won't arbitrarily split expressions that you've chosen to keep on one line. **Non-invasive formatting tries to minimize the amount of line-breaks not set by the programmer** by following these key principles:
34
+
35
+
-**Single line expressions are never broken into multiple lines** (with the exception of loops like `for`, `while`, `repeat`, because they don't yield useful values and can only perform side effects, so they are not normal expressions in that sense)
36
+
-**Both hugging and not hugging is allowed** for function calls and other constructs
37
+
-**Preserves programmer intent** regarding line structure and formatting choices
38
+
39
+
This is particularly important in R because:
34
40
35
41
-**Data analysis workflows**: Short, expressive one-liners are common and meaningful
36
42
-**Interactive development**: Code is often built incrementally, and forced line breaks can disrupt the flow
37
43
-**Mathematical expressions**: Complex formulas are often more readable when kept compact
38
44
-**Functional style**: R's functional nature benefits from preserving the structure of nested calls
39
45
46
+
#### Why This Matters for Numerical Computing
47
+
48
+
R is a numerical language, and numerical expressions tend to get ugly when broken up by line length limits. Consider this mathematical expression:
49
+
50
+
```r
51
+
# non_invasive_numerical_example : format
52
+
# Without non-invasive formatting, this compact expression:
# Might get broken up into this less readable form:
56
+
# n <- 1 +
57
+
# (length(replacement_id) - 1) *
58
+
# (vectorToSearch == valueTypeToReplace)
59
+
```
60
+
61
+
This forced line breaking can lead to several problems:
62
+
- People might use shorter, less descriptive variable names just to fit expressions on one line
63
+
- Intermediate results might be stored in variables that have no meaningful purpose or don't correspond to established mathematical formulas
64
+
- The mathematical relationship becomes harder to understand
65
+
66
+
#### Consistency Between Related Lines
67
+
68
+
Sometimes you want consistency between multiple consecutive lines, especially when they follow the same pattern but have slight variations. For example, if you have two lines that only differ by a prefix or suffix, it's better to keep them both on single lines for easy comparison:
The same principle applies to switch statements where one arm might have a longer expression. Non-invasive formatting keeps consistency across all arms:
Roughly applies specific formatting rules to different R code constructs. The formatter analyzes the abstract syntax tree to make intelligent decisions about spacing, line breaks, and indentation.
Copy file name to clipboardExpand all lines: docs/content/formatter.md
+64-1Lines changed: 64 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,13 +30,76 @@ The formatter follows these key principles:
30
30
31
31
R is an expression-based language with a strong focus on numerical computing and data analysis. Unlike many other programming languages, R code is often written interactively and exploratively, where preserving the original intent and structure of expressions is crucial for readability and debugging.
32
32
33
-
The non-invasive approach means roughly respects your existing line breaks and won't arbitrarily split expressions that you've chosen to keep on one line. This is particularly important in R because:
33
+
The non-invasive approach means roughly respects your existing line breaks and won't arbitrarily split expressions that you've chosen to keep on one line. **Non-invasive formatting tries to minimize the amount of line-breaks not set by the programmer** by following these key principles:
34
+
35
+
-**Single line expressions are never broken into multiple lines** (with the exception of loops like `for`, `while`, `repeat`, because they don't yield useful values and can only perform side effects, so they are not normal expressions in that sense)
36
+
-**Both hugging and not hugging is allowed** for function calls and other constructs
37
+
-**Preserves programmer intent** regarding line structure and formatting choices
38
+
39
+
This is particularly important in R because:
34
40
35
41
-**Data analysis workflows**: Short, expressive one-liners are common and meaningful
36
42
-**Interactive development**: Code is often built incrementally, and forced line breaks can disrupt the flow
37
43
-**Mathematical expressions**: Complex formulas are often more readable when kept compact
38
44
-**Functional style**: R's functional nature benefits from preserving the structure of nested calls
39
45
46
+
#### Why This Matters for Numerical Computing
47
+
48
+
R is a numerical language, and numerical expressions tend to get ugly when broken up by line length limits. Consider this mathematical expression:
49
+
50
+
```r
51
+
# Without non-invasive formatting, this compact expression:
# Might get broken up into this less readable form:
55
+
# n <- 1 +
56
+
# (length(replacement_id) - 1) *
57
+
# (vectorToSearch == valueTypeToReplace)
58
+
```
59
+
60
+
This forced line breaking can lead to several problems:
61
+
- People might use shorter, less descriptive variable names just to fit expressions on one line
62
+
- Intermediate results might be stored in variables that have no meaningful purpose or don't correspond to established mathematical formulas
63
+
- The mathematical relationship becomes harder to understand
64
+
65
+
#### Consistency Between Related Lines
66
+
67
+
Sometimes you want consistency between multiple consecutive lines, especially when they follow the same pattern but have slight variations. For example, if you have two lines that only differ by a prefix or suffix, it's better to keep them both on single lines for easy comparison:
The same principle applies to switch statements where one arm might have a longer expression. Non-invasive formatting keeps consistency across all arms:
Roughly applies specific formatting rules to different R code constructs. The formatter analyzes the abstract syntax tree to make intelligent decisions about spacing, line breaks, and indentation.
0 commit comments