为所有本地项目切换活动的IntelliJ代码样式 [英] Switch active IntelliJ Code Style for all local projects

查看:59
本文介绍了为所有本地项目切换活动的IntelliJ代码样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按照 Google Java样式指南来规范我的Java代码样式..我下载了 intellij-java-google-style.xml ,进行了一些自定义调整,并将其导入了全局IDE部分的IntelliJ中:

I would like to standardize my java code style as per Google Java Style Guide. I downloaded intellij-java-google-style.xml, made a few custom tweaks and imported it in IntelliJ in the global IDE section:

首选项... >>编辑器>>代码样式>>齿轮图标>>导入方案>> IntelliJ代码样式xml

Preferences... >> Editor >> Code Style >> gear icon >> Import Scheme >> IntelliJ code style xml

这会将代码样式应用于当前项目.但是,现有的IntelliJ项目保留其现有的代码样式.我可以在其他项目的下拉菜单中看到新的Google样式,但是我必须为每个项目手动重新选择它.我的问题是我有数百个项目.

This applies the Code Style to the current project. However, existing IntelliJ projects keep their existing code style. I can see the new Google Style in the other projects' drop-down menus, but I have to manually reselect it for each project. My problem is I have hundreds of projects.

我想为所有现有项目自动选择新的Google代码样式,而不必对每个项目都进行首选项"设置.但是如何?

I would like to automatically select the new Google code style for all existing projects without having to go through the Preferences for each one. But how?

注意::我实际上不想在项目中自动设置代码的格式,这将是一个噩梦.

NOTE: I don't want to actually auto-format the code in my projects, that would be a git nightmare.

推荐答案

IntelliJ没有提供一种自动更改现有本地存储库上的代码样式的方法.因此,我们需要跳出框框思考.

IntelliJ does not provide a way to automatically change code style on existing local repos. So we need to think outside the box.

事实证明,IntelliJ将活动代码样式存储在 .idea/codeStyles/codeStyleConfig.xml 中:

It turns out IntelliJ stores the active code style in .idea/codeStyles/codeStyleConfig.xml:

<component name="ProjectCodeStyleConfiguration">
  <state>
    <option name="PREFERRED_PROJECT_CODE_STYLE" value="OldCodeStyle" />
  </state>
</component>

我们可以对文件夹中的所有代码样式配置文件进行递归搜索和替换,并替换每个文件中的 value 字段.首先,请确保按照问题所述将Google Style XML导入IntelliJ,并将其命名为 GoogleStyle .

We can do a recursive search-and-replace on all the code style config files in a folder and replace the value field in each. First, make sure you import the Google Style XML into IntelliJ as described in the question and call it GoogleStyle.

这是 bash 命令. find 递归搜索 codeStyleConfig.xml 的所有路径. sed GoogleStyle 替换当前的代码样式.

Here's the bash command. find recursively searches for all the paths to codeStyleConfig.xml. sed replaces the current code style with GoogleStyle.

find ~/projects -name codeStyleConfig.xml -type f -print0 | xargs -0 sed -i "" -E "s/(.+PREFERRED_PROJECT_CODE_STYLE\" value=\")(.+)(\" \/>)/\1GoogleStyle\3/"

此命令已在MacOS 11和IntelliJ 2020.2上进行了测试.我尚未在Linux上进行测试.要了解该命令的工作原理,请参见这里.对于Windows,您可以使用Notepad ++进行正则表达式查找和替换,安装 Cygwin 之类的bash库或查找 PowerShell 等效项(请参见).

This command has been tested on MacOS 11 and IntelliJ 2020.2. I have not tested on Linux. To understand how the command works, see here and here. For Windows, you can use Notepad++ to do a regex find-and-replace, install a bash library like Cygwin or find PowerShell equivalents (see here and here).

这篇关于为所有本地项目切换活动的IntelliJ代码样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆