自动更正功能线长度在图书馆 - Auto-correct line lengths in a library

- 此内容更新于:2016-02-01
主题:

我工作在一个巨大的项目,我们决定让所有符合80个字符每行代码。它是一个基于ruby的项目包装为rubyc,我决定使用Rubocop和命令:我有1714个错误,线的长度大于80个字符。除此之外,还有许多其他错误检测到Rubocop我想忽略。我在寻找最简单的方法自动修正所有的线长侵犯,满足80字符的限制在C和Ruby。

原文:

I am working on a huge project and we have decided to make all code conform to 80 characters per line. It is a ruby based project with wrappers for C. For Ruby, I decided to use Rubocop and with the command:

rubocop --only LineLength

I got 1,714 errors, where length of the line was greater than 80 characters. Aside from that, there were many other errors detected by Rubocop which I want to ignore for now.

I am looking for the easiest way to auto-correct all the line length violations only, to satisfy the 80 character limit both in C and Ruby.

网友:我建议你首先扫描代码以确定最常见的原因,超过80个字符。我不会感到惊讶如果罪魁祸首是长变量和方法名称。如果是这样,你可以减少“长”线(>80字符)仅通过缩短这些字符串。“仅仅”也许是不正确的词,因为这是没有简单的任务。你需要避免缩短Ruby的内置方法的名字,当然,以及一些引用的字符串。(我说“一些”,因为你可能会定义一个方法,然后发现它引用的字符串:。)…

(原文:I suggest you first scan the code to determine the most common reasons that lines exceed 80 characters. I would not be surprised if a main culprit is long variable and method names. If so, you might be able reduce the number of "long" lines (> 80 chars) considerably by merely shortening those strings. "Merely" is perhaps not the right word, since that is no simple task. You need to avoid shortening names of Ruby's built-in methods, of course, as well as some quoted strings. (I say "some" because you might define a method doit and then find it referenced as a string: A.send("doit").)...)

网友:…你可能首先scannin字符串的代码你会放在一个数组,然后。明年从头到尾地阅读和删除字符串,显然不能缩短。然后创建一个哈希映射每个字符串,然后手动修改每个值短但仍有意义。后确认没有重复的值的散列,使用散列来缩短代码中的字符串…

(原文:...You might start by scannin your code for strings which you'd put in an array arr, then arr.uniq!.sort!. Next go though and remove strings that obviously cannot be shortened. Then create a hash that maps each string to itself, then manually modify each value to something shorter but still meaningful. After confirming there are no duplicate values in the hash, use the hash to shorten strings in the code...)

网友:…此外,还有您可以安全地断裂线的地方,如垂直管道周围的块变量后,逗号不内引用字符串,等等。您可以使用正则表达式。最好的运气!完成后,请用一个编辑报告,解释你所做的和你遇到的任何缺陷。这将是特别有趣如果你报告百分比减少你每个技术实现的使用。如果你这样做,请让我知道在一个评论。

(原文:...In addition, there are places where you can safely break lines, such as after vertical pipes surrounding block variables, commas that are not within quoted strings, and so on. You might use regular expressions for that. Best of luck! After you are finished, please report back with an edit that explains what you did and any pitfalls you encountered. It would be especially interesting if you report percentage reductions in long lines you achieved by each technique you use. If you do that, please let me know in a comment.)