R language aware code reformatting/refactoring tools?

醉酒当歌 提交于 2019-12-03 22:10:43

Since this is still seem relevant I thought to mention styler which reformats r code according to the tidyverse style.

It ticks some of your boxes e.g. basic formatting but doesn't rename variables (although the linter lintr at least is able to show those).

Styler comes as an R package with functions the accept code (e.g. style_text(), but it can be used on the command line as well:

for example this code in tmp.r

a <-c(1,2,3) 
if(foo) {
  b=2 }
myVar=2

and running:

Rscript -e 'styler::style_file("tmp.r")'

would overwrite tmp.r into this:

a <- c(1, 2, 3)
if (foo) {
  b <- 2
}
myVar <- 2

IMHO, write your own. Writing a pretty printer is actually quite difficult. It requires understanding tokenizing, parsing, building ASTs or other IRs, tracking symbol tables and scopes, templating, etc.

But if you can do it, you'll really learn a lot about programming languages in general. You'll also look pretty impressive to your coworkers and it's amazing to put on a resume. It's also a lot of fun.

I'd recommend "Language Implementation Patterns: Create Your Own Domain-Specific and General Programming Languages" by Terence Parr. It's a little rough to read, but the content is pretty good. It's written at an introductory level to parsers and it's pretty short, but it contains all the parts you'd need to write this tool yourself.

If you do build it, open source it, come back here and tell us about it, and put up a site with a few ads to make yourself a few bucks. That way everyone can use your awesome creation and you'll get a few dollars in the process.

Best of luck...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!