简体   繁体   English

水平列表到垂直列表,反之亦然 - 基于IntelliJ IDEA的IDE

[英]Horizontal list to vertical list and vice versa - IntelliJ IDEA based IDE

Is there keyboard shortcut to convert horizontal list to vertical list and vice versa in IntelliJ IDEA and based on it IDE PhpStorm? 在IntelliJ IDEA中是否有键盘快捷键将水平列表转换为垂直列表,反之亦然,并基于IDE PhpStorm? For example I have array 例如我有数组

$arr = [
    'test',
    'a' => 'b',
];

and I want to make it single-line, I can select text and use Ctrl + Shift + J , I get 我想让它成为单行,我可以选择文本并使用Ctrl + Shift + J ,我得到

$arr = ['test', 'a' => 'b', ];

it is almost good, I can remove last , manually. 这几乎是好的,我可以删除最后,手动。 But how to make the opposite: convert horizontal list to vertical? 但如何反过来:将水平列表转换为垂直列表? It is not only about arrays, this question is also about function signatures for example 它不仅仅是关于数组,这个问题也是关于函数签名的例子

public function test($arg1, $arg2, $arg3, $arg4)

and function calls 和函数调用

test($arg1, $arg2, $arg3, $arg4);

sometimes string become too long and it is needed to split it for readability like this: 有时字符串变得太长,需要将它拆分为可读性,如下所示:

test(
    $arg1,
    $arg2,
    $arg3,
    $arg4
);

Note that this question is not about code folding, I want to really change formatting, not just show-hide just for me. 请注意,这个问题不是关于代码折叠,我想真正改变格式,而不仅仅是为我显示隐藏。

Since IntelliJ IDEA 2019.2 自IntelliJ IDEA 2019.2

Split any list using the new intention in IDE: Show Context Actions with Alt + Enter then choose option Split comma-separated values into multiple lines . 使用IDE中的新用法拆分任何列表:使用Alt + Enter显示上下文操作,然后选择选项将逗号分隔值拆分为多行 The reversed intention is also available for cases where you want to combine items into one line: Alt + Enter then choose option Join comma-separated values into a single line . 如果要将项目合并为一行,则可以使用相反的意图: Alt + Enter然后选择选项将逗号分隔值加入一行

gif图像展示了新的意图

Prior to IntelliJ IDEA 2019.2 在IntelliJ IDEA 2019.2之前

Vertical list to horizontal (join) 垂直列表到水平(加入)

Select items which should be joined and use Join Lines command Ctrl + Shift + J . 选择应该连接的项目并使用“连接线”命令Ctrl + Shift + J.

Horizontal list to vertical (split) 水平列表到垂直(拆分)

Use Realigner plugin, you can install it in Settings > Plugins > Browse repositories… (or Marketplace) It adds Split command with shortcut Ctrl + Shift + Alt + P . 使用Realigner插件,您可以在设置>插件>浏览存储库...(或市场)中安装它。它使用快捷键Ctrl + Shift + Alt + P添加Split命令。 After calling of this command you will see dialog, configure it like this: 调用此命令后,您将看到对话框,如下所示进行配置:

按分隔符对话框拆分

delimiter is comma only, without spaces. 分隔符只是逗号,没有空格。 After clicking on "OK" plugin will split your list, but also settings will be remembered, so in future you can split any list using command shortcut then Enter without spending of time. 单击“确定”后,插件将拆分列表,但也会记住设置,因此将来您可以使用命令快捷方式拆分任何列表,然后输入而不花费时间。 But after splitting code will not have indents, need to run Reformat Code. 但是在拆分代码后不会有缩进,需要运行Reformat Code。 As a result we can achieve target in two steps: 因此,我们可以分两步实现目标:

  1. Split Ctrl + Shift + Alt + P then Enter . 按Ctrl + Shift + Alt + P然后按Enter键
  2. Reformat Code Ctrl + Alt + L . 重新格式化代码Ctrl + Alt + L.

It is not perfect, but better then manual editing. 它并不完美,但比手动编辑更好。 Potential problem: Realigner doesn't analyse code, it just split text by delimiter. 潜在问题:Realigner不分析代码,它只是通过分隔符分割文本。 It would be better to create a macro for these commands and add shortcut for it, I tried but without success, it is easy to create macro but result is buggy. 为这些命令创建一个宏并为它添加快捷方式会更好,我试过但没有成功,很容易创建宏但结果是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM