diff --git a/changelog/@unreleased/pr-1112.v2.yml b/changelog/@unreleased/pr-1112.v2.yml
new file mode 100644
index 000000000..98431b879
--- /dev/null
+++ b/changelog/@unreleased/pr-1112.v2.yml
@@ -0,0 +1,5 @@
+type: fix
+fix:
+ description: Ensure Actions on Save is configured properly in IntelliJ <=2023.3.6
+ links:
+ - https://github.com/palantir/palantir-java-format/pull/1112
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 b117cb290..90fe0711d 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
@@ -49,28 +49,21 @@ class ConfigureJavaFormatterXml {
}
private static void configureOnSaveAction(Node onSaveOptions) {
- // If myRunOnSave is set to true, IntelliJ removes it. If it's set to false, we still need to remove it to run
- // the formatter. So we should just remove it so we do run on save.
- matchChild(onSaveOptions, 'option', [name: 'myRunOnSave']).ifPresent { myRunOnSave ->
- onSaveOptions.remove(myRunOnSave)
- }
-
- def myAllFilesTypesSelected = matchChild(onSaveOptions, 'option', [name: 'myAllFileTypesSelected'])
- def myAllFileTypesSelectedAlreadySet = myAllFilesTypesSelected
- .map { Boolean.parseBoolean(it.attribute('value')) }
- // If myAllFileTypesSelected is elided then it is disabled by default
- .orElse(false)
-
- if (myAllFileTypesSelectedAlreadySet) {
- // If the user has already configured IntelliJ to format all file types and turned on formatting on save,
- // we leave the configuration as is as it will format java code, and we don't want to disable formatting
- // for other file types
- return
- }
+ // In IntelliJ <2023.3.7, the myRunOnSave option is default false.
+ // In IntelliJ >=2023.3.7, it is default true, and if it is set to true IntelliJ will remove it from the XML
+ // the next time the file is saved or the actions on save options window is saved.
+ // So to cover all cases, we always write it out and ensure it is true
+ def myRunOnSave = matchOrCreateChild(onSaveOptions, 'option', [name: 'myRunOnSave'])
+ myRunOnSave.attributes().put('value', 'true')
- myAllFilesTypesSelected.ifPresent { onSaveOptions.remove(it) }
+ // In IntelliJ <2023.3.7, the myAllFileTypesSelected option is default true.
+ // In IntelliJ >=2023.3.7, it is default false, and if it is set to false IntelliJ will remove it from the XML
+ // the next time the file is saved or the actions on save options window is saved.
+ // People may have manually set this to true, so we respect that setting is so. However, if it doesn't exist,
+ // we make sure it is explicitly set false to work in all IntelliJ versions.
+ matchOrCreateChild(onSaveOptions, 'option', [name: 'myAllFileTypesSelected'], [value: 'false'])
- // ...but ensure java is formatted
+ // Ensure that is per-file type settings are enabled, we ensure JAVA is part of the list.
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/test/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXmlTest.groovy b/gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/ConfigureJavaFormatterXmlTest.groovy
index 8b5a125a1..91c0faed5 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
@@ -122,6 +122,8 @@ class ConfigureJavaFormatterXmlTest extends Specification {
then:
def expected = """
+
+
+