Skip to content

No line break after pipe inserted #470

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

Closed
msberends opened this issue Feb 5, 2019 · 5 comments · Fixed by #503
Closed

No line break after pipe inserted #470

msberends opened this issue Feb 5, 2019 · 5 comments · Fixed by #503

Comments

@msberends
Copy link

I was quite surprised to learn that

styler::style_text("my_df %>% group_by(var1, var2, var3) %>% summarise(a = mean(value), b = median(value), c = sum(value)) %>% filter(a > 2, b < 1)")

leads to

my_df %>% group_by(var1, var2, var3) %>% summarise(a = mean(value), b = median(value), c = sum(value)) %>% filter(a > 2, b < 1)

and not something like

my_df %>%
  group_by(var1, var2, var3) %>%
  summarise(a = mean(value), b = median(value)) %>%
  filter(a > 2, b < 1)

So pipes are not followed by a line break. I looked at http://styler.r-lib.org/articles/introducing_styler.html and https://lorenzwalthert.netlify.com/post/customizing-styler-the-quick-way/ how the tidyverse default style is being applied, and I don't see anything resembling the proposed style on https://style.tidyverse.org/pipes.html#whitespace:

# Good
iris %>%
  group_by(Species) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  gather(measure, value, -Species) %>%
  arrange(value)

# Bad
iris %>% group_by(Species) %>% summarize_all(mean) %>%
ungroup %>% gather(measure, value, -Species) %>%
arrange(value)

Isn't or shouldn't this be the default behaviour of styler::tidyverse_style()?? Can I change this behaviour?

@msberends
Copy link
Author

Also related to #74.
I think a lot of syntax writers end almost every single line of code with a pipe these days. I would really like to have it just cleanly styled with a keyboard shortcut 🙂

@lorenzwalthert lorenzwalthert changed the title Pipe perfectly ignored No line break after pipe insterted Feb 5, 2019
@lorenzwalthert
Copy link
Collaborator

Thanks for considering old issues @msberends. The paragraph in tidyvese/style that proposed single-line pipes (linked to #74) has been weakened in my eyes so I think for strict = TRUE, we should insert a line break after the pipe unless @krlmlr strongly opposes to this.

@msberends
Copy link
Author

Any news?

@lorenzwalthert
Copy link
Collaborator

Yes, I just saw it the other day. I think we can do it before we release 1.1.1, i..e within the next 10 days.

@msberends
Copy link
Author

For now, this is what I use (and others can):

style_with_pipe <- function () {
  context <- rstudioapi::getConsoleEditorContext()
  text <- context$selection[[1]]$text
  if (all(nchar(text) == 0)) {
    # try source editor
    context <- rstudioapi::getSourceEditorContext()
    text <- context$selection[[1]]$text
  }
  if (all(nchar(text) == 0)) {
    stop("No code selected")
  }
  # replace pipes
  text <- gsub("( |\n)*%>%( |\n)*", " %>%\n", text)
  # line after comma, will else be too cluttered
  text <- gsub(' ?, ?', ',\n', text)

  # do everything else defined by styler::style_text
  out <- styler::style_text(text)
  
  # don't put brackets on new lines on their own, and remove empty lines
  toinsert <- paste0(out[out != ""], collapse = "\n")
  toinsert <- gsub(" *\n *[)]",")", toinsert)
  
  rstudioapi::modifyRange(location = context$selection[[1]]$range,
                          text = toinsert,
                          id = context$id)
}

@lorenzwalthert lorenzwalthert changed the title No line break after pipe insterted No line break after pipe inserted May 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants