Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single indent the long "flat" form of function definitions #24

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, itll reflect this after tidyverse/style#223 is merged

(#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"/>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lionel- we will need you to regenerate this svg video thing


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