5

Is there any way to tell the gradle idea plugin to use a custom code style xml when generating the project's files?

I can always copy the xml into "~/Library/Preferences/IntelliJIdea13/codestyles" and then change the code style once I import the project but I would like the gradle plugin to do this automatically for me as part of its generation.

Thanks!

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
jlordiales
  • 195
  • 1
  • 10

2 Answers2

5

Just in case someone else is trying to do this, I managed to solve it using the plugin hooks to modify the project's ipr file before it gets written to disk. Basically, adding the following to your build.gradle:

idea {
  project {
    ipr {
      withXml { provider -> addCodeStyle(provider) }
    }
  }
}

  def addCodeStyle(provider) {
      def project = provider.asNode()
      project.appendNode('component', [name: 'ProjectCodeStyleSettingsManager'])

      def codeStyleNode = findComponent(project, 'ProjectCodeStyleSettingsManager')
      codeStyleNode.appendNode('option', [name: 'USE_PER_PROJECT_SETTINGS', value: 'true'])
      def projectSettingsNode = codeStyleNode.appendNode('option', [name: 'PER_PROJECT_SETTINGS']).appendNode('value')    

      def codeStyleUrl = "fileUrl".toURL()

      //If you want to read from a file you could do new File(path).text
      def codeStyleXml = new XmlParser().parseText(codeStyleUrl.text)
      codeStyleXml.children().each { option ->
          projectSettingsNode.append(option)
      }
  }

This assumes that your xml with the code style preferences follows the format:

<?xml version="1.0" encoding="UTF-8"?>
<code_scheme name="X">
  <option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="6" />
  <option name="NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND" value="9" />

Which I think is the standard format when you export your preferences from IntelliJ.

jlordiales
  • 195
  • 1
  • 10
  • This doesn't appear to work exactly as shown here. In particular, `No signature of method: org.gradle.plugins.ide.api.XmlFileContentMerger.addCodeStyle() is applicable for argument types: (org.gradle.internal.xml.XmlTransformer$XmlProviderImpl)`. I tried figuring out the problem, but I suspect my gradle foo is too weak. – Micah Zoltu Dec 22 '15 at 18:09
  • Got it working, submitted an edit with what I had to do to get it to work. Mainly, needed to define `findComponent(...)` and had to trick gradle into calling my method. – Micah Zoltu Dec 22 '15 at 18:46
2

The Accepted answer didn't work for me with IDEA 2016.2.5. No error, but code style was not applied ( this was after updating the code as suggested in the comments). I ended up using this ( which works with folder based config ):

task copyCodeStyle(type: Copy) {
    from 'codeStyleSettings.xml'
    into '.idea'
}
tasks.idea.dependsOn copyCodeStyle

the codeStyleSettings.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="ProjectCodeStyleSettingsManager">
    <option name="PER_PROJECT_SETTINGS">
      <value>
        <option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="8" />
        <option name="NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND" value="300" />
Alpar
  • 2,807
  • 23
  • 16
  • at this point, what are you gaining over just putting `codeStyleSettings.xml` into version control directly? – Brad Mace Oct 27 '21 at 19:22