3 ms·
One of my side project was an auto formatter for R. There are some limits in existing formatters: - I think most of them doesn't recognize multi line string li
by dracodoc 9y ago
One of my side project was an auto formatter for R. There are some limits in existing formatters:
- I think most of them doesn't recognize multi line string literals, which is difficult if you consider the case that you can have "" in comments, comment symbol in "" string literals and line breaks. The only way to deal with it is to scan linearly with context.
- It's tricky to wrap a long line:
+ some points in a long line are more suitable for breaking points in logic level
+ but sometimes you want less lines and not to break too often, even it's more clear in logic. The lines could be just some parameter list that will be both well represented in one column or multi columns.
+ with nested code the natural indent position could be at the far right, which make each line very short if you stick to 80 columns rule.
After quite some efforts my code can deal with all the comments, multi line strings, all the operators I known (I need to separate unary and binary operators to determine whether to insert space), but the script take several seconds to run, and I haven't start to deal with indent. I probably can save some time if I do more optimization, but I don't have time to finish it now.
This python formatter talked about its algorithm, worth a read.
https://github.com/google/yapf https://github.com/google/yapf