From 2b01c41b31dab3592fa2a3def61bc5eb1d27916b Mon Sep 17 00:00:00 2001 From: Callum Rogers Date: Tue, 25 Jul 2023 17:16:26 +0100 Subject: [PATCH] Actually ensure we optimise on save --- .../gradle/ConfigureJavaFormatterXml.groovy | 22 +- .../gradle/PalantirJavaFormatIdeaPlugin.java | 6 +- .../ConfigureJavaFormatterXmlTest.groovy | 208 +++++------------- 3 files changed, 78 insertions(+), 158 deletions(-) diff --git a/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXml.groovy b/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXml.groovy index 12e1261d..8aa24d2e 100644 --- a/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXml.groovy +++ b/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXml.groovy @@ -35,11 +35,21 @@ class ConfigureJavaFormatterXml { matchOrCreateChild(externalDependencies, 'plugin', [id: 'palantir-java-format']) } - static void configureFormatOnSave(Node rootNode) { - def formatOnSaveOptions = matchOrCreateChild(rootNode, 'component', [name: 'FormatOnSaveOptions']) + static void configureWorkspaceXml(Node rootNode) { + configureFormatOnSave(rootNode) + configureOptimizeOnSave(rootNode) + } + + private static void configureFormatOnSave(Node rootNode) { + configureOnSaveAction(matchOrCreateChild(rootNode, 'component', [name: 'FormatOnSaveOptions'])) + } + private static void configureOptimizeOnSave(Node rootNode) { + configureOnSaveAction(matchOrCreateChild(rootNode, 'component', [name: 'OptimizeOnSaveOptions'])) + } - def myRunOnSave = matchOrCreateChild(formatOnSaveOptions, 'option', [name: 'myRunOnSave']) + private static void configureOnSaveAction(Node onSaveOptions) { + def myRunOnSave = matchOrCreateChild(onSaveOptions, 'option', [name: 'myRunOnSave']) def myRunOnSaveAlreadySet = Optional.ofNullable(myRunOnSave.attribute('value')) .map(Boolean::parseBoolean) @@ -47,7 +57,7 @@ class ConfigureJavaFormatterXml { myRunOnSave.attributes().put('value', 'true') - def myAllFileTypesSelectedAlreadySet = matchChild(formatOnSaveOptions, 'option', [name: 'myAllFileTypesSelected']) + def myAllFileTypesSelectedAlreadySet = matchChild(onSaveOptions, 'option', [name: 'myAllFileTypesSelected']) .map { Boolean.parseBoolean(it.attribute('value')) } .orElse(true) @@ -59,10 +69,10 @@ class ConfigureJavaFormatterXml { } // Otherwise we setup intellij to not format all files... - matchOrCreateChild(formatOnSaveOptions, 'option', [name: 'myAllFileTypesSelected']).attributes().put('value', 'false') + matchOrCreateChild(onSaveOptions, 'option', [name: 'myAllFileTypesSelected']).attributes().put('value', 'false') // ...but ensure java is formatted - def mySelectedFileTypes = matchOrCreateChild(formatOnSaveOptions, 'option', [name: 'mySelectedFileTypes']) + def mySelectedFileTypes = matchOrCreateChild(onSaveOptions, 'option', [name: 'mySelectedFileTypes']) def set = matchOrCreateChild(mySelectedFileTypes, 'set') matchOrCreateChild(set, 'option', [value: 'JAVA']) } diff --git a/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatIdeaPlugin.java b/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatIdeaPlugin.java index dba0a414..9a78f207 100644 --- a/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatIdeaPlugin.java +++ b/gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatIdeaPlugin.java @@ -68,7 +68,7 @@ private static void configureLegacyIdea(Project project, Configuration implConfi }); ideaModel.getWorkspace().getIws().withXml(xmlProvider -> { - ConfigureJavaFormatterXml.configureFormatOnSave(xmlProvider.asNode()); + ConfigureJavaFormatterXml.configureWorkspaceXml(xmlProvider.asNode()); }); } @@ -91,7 +91,7 @@ private static void configureIntelliJImport(Project project, Configuration implC project.file(".idea/externalDependencies.xml"), node -> ConfigureJavaFormatterXml.configureExternalDependencies(node)); createOrUpdateIdeaXmlFile( - project.file(".idea/workspace.xml"), node -> ConfigureJavaFormatterXml.configureFormatOnSave(node)); + project.file(".idea/workspace.xml"), node -> ConfigureJavaFormatterXml.configureWorkspaceXml(node)); // Still configure legacy idea if using intellij import updateIdeaXmlFileIfExists(project.file(project.getName() + ".ipr"), node -> { @@ -99,7 +99,7 @@ private static void configureIntelliJImport(Project project, Configuration implC ConfigureJavaFormatterXml.configureExternalDependencies(node); }); updateIdeaXmlFileIfExists(project.file(project.getName() + ".iws"), node -> { - ConfigureJavaFormatterXml.configureFormatOnSave(node); + ConfigureJavaFormatterXml.configureWorkspaceXml(node); }); }); } diff --git a/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXmlTest.groovy b/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXmlTest.groovy index c15e5cf2..24bfca12 100644 --- a/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXmlTest.groovy +++ b/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXmlTest.groovy @@ -17,6 +17,7 @@ package com.palantir.javaformat.gradle import spock.lang.Specification +import spock.lang.Unroll class ConfigureJavaFormatterXmlTest extends Specification { @@ -60,6 +61,7 @@ class ConfigureJavaFormatterXmlTest extends Specification { """.stripIndent() + public static final ArrayList ACTIONS_ON_SAVE = ['Format', 'Optimize'] void testConfigure_missingEntireBlock_added() { def node = new XmlParser().parseText(MISSING_ENTIRE_BLOCK) @@ -104,7 +106,8 @@ class ConfigureJavaFormatterXmlTest extends Specification { xmlToString(node) == EXPECTED } - void 'adds FormatOnSave block where none exists'() { + @Unroll + void 'adds #action OnSave block where none exists'(action) { // language=xml def node = new XmlParser().parseText ''' @@ -112,33 +115,34 @@ class ConfigureJavaFormatterXmlTest extends Specification { '''.stripIndent(true) when: - ConfigureJavaFormatterXml.configureFormatOnSave(node) - def newXml = xmlToString(node).strip() + ConfigureJavaFormatterXml.configureWorkspaceXml(node) + + def newXml = xmlSubcomponentToString(node, "${action}OnSaveOptions").strip() then: - // language=xml - def expected = ''' - - - - - - '''.stripIndent(true).strip() + def expected = """ + + + + """.stripIndent(true).strip() newXml == expected + + where: + action << ACTIONS_ON_SAVE } - void 'adds Java to existing FormatOnSave block'() { - // language=xml - def node = new XmlParser().parseText ''' + @Unroll + void 'adds Java to existing #action OnSave block'(action) { + def node = new XmlParser().parseText """ - + - '''.stripIndent(true) + """.stripIndent(true) when: - ConfigureJavaFormatterXml.configureFormatOnSave(node) - def newXml = xmlToString(node).strip() + ConfigureJavaFormatterXml.configureWorkspaceXml(node) + def newXml = xmlSubcomponentToString(node, "${action}OnSaveOptions") then: - // language=xml - def expected = ''' - - - - - - '''.stripIndent(true).strip() + def expected = """ + + + + """.stripIndent(true).strip() newXml == expected + + where: + action << ACTIONS_ON_SAVE } - void 'if all file types are already formatted on save, dont change anything'() { - // language=xml - def node = new XmlParser().parseText ''' + @Unroll + void 'if all file types are already configured to #action on save, dont change anything'() { + def node = new XmlParser().parseText """ - + - '''.stripIndent(true) + """.stripIndent(true) when: - ConfigureJavaFormatterXml.configureFormatOnSave(node) - def newXml = xmlToString(node).strip() + ConfigureJavaFormatterXml.configureWorkspaceXml(node) + def newXml = xmlSubcomponentToString(node, "${action}OnSaveOptions").strip() then: - // language=xml - def expected = ''' - - - - - '''.stripIndent(true).strip() - - newXml == expected - } - - void 'adds OptimizeOnSave block where none exists'() { - // language=xml - def node = new XmlParser().parseText ''' - - - '''.stripIndent(true) - - when: - ConfigureJavaFormatterXml.configureFormatOnSave(node) - def newXml = xmlToString(node).strip() - - then: - // language=xml - def expected = ''' - - - - - - '''.stripIndent(true).strip() + def expected = """ + + + """.stripIndent(true).strip() newXml == expected - } - - void 'adds Java to existing OptimizeOnSave block'() { - // language=xml - def node = new XmlParser().parseText ''' - - - - - - '''.stripIndent(true) - - when: - ConfigureJavaFormatterXml.configureFormatOnSave(node) - def newXml = xmlToString(node).strip() - - then: - // language=xml - def expected = ''' - - - - - - '''.stripIndent(true).strip() - newXml == expected + where: + action << ACTIONS_ON_SAVE } - void 'if all file types are already optimised on save, dont change anything'() { - // language=xml - def node = new XmlParser().parseText ''' - - - - - '''.stripIndent(true) - - when: - ConfigureJavaFormatterXml.configureFormatOnSave(node) - def newXml = xmlToString(node).strip() - - then: - // language=xml - def expected = ''' - - - - - '''.stripIndent(true).strip() - - newXml == expected + private static String xmlSubcomponentToString(Node node, String name) { + xmlToString(node.children().find { it.@name == name }).strip() } static String xmlToString(Node node) {