如何使用Eclipse使用行注释将Java从Allman重新格式化为K&R?(How to reformat Java from Allman to K&R with line comments us

编程入门 行业动态 更新时间:2024-10-18 10:32:23
如何使用Eclipse使用行注释将Java从Allman重新格式化为K&R?(How to reformat Java from Allman to K&R with line comments using Eclipse?)

我开始使用Allman风格进行编码,使用对齐的大括号:

void foobar() { if(foo) { bar(); } }

几十年后,我决定要增加屏幕空间; 此外,我的客户使用不匹配的大括号,因此很难从工作中来回切换到我的个人代码。 所以我想将现有的所有代码转换为K&R:

void foobar() { if(foo) { bar(); } }

Eclipse 4.4有足够的代码格式化程序,如果我选择源代码树,我甚至可以批量格式化文件。 问题是,如果我有一个K&R括号的行注释,Eclipse会拒绝将一个支架移动到一条线上,让我有一个大杂烩的编码风格:

void foobar() { if(foo) //if foo { bar(); } }

我怎么能告诉Eclipse移动支架,即使支架上的线有注释?

void foobar() { if(foo) { //if foo bar(); } }

I started out coding using the Allman style, with aligned braces:

void foobar() { if(foo) { bar(); } }

After decades I've decided I want that extra screen space; and besides, my client uses non-matched braces, so it's hard to switch back and forth from work to my personal code. So I want to convert all my existing code to K&R:

void foobar() { if(foo) { bar(); } }

Eclipse 4.4 has a sufficient code formatter, and if I select my source tree I can even format files in bulk. The problem is that if I have a line comment where a K&R brace would be, Eclipse will refuse to move the brace up a line, leaving me with a hodgepodge of coding styles:

void foobar() { if(foo) //if foo { bar(); } }

How can I tell Eclipse to move the brace up, even if there is a comment on the line where the brace goes?

void foobar() { if(foo) { //if foo bar(); } }

最满意答案

正则表达式替换文件; 更换

\)\s*(//.*)(\r?\n)(\s*)\{ 1 2 3

有:

) {\2\3 \1\2

更新(Garret Wilson):经过大量实验,理想的正则表达式替换解决了所有变化并满足了我的需求:

^([^/]*(?:\)|\>|do|else|Exception|static|try))\s*(//.*)(\r?\n)(\s*)\{

替换为:

\1 { \2

A regular expression replacement over files; replace

\)\s*(//.*)(\r?\n)(\s*)\{ 1 2 3

with:

) {\2\3 \1\2

Update (Garret Wilson): After much experimentation, the ideal regular expression replacement that addressed all the variations and met my needs was:

^([^/]*(?:\)|\>|do|else|Exception|static|try))\s*(//.*)(\r?\n)(\s*)\{

replaced with:

\1 { \2

更多推荐

本文发布于:2023-08-07 11:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464512.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   如何使用   格式   Eclipse   Allman

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!