diff --git a/NEWS.md b/NEWS.md index dc6b65b2d..bdd0a9d35 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # styler 1.6.2.9000 (Development version) +* Alignment detection respects stylerignore (#850). # styler 1.6.2 diff --git a/R/detect-alignment.R b/R/detect-alignment.R index 2143ac5cb..49093011f 100644 --- a/R/detect-alignment.R +++ b/R/detect-alignment.R @@ -49,6 +49,10 @@ token_is_on_aligned_line <- function(pd_flat) { pd_flat$lag_newlines <- pd_flat$pos_id <- NULL pd_flat$.lag_spaces <- lag(pd_flat$spaces) pd_by_line <- split(pd_flat, line_idx) + pd_by_line[purrr::map_lgl(pd_by_line, ~ any(.x$stylerignore))] <- NULL + if (length(pd_by_line) < 1) { + return(TRUE) + } last_line_is_closing_brace_only <- nrow(last(pd_by_line)) == 1 relevant_idx <- seq2(2, ifelse(last_line_is_closing_brace_only, length(pd_by_line) - 1, @@ -86,7 +90,11 @@ token_is_on_aligned_line <- function(pd_flat) { } pd_by_line <- alignment_drop_comments(pd_by_line) %>% - alignment_ensure_no_closing_brace(last_line_is_closing_brace_only) %>% + alignment_ensure_no_closing_brace(last_line_is_closing_brace_only) + if (length(pd_by_line) < 1) { + return(TRUE) + } + pd_by_line <- pd_by_line %>% alignment_ensure_trailing_comma() # now, pd only contains arguments separated by values, ideal for iterating # over columns. diff --git a/tests/testthat/stylerignore/alignment-in.R b/tests/testthat/stylerignore/alignment-in.R new file mode 100644 index 000000000..b128cbac5 --- /dev/null +++ b/tests/testthat/stylerignore/alignment-in.R @@ -0,0 +1,5 @@ +ps( + interaction_constraints = p_uty(tgs = "train"), + monotone_constraints = p_uty(dfault = 0, tags = c("train", "control"), custom_check = function(x) { checkmate::check_integerish(x, lower = -1, upper = 1, any.missing = FALSE) }), # styler: off + normalize_type = p_fct(c("tee", "forest"), default = "tree", tags = "train"), +) diff --git a/tests/testthat/stylerignore/alignment-in_tree b/tests/testthat/stylerignore/alignment-in_tree new file mode 100644 index 000000000..18ae34374 --- /dev/null +++ b/tests/testthat/stylerignore/alignment-in_tree @@ -0,0 +1,110 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + °--expr: ps( + [0/0] {1} + ¦--expr: ps [0/0] {3} + ¦ °--SYMBOL_FUNCTION_CALL: ps [0/0] {2} + ¦--'(': ( [0/2] {4} + ¦--SYMBOL_SUB: inter [1/1] {5} + ¦--EQ_SUB: = [0/1] {6} + ¦--expr: p_uty [0/0] {7} + ¦ ¦--expr: p_uty [0/0] {9} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: p_uty [0/0] {8} + ¦ ¦--'(': ( [0/0] {10} + ¦ ¦--SYMBOL_SUB: tgs [0/1] {11} + ¦ ¦--EQ_SUB: = [0/1] {12} + ¦ ¦--expr: "trai [0/0] {14} + ¦ ¦ °--STR_CONST: "trai [0/0] {13} + ¦ °--')': ) [0/0] {15} + ¦--',': , [0/2] {16} + ¦--SYMBOL_SUB: monot [1/4] {17} + ¦--EQ_SUB: = [0/1] {18} + ¦--expr: p_uty [0/0] {19} + ¦ ¦--expr: p_uty [0/0] {21} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: p_uty [0/0] {20} + ¦ ¦--'(': ( [0/0] {22} + ¦ ¦--SYMBOL_SUB: dfaul [0/1] {23} + ¦ ¦--EQ_SUB: = [0/1] {24} + ¦ ¦--expr: 0 [0/0] {26} + ¦ ¦ °--NUM_CONST: 0 [0/0] {25} + ¦ ¦--',': , [0/1] {27} + ¦ ¦--SYMBOL_SUB: tags [0/1] {28} + ¦ ¦--EQ_SUB: = [0/1] {29} + ¦ ¦--expr: c("tr [0/0] {30} + ¦ ¦ ¦--expr: c [0/0] {32} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: c [0/0] {31} + ¦ ¦ ¦--'(': ( [0/0] {33} + ¦ ¦ ¦--expr: "trai [0/0] {35} + ¦ ¦ ¦ °--STR_CONST: "trai [0/0] {34} + ¦ ¦ ¦--',': , [0/1] {36} + ¦ ¦ ¦--expr: "cont [0/0] {38} + ¦ ¦ ¦ °--STR_CONST: "cont [0/0] {37} + ¦ ¦ °--')': ) [0/0] {39} + ¦ ¦--',': , [0/1] {40} + ¦ ¦--SYMBOL_SUB: custo [0/1] {41} + ¦ ¦--EQ_SUB: = [0/1] {42} + ¦ ¦--expr: funct [0/0] {43} + ¦ ¦ ¦--FUNCTION: funct [0/0] {44} + ¦ ¦ ¦--'(': ( [0/0] {45} + ¦ ¦ ¦--SYMBOL_FORMALS: x [0/0] {46} + ¦ ¦ ¦--')': ) [0/1] {47} + ¦ ¦ °--expr: { ch [0/0] {48} + ¦ ¦ ¦--'{': { [0/2] {49} + ¦ ¦ ¦--expr: check [0/1] {50} + ¦ ¦ ¦ ¦--expr: check [0/0] {51} + ¦ ¦ ¦ ¦ ¦--SYMBOL_PACKAGE: check [0/0] {52} + ¦ ¦ ¦ ¦ ¦--NS_GET: :: [0/0] {53} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: check [0/0] {54} + ¦ ¦ ¦ ¦--'(': ( [0/0] {55} + ¦ ¦ ¦ ¦--expr: x [0/0] {57} + ¦ ¦ ¦ ¦ °--SYMBOL: x [0/0] {56} + ¦ ¦ ¦ ¦--',': , [0/1] {58} + ¦ ¦ ¦ ¦--SYMBOL_SUB: lower [0/1] {59} + ¦ ¦ ¦ ¦--EQ_SUB: = [0/1] {60} + ¦ ¦ ¦ ¦--expr: -1 [0/0] {61} + ¦ ¦ ¦ ¦ ¦--'-': - [0/0] {62} + ¦ ¦ ¦ ¦ °--expr: 1 [0/0] {64} + ¦ ¦ ¦ ¦ °--NUM_CONST: 1 [0/0] {63} + ¦ ¦ ¦ ¦--',': , [0/1] {65} + ¦ ¦ ¦ ¦--SYMBOL_SUB: upper [0/1] {66} + ¦ ¦ ¦ ¦--EQ_SUB: = [0/1] {67} + ¦ ¦ ¦ ¦--expr: 1 [0/0] {69} + ¦ ¦ ¦ ¦ °--NUM_CONST: 1 [0/0] {68} + ¦ ¦ ¦ ¦--',': , [0/1] {70} + ¦ ¦ ¦ ¦--SYMBOL_SUB: any.m [0/1] {71} + ¦ ¦ ¦ ¦--EQ_SUB: = [0/1] {72} + ¦ ¦ ¦ ¦--expr: FALSE [0/0] {74} + ¦ ¦ ¦ ¦ °--NUM_CONST: FALSE [0/0] {73} + ¦ ¦ ¦ °--')': ) [0/0] {75} + ¦ ¦ °--'}': } [0/0] {76} + ¦ °--')': ) [0/0] {77} + ¦--',': , [0/1] {78} + ¦--COMMENT: # sty [0/2] {79} + ¦--SYMBOL_SUB: norma [1/10] {80} + ¦--EQ_SUB: = [0/1] {81} + ¦--expr: p_fct [0/0] {82} + ¦ ¦--expr: p_fct [0/0] {84} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: p_fct [0/0] {83} + ¦ ¦--'(': ( [0/0] {85} + ¦ ¦--expr: c("te [0/0] {86} + ¦ ¦ ¦--expr: c [0/0] {88} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: c [0/0] {87} + ¦ ¦ ¦--'(': ( [0/0] {89} + ¦ ¦ ¦--expr: "tee" [0/0] {91} + ¦ ¦ ¦ °--STR_CONST: "tee" [0/0] {90} + ¦ ¦ ¦--',': , [0/1] {92} + ¦ ¦ ¦--expr: "fore [0/0] {94} + ¦ ¦ ¦ °--STR_CONST: "fore [0/0] {93} + ¦ ¦ °--')': ) [0/0] {95} + ¦ ¦--',': , [0/1] {96} + ¦ ¦--SYMBOL_SUB: defau [0/1] {97} + ¦ ¦--EQ_SUB: = [0/1] {98} + ¦ ¦--expr: "tree [0/0] {100} + ¦ ¦ °--STR_CONST: "tree [0/0] {99} + ¦ ¦--',': , [0/1] {101} + ¦ ¦--SYMBOL_SUB: tags [0/1] {102} + ¦ ¦--EQ_SUB: = [0/1] {103} + ¦ ¦--expr: "trai [0/0] {105} + ¦ ¦ °--STR_CONST: "trai [0/0] {104} + ¦ °--')': ) [0/0] {106} + ¦--',': , [0/0] {107} + °--')': ) [1/0] {108} diff --git a/tests/testthat/stylerignore/alignment-out.R b/tests/testthat/stylerignore/alignment-out.R new file mode 100644 index 000000000..b128cbac5 --- /dev/null +++ b/tests/testthat/stylerignore/alignment-out.R @@ -0,0 +1,5 @@ +ps( + interaction_constraints = p_uty(tgs = "train"), + monotone_constraints = p_uty(dfault = 0, tags = c("train", "control"), custom_check = function(x) { checkmate::check_integerish(x, lower = -1, upper = 1, any.missing = FALSE) }), # styler: off + normalize_type = p_fct(c("tee", "forest"), default = "tree", tags = "train"), +) diff --git a/tests/testthat/test-stylerignore.R b/tests/testthat/test-stylerignore.R index c3c1af875..bf3950b4e 100644 --- a/tests/testthat/test-stylerignore.R +++ b/tests/testthat/test-stylerignore.R @@ -120,3 +120,9 @@ test_that("no token added or removed in complex case", { transformer = style_text ), NA) }) + +test_that("stylerignore sequences are respected in alignment detection", { + expect_warning(test_collection("stylerignore", "alignment", + transformer = style_text + ), NA) +})