Skip to content

Commit

Permalink
Actually ensure we optimise on save
Browse files Browse the repository at this point in the history
  • Loading branch information
CRogers committed Jul 25, 2023
1 parent b6bf1fd commit 2b01c41
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,29 @@ 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)
.orElse(false)

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)

Expand All @@ -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'])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
}

Expand All @@ -91,15 +91,15 @@ 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 -> {
ConfigureJavaFormatterXml.configureJavaFormat(node, uris);
ConfigureJavaFormatterXml.configureExternalDependencies(node);
});
updateIdeaXmlFileIfExists(project.file(project.getName() + ".iws"), node -> {
ConfigureJavaFormatterXml.configureFormatOnSave(node);
ConfigureJavaFormatterXml.configureWorkspaceXml(node);
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.palantir.javaformat.gradle

import spock.lang.Specification
import spock.lang.Unroll

class ConfigureJavaFormatterXmlTest extends Specification {

Expand Down Expand Up @@ -60,6 +61,7 @@ class ConfigureJavaFormatterXmlTest extends Specification {
</component>
</root>
""".stripIndent()
public static final ArrayList<String> ACTIONS_ON_SAVE = ['Format', 'Optimize']

void testConfigure_missingEntireBlock_added() {
def node = new XmlParser().parseText(MISSING_ENTIRE_BLOCK)
Expand Down Expand Up @@ -104,41 +106,43 @@ 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 '''
<root>
</root>
'''.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 = '''
<root>
<component name="FormatOnSaveOptions">
<option name="myRunOnSave" value="true"/>
<option name="myAllFileTypesSelected" value="false"/>
<option name="mySelectedFileTypes">
<set>
<option value="JAVA"/>
</set>
</option>
</component>
</root>
'''.stripIndent(true).strip()
def expected = """
<component name="${action}OnSaveOptions">
<option name="myRunOnSave" value="true"/>
<option name="myAllFileTypesSelected" value="false"/>
<option name="mySelectedFileTypes">
<set>
<option value="JAVA"/>
</set>
</option>
</component>
""".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 """
<root>
<component name="FormatOnSaveOptions">
<component name="${action}OnSaveOptions">
<option name="myRunOnSave" value="true"/>
<option name="myAllFileTypesSelected" value="false"/>
<option name="mySelectedFileTypes">
Expand All @@ -148,156 +152,62 @@ class ConfigureJavaFormatterXmlTest extends Specification {
</option>
</component>
</root>
'''.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 = '''
<root>
<component name="FormatOnSaveOptions">
<option name="myRunOnSave" value="true"/>
<option name="myAllFileTypesSelected" value="false"/>
<option name="mySelectedFileTypes">
<set>
<option value="Go"/>
<option value="JAVA"/>
</set>
</option>
</component>
</root>
'''.stripIndent(true).strip()
def expected = """
<component name="${action}OnSaveOptions">
<option name="myRunOnSave" value="true"/>
<option name="myAllFileTypesSelected" value="false"/>
<option name="mySelectedFileTypes">
<set>
<option value="Go"/>
<option value="JAVA"/>
</set>
</option>
</component>
""".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 """
<root>
<component name="FormatOnSaveOptions">
<component name="${action}OnSaveOptions">
<option name="myRunOnSave" value="true"/>
<!-- if myAllFileTypesSelected does not exist, it defaults to true -->
</component>
</root>
'''.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 = '''
<root>
<component name="FormatOnSaveOptions">
<option name="myRunOnSave" value="true"/>
</component>
</root>
'''.stripIndent(true).strip()

newXml == expected
}

void 'adds OptimizeOnSave block where none exists'() {
// language=xml
def node = new XmlParser().parseText '''
<root>
</root>
'''.stripIndent(true)

when:
ConfigureJavaFormatterXml.configureFormatOnSave(node)
def newXml = xmlToString(node).strip()

then:
// language=xml
def expected = '''
<root>
<component name="OptimizeOnSaveOptions">
<option name="myRunOnSave" value="true"/>
<option name="myAllFileTypesSelected" value="false"/>
<option name="mySelectedFileTypes">
<set>
<option value="JAVA"/>
</set>
</option>
</component>
</root>
'''.stripIndent(true).strip()
def expected = """
<component name="${action}OnSaveOptions">
<option name="myRunOnSave" value="true"/>
</component>
""".stripIndent(true).strip()

newXml == expected
}

void 'adds Java to existing OptimizeOnSave block'() {
// language=xml
def node = new XmlParser().parseText '''
<root>
<component name="OptimizeOnSaveOptions">
<option name="myRunOnSave" value="true"/>
<option name="myAllFileTypesSelected" value="false"/>
<option name="mySelectedFileTypes">
<set>
<option value="Go"/>
</set>
</option>
</component>
</root>
'''.stripIndent(true)

when:
ConfigureJavaFormatterXml.configureFormatOnSave(node)
def newXml = xmlToString(node).strip()

then:
// language=xml
def expected = '''
<root>
<component name="OptimizeOnSaveOptions">
<option name="myRunOnSave" value="true"/>
<option name="myAllFileTypesSelected" value="false"/>
<option name="mySelectedFileTypes">
<set>
<option value="Go"/>
<option value="JAVA"/>
</set>
</option>
</component>
</root>
'''.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 '''
<root>
<component name="FormatOnSaveOptions">
<option name="myRunOnSave" value="true"/>
<!-- if myAllFileTypesSelected does not exist, it defaults to true -->
</component>
</root>
'''.stripIndent(true)

when:
ConfigureJavaFormatterXml.configureFormatOnSave(node)
def newXml = xmlToString(node).strip()

then:
// language=xml
def expected = '''
<root>
<component name="OptimizeOnSaveOptions">
<option name="myRunOnSave" value="true"/>
</component>
</root>
'''.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) {
Expand Down

0 comments on commit 2b01c41

Please sign in to comment.