diff --git a/NEWS.md b/NEWS.md index 183dcdbfd..79a96352e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -16,29 +16,32 @@ [definition for aligned function calls](https://styler.r-lib.org/articles/detect-alignment.html) (#537). -* curlyl-curly (`{{`) syntactic sugar introduced with rlang 0.4.0 is now +* curly-curly (`{{`) syntactic sugar introduced with rlang 0.4.0 is now explicitly handled, as opposed previously where it was just treated as two - consequtive curly braces (#528). + consecutive curly braces (#528). * `style_pkg()`, `style_dir()` and the Addins can now style `.Rprofile`, and hidden files are now also styled (#530). ## Minor improvements and fixes -* brace expressions in function calls are formatted in a less compact way. This - improves the formatting of `tryCatch()` in many cases (#543). - -* escape characters in roxygen code examples are now correctly escaped (#512). +* Brace expressions in function calls are formatted in a less compact way to + improve readability. Typical use case: `tryCatch()` (#543). -* style selection Addin now preserves line break when the last line selected is +* Arguments in function declarations in a context which is indented multiple + times should now be correct. This typically affects `R6::R6Class()` (#546). + +* Escape characters in roxygen code examples are now correctly escaped (#512). + +* Style selection Addin now preserves line break when the last line selected is an entire line (#520). -* style file Addin can now properly handle cancelling (#511). +* Style file Addin can now properly handle cancelling (#511). * The body of a multi-line function declaration is now indented correctly for `strict = FALSE` and also wrapped in curly braces for `strict = TRUE` (#536). -* advice for contributors in `CONTRIBUTING.md` was updated (#508). +* Advice for contributors in `CONTRIBUTING.md` was updated (#508). ## Adaption diff --git a/R/visit.R b/R/visit.R index a79fd82f2..4b3a668b0 100644 --- a/R/visit.R +++ b/R/visit.R @@ -74,7 +74,9 @@ context_to_terminals <- function(pd_nested, } pd_transformed <- context_towards_terminals( - pd_nested, outer_lag_newlines, outer_indent, outer_spaces, outer_indention_refs + pd_nested, + outer_lag_newlines, outer_indent, + outer_spaces, outer_indention_refs ) pd_transformed$child <- pmap( @@ -110,7 +112,11 @@ context_towards_terminals <- function(pd_nested, outer_indent, outer_spaces, outer_indention_refs) { - pd_nested$indent <- pd_nested$indent + outer_indent + pd_nested$indent <- pd_nested$indent + ifelse( + is.na(pd_nested$indention_ref_pos_id), + outer_indent, + 0 + ) ref_pos_id_is_na <- !is.na(pd_nested$indention_ref_pos_id) pd_nested$indention_ref_pos_id[!ref_pos_id_is_na] <- outer_indention_refs pd_nested$lag_newlines[1] <- pd_nested$lag_newlines[1] + outer_lag_newlines diff --git a/tests/testthat/indention_operators/eq_sub_complex_indention-in.R b/tests/testthat/indention_operators/eq_sub_complex_indention-in.R index fc01907d8..b2e301665 100644 --- a/tests/testthat/indention_operators/eq_sub_complex_indention-in.R +++ b/tests/testthat/indention_operators/eq_sub_complex_indention-in.R @@ -6,3 +6,27 @@ call(a = 5, b ) + +# multiple nested levels +{ + v <- function(x = + 122, + y) { + } +} + + +{ + v <- function(x = 122, + y) { + } +} + +MyClass <- R6::R6Class( + "MyClass", + public = list(initialize = function(my_arg, + my_named_arg = 1) { + return(invisible()) + } + ), +) diff --git a/tests/testthat/indention_operators/eq_sub_complex_indention-in_tree b/tests/testthat/indention_operators/eq_sub_complex_indention-in_tree index e0aa7cf38..6a104d82d 100644 --- a/tests/testthat/indention_operators/eq_sub_complex_indention-in_tree +++ b/tests/testthat/indention_operators/eq_sub_complex_indention-in_tree @@ -1,25 +1,113 @@ -ROOT (token: short_text [lag_newlines/spaces] {pos_id}) - ¦--expr: [0/0] {1} - ¦ ¦--expr: [0/0] {3} - ¦ ¦ °--SYMBOL_FUNCTION_CALL: call [0/0] {2} - ¦ ¦--'(': ( [0/0] {4} - ¦ ¦--SYMBOL_SUB: a [0/1] {5} - ¦ ¦--EQ_SUB: = [0/7] {6} - ¦ ¦--expr: [1/0] {8} - ¦ ¦ °--NUM_CONST: 5 [0/0] {7} - ¦ ¦--',': , [0/5] {9} - ¦ ¦--expr: [1/0] {11} - ¦ ¦ °--SYMBOL: b [0/0] {10} - ¦ °--')': ) [0/0] {12} - °--expr: [2/0] {13} - ¦--expr: [0/0] {15} - ¦ °--SYMBOL_FUNCTION_CALL: call [0/0] {14} - ¦--'(': ( [0/0] {16} - ¦--SYMBOL_SUB: a [0/1] {17} - ¦--EQ_SUB: = [0/7] {18} - ¦--expr: [1/0] {20} - ¦ °--NUM_CONST: 5 [0/0] {19} - ¦--',': , [0/5] {21} - ¦--expr: [1/5] {23} - ¦ °--SYMBOL: b [0/0] {22} - °--')': ) [1/0] {24} +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--expr: [0/0] {1} + ¦ ¦--expr: [0/0] {3} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: call [0/0] {2} + ¦ ¦--'(': ( [0/0] {4} + ¦ ¦--SYMBOL_SUB: a [0/1] {5} + ¦ ¦--EQ_SUB: = [0/7] {6} + ¦ ¦--expr: [1/0] {8} + ¦ ¦ °--NUM_CONST: 5 [0/0] {7} + ¦ ¦--',': , [0/5] {9} + ¦ ¦--expr: [1/0] {11} + ¦ ¦ °--SYMBOL: b [0/0] {10} + ¦ °--')': ) [0/0] {12} + ¦--expr: [2/0] {13} + ¦ ¦--expr: [0/0] {15} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: call [0/0] {14} + ¦ ¦--'(': ( [0/0] {16} + ¦ ¦--SYMBOL_SUB: a [0/1] {17} + ¦ ¦--EQ_SUB: = [0/7] {18} + ¦ ¦--expr: [1/0] {20} + ¦ ¦ °--NUM_CONST: 5 [0/0] {19} + ¦ ¦--',': , [0/5] {21} + ¦ ¦--expr: [1/5] {23} + ¦ ¦ °--SYMBOL: b [0/0] {22} + ¦ °--')': ) [1/0] {24} + ¦--COMMENT: # mul [2/0] {25} + ¦--expr: [1/0] {26} + ¦ ¦--'{': { [0/2] {27} + ¦ ¦--expr: [1/0] {28} + ¦ ¦ ¦--expr: [0/1] {30} + ¦ ¦ ¦ °--SYMBOL: v [0/0] {29} + ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {31} + ¦ ¦ °--expr: [0/0] {32} + ¦ ¦ ¦--FUNCTION: funct [0/0] {33} + ¦ ¦ ¦--'(': ( [0/0] {34} + ¦ ¦ ¦--SYMBOL_FORMALS: x [0/1] {35} + ¦ ¦ ¦--EQ_FORMALS: = [0/2] {36} + ¦ ¦ ¦--expr: [1/0] {38} + ¦ ¦ ¦ °--NUM_CONST: 122 [0/0] {37} + ¦ ¦ ¦--',': , [0/2] {39} + ¦ ¦ ¦--SYMBOL_FORMALS: y [1/0] {40} + ¦ ¦ ¦--')': ) [0/1] {41} + ¦ ¦ °--expr: [0/0] {42} + ¦ ¦ ¦--'{': { [0/7] {43} + ¦ ¦ °--'}': } [1/0] {44} + ¦ °--'}': } [1/0] {45} + ¦--expr: [3/0] {46} + ¦ ¦--'{': { [0/8] {47} + ¦ ¦--expr: [1/0] {48} + ¦ ¦ ¦--expr: [0/1] {50} + ¦ ¦ ¦ °--SYMBOL: v [0/0] {49} + ¦ ¦ ¦--LEFT_ASSIGN: <- [0/1] {51} + ¦ ¦ °--expr: [0/0] {52} + ¦ ¦ ¦--FUNCTION: funct [0/0] {53} + ¦ ¦ ¦--'(': ( [0/0] {54} + ¦ ¦ ¦--SYMBOL_FORMALS: x [0/1] {55} + ¦ ¦ ¦--EQ_FORMALS: = [0/1] {56} + ¦ ¦ ¦--expr: [0/0] {58} + ¦ ¦ ¦ °--NUM_CONST: 122 [0/0] {57} + ¦ ¦ ¦--',': , [0/22] {59} + ¦ ¦ ¦--SYMBOL_FORMALS: y [1/0] {60} + ¦ ¦ ¦--')': ) [0/1] {61} + ¦ ¦ °--expr: [0/0] {62} + ¦ ¦ ¦--'{': { [0/8] {63} + ¦ ¦ °--'}': } [1/0] {64} + ¦ °--'}': } [1/0] {65} + °--expr: [2/0] {66} + ¦--expr: [0/1] {68} + ¦ °--SYMBOL: MyCla [0/0] {67} + ¦--LEFT_ASSIGN: <- [0/1] {69} + °--expr: [0/0] {70} + ¦--expr: [0/0] {71} + ¦ ¦--SYMBOL_PACKAGE: R6 [0/0] {72} + ¦ ¦--NS_GET: :: [0/0] {73} + ¦ °--SYMBOL_FUNCTION_CALL: R6Cla [0/0] {74} + ¦--'(': ( [0/8] {75} + ¦--expr: [1/0] {77} + ¦ °--STR_CONST: "MyCl [0/0] {76} + ¦--',': , [0/8] {78} + ¦--SYMBOL_SUB: publi [1/1] {79} + ¦--EQ_SUB: = [0/1] {80} + ¦--expr: [0/0] {81} + ¦ ¦--expr: [0/0] {83} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: list [0/0] {82} + ¦ ¦--'(': ( [0/0] {84} + ¦ ¦--SYMBOL_SUB: initi [0/1] {85} + ¦ ¦--EQ_SUB: = [0/1] {86} + ¦ ¦--expr: [0/8] {87} + ¦ ¦ ¦--FUNCTION: funct [0/0] {88} + ¦ ¦ ¦--'(': ( [0/0] {89} + ¦ ¦ ¦--SYMBOL_FORMALS: my_ar [0/0] {90} + ¦ ¦ ¦--',': , [0/44] {91} + ¦ ¦ ¦--SYMBOL_FORMALS: my_na [1/1] {92} + ¦ ¦ ¦--EQ_FORMALS: = [0/1] {93} + ¦ ¦ ¦--expr: [0/0] {95} + ¦ ¦ ¦ °--NUM_CONST: 1 [0/0] {94} + ¦ ¦ ¦--')': ) [0/1] {96} + ¦ ¦ °--expr: [0/0] {97} + ¦ ¦ ¦--'{': { [0/16] {98} + ¦ ¦ ¦--expr: [1/8] {99} + ¦ ¦ ¦ ¦--expr: [0/0] {101} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: retur [0/0] {100} + ¦ ¦ ¦ ¦--'(': ( [0/0] {102} + ¦ ¦ ¦ ¦--expr: [0/0] {103} + ¦ ¦ ¦ ¦ ¦--expr: [0/0] {105} + ¦ ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: invis [0/0] {104} + ¦ ¦ ¦ ¦ ¦--'(': ( [0/0] {106} + ¦ ¦ ¦ ¦ °--')': ) [0/0] {107} + ¦ ¦ ¦ °--')': ) [0/0] {108} + ¦ ¦ °--'}': } [1/0] {109} + ¦ °--')': ) [1/0] {110} + ¦--',': , [0/0] {111} + °--')': ) [1/0] {112} diff --git a/tests/testthat/indention_operators/eq_sub_complex_indention-out.R b/tests/testthat/indention_operators/eq_sub_complex_indention-out.R index 1eae09a4c..34e4f8687 100644 --- a/tests/testthat/indention_operators/eq_sub_complex_indention-out.R +++ b/tests/testthat/indention_operators/eq_sub_complex_indention-out.R @@ -6,3 +6,27 @@ call(a = 5, b ) + +# multiple nested levels +{ + v <- function(x = + 122, + y) { + } +} + + +{ + v <- function(x = 122, + y) { + } +} + +MyClass <- R6::R6Class( + "MyClass", + public = list(initialize = function(my_arg, + my_named_arg = 1) { + return(invisible()) + } + ), +)