From 4017b39a9cc526b2026449388ba20511d6c989c0 Mon Sep 17 00:00:00 2001 From: "Pavel N. Krivitsky" Date: Mon, 9 Apr 2018 19:41:57 +1000 Subject: [PATCH 1/2] Lines longer than option "width" are now warped. References r-lib/styler#247. --- R/rules-line-break.R | 30 ++++++++++++++++++++++++++++++ R/transform-files.R | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/R/rules-line-break.R b/R/rules-line-break.R index 055237060..ae1a0539b 100644 --- a/R/rules-line-break.R +++ b/R/rules-line-break.R @@ -125,3 +125,33 @@ remove_line_break_in_empty_fun_call <- function(pd) { } pd } + +mk_add_line_break_at_pos_id <- function(ids) { + ids <- ids + + function(pd_flat){ + min_child_ids <- sapply(seq_len(nrow(pd_flat)), function(i){ + row <- pd_flat[i,] + m <- row$pos_id + while(!row$terminal){ + row <- row$child[[1]][1,] + m <- min(m, row$pos_id) + } + m + }) + + max_child_ids <- sapply(seq_len(nrow(pd_flat)), function(i){ + row <- pd_flat[i,] + m <- row$pos_id + while(!row$terminal){ + row <- row$child[[1]][nrow(row$child[[1]]),] + m <- max(m, row$pos_id) + } + m + }) + + nls <- apply(cbind(min_child_ids, max_child_ids), 1 , function(x) sum(ids >= x[1] & ids <= x[2])) + pd_flat$lag_newlines <- pd_flat$lag_newlines + nls + pd_flat + } +} diff --git a/R/transform-files.R b/R/transform-files.R index 7c55ca6ec..7336b1e13 100644 --- a/R/transform-files.R +++ b/R/transform-files.R @@ -103,15 +103,34 @@ parse_transform_serialize <- function(text, transformers) { ) return("") } - transformed_pd <- apply_transformers(pd_nested, transformers) - flattened_pd <- post_visit(transformed_pd, list(extract_terminals)) %>% - enrich_terminals(transformers$use_raw_indention) %>% - apply_ref_indention() %>% - set_regex_indention( - pattern = transformers$reindention$regex_pattern, - target_indention = transformers$reindention$indention, - comments_only = transformers$reindention$comments_only - ) + + width <- getOption("width") + + newline_ids <- integer(0) + tf <- transformers + repeat{ + add_line_break_at_pos_id <- mk_add_line_break_at_pos_id(newline_ids) + tf$line_break <- c(lst(add_line_break_at_pos_id),transformers$line_break) + transformed_pd <- apply_transformers(pd_nested, tf) + + flattened_pd <- post_visit(transformed_pd, list(extract_terminals)) %>% + enrich_terminals(transformers$use_raw_indention) %>% + apply_ref_indention() %>% + set_regex_indention( + pattern = transformers$reindention$regex_pattern, + target_indention = transformers$reindention$indention, + comments_only = transformers$reindention$comments_only + ) + + overflowed <- flattened_pd$pos_id[flattened_pd$col2>width] + + if(length(overflowed)){ + newline_ids <- unique(c(newline_ids, min(overflowed))) + }else{ + break + } + } + serialized_transformed_text <- serialize_parse_data_flattened(flattened_pd, start_line = start_line) From f498570f9e6f187cd6d20905ab35474c8bb23a05 Mon Sep 17 00:00:00 2001 From: "Pavel N. Krivitsky" Date: Mon, 9 Apr 2018 19:41:57 +1000 Subject: [PATCH 2/2] Lines longer than option "width" are now warped. References r-lib/styler#247. --- R/rules-line-break.R | 30 ++++++++++++++++++++++++++++++ R/transform-files.R | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/R/rules-line-break.R b/R/rules-line-break.R index 055237060..ae1a0539b 100644 --- a/R/rules-line-break.R +++ b/R/rules-line-break.R @@ -125,3 +125,33 @@ remove_line_break_in_empty_fun_call <- function(pd) { } pd } + +mk_add_line_break_at_pos_id <- function(ids) { + ids <- ids + + function(pd_flat){ + min_child_ids <- sapply(seq_len(nrow(pd_flat)), function(i){ + row <- pd_flat[i,] + m <- row$pos_id + while(!row$terminal){ + row <- row$child[[1]][1,] + m <- min(m, row$pos_id) + } + m + }) + + max_child_ids <- sapply(seq_len(nrow(pd_flat)), function(i){ + row <- pd_flat[i,] + m <- row$pos_id + while(!row$terminal){ + row <- row$child[[1]][nrow(row$child[[1]]),] + m <- max(m, row$pos_id) + } + m + }) + + nls <- apply(cbind(min_child_ids, max_child_ids), 1 , function(x) sum(ids >= x[1] & ids <= x[2])) + pd_flat$lag_newlines <- pd_flat$lag_newlines + nls + pd_flat + } +} diff --git a/R/transform-files.R b/R/transform-files.R index 7c55ca6ec..7336b1e13 100644 --- a/R/transform-files.R +++ b/R/transform-files.R @@ -103,15 +103,34 @@ parse_transform_serialize <- function(text, transformers) { ) return("") } - transformed_pd <- apply_transformers(pd_nested, transformers) - flattened_pd <- post_visit(transformed_pd, list(extract_terminals)) %>% - enrich_terminals(transformers$use_raw_indention) %>% - apply_ref_indention() %>% - set_regex_indention( - pattern = transformers$reindention$regex_pattern, - target_indention = transformers$reindention$indention, - comments_only = transformers$reindention$comments_only - ) + + width <- getOption("width") + + newline_ids <- integer(0) + tf <- transformers + repeat{ + add_line_break_at_pos_id <- mk_add_line_break_at_pos_id(newline_ids) + tf$line_break <- c(lst(add_line_break_at_pos_id),transformers$line_break) + transformed_pd <- apply_transformers(pd_nested, tf) + + flattened_pd <- post_visit(transformed_pd, list(extract_terminals)) %>% + enrich_terminals(transformers$use_raw_indention) %>% + apply_ref_indention() %>% + set_regex_indention( + pattern = transformers$reindention$regex_pattern, + target_indention = transformers$reindention$indention, + comments_only = transformers$reindention$comments_only + ) + + overflowed <- flattened_pd$pos_id[flattened_pd$col2>width] + + if(length(overflowed)){ + newline_ids <- unique(c(newline_ids, min(overflowed))) + }else{ + break + } + } + serialized_transformed_text <- serialize_parse_data_flattened(flattened_pd, start_line = start_line)