使 IntelliJ 在行完成时重新格式化当前行

发布于 2024-11-04 01:58:12 字数 299 浏览 0 评论 0原文

有没有办法在 IntelliJ 中完成后重新格式化我的行? (对于熟悉的人来说,FlashDevelop 上提供了这种功能。)

假设我刚刚在 PHP 中输​​入了这段代码:

$var=array("a","b","c");

输入分号后,我希望编辑器将其重新格式化为(或任何我想要的格式)配置状态):

$var = array("a", "b", "c");

此自动重新格式化触发器也可以在括号和其他行终止符上执行。

Is there a way to have my line reformatted upon completing it in IntelliJ? (For those familiar, this kind of feature is available on FlashDevelop.)

Let's says I just entered this code in PHP:

$var=array("a","b","c");

Upon entering the semicolon, I would like the editor to reformat it to (or whatever my configuration states):

$var = array("a", "b", "c");

This auto-reformat trigger could also be executed on brackets and other line-terminators characters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眉目亦如画i 2024-11-11 01:58:12

IDEA中没有这样的功能。您可以在需要时使用重新格式化代码 (Ctrl + Alt + L) 操作。

There is no such feature in IDEA. You can use Reformat Code (Ctrl + Alt + L) action when needed.

海螺姑娘 2024-11-11 01:58:12

你实际上可以做类似的事情。

Go to where you want to insert ;
Tools/Start Macro Recording
Type in ;
Then select Code/Auto Indent Lines
Tools/Stop Macro Recording
Name the macro "reformat"
Now go to settings, Keymap, Macros, select "reformat"
Assign a keyboard shortcut of ;

在宏运行后,您可能需要稍微尝试一下才能将光标置于正确的位置。

You can actually do something like.

Go to where you want to insert ;
Tools/Start Macro Recording
Type in ;
Then select Code/Auto Indent Lines
Tools/Stop Macro Recording
Name the macro "reformat"
Now go to settings, Keymap, Macros, select "reformat"
Assign a keyboard shortcut of ;

You may have to play with it a bit to get your cursor in the right spot after the macro runs.

陈甜 2024-11-11 01:58:12

这是基于上面的jhormans回答,我知道这个问题已经有6年多了。但IntelliJ IDEA 2017.1.5仍然没有像VisualStudios那样按下分号时自动重新格式化当前行的功能。

解决方法是按照 jhorman 的描述创建一个宏,但在按“;”之后运行代码->重新格式化代码。然后停止录制,并在“设置/键盘映射/宏”下将快捷键“Shift+逗号”添加到宏中。现在,当在代码中按 Shift + 逗号输入分号时,代码也会被重新格式化。唯一的缺点:每次按下 Shift+Comma 时,整个代码都会重新格式化。对于大文件,这可能需要一秒钟的时间。

仅当您只有一个行终止符(如分号)时,此解决方法才有效。要自动重新格式化多行终止符(右括号、分号等)上的代码,您需要将快捷方式添加到“设置”-> “键盘映射”-> “主菜单”-> 「代码」->直接“重新格式化代码”。在这种情况下,无需创建宏。

据我所知,仅格式化当前行是不可能的。

This is based on jhormans answer above and i know that the question is more than 6 years old. But IntelliJ IDEA 2017.1.5 still does not have a feature that automatically reformats the current line when semicolon is pressed like VisualStudios.

The workaround is to create a macro as described by jhorman but after pressing ';' run Code -> Reformat Code. Then stop recording and under Settings/Keymap/Macros add the shortcut "Shift+Comma" to the macro. Now when typing the semicolon by pressing Shift + Comma in the code, the code will be reformated aswell. The only shortcoming: the whole code will be reformated each time Shift+Comma is pressed. This may take a second on large files.

This workaround will only work if you just have one line terminator like semicolon. To automatically reformat the code on multiple line terminators (closing brackets, semicolon, ...) you would need to add the shortcuts to "Settings" -> "Keymap" -> "Main Menu" -> "Code" -> "Reformat Code" directly. In this case there's no need to create a macro.

Just formatting the current line isn't possible as far as i know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文