Skip to content

Commit

Permalink
Single indent the long "flat" form of function definitions (#24)
Browse files Browse the repository at this point in the history
* Use single indent not double indent in long flat form

* NEWS file and bullet
  • Loading branch information
DavisVaughan authored Oct 3, 2024
1 parent 1d4a809 commit a8c298b
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 57 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Suggests:
rstudioapi,
testthat (>= 3.0.0)
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Config/testthat/edition: 3
URL: https://github.com/lionel-/codegrip
BugReports: https://github.com/lionel-/codegrip/issues
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# codegrip (development version)

* The long "flat" form for function definitions now aligns arguments using a
single indent rather than a double indent, which follows the
[tidyverse style guide](https://style.tidyverse.org/functions.html#long-lines-1)
(#20).
2 changes: 1 addition & 1 deletion R/reshape-addin.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#' Note that for function definitions, `addin_reshape()` cycles through two
#' different long shapes. The traditional L form uses more horizontal space
#' whereas the flat form uses less horizontal space and the arguments are
#' always aligned at double indent:
#' always aligned at single indent:
#' ```
#' foo <- function(a, b, c) {
#' NULL
Expand Down
10 changes: 2 additions & 8 deletions R/reshape-call.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@ node_call_longer <- function(node, ..., L = FALSE, info) {
if (prefix) {
body <- node_text(node_call_body(node), info = info)
suffix <- paste0(" ", body)
indent_factor <- 2
} else {
suffix <- ""
indent_factor <- 1
}

fn <- node_text(set[[1]], info = info)
Expand All @@ -82,11 +80,7 @@ node_call_longer <- function(node, ..., L = FALSE, info) {
new_indent_n <- xml_col2(left_paren)
} else {
fn <- paste0(fn, left_paren_text, "\n")
if (prefix) {
new_indent_n <- current_indent_n + base_indent * indent_factor
} else {
new_indent_n <- current_indent_n + base_indent
}
new_indent_n <- current_indent_n + base_indent
}

if (L) {
Expand Down Expand Up @@ -125,7 +119,7 @@ node_call_longer <- function(node, ..., L = FALSE, info) {
if (L) {
arg_indent_n <- new_indent_n - current_indent_n - arg_parent_indent_n
} else {
arg_indent_n <- base_indent * indent_factor - arg_parent_indent_n
arg_indent_n <- base_indent - arg_parent_indent_n
}

if (length(arg)) {
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ codegrip provides [RStudio addins](http://rstudio.github.io/rstudioaddins/) and

<img src="man/figures/README/reshape-call.svg"/>

Note that for function definitions, `addin_reshape` cycles through two different long shapes. The traditional L form uses more horizontal space whereas the flat form uses less horizontal space and the arguments are always aligned at double indent:
Note that for function definitions, `addin_reshape` cycles through two different long shapes. The traditional L form uses more horizontal space whereas the flat form uses less horizontal space and the arguments are always aligned at single indent:

<img src="man/figures/README/reshape-def.svg"/>

Expand Down Expand Up @@ -61,7 +61,7 @@ Not yet implemented:

## Using in Visual Studio Code

`addin_reshape` is available for keybinding in VS Code. See [here](https://github.com/REditorSupport/vscode-R/wiki/RStudio-addin-support#enabling-rstudio-addin-support) for instructions on enabling general addin support.
`addin_reshape` is available for keybinding in VS Code. See [here](https://github.com/REditorSupport/vscode-R/wiki/RStudio-addin-support#enabling-rstudio-addin-support) for instructions on enabling general addin support.

Once addins are enabled, add the following to `keybindings.json`:

Expand Down
2 changes: 1 addition & 1 deletion man/addin_reshape.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 36 additions & 36 deletions tests/testthat/_snaps/reshape-call.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
)
function(
a
a
) NULL
Code
print_longer("(b, c)")
Expand All @@ -25,8 +25,8 @@
)
function(
b,
c
b,
c
) NULL
Code
print_longer("(a, b, c)")
Expand All @@ -38,9 +38,9 @@
)
function(
a,
b,
c
a,
b,
c
) NULL
Code
print_longer("(a = 1, b, c = 3)")
Expand All @@ -52,9 +52,9 @@
)
function(
a = 1,
b,
c = 3
a = 1,
b,
c = 3
) NULL
Code
# Leading indentation is preserved. First line is not indented
Expand All @@ -73,7 +73,7 @@
)
function(
a
a
) NULL
Code
print_longer(" (a, b)")
Expand All @@ -84,8 +84,8 @@
)
function(
a,
b
a,
b
) NULL
Code
# Multiline args are indented as is
Expand All @@ -100,11 +100,11 @@
)
function(
a,
b = foo(
bar
),
c
a,
b = foo(
bar
),
c
) NULL
Code
print_longer("(a, b =\n 2, c)")
Expand All @@ -117,10 +117,10 @@
)
function(
a,
b =
2,
c
a,
b =
2,
c
) NULL
Code
print_longer(" (a, b = foo(\n bar \n ), c)")
Expand All @@ -134,11 +134,11 @@
)
function(
a,
b = foo(
bar
),
c
a,
b = foo(
bar
),
c
) NULL
Code
# Wrong indentation is preserved
Expand All @@ -153,11 +153,11 @@
)
function(
a,
b = foo(
bar
),
c
a,
b = foo(
bar
),
c
) NULL
Code
print_longer(" (a, b = foo(\n bar\n), c)")
Expand All @@ -171,11 +171,11 @@
)
function(
a,
b = foo(
bar
),
c
a,
b = foo(
bar
),
c
) NULL

# can reshape call longer (L shape)
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/_snaps/reshape.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@
Output
i: 1
function(
a
a
) NULL
i: 2
function(a) NULL
i: 3
function(
a
a
) NULL
Code
Expand All @@ -86,9 +86,9 @@
i: 2
function(
a,
b = 1,
c
a,
b = 1,
c
) NULL
i: 3
Expand All @@ -108,7 +108,7 @@
Output
i: 1
if (
a
a
) NULL
i: 2
Expand All @@ -120,7 +120,7 @@
Output
i: 1
if (
a
a
) b else c
i: 2
Expand All @@ -132,7 +132,7 @@
Output
i: 1
while (
a
a
) NULL
i: 2
Expand Down

0 comments on commit a8c298b

Please sign in to comment.