From 98af12431a6928e52b45b4be1a7a47674410d91d Mon Sep 17 00:00:00 2001 From: Lorenz Date: Fri, 30 Apr 2021 14:43:43 +0200 Subject: [PATCH 1/4] recognize alignment as well with = and comma ignored --- R/detect-alignment.R | 6 +++--- tests/testthat/alignment/tribble-out.R | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/detect-alignment.R b/R/detect-alignment.R index e16bf87ea..3bc5b1286 100644 --- a/R/detect-alignment.R +++ b/R/detect-alignment.R @@ -93,7 +93,7 @@ token_is_on_aligned_line <- function(pd_flat) { n_cols <- map_int(pd_by_line, ~ sum(.x$token == "','")) previous_line <- 0 - start_eval <- ifelse(all(alignment_col1_is_named(pd_by_line)), 1, 2) + start_eval <- ifelse(alignment_col1_is_named(pd_by_line), 1, 2) for (column in seq2(1, max(n_cols))) { by_line <- alignment_serialize_column(pd_by_line, column) %>% compact() %>% @@ -109,8 +109,8 @@ token_is_on_aligned_line <- function(pd_flat) { } is_aligned <- length(unique(current_col)) == 1L - if (!is_aligned && !any(pd_flat$token == "EQ_SUB")) { - # check 2: by character after comma, e.g. tribble. Must not have = + if (!is_aligned) { + # check 2: by character after comma, e.g. tribble. current_col <- nchar(gsub("^(,[\\s\\t]*)[^ ]*", "\\1", by_line, perl = TRUE)) if (column > 1) { diff --git a/tests/testthat/alignment/tribble-out.R b/tests/testthat/alignment/tribble-out.R index 36eab57bb..2e618487e 100644 --- a/tests/testthat/alignment/tribble-out.R +++ b/tests/testthat/alignment/tribble-out.R @@ -26,5 +26,5 @@ tribble( # has EQ_SUB which don't match, not tribble-like mlr3misc:::rowwise_table( x = 23, zy = 3, - y = 1, k = 1, + y = 1, k = 1, ) From f6939f8d8ca3f0a15d967426d325677a6e59c855 Mon Sep 17 00:00:00 2001 From: Lorenz Date: Fri, 30 Apr 2021 15:42:58 +0200 Subject: [PATCH 2/4] check more regex --- NEWS.md | 26 +++-- R/detect-alignment-utils.R | 2 +- R/detect-alignment.R | 10 +- tests/testthat/alignment/named-in.R | 12 +++ tests/testthat/alignment/named-in_tree | 127 +++++++++++++++++++------ tests/testthat/alignment/named-out.R | 12 +++ vignettes/detect-alignment.Rmd | 66 ++++++------- 7 files changed, 176 insertions(+), 79 deletions(-) diff --git a/NEWS.md b/NEWS.md index 4e72d6721..2d791c07f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -18,21 +18,31 @@ ) ``` -* similarly, `tibble::tribble()`-like alignment for column > 1 is now detected - when left aligned (#785). +* similarly, left aligned after comma is now detected (#785). ``` # previously detected + call( + x = 12345, "It's old", + y2 = 17, "before" + ) + tribble( - ~x, ~d, - "axa'fa", 1:6, - "b", 422231 + ~x, ~y, + "another", 1:3, + "b", 1211234 ) # newly detected + call( + x = 2, p = "another", + y = "hhjkjkbew", x = 3 + ) + + tribble( - ~x, ~d, - "axa'fa", 1:6, - "b", 422231 + ~x, ~y, + "another", 1:3, + "b", 1211234 ) ``` Also see `vignette("detect-alignment")`. diff --git a/R/detect-alignment-utils.R b/R/detect-alignment-utils.R index c3b4c766b..cc4c32103 100644 --- a/R/detect-alignment-utils.R +++ b/R/detect-alignment-utils.R @@ -75,7 +75,7 @@ alignment_ensure_trailing_comma <- function(pd_by_line) { #' excluding first and last column. #' @importFrom purrr map_lgl #' @keywords internal -alignment_col1_is_named <- function(relevant_pd_by_line) { +alignment_col1_all_named <- function(relevant_pd_by_line) { map_lgl(relevant_pd_by_line, function(x) { if (nrow(x) < 3) { return(FALSE) diff --git a/R/detect-alignment.R b/R/detect-alignment.R index 3bc5b1286..2143ac5cb 100644 --- a/R/detect-alignment.R +++ b/R/detect-alignment.R @@ -93,7 +93,7 @@ token_is_on_aligned_line <- function(pd_flat) { n_cols <- map_int(pd_by_line, ~ sum(.x$token == "','")) previous_line <- 0 - start_eval <- ifelse(alignment_col1_is_named(pd_by_line), 1, 2) + start_eval <- ifelse(alignment_col1_all_named(pd_by_line), 1, 2) for (column in seq2(1, max(n_cols))) { by_line <- alignment_serialize_column(pd_by_line, column) %>% compact() %>% @@ -110,14 +110,18 @@ token_is_on_aligned_line <- function(pd_flat) { is_aligned <- length(unique(current_col)) == 1L if (!is_aligned) { - # check 2: by character after comma, e.g. tribble. - current_col <- nchar(gsub("^(,[\\s\\t]*)[^ ]*", "\\1", by_line, perl = TRUE)) + # check 2: left aligned after , + current_col <- nchar(gsub("^(,[\\s\\t]*)[^ ]*.*$", "\\1", by_line, perl = TRUE)) if (column > 1) { # must add previous columns, as first column might not align current_col <- current_col + previous_line } is_aligned <- length(unique(current_col)) == 1L + if (is_aligned) { + # if left aligned after , + start_eval <- 2 + } } if (is_aligned) { previous_line <- previous_line + nchar(by_line) diff --git a/tests/testthat/alignment/named-in.R b/tests/testthat/alignment/named-in.R index fa2a7c19a..1349ffae6 100644 --- a/tests/testthat/alignment/named-in.R +++ b/tests/testthat/alignment/named-in.R @@ -209,3 +209,15 @@ xgle( 1212, 232, f(n = 2), 1, 2, "kFlya" ) + +# left aligned after , +call( + x = 2, y = "another", + y = "hhjkjkbew", x = 3 +) + +call( + k = ff("pk"), k = 3, + b = f(-g), 22 + 1, + 44, 323 +) diff --git a/tests/testthat/alignment/named-in_tree b/tests/testthat/alignment/named-in_tree index 4367e9c41..90283b1c8 100644 --- a/tests/testthat/alignment/named-in_tree +++ b/tests/testthat/alignment/named-in_tree @@ -866,32 +866,101 @@ ROOT (token: short_text [lag_newlines/spaces] {pos_id}) ¦ ¦ °--NUM_CONST: 1 [0/0] {862} ¦ ¦--',': , [0/0] {864} ¦ °--')': ) [1/0] {865} - °--expr: xgle( [2/0] {866} - ¦--expr: xgle [0/0] {868} - ¦ °--SYMBOL_FUNCTION_CALL: xgle [0/0] {867} - ¦--'(': ( [0/2] {869} - ¦--expr: 1212 [1/0] {871} - ¦ °--NUM_CONST: 1212 [0/0] {870} - ¦--',': , [0/1] {872} - ¦--expr: 232 [0/0] {874} - ¦ °--NUM_CONST: 232 [0/0] {873} - ¦--',': , [0/1] {875} - ¦--expr: f(n = [0/0] {876} - ¦ ¦--expr: f [0/0] {878} - ¦ ¦ °--SYMBOL_FUNCTION_CALL: f [0/0] {877} - ¦ ¦--'(': ( [0/0] {879} - ¦ ¦--SYMBOL_SUB: n [0/1] {880} - ¦ ¦--EQ_SUB: = [0/1] {881} - ¦ ¦--expr: 2 [0/0] {883} - ¦ ¦ °--NUM_CONST: 2 [0/0] {882} - ¦ °--')': ) [0/0] {884} - ¦--',': , [0/2] {885} - ¦--expr: 1 [1/0] {887} - ¦ °--NUM_CONST: 1 [0/0] {886} - ¦--',': , [0/6] {888} - ¦--expr: 2 [0/0] {890} - ¦ °--NUM_CONST: 2 [0/0] {889} - ¦--',': , [0/2] {891} - ¦--expr: "kFly [0/0] {893} - ¦ °--STR_CONST: "kFly [0/0] {892} - °--')': ) [1/0] {894} + ¦--expr: xgle( [2/0] {866} + ¦ ¦--expr: xgle [0/0] {868} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: xgle [0/0] {867} + ¦ ¦--'(': ( [0/2] {869} + ¦ ¦--expr: 1212 [1/0] {871} + ¦ ¦ °--NUM_CONST: 1212 [0/0] {870} + ¦ ¦--',': , [0/1] {872} + ¦ ¦--expr: 232 [0/0] {874} + ¦ ¦ °--NUM_CONST: 232 [0/0] {873} + ¦ ¦--',': , [0/1] {875} + ¦ ¦--expr: f(n = [0/0] {876} + ¦ ¦ ¦--expr: f [0/0] {878} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: f [0/0] {877} + ¦ ¦ ¦--'(': ( [0/0] {879} + ¦ ¦ ¦--SYMBOL_SUB: n [0/1] {880} + ¦ ¦ ¦--EQ_SUB: = [0/1] {881} + ¦ ¦ ¦--expr: 2 [0/0] {883} + ¦ ¦ ¦ °--NUM_CONST: 2 [0/0] {882} + ¦ ¦ °--')': ) [0/0] {884} + ¦ ¦--',': , [0/2] {885} + ¦ ¦--expr: 1 [1/0] {887} + ¦ ¦ °--NUM_CONST: 1 [0/0] {886} + ¦ ¦--',': , [0/6] {888} + ¦ ¦--expr: 2 [0/0] {890} + ¦ ¦ °--NUM_CONST: 2 [0/0] {889} + ¦ ¦--',': , [0/2] {891} + ¦ ¦--expr: "kFly [0/0] {893} + ¦ ¦ °--STR_CONST: "kFly [0/0] {892} + ¦ °--')': ) [1/0] {894} + ¦--COMMENT: # lef [2/0] {895} + ¦--expr: call( [1/0] {896} + ¦ ¦--expr: call [0/0] {898} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: call [0/0] {897} + ¦ ¦--'(': ( [0/2] {899} + ¦ ¦--SYMBOL_SUB: x [1/1] {900} + ¦ ¦--EQ_SUB: = [0/1] {901} + ¦ ¦--expr: 2 [0/0] {903} + ¦ ¦ °--NUM_CONST: 2 [0/0] {902} + ¦ ¦--',': , [0/11] {904} + ¦ ¦--SYMBOL_SUB: y [0/1] {905} + ¦ ¦--EQ_SUB: = [0/1] {906} + ¦ ¦--expr: "anot [0/0] {908} + ¦ ¦ °--STR_CONST: "anot [0/0] {907} + ¦ ¦--',': , [0/2] {909} + ¦ ¦--SYMBOL_SUB: y [1/1] {910} + ¦ ¦--EQ_SUB: = [0/1] {911} + ¦ ¦--expr: "hhjk [0/0] {913} + ¦ ¦ °--STR_CONST: "hhjk [0/0] {912} + ¦ ¦--',': , [0/1] {914} + ¦ ¦--SYMBOL_SUB: x [0/1] {915} + ¦ ¦--EQ_SUB: = [0/1] {916} + ¦ ¦--expr: 3 [0/0] {918} + ¦ ¦ °--NUM_CONST: 3 [0/0] {917} + ¦ °--')': ) [1/0] {919} + °--expr: call( [2/0] {920} + ¦--expr: call [0/0] {922} + ¦ °--SYMBOL_FUNCTION_CALL: call [0/0] {921} + ¦--'(': ( [0/2] {923} + ¦--SYMBOL_SUB: k [1/1] {924} + ¦--EQ_SUB: = [0/1] {925} + ¦--expr: ff("p [0/0] {926} + ¦ ¦--expr: ff [0/0] {928} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: ff [0/0] {927} + ¦ ¦--'(': ( [0/0] {929} + ¦ ¦--expr: "pk" [0/0] {931} + ¦ ¦ °--STR_CONST: "pk" [0/0] {930} + ¦ °--')': ) [0/0] {932} + ¦--',': , [0/1] {933} + ¦--SYMBOL_SUB: k [0/1] {934} + ¦--EQ_SUB: = [0/1] {935} + ¦--expr: 3 [0/0] {937} + ¦ °--NUM_CONST: 3 [0/0] {936} + ¦--',': , [0/2] {938} + ¦--SYMBOL_SUB: b [1/1] {939} + ¦--EQ_SUB: = [0/1] {940} + ¦--expr: f(-g) [0/0] {941} + ¦ ¦--expr: f [0/0] {943} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: f [0/0] {942} + ¦ ¦--'(': ( [0/0] {944} + ¦ ¦--expr: -g [0/0] {945} + ¦ ¦ ¦--'-': - [0/0] {946} + ¦ ¦ °--expr: g [0/0] {948} + ¦ ¦ °--SYMBOL: g [0/0] {947} + ¦ °--')': ) [0/0] {949} + ¦--',': , [0/4] {950} + ¦--expr: 22 + [0/0] {951} + ¦ ¦--expr: 22 [0/1] {953} + ¦ ¦ °--NUM_CONST: 22 [0/0] {952} + ¦ ¦--'+': + [0/1] {954} + ¦ °--expr: 1 [0/0] {956} + ¦ °--NUM_CONST: 1 [0/0] {955} + ¦--',': , [0/2] {957} + ¦--expr: 44 [1/0] {959} + ¦ °--NUM_CONST: 44 [0/0] {958} + ¦--',': , [0/11] {960} + ¦--expr: 323 [0/0] {962} + ¦ °--NUM_CONST: 323 [0/0] {961} + °--')': ) [1/0] {963} diff --git a/tests/testthat/alignment/named-out.R b/tests/testthat/alignment/named-out.R index ca3343595..7fda760a3 100644 --- a/tests/testthat/alignment/named-out.R +++ b/tests/testthat/alignment/named-out.R @@ -207,3 +207,15 @@ xgle( 1212, 232, f(n = 2), 1, 2, "kFlya" ) + +# left aligned after , +call( + x = 2, y = "another", + y = "hhjkjkbew", x = 3 +) + +call( + k = ff("pk"), k = 3, + b = f(-g), 22 + 1, + 44, 323 +) diff --git a/vignettes/detect-alignment.Rmd b/vignettes/detect-alignment.Rmd index b99631dd0..a8c8b2b12 100644 --- a/vignettes/detect-alignment.Rmd +++ b/vignettes/detect-alignment.Rmd @@ -49,23 +49,24 @@ spacing around operators and commas. ```{r} tibble::tribble( - ~key_here, ~value_here, - "left", "right", # comments are allowed - "long string", "shrt" # columns can overlap ('~' above ',') + ~key_here, ~right_aligned, + "left", "right", # comments are allowed + "long string", "shrt" # columns can overlap ('~' above ',') ) tibble::tribble( - ~key_here, ~value_here, + ~key_here, ~left_aligned, "left", "right", # comments are allowed "long string", "shrt" # columns can overlap ('~' above ',') ) - +# right-aligned after = purrr::map(x, fun, # arguments on same line as opening brace are not considered arg2 = 2, ar = f(k, x) ) +# left aligned after = purrr::map(x, fun, # arguments on same line as opening brace are not considered arg2 = 2, ar = f(k, x) @@ -84,18 +85,19 @@ unnamed: ```{r} call( # column 1 | column 2 | - abkj = f(2), 7, - more_ = "a", 2 # more + abkj = f(2), 7, # | row 1 + more_ = "a", 2 # | row 2 ) ``` +**For alignment detection, the first column is omitted if not all arguments +in that column are named** + ## Function calls Below, we try to explain in an intuitive way how your code should look like to be recognized as aligned. -**If all arguments in the first column are named**: - Make commas match position vertically and align everything right before commas: ```{r} @@ -114,6 +116,13 @@ fell( y = 23, # nothing in column 2 for row 2 zz = NULL, finally = "stuff" ) + +# or if not all arguments of the first column are named +gell( + p = 2, g = gg(x), n = 3 * 3, # + 31, fds = -1, gz = f / 3, +) + ``` ... or match position of `=` vertically and align everything after this operator @@ -137,49 +146,30 @@ fell( y = 23, # nothing in column 2 for row 2 zz = NULL, finally = "stuff" ) -``` - - -**If not all arguments of the first column are named:**^[In the below example, -the first argument of the first column is named (`p = 2`). The second argument -of the first column is not (`31`).] The same as above, but the first column is -ignored. - -```{r} -# not all arguments of first column named, hence, all (except the first -# column's) commas must match position -gell( - p = 2, g = gg(x), n = 3 * 3, # - 31, fds = -1, gz = f / 3, -) -# or all (except the first column's) `=` must match position +# or if not all arguments of the first column are named gell( p = 2, g = gg(x), n = 3 * 3, # 31, fds = -1, gz = f / 3 + 1, ) ``` -**If the function call is tribble-like** + +... or match the start of the token after `,` ```{r} -# you can right align, then it's the last case discussed -tibble::tribble( - ~x, ~d, - "axa'fa", 1:6, - "b", 422231 +call( + x = 2, p = "another", + y = "hhjkjkbew", x = 3 ) -# or you left align, then all tokens after commas must match in position -tibble::tribble( - ~x, ~d, - "axa'fa", 1:6, - "b", 4:6 +tribble( + ~x, ~y, + "another", 1:3, + "b", 1211234 ) ``` - - ## Comments not supported yet. From d765e54a5fef9d691cf3059d23ecbd00275c5c33 Mon Sep 17 00:00:00 2001 From: Lorenz Date: Fri, 30 Apr 2021 15:47:07 +0200 Subject: [PATCH 3/4] bump ver --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7e3172006..4504ecb24 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: styler Title: Non-Invasive Pretty Printing of R Code -Version: 1.4.1.9002 +Version: 1.4.1.9003 Authors@R: c(person(given = "Kirill", family = "Müller", From f8a81d9206ef254163d8a3e5856fb613dca1fa65 Mon Sep 17 00:00:00 2001 From: Lorenz Date: Fri, 30 Apr 2021 15:58:58 +0200 Subject: [PATCH 4/4] roxygenize --- R/detect-alignment-utils.R | 4 ++-- ...ignment_col1_is_named.Rd => alignment_col1_all_named.Rd} | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) rename man/{alignment_col1_is_named.Rd => alignment_col1_all_named.Rd} (76%) diff --git a/R/detect-alignment-utils.R b/R/detect-alignment-utils.R index cc4c32103..cb86f4bb5 100644 --- a/R/detect-alignment-utils.R +++ b/R/detect-alignment-utils.R @@ -90,7 +90,7 @@ alignment_col1_all_named <- function(relevant_pd_by_line) { #' Serialize all lines for a given column #' @param column The index of the column to serialize. -#' @inheritParams alignment_col1_is_named +#' @inheritParams alignment_col1_all_named #' @importFrom purrr map #' @keywords internal alignment_serialize_column <- function(relevant_pd_by_line, column) { @@ -101,7 +101,7 @@ alignment_serialize_column <- function(relevant_pd_by_line, column) { #' #' #' @inheritParams alignment_serialize_column -#' @inheritParams alignment_col1_is_named +#' @inheritParams alignment_col1_all_named #' @keywords internal alignment_serialize_line <- function(relevant_pd_by_line, column) { # TODO diff --git a/man/alignment_col1_is_named.Rd b/man/alignment_col1_all_named.Rd similarity index 76% rename from man/alignment_col1_is_named.Rd rename to man/alignment_col1_all_named.Rd index 7ce7c9e47..ba03141f1 100644 --- a/man/alignment_col1_is_named.Rd +++ b/man/alignment_col1_all_named.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/detect-alignment-utils.R -\name{alignment_col1_is_named} -\alias{alignment_col1_is_named} +\name{alignment_col1_all_named} +\alias{alignment_col1_all_named} \title{Checks if all arguments of column 1 are named} \usage{ -alignment_col1_is_named(relevant_pd_by_line) +alignment_col1_all_named(relevant_pd_by_line) } \arguments{ \item{relevant_pd_by_line}{A list with parse tables of a multi-line call,