简体   繁体   English

Android Studio 中的自动代码格式化

[英]Auto code formatting in Android Studio

Is it possible to format code automatically on save ?是否可以format code automatically on save I've seen a solution in which a macro assigned to a shortcut is proposed but I need to be able to make this a project setting.我见过一个解决方案,其中建议分配给快捷方式的宏,但我需要能够将其设为项目设置。 This way, anyone joining our project doesn't have to configure a macro for himself.这样,任何加入我们项目的人都不必为自己配置宏。

In Eclipse this was easily achieved using save actions and putting the config files under version control.在 Eclipse 中,这可以通过使用save actions和将配置文件置于版本控制下轻松实现。 Is this possible with Android Studio?这可以用 Android Studio 实现吗?

保存时使用 Flutter 自动格式化代码:勾选 - 文件|设置|语言和框架|Flutter - '保存时格式化代码'。

It is not possible other than via a macro.除了通过宏之外,这是不可能的。 There is an outstanding feature request -- IDEABKL-5806 Automatically trigger code formatter on file save -- for such an action.对于这样的操作,有一个出色的功能请求——IDEABKL-5806 自动触发文件保存时的代码格式化程序 There a bit of discussion on the ticket you may want to read.您可能想阅读有关票证的一些讨论。 I recommend you vote for it.我建议你投票给它。 It is however a 4 year old request.然而,这是一个 4 岁的请求。 And based on comments by JetBrains, it does not appear like it is a high priority item.根据 JetBrains 的评论,它似乎不是高优先级项目。 Additional votes may change that.额外的投票可能会改变这一点。

In the meantime, using the auto format on commit option would be an option for you.同时,在提交时使用自动格式选项将是您的一个选择。

Finally, if you want an easier way to set up the macros on other developer's systems, the macros are stored in the file .IntelliJIDEA/config/options/macros.xml (see Directories used by the IDE to store settings, caches, plugins and logs for more information on the location of the config directory).最后,如果您想要更简单的方法在其他开发人员的系统上设置宏,宏存储在文件.IntelliJIDEA/config/options/macros.xml (请参阅IDE 用于存储设置、缓存、插件和有关 config 目录位置的更多信息,请查看日志)。 Keymaps are stored in .IntelliJIDEA/config/keymaps/{map-name}.xml You could copy those files over.键映射存储在.IntelliJIDEA/config/keymaps/{map-name}.xml您可以复制这些文件。

Another option would be download the save actions plugin from jetbrains officials (as the search inside the plugin section in IDE doesn't find it)另一种选择是从jetbrains 官方下载保存操作插件(因为在 IDE 插件部分中的搜索没有找到它)

Next, import the downloaded JAR file through IDE settings:接下来,通过IDE设置导入下载的JAR文件:

File > Settings > Plugins > Install plugins from disk文件 > 设置 > 插件 > 从磁盘安装插件

Restart the IDE and the Save Actions configuration will be shown on the root of IDE Settings重新启动 IDE,IDE 设置的根目录中将显示Save Actions配置

I don't know if this question is answered elsewhere or not, in Android Studio, I can find auto-format on save for Flutter in Preferences > Languages and Framework > Flutter as image here.我不知道这个问题是否在其他地方得到了回答,在 Android Studio 中,我可以在Preferences > Languages and Framework > Flutter as image here 中找到保存 Flutter 的自动格式。 Autoformat on save option for Flutter Flutter 的自动格式化保存选项

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

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