Added an option to tidy_source() to enforce a strict maximum line length. #71
Conversation
… with other functions.
…OL COMBINING SPRECHGESANG STEM character) to save space.
The behaviour with inline comments is now a little better. Unfortunately, if I've tried changing that code, but it doesn't work very well in |
Much appreciated! I'll review this tomorrow. |
I did a simpler (and less efficient) hack to It works for the pdf book I'm writing in most of the cases. I don't know if this approach may conflict with other use-cases. PS: bookdown + formatR are awesome!
|
Just FYI, I submitted an Enhancement request to implement this at the |
The R Bugzilla (https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17390) request was closed, though they are open to the possibility of a patch. |
@yihui , what about using |
For the record: styler (as of version 1.0.2) does not currently support line width enforcement, but there is WIP to achieve this (contributed by @krivit, will be finalized by @lorenzwalthert), see r-lib/styler#414. |
This patch adds another option,
width.strict=
totidy_source()
, defaulting toFALSE
. IfTRUE
, instead of callingdeparse()
withwidth.cutoff=
directly, it calls a new function,strict_deparse(..., width.max)
, which performs a binary search for the highestwidth.cutoff=
value to pass todeparse()
such that the longest line in its output is, after stripping trailing whitespace, at mostwidth.max=
in length.It seems to work quite well in most situations, though it still doesn't handle inline comments very well: the magic constant
%InLiNe_IdEnTiFiEr%
counts towards the line length, though it shouldn't. A possible solution might be to replace this magic constant with something much shorter, perhaps involving non-ASCII characters (which are legal in variable names in R, as far as I know) to reduce chances of a collision.