From 09b12fff793317251c110209db82901c28f21853 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 8 Sep 2017 18:07:39 +0200 Subject: [PATCH 1/6] add docs to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 09a72cbe9..10c71b274 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .Rhistory .RData inst/doc +docs From 7ce6028c8a7f0d58905244a950e823d5ce3e1e67 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Wed, 30 Aug 2017 20:47:38 +0200 Subject: [PATCH 2/6] introduce class vertical for style_text --- DESCRIPTION | 1 + NAMESPACE | 1 + R/vertical.R | 14 ++++++++++++++ R/ws.R | 3 ++- man/construct_vertical.Rd | 15 +++++++++++++++ 5 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 R/vertical.R create mode 100644 man/construct_vertical.Rd diff --git a/DESCRIPTION b/DESCRIPTION index d14b23246..690295a90 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -53,5 +53,6 @@ Collate: 'transform.R' 'unindent.R' 'utils.R' + 'vertical.R' 'visit.R' 'ws.R' diff --git a/NAMESPACE b/NAMESPACE index a13d8eeb6..8bf3e827e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +S3method(print,vertical) export(create_style_guide) export(style_dir) export(style_file) diff --git a/R/vertical.R b/R/vertical.R new file mode 100644 index 000000000..05802df17 --- /dev/null +++ b/R/vertical.R @@ -0,0 +1,14 @@ +#' Construct an object of class vertical +#' +#' Sole puropse of the class vertical is to have a print method that +#' aligns the output vertically. +#' @param x A character vector or an object of class "vertical". +construct_vertical <- function(x) { + stopifnot(inherits(x, what = c("character", "vertical"))) + structure(x, class = "vertical") +} + +#' @export +print.vertical <- function(x, ...) { + cat(x, sep = "\n") +} diff --git a/R/ws.R b/R/ws.R index 2fe6171c3..a40b637de 100644 --- a/R/ws.R +++ b/R/ws.R @@ -80,7 +80,8 @@ style_text <- function(text, transformers = style(...)) { transformer <- make_transformer(transformers) - transformer(text) + styled_text <- transformer(text) + construct_vertical(styled_text) } #' Prettify arbitrary R code diff --git a/man/construct_vertical.Rd b/man/construct_vertical.Rd new file mode 100644 index 000000000..a3f91a592 --- /dev/null +++ b/man/construct_vertical.Rd @@ -0,0 +1,15 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/vertical.R +\name{construct_vertical} +\alias{construct_vertical} +\title{Construct an object of class vertical} +\usage{ +construct_vertical(x) +} +\arguments{ +\item{x}{A character vector or an object of class "vertical".} +} +\description{ +Sole puropse of the class vertical is to have a print method that +aligns the output vertically. +} From 828ecc76173a16883235c3f462c7349599f31f47 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 8 Sep 2017 18:18:09 +0200 Subject: [PATCH 3/6] unclass before writing back --- R/serialized_tests.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/serialized_tests.R b/R/serialized_tests.R index bf95ddb6e..28e9531ba 100644 --- a/R/serialized_tests.R +++ b/R/serialized_tests.R @@ -100,7 +100,8 @@ transform_and_check <- function(in_item, out_item, write.table(out_tree, col.names = FALSE, row.names = FALSE, quote = FALSE) } transformed <- read_in %>% - transformer(...) + transformer(...) %>% + unclass() transformed <- suppressMessages(utf8::transform_lines_enc(out_item, function(x) transformed, write_back = write_back)) From b3233bbaed12d4bb51c256bd8ed90e9bfa688aba Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 8 Sep 2017 18:18:37 +0200 Subject: [PATCH 4/6] change character tests into test_collection() tests --- tests/testthat/strict/eof-in.R | 1 + tests/testthat/strict/eof-in_tree | 3 +++ tests/testthat/strict/eof-out.R | 1 + tests/testthat/strict/eol-in.R | 2 ++ tests/testthat/strict/eol-in_tree | 9 +++++++++ tests/testthat/strict/eol-out.R | 2 ++ tests/testthat/test-strict.R | 11 ++++++++--- 7 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 tests/testthat/strict/eof-in.R create mode 100644 tests/testthat/strict/eof-in_tree create mode 100644 tests/testthat/strict/eof-out.R create mode 100644 tests/testthat/strict/eol-in.R create mode 100644 tests/testthat/strict/eol-in_tree create mode 100644 tests/testthat/strict/eol-out.R diff --git a/tests/testthat/strict/eof-in.R b/tests/testthat/strict/eof-in.R new file mode 100644 index 000000000..d35e62fcf --- /dev/null +++ b/tests/testthat/strict/eof-in.R @@ -0,0 +1 @@ +blabla diff --git a/tests/testthat/strict/eof-in_tree b/tests/testthat/strict/eof-in_tree new file mode 100644 index 000000000..e7fc0c9ff --- /dev/null +++ b/tests/testthat/strict/eof-in_tree @@ -0,0 +1,3 @@ +ROOT (token: short_text [lag_newlines/spaces] {id}) + °--expr: [0/0] {3} + °--SYMBOL: blabl [0/0] {1} diff --git a/tests/testthat/strict/eof-out.R b/tests/testthat/strict/eof-out.R new file mode 100644 index 000000000..d35e62fcf --- /dev/null +++ b/tests/testthat/strict/eof-out.R @@ -0,0 +1 @@ +blabla diff --git a/tests/testthat/strict/eol-in.R b/tests/testthat/strict/eol-in.R new file mode 100644 index 000000000..16c24f266 --- /dev/null +++ b/tests/testthat/strict/eol-in.R @@ -0,0 +1,2 @@ +a() +b # comment diff --git a/tests/testthat/strict/eol-in_tree b/tests/testthat/strict/eol-in_tree new file mode 100644 index 000000000..a8788f31c --- /dev/null +++ b/tests/testthat/strict/eol-in_tree @@ -0,0 +1,9 @@ +ROOT (token: short_text [lag_newlines/spaces] {id}) + ¦--expr: [0/0] {6} + ¦ ¦--expr: [0/0] {3} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: a [0/0] {1} + ¦ ¦--'(': ( [0/0] {2} + ¦ °--')': ) [0/0] {4} + ¦--expr: [1/1] {13} + ¦ °--SYMBOL: b [0/0] {10} + °--COMMENT: # com [0/0] {11} diff --git a/tests/testthat/strict/eol-out.R b/tests/testthat/strict/eol-out.R new file mode 100644 index 000000000..86d6befd5 --- /dev/null +++ b/tests/testthat/strict/eol-out.R @@ -0,0 +1,2 @@ +a() +b # comment diff --git a/tests/testthat/test-strict.R b/tests/testthat/test-strict.R index c4075ca13..4270c9fa5 100644 --- a/tests/testthat/test-strict.R +++ b/tests/testthat/test-strict.R @@ -15,12 +15,17 @@ test_that("can style example source file with strict = FALSE", { }) test_that("removes space at EOL", { - expect_equal(style_text("a() "), "a()") - expect_equal(style_text("a() # comment "), "a() # comment") + expect_warning(test_collection( + "strict", "eol", + transformer = style_text, + strict = FALSE), NA) }) test_that("removes blank lines at EOF", { - expect_equal(style_text(c("a() ", "", "")), "a()") + expect_warning(test_collection( + "strict", "eof", + transformer = style_text, + strict = FALSE), NA) }) From 4d535bc939a1db5ed3f7c404ae48f3bce2223c9f Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 8 Sep 2017 18:22:16 +0200 Subject: [PATCH 5/6] move and adapt some tests move some tests to test-spacing and change them to test_collection() tests --- tests/testthat/spacing/spacing_comma-in.R | 1 + tests/testthat/spacing/spacing_comma-in_tree | 20 ++++++++++++++++++++ tests/testthat/spacing/spacing_comma-out.R | 1 + tests/testthat/spacing/spacing_if-in.R | 1 + tests/testthat/spacing/spacing_if-in_tree | 12 ++++++++++++ tests/testthat/spacing/spacing_if-out.R | 1 + tests/testthat/test-spacing.R | 13 +++++++++++++ tests/testthat/test-strict.R | 10 ---------- 8 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 tests/testthat/spacing/spacing_comma-in.R create mode 100644 tests/testthat/spacing/spacing_comma-in_tree create mode 100644 tests/testthat/spacing/spacing_comma-out.R create mode 100644 tests/testthat/spacing/spacing_if-in.R create mode 100644 tests/testthat/spacing/spacing_if-in_tree create mode 100644 tests/testthat/spacing/spacing_if-out.R diff --git a/tests/testthat/spacing/spacing_comma-in.R b/tests/testthat/spacing/spacing_comma-in.R new file mode 100644 index 000000000..26275c7b2 --- /dev/null +++ b/tests/testthat/spacing/spacing_comma-in.R @@ -0,0 +1 @@ +c( 1, 16 , 333 , 33 , 1) diff --git a/tests/testthat/spacing/spacing_comma-in_tree b/tests/testthat/spacing/spacing_comma-in_tree new file mode 100644 index 000000000..f7e11d38a --- /dev/null +++ b/tests/testthat/spacing/spacing_comma-in_tree @@ -0,0 +1,20 @@ +ROOT (token: short_text [lag_newlines/spaces] {id}) + °--expr: [0/0] {29} + ¦--expr: [0/0] {3} + ¦ °--SYMBOL_FUNCTION_CALL: c [0/0] {1} + ¦--'(': ( [0/4] {2} + ¦--expr: [0/0] {5} + ¦ °--NUM_CONST: 1 [0/0] {4} + ¦--',': , [0/7] {6} + ¦--expr: [0/4] {10} + ¦ °--NUM_CONST: 16 [0/0] {9} + ¦--',': , [0/1] {11} + ¦--expr: [0/1] {15} + ¦ °--NUM_CONST: 333 [0/0] {14} + ¦--',': , [0/1] {16} + ¦--expr: [0/1] {20} + ¦ °--NUM_CONST: 33 [0/0] {19} + ¦--',': , [0/2] {21} + ¦--expr: [0/0] {25} + ¦ °--NUM_CONST: 1 [0/0] {24} + °--')': ) [0/0] {26} diff --git a/tests/testthat/spacing/spacing_comma-out.R b/tests/testthat/spacing/spacing_comma-out.R new file mode 100644 index 000000000..1cb728413 --- /dev/null +++ b/tests/testthat/spacing/spacing_comma-out.R @@ -0,0 +1 @@ +c(1, 16, 333, 33, 1) diff --git a/tests/testthat/spacing/spacing_if-in.R b/tests/testthat/spacing/spacing_if-in.R new file mode 100644 index 000000000..c3545b0c0 --- /dev/null +++ b/tests/testthat/spacing/spacing_if-in.R @@ -0,0 +1 @@ +if(TRUE) x else y diff --git a/tests/testthat/spacing/spacing_if-in_tree b/tests/testthat/spacing/spacing_if-in_tree new file mode 100644 index 000000000..52e4fd13f --- /dev/null +++ b/tests/testthat/spacing/spacing_if-in_tree @@ -0,0 +1,12 @@ +ROOT (token: short_text [lag_newlines/spaces] {id}) + °--expr: [0/0] {15} + ¦--IF: if [0/0] {1} + ¦--'(': ( [0/0] {2} + ¦--expr: [0/0] {4} + ¦ °--NUM_CONST: TRUE [0/0] {3} + ¦--')': ) [0/1] {5} + ¦--expr: [0/1] {9} + ¦ °--SYMBOL: x [0/0] {7} + ¦--ELSE: else [0/1] {8} + °--expr: [0/0] {13} + °--SYMBOL: y [0/0] {11} diff --git a/tests/testthat/spacing/spacing_if-out.R b/tests/testthat/spacing/spacing_if-out.R new file mode 100644 index 000000000..9778ab263 --- /dev/null +++ b/tests/testthat/spacing/spacing_if-out.R @@ -0,0 +1 @@ +if (TRUE) x else y diff --git a/tests/testthat/test-spacing.R b/tests/testthat/test-spacing.R index 1782452c5..540a6c344 100644 --- a/tests/testthat/test-spacing.R +++ b/tests/testthat/test-spacing.R @@ -18,3 +18,16 @@ test_that("comments and strict = FALSE", { "spacing", "comments", transformer = style_text, stric = FALSE), NA) }) + +test_that("Space placed after 'if' and before '('", { + expect_warning(test_collection( + "spacing", "spacing_if", + transformer = style_text), NA) +}) + +test_that("space before comma is removed", { + expect_warning(test_collection( + "spacing", "spacing_comma", + transformer = style_text), NA) +}) + diff --git a/tests/testthat/test-strict.R b/tests/testthat/test-strict.R index 4270c9fa5..331a9cc47 100644 --- a/tests/testthat/test-strict.R +++ b/tests/testthat/test-strict.R @@ -27,13 +27,3 @@ test_that("removes blank lines at EOF", { transformer = style_text, strict = FALSE), NA) }) - - -test_that("Space placed after 'if' and before '('", { - expect_equal(style_text(c("if(TRUE) x else y")), "if (TRUE) x else y") -}) - -test_that("space before comma is removed", { - expect_equal(style_text("c( 1, 16 , 333 , 33 , 1)"), - "c(1, 16, 333, 33, 1)") -}) From bb43d4c03e328e1f02b5f5e49642c10864e8826a Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 8 Sep 2017 18:24:23 +0200 Subject: [PATCH 6/6] drop cat() statements as style_text() returns class vertical --- README.Rmd | 4 +--- README.md | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/README.Rmd b/README.Rmd index c34185c98..eada50a94 100644 --- a/README.Rmd +++ b/README.Rmd @@ -39,14 +39,12 @@ You can style a simple character vector of code with `style_text()`: ```{r echo=FALSE, message=FALSE} pkgload::load_all() library("magrittr") -#knitr::opts_chunk$set(comment = NA) ``` ```{r} ugly_code <- "a<-function( x){1+1} " -style_text(ugly_code) %>% - cat(sep = "\n") +style_text(ugly_code) ``` diff --git a/README.md b/README.md index 96d10bb85..486d0bbb8 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,7 @@ You can style a simple character vector of code with `style_text()`: ``` r ugly_code <- "a<-function( x){1+1} " -style_text(ugly_code) %>% - cat(sep = "\n") +style_text(ugly_code) #> a <- function(x) { #> 1 + 1 #> }