如何在 Intellij IDEA 17 中配置 Google Java 代码格式化程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42979700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure Google Java Code Formatter in Intellij IDEA 17?
提问by Lorenzo Lerate
In the latest versions of IntelliJ IDEA it seems that it isn't possible to import code style files like intellij-java-google-style.xml.
在 IntelliJ IDEA 的最新版本中,似乎无法导入像intellij-java-google-style.xml这样的代码样式文件。
I would like use the Google Java Code Style in this IDE but it seems there isn't a trivial solution.
我想在这个 IDE 中使用 Google Java 代码样式,但似乎没有一个简单的解决方案。
回答by Lorenzo Lerate
This problem can be solve installing the google-java-format Plugin.
这个问题可以通过安装google-java-format Plugin来解决。
Open plugins window (CTRL+Shift+A): plugins
Click on browse repositories.
Search for google-java-format
Install the plugin.
Restart the IDE.
Enable the plugin executing the action (Ctrl+Shift+A): Reformat with google-java-format
打开插件窗口(CTRL+ Shift+ A):插件
单击浏览存储库。
搜索google-java-format
安装插件。
重新启动 IDE。
启用执行操作的插件 (Ctrl+Shift+A):使用 google-java-format 重新格式化
Additional notes from the google-java-format README
来自google-java-format README 的附加说明
The plugin will be disabled by default. To enable it in the current project, go to File→Settings...→google-java-format Settings (or IntelliJ IDEA→Preferences...→Other Settings→google-java-format Settings on macOS) and check the Enable google-java-format checkbox. (A notification will be presented when you first open a project offering to do this for you.)
To enable it by default in new projects, use File→Other Settings→Default Settings....
默认情况下该插件将被禁用。要在当前项目中启用它,请转到 File→Settings...→google-java-format Settings(或 IntelliJ IDEA→Preferences...→Other Settings→google-java-format Settings on macOS)并选中 Enable google -java 格式复选框。(当你第一次打开一个项目来为你做这件事时,会出现一条通知。)
要在新项目中默认启用它,请使用文件→其他设置→默认设置...。
回答by Rancho
After doing above steps mentioned by Lorenzo, for applying the setting in the current project :- Intellij>Preferences>google-java-format setting and enable it in intellij; as it wont be enabled by default after doing restart as well.
完成Lorenzo提到的上述步骤后,在当前项目中应用设置:- Intellij>Preferences>google-java-format 设置并在intellij中启用它;因为它也不会在重新启动后默认启用。
If you want to have this setting for the projects which will you create after installing this plugin , then Go to File >Other Settings> Default Settings> google-java-format-setting and enable it
如果您想为安装此插件后创建的项目设置此设置,请转到文件 > 其他设置 > 默认设置 > google-java-format-setting 并启用它
回答by Kamiel Ahmadpour
You can also simply add this xml file to Intellij configuration folder
您也可以简单地将此 xml 文件添加到 Intellij 配置文件夹
/home/USERNAME/.IntelliJIdea17/config/codestyles
/home/USERNAME/.IntelliJIdea17/config/codestyles
Then you can make this code style the default one.
然后您可以将此代码样式设为默认样式。
File/Settings/Editor/Code Style ==> Scheme = GoogleStyle
文件/设置/编辑器/代码样式 ==> 方案 = GoogleStyle
回答by rahul maindargi
As mentioned Here, Download the intellij-java-google-style.xml file from the This Link.
正如这里提到的,从这个链接下载 intellij-java-google-style.xml 文件。
Then in Intellij, go Under Settings -> Editor -> Code Style. there in Scheme settings (settings icon on right side) -> import schemes-> intellij idea code style xml. Select the xml downloaded in first step. then in Scheme dropdown select the GoogleStyle IDE (newly added style). Click on apply and close.
然后在 Intellij 中,转到设置 -> 编辑器 -> 代码样式。在方案设置(右侧的设置图标)-> 导入方案-> intellij idea 代码样式 xml 中。选择第一步下载的xml。然后在 Scheme 下拉列表中选择 GoogleStyle IDE(新添加的样式)。单击应用并关闭。