From 1477a3e38def3e1cd4faa756a638b577006cb15c Mon Sep 17 00:00:00 2001 From: lorenzwalthert Date: Mon, 5 Aug 2019 19:23:51 +0200 Subject: [PATCH] ignore mulit-line for / while condition without braces for strict = FALSE --- R/indent.R | 11 +- ...hile_for_if_without_curly_non_strict-out.R | 12 +- ...or_without_curly_same_line_non_strict-in.R | 31 ++++ ...without_curly_same_line_non_strict-in_tree | 157 ++++++++++++++++++ ...r_without_curly_same_line_non_strict-out.R | 31 ++++ tests/testthat/test-indention_operators.R | 3 + 6 files changed, 229 insertions(+), 16 deletions(-) create mode 100644 tests/testthat/indention_operators/while_for_without_curly_same_line_non_strict-in.R create mode 100644 tests/testthat/indention_operators/while_for_without_curly_same_line_non_strict-in_tree create mode 100644 tests/testthat/indention_operators/while_for_without_curly_same_line_non_strict-out.R diff --git a/R/indent.R b/R/indent.R index 78a27c566..ae8ab40f6 100644 --- a/R/indent.R +++ b/R/indent.R @@ -95,16 +95,7 @@ indent_without_paren_for_while_fun <- function(pd, indent_by) { if (is_curly_expr(pd$child[[nrow]])) { return(pd) } - if (pd$token[1] == "FOR") { - is_multi_line <- pd_is_multi_line( - pd$child[[which(pd$token == "forcond")]] - ) - } else if (pd$token[1] %in% c("WHILE", "FUNCTION")) { - start <- which(pd$token == "'('") - end <- which(pd$token == "')'") - is_multi_line <- any(pd[seq2(start, end),]$multi_line) - } - if (pd$token[1] %in% tokens && !is_multi_line) { + if (pd$token[1] %in% tokens) { other_trigger_tokens <- c( math_token, logical_token, diff --git a/tests/testthat/indention_operators/while_for_if_without_curly_non_strict-out.R b/tests/testthat/indention_operators/while_for_if_without_curly_non_strict-out.R index 00490d0db..c4fadaafb 100644 --- a/tests/testthat/indention_operators/while_for_if_without_curly_non_strict-out.R +++ b/tests/testthat/indention_operators/while_for_if_without_curly_non_strict-out.R @@ -12,17 +12,17 @@ for (i in 1:3) # for (i in 1:3) # - print(i) +print(i) for (i in # 1:3) # - print(i) +print(i) for ( # i in # 1:3 # ) # - print(i) +print(i) while (x > 3) # @@ -40,19 +40,19 @@ while ( 2 > # here 3 # ) # - FALSE +FALSE while ( 2 > # here 3 # ) - FALSE +FALSE while ( 2 > # here 3 ) # - FALSE +FALSE while ( # 2 > diff --git a/tests/testthat/indention_operators/while_for_without_curly_same_line_non_strict-in.R b/tests/testthat/indention_operators/while_for_without_curly_same_line_non_strict-in.R new file mode 100644 index 000000000..b26d46bc8 --- /dev/null +++ b/tests/testthat/indention_operators/while_for_without_curly_same_line_non_strict-in.R @@ -0,0 +1,31 @@ +while(x == 2) h( + 2 +) + +while(x == 2) h( # comment + 2 +) + +while(x == 2 && + 2 + 2 == 2) h( + 2 +) + + +for(x in 1:22) h( + 2 +) + +for(x in 1:22) h( # comment + 2 +) + +for(k in f( + 2:22 +)) h( + 2 + ) + +for(k in f( + 2:22 # comment +)) h(2) diff --git a/tests/testthat/indention_operators/while_for_without_curly_same_line_non_strict-in_tree b/tests/testthat/indention_operators/while_for_without_curly_same_line_non_strict-in_tree new file mode 100644 index 000000000..6311c2398 --- /dev/null +++ b/tests/testthat/indention_operators/while_for_without_curly_same_line_non_strict-in_tree @@ -0,0 +1,157 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--expr: [0/0] {1} + ¦ ¦--WHILE: while [0/0] {2} + ¦ ¦--'(': ( [0/0] {3} + ¦ ¦--expr: [0/0] {4} + ¦ ¦ ¦--expr: [0/1] {6} + ¦ ¦ ¦ °--SYMBOL: x [0/0] {5} + ¦ ¦ ¦--EQ: == [0/1] {7} + ¦ ¦ °--expr: [0/0] {9} + ¦ ¦ °--NUM_CONST: 2 [0/0] {8} + ¦ ¦--')': ) [0/1] {10} + ¦ °--expr: [0/0] {11} + ¦ ¦--expr: [0/0] {13} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: h [0/0] {12} + ¦ ¦--'(': ( [0/2] {14} + ¦ ¦--expr: [1/0] {16} + ¦ ¦ °--NUM_CONST: 2 [0/0] {15} + ¦ °--')': ) [1/0] {17} + ¦--expr: [2/0] {18} + ¦ ¦--WHILE: while [0/0] {19} + ¦ ¦--'(': ( [0/0] {20} + ¦ ¦--expr: [0/0] {21} + ¦ ¦ ¦--expr: [0/1] {23} + ¦ ¦ ¦ °--SYMBOL: x [0/0] {22} + ¦ ¦ ¦--EQ: == [0/1] {24} + ¦ ¦ °--expr: [0/0] {26} + ¦ ¦ °--NUM_CONST: 2 [0/0] {25} + ¦ ¦--')': ) [0/1] {27} + ¦ °--expr: [0/0] {28} + ¦ ¦--expr: [0/0] {30} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: h [0/0] {29} + ¦ ¦--'(': ( [0/1] {31} + ¦ ¦--COMMENT: # com [0/2] {32} + ¦ ¦--expr: [1/0] {34} + ¦ ¦ °--NUM_CONST: 2 [0/0] {33} + ¦ °--')': ) [1/0] {35} + ¦--expr: [2/0] {36} + ¦ ¦--WHILE: while [0/0] {37} + ¦ ¦--'(': ( [0/0] {38} + ¦ ¦--expr: [0/0] {39} + ¦ ¦ ¦--expr: [0/1] {40} + ¦ ¦ ¦ ¦--expr: [0/1] {42} + ¦ ¦ ¦ ¦ °--SYMBOL: x [0/0] {41} + ¦ ¦ ¦ ¦--EQ: == [0/1] {43} + ¦ ¦ ¦ °--expr: [0/0] {45} + ¦ ¦ ¦ °--NUM_CONST: 2 [0/0] {44} + ¦ ¦ ¦--AND2: && [0/6] {46} + ¦ ¦ °--expr: [1/0] {47} + ¦ ¦ ¦--expr: [0/1] {48} + ¦ ¦ ¦ ¦--expr: [0/1] {50} + ¦ ¦ ¦ ¦ °--NUM_CONST: 2 [0/0] {49} + ¦ ¦ ¦ ¦--'+': + [0/1] {51} + ¦ ¦ ¦ °--expr: [0/0] {53} + ¦ ¦ ¦ °--NUM_CONST: 2 [0/0] {52} + ¦ ¦ ¦--EQ: == [0/1] {54} + ¦ ¦ °--expr: [0/0] {56} + ¦ ¦ °--NUM_CONST: 2 [0/0] {55} + ¦ ¦--')': ) [0/1] {57} + ¦ °--expr: [0/0] {58} + ¦ ¦--expr: [0/0] {60} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: h [0/0] {59} + ¦ ¦--'(': ( [0/2] {61} + ¦ ¦--expr: [1/0] {63} + ¦ ¦ °--NUM_CONST: 2 [0/0] {62} + ¦ °--')': ) [1/0] {64} + ¦--expr: [3/0] {65} + ¦ ¦--FOR: for [0/0] {66} + ¦ ¦--forcond: [0/1] {67} + ¦ ¦ ¦--'(': ( [0/0] {68} + ¦ ¦ ¦--SYMBOL: x [0/1] {69} + ¦ ¦ ¦--IN: in [0/1] {70} + ¦ ¦ ¦--expr: [0/0] {71} + ¦ ¦ ¦ ¦--expr: [0/0] {73} + ¦ ¦ ¦ ¦ °--NUM_CONST: 1 [0/0] {72} + ¦ ¦ ¦ ¦--':': : [0/0] {74} + ¦ ¦ ¦ °--expr: [0/0] {76} + ¦ ¦ ¦ °--NUM_CONST: 22 [0/0] {75} + ¦ ¦ °--')': ) [0/0] {77} + ¦ °--expr: [0/0] {78} + ¦ ¦--expr: [0/0] {80} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: h [0/0] {79} + ¦ ¦--'(': ( [0/2] {81} + ¦ ¦--expr: [1/0] {83} + ¦ ¦ °--NUM_CONST: 2 [0/0] {82} + ¦ °--')': ) [1/0] {84} + ¦--expr: [2/0] {85} + ¦ ¦--FOR: for [0/0] {86} + ¦ ¦--forcond: [0/1] {87} + ¦ ¦ ¦--'(': ( [0/0] {88} + ¦ ¦ ¦--SYMBOL: x [0/1] {89} + ¦ ¦ ¦--IN: in [0/1] {90} + ¦ ¦ ¦--expr: [0/0] {91} + ¦ ¦ ¦ ¦--expr: [0/0] {93} + ¦ ¦ ¦ ¦ °--NUM_CONST: 1 [0/0] {92} + ¦ ¦ ¦ ¦--':': : [0/0] {94} + ¦ ¦ ¦ °--expr: [0/0] {96} + ¦ ¦ ¦ °--NUM_CONST: 22 [0/0] {95} + ¦ ¦ °--')': ) [0/0] {97} + ¦ °--expr: [0/0] {98} + ¦ ¦--expr: [0/0] {100} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: h [0/0] {99} + ¦ ¦--'(': ( [0/1] {101} + ¦ ¦--COMMENT: # com [0/2] {102} + ¦ ¦--expr: [1/0] {104} + ¦ ¦ °--NUM_CONST: 2 [0/0] {103} + ¦ °--')': ) [1/0] {105} + ¦--expr: [2/0] {106} + ¦ ¦--FOR: for [0/0] {107} + ¦ ¦--forcond: [0/1] {108} + ¦ ¦ ¦--'(': ( [0/0] {109} + ¦ ¦ ¦--SYMBOL: k [0/1] {110} + ¦ ¦ ¦--IN: in [0/1] {111} + ¦ ¦ ¦--expr: [0/0] {112} + ¦ ¦ ¦ ¦--expr: [0/0] {114} + ¦ ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: f [0/0] {113} + ¦ ¦ ¦ ¦--'(': ( [0/2] {115} + ¦ ¦ ¦ ¦--expr: [1/0] {116} + ¦ ¦ ¦ ¦ ¦--expr: [0/0] {118} + ¦ ¦ ¦ ¦ ¦ °--NUM_CONST: 2 [0/0] {117} + ¦ ¦ ¦ ¦ ¦--':': : [0/0] {119} + ¦ ¦ ¦ ¦ °--expr: [0/0] {121} + ¦ ¦ ¦ ¦ °--NUM_CONST: 22 [0/0] {120} + ¦ ¦ ¦ °--')': ) [1/0] {122} + ¦ ¦ °--')': ) [0/0] {123} + ¦ °--expr: [0/0] {124} + ¦ ¦--expr: [0/0] {126} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: h [0/0] {125} + ¦ ¦--'(': ( [0/8] {127} + ¦ ¦--expr: [1/6] {129} + ¦ ¦ °--NUM_CONST: 2 [0/0] {128} + ¦ °--')': ) [1/0] {130} + °--expr: [2/0] {131} + ¦--FOR: for [0/0] {132} + ¦--forcond: [0/1] {133} + ¦ ¦--'(': ( [0/0] {134} + ¦ ¦--SYMBOL: k [0/1] {135} + ¦ ¦--IN: in [0/1] {136} + ¦ ¦--expr: [0/0] {137} + ¦ ¦ ¦--expr: [0/0] {139} + ¦ ¦ ¦ °--SYMBOL_FUNCTION_CALL: f [0/0] {138} + ¦ ¦ ¦--'(': ( [0/2] {140} + ¦ ¦ ¦--expr: [1/1] {141} + ¦ ¦ ¦ ¦--expr: [0/0] {143} + ¦ ¦ ¦ ¦ °--NUM_CONST: 2 [0/0] {142} + ¦ ¦ ¦ ¦--':': : [0/0] {144} + ¦ ¦ ¦ °--expr: [0/0] {146} + ¦ ¦ ¦ °--NUM_CONST: 22 [0/0] {145} + ¦ ¦ ¦--COMMENT: # com [0/0] {147} + ¦ ¦ °--')': ) [1/0] {148} + ¦ °--')': ) [0/0] {149} + °--expr: [0/0] {150} + ¦--expr: [0/0] {152} + ¦ °--SYMBOL_FUNCTION_CALL: h [0/0] {151} + ¦--'(': ( [0/0] {153} + ¦--expr: [0/0] {155} + ¦ °--NUM_CONST: 2 [0/0] {154} + °--')': ) [0/0] {156} diff --git a/tests/testthat/indention_operators/while_for_without_curly_same_line_non_strict-out.R b/tests/testthat/indention_operators/while_for_without_curly_same_line_non_strict-out.R new file mode 100644 index 000000000..7342282c8 --- /dev/null +++ b/tests/testthat/indention_operators/while_for_without_curly_same_line_non_strict-out.R @@ -0,0 +1,31 @@ +while (x == 2) h( + 2 +) + +while (x == 2) h( # comment + 2 +) + +while (x == 2 && + 2 + 2 == 2) h( + 2 +) + + +for (x in 1:22) h( + 2 +) + +for (x in 1:22) h( # comment + 2 +) + +for (k in f( + 2:22 +)) h( + 2 +) + +for (k in f( + 2:22 # comment +)) h(2) diff --git a/tests/testthat/test-indention_operators.R b/tests/testthat/test-indention_operators.R index ad9da04af..b3013a87e 100644 --- a/tests/testthat/test-indention_operators.R +++ b/tests/testthat/test-indention_operators.R @@ -22,6 +22,9 @@ test_that("while / for / if without curly brackets", { expect_warning(test_collection("indention_operators", "while_for_if_without_curly_non_strict", transformer = style_text, strict = FALSE), NA) + expect_warning(test_collection("indention_operators", + "while_for_without_curly_same_line_non_strict", + transformer = style_text, strict = FALSE), NA) }) test_that("function multiline without curly brackets", {