Skip to content

Introducing class vertical #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.Rhistory
.RData
inst/doc
docs
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ Collate:
'transform.R'
'unindent.R'
'utils.R'
'vertical.R'
'visit.R'
'ws.R'
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 2 additions & 1 deletion R/serialized_tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 14 additions & 0 deletions R/vertical.R
Original file line number Diff line number Diff line change
@@ -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")
}
3 changes: 2 additions & 1 deletion R/ws.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)

```

Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
#> }
Expand Down
15 changes: 15 additions & 0 deletions man/construct_vertical.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat/spacing/spacing_comma-in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c( 1, 16 , 333 , 33 , 1)
20 changes: 20 additions & 0 deletions tests/testthat/spacing/spacing_comma-in_tree

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat/spacing/spacing_comma-out.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c(1, 16, 333, 33, 1)
1 change: 1 addition & 0 deletions tests/testthat/spacing/spacing_if-in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if(TRUE) x else y
12 changes: 12 additions & 0 deletions tests/testthat/spacing/spacing_if-in_tree

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat/spacing/spacing_if-out.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if (TRUE) x else y
1 change: 1 addition & 0 deletions tests/testthat/strict/eof-in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
blabla
3 changes: 3 additions & 0 deletions tests/testthat/strict/eof-in_tree

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat/strict/eof-out.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
blabla
2 changes: 2 additions & 0 deletions tests/testthat/strict/eol-in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a()
b # comment
9 changes: 9 additions & 0 deletions tests/testthat/strict/eol-in_tree

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/testthat/strict/eol-out.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a()
b # comment
13 changes: 13 additions & 0 deletions tests/testthat/test-spacing.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

21 changes: 8 additions & 13 deletions tests/testthat/test-strict.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,15 @@ 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()")
})


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)")
expect_warning(test_collection(
"strict", "eof",
transformer = style_text,
strict = FALSE), NA)
})