Skip to content

Commit 7b8e52b

Browse files
Merge pull request #49 from lorenzwalthert/indent_curly
* Indention based on curly brackets * spacing across different levels of nesting (e.g. a space after `)` in `function(x) {...}`)
2 parents 2123834 + 698d448 commit 7b8e52b

23 files changed

+361
-8
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ importFrom(purrr,partial)
1616
importFrom(purrr,pmap)
1717
importFrom(purrr,pwalk)
1818
importFrom(purrr,reduce)
19-
importFrom(readr,write_tsv)
19+
importFrom(utils,write.table)

R/get_transformers.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ get_transformers_flat <- function(strict = TRUE) {
4949
get_transformers_nested <- function(strict = TRUE, indent_by = 2) {
5050
c(create_filler,
5151
partial(indent_round, indent_by = indent_by),
52+
partial(indent_curly, indent_by = indent_by),
5253
strip_eol_spaces,
53-
get_transformers_flat(strict)
54+
get_transformers_flat(strict),
55+
set_space_between_levels
5456
)
5557
}

R/modify_pd.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ indent_round <- function(pd, indent_by) {
2121
pd
2222
}
2323

24+
25+
#' @rdname update_indention
26+
indent_curly <- function(pd, indent_by) {
27+
opening <- which(pd$token == "'{'")
28+
if (length(opening) > 0) {
29+
start <- opening + 1
30+
stop <- nrow(pd) - 1
31+
} else {
32+
start <- stop <- 0
33+
}
34+
pd <- pd %>%
35+
mutate(indent = indent + ifelse(seq_len(nrow(pd)) %in% start:stop, indent_by, 0)) %>%
36+
select_(~indent, ~newlines, ~everything())
37+
pd
38+
}
39+
2440
#' Strip EOL spaces
2541
#'
2642
#' Remove end-of-line spaces.

R/rules.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,19 @@ set_space_after_comma <- function(pd_flat) {
9090
pd_flat$spaces[comma_after & (pd_flat$newlines == 0L)] <- 1L
9191
pd_flat
9292
}
93+
94+
#' Set space between levels of nesting
95+
#'
96+
#' With the nested approach, certain rules do not have an effect anymore because
97+
#' of the nature of the nested structure. Setting spacing before curly
98+
#' brackets in for / if / while statements and function declarations will be
99+
#' such a case since a curly bracket is always at the first position in a
100+
#' parse table, so spacing cannot be set after the previous token.
101+
#' @param pd_flat A flat parse table.
102+
set_space_between_levels <- function(pd_flat) {
103+
if (pd_flat$token[1] %in% c("FUNCTION", "FOR", "IF", "WHILE")) {
104+
pd_flat$spaces[nrow(pd_flat) - 1] <- 1L
105+
}
106+
pd_flat
107+
}
108+

R/serialized_tests.R

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ construct_tree <- function(in_paths, suffix = "_tree") {
8484
#' @param write_tree Whether or not the tree structure of the test should be
8585
#' computed and written to a file.
8686
#' @param out_tree Name of tree file if written out.
87-
#' @importFrom readr write_tsv
87+
#' @importFrom utils write.table
8888
transform_and_check <- function(in_item, out_item,
8989
in_name = in_item, out_name = out_item,
9090
transformer, write_back,
@@ -94,7 +94,7 @@ transform_and_check <- function(in_item, out_item,
9494
read_in <- utf8::read_lines_enc(in_item)
9595
if (write_tree) {
9696
create_tree(read_in) %>%
97-
write_tsv(out_tree, col_names = FALSE)
97+
write.table(out_tree, col.names = FALSE, row.names = FALSE, quote = FALSE)
9898
}
9999
transformed <- read_in %>%
100100
transformer()
@@ -143,11 +143,37 @@ style_indent_round <- function(text) {
143143

144144

145145
#' @describeIn test_transformer Nest and unnest `text` without applying any
146-
#' transformations but remove indention due to the way the serialization is
147-
#' set up.
146+
#' transformations but remove EOL spaces and indention due to the way the
147+
#' serialization is set up.
148148
style_empty <- function(text) {
149149
text %>%
150150
compute_parse_data_nested() %>%
151-
visit(funs = c(create_filler)) %>%
151+
visit(funs = c(create_filler, strip_eol_spaces)) %>%
152+
serialize_parse_data_nested()
153+
}
154+
155+
#' @describeIn test_transformer Transformations for indention based on curly
156+
#' brackets only.
157+
style_indent_curly <- function(text) {
158+
text %>%
159+
compute_parse_data_nested() %>%
160+
visit(funs = c(create_filler,
161+
partial(indent_curly, indent_by = 2),
162+
strip_eol_spaces)) %>%
163+
164+
serialize_parse_data_nested()
165+
}
166+
167+
168+
#' @describeIn test_transformer Transformations for indention based on curly
169+
#' brackets and round brackets.
170+
style_indent_curly_round <- function(text) {
171+
text %>%
172+
compute_parse_data_nested() %>%
173+
visit(funs = c(create_filler,
174+
partial(indent_curly, indent_by = 2),
175+
partial(indent_round, indent_by = 2),
176+
strip_eol_spaces)) %>%
177+
152178
serialize_parse_data_nested()
153179
}

man/set_space_between_levels.Rd

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
{1 + 3}
3+
{2 + sin(pi)}
4+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
ROOT (token: short_text [newlines/spaces])
2+
°--expr: [0/0]
3+
¦--'{': { [1/9]
4+
¦--expr: [1/0]
5+
¦ ¦--'{': { [0/0]
6+
¦ ¦--expr: [0/0]
7+
¦ ¦ ¦--expr: [0/1]
8+
¦ ¦ ¦ °--NUM_CONST: 1 [0/0]
9+
¦ ¦ ¦--'+': + [0/1]
10+
¦ ¦ °--expr: [0/0]
11+
¦ ¦ °--NUM_CONST: 3 [0/0]
12+
¦ °--'}': } [0/0]
13+
¦--expr: [1/6]
14+
¦ ¦--'{': { [0/0]
15+
¦ ¦--expr: [0/0]
16+
¦ ¦ ¦--expr: [0/1]
17+
¦ ¦ ¦ °--NUM_CONST: 2 [0/0]
18+
¦ ¦ ¦--'+': + [0/1]
19+
¦ ¦ °--expr: [0/0]
20+
¦ ¦ ¦--expr: [0/0]
21+
¦ ¦ ¦ °--SYMBOL: pi [0/0]
22+
¦ ¦ ¦--'(': ( [0/0]
23+
¦ ¦ °--')': ) [0/0]
24+
¦ °--'}': } [0/0]
25+
°--'}': } [0/0]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
{1 + 3}
3+
{2 + sin(pi)}
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
a <- function(x) {
2+
x <- c(1,
3+
2 + 3,
4+
sin(pi))
5+
6+
if(x > 10) {
7+
return("done")
8+
}
9+
}

0 commit comments

Comments
 (0)