Skip to content
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

Document formatting ensures trailing empty line #509

Merged

Conversation

renkun-ken
Copy link
Member

Closes #462

In document formatting, a trailing new line is ensured in the end of the new text.

@lorenzwalthert
Copy link
Contributor

I read this as if the last line is not empty, add one (if trailing_empty_line). However, if you have already 5 empty lines, that won't remove 4. You can also just c/p the ensure_last_n_empty() from {styler}:

ensure_last_n_empty <- function(x, n = 1) {
  if (all(x == "")) {
    return("")
  }
  x <- c(x, "", "")
  x <- x[seq(1, length(x) - which(rev(x) != "")[1] + 1L)]
  c(x, rep("", n))
}
styler:::ensure_last_n_empty("hi")
#> [1] "hi" ""
styler:::ensure_last_n_empty(c("hi", ""))
#> [1] "hi" ""
styler:::ensure_last_n_empty(c("hi", "", ""))
#> [1] "hi" ""

Created on 2021-12-28 by the reprex package (v2.0.1)

@renkun-ken
Copy link
Member Author

renkun-ken commented Dec 28, 2021

Thanks for pointing to styler:::ensure_last_n_empty(). I actually tried it, but I wonder if we should regard removing trailing empty lines as part of styler rather than languageserver?

The current release does not remove any trailing empty lines, and the latest dev removes all trailing empty lines. I think it might make some sense to leave it to styler to determine whether it is appropriate to remove all trailing empty lines? Looks like it should be subject to user preference and should be customizable via styler some how, if possible. On languageserver side, we only need to ensure there is at least a trailing empty line to make things work (e.g. .Rprofile).

@lorenzwalthert
Copy link
Contributor

lorenzwalthert commented Dec 28, 2021

Ok, makes sense. {styler} dev does remove these additional lines, so with the next CRAN release, people will have this functionality anyways. Thanks for looking into it @renkun-ken, appreciate it that you help fix a problem that is fundamentally not a {languageserver}, but a {styler} problem. 👍

@renkun-ken renkun-ken merged commit 501bb6e into REditorSupport:master Dec 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"No newline at end of file" after formatting file in VS Code
3 participants