From d3ae4ca5d45bb81bddb22a3f1a837926e1c3cbe9 Mon Sep 17 00:00:00 2001 From: Finlay Williams Date: Mon, 23 Sep 2024 15:21:22 +0100 Subject: [PATCH 1/7] auto formatter --- .../versions/intellij/VersionPropsBlock.java | 73 +++++++++++++++++++ .../VersionPropsFormattingModelBuilder.java | 56 ++++++++++++++ .../src/main/resources/META-INF/plugin.xml | 1 + 3 files changed, 130 insertions(+) create mode 100644 gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsBlock.java create mode 100644 gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsFormattingModelBuilder.java diff --git a/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsBlock.java b/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsBlock.java new file mode 100644 index 0000000..56be8c9 --- /dev/null +++ b/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsBlock.java @@ -0,0 +1,73 @@ +/* + * (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.gradle.versions.intellij; + +import com.intellij.formatting.Alignment; +import com.intellij.formatting.Block; +import com.intellij.formatting.Indent; +import com.intellij.formatting.Spacing; +import com.intellij.formatting.SpacingBuilder; +import com.intellij.formatting.Wrap; +import com.intellij.formatting.WrapType; +import com.intellij.lang.ASTNode; +import com.intellij.psi.TokenType; +import com.intellij.psi.formatter.common.AbstractBlock; +import java.util.ArrayList; +import java.util.List; +import org.jetbrains.annotations.Nullable; + +public class VersionPropsBlock extends AbstractBlock { + + private final SpacingBuilder spacingBuilder; + + protected VersionPropsBlock( + ASTNode node, @Nullable Wrap wrap, @Nullable Alignment alignment, SpacingBuilder spacingBuilder) { + super(node, wrap, alignment); + this.spacingBuilder = spacingBuilder; + } + + @Override + protected List buildChildren() { + List blocks = new ArrayList<>(); + ASTNode child = myNode.getFirstChildNode(); + while (child != null) { + if (child.getElementType() != TokenType.WHITE_SPACE) { + Block block = new VersionPropsBlock( + child, Wrap.createWrap(WrapType.NONE, false), Alignment.createAlignment(), spacingBuilder); + blocks.add(block); + } + child = child.getTreeNext(); + } + return blocks; + } + + @Override + public Indent getIndent() { + return Indent.getNoneIndent(); + } + + @Nullable + @Override + public Spacing getSpacing(@Nullable Block child1, Block child2) { + return spacingBuilder.getSpacing(this, child1, child2); + } + + @Override + public boolean isLeaf() { + return myNode.getFirstChildNode() == null; + } +} diff --git a/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsFormattingModelBuilder.java b/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsFormattingModelBuilder.java new file mode 100644 index 0000000..b34ee11 --- /dev/null +++ b/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsFormattingModelBuilder.java @@ -0,0 +1,56 @@ +/* + * (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.gradle.versions.intellij; + +import com.intellij.formatting.Alignment; +import com.intellij.formatting.Block; +import com.intellij.formatting.FormattingContext; +import com.intellij.formatting.FormattingModel; +import com.intellij.formatting.FormattingModelBuilder; +import com.intellij.formatting.FormattingModelProvider; +import com.intellij.formatting.SpacingBuilder; +import com.intellij.formatting.Wrap; +import com.intellij.formatting.WrapType; +import com.intellij.lang.ASTNode; +import com.intellij.psi.codeStyle.CodeStyleSettings; +import com.palantir.gradle.versions.intellij.psi.VersionPropsTypes; + +public class VersionPropsFormattingModelBuilder implements FormattingModelBuilder { + private static SpacingBuilder createSpaceBuilder(CodeStyleSettings settings) { + return new SpacingBuilder(settings, VersionPropsLanguage.INSTANCE) + .around(VersionPropsTypes.COLON) + .none() + .around(VersionPropsTypes.EQUALS) + .spaceIf(settings.getCommonSettings(VersionPropsLanguage.INSTANCE.getID()) + .SPACE_AROUND_ASSIGNMENT_OPERATORS) + .before(VersionPropsTypes.PROPERTY) + .none(); + } + + @Override + public FormattingModel createModel(FormattingContext formattingContext) { + final CodeStyleSettings codeStyleSettings = formattingContext.getCodeStyleSettings(); + ASTNode root = formattingContext.getNode(); + Block block = new VersionPropsBlock( + root, + Wrap.createWrap(WrapType.NONE, false), + Alignment.createAlignment(), + createSpaceBuilder(codeStyleSettings)); + return FormattingModelProvider.createFormattingModelForPsiFile( + formattingContext.getContainingFile(), block, codeStyleSettings); + } +} diff --git a/gradle-consistent-versions-idea-plugin/src/main/resources/META-INF/plugin.xml b/gradle-consistent-versions-idea-plugin/src/main/resources/META-INF/plugin.xml index b2fb788..e7ddfc2 100644 --- a/gradle-consistent-versions-idea-plugin/src/main/resources/META-INF/plugin.xml +++ b/gradle-consistent-versions-idea-plugin/src/main/resources/META-INF/plugin.xml @@ -16,6 +16,7 @@ + Date: Mon, 23 Sep 2024 15:26:33 +0100 Subject: [PATCH 2/7] fix style --- .../gradle/versions/intellij/VersionPropsBlock.java | 8 ++++---- .../intellij/VersionPropsFormattingModelBuilder.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsBlock.java b/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsBlock.java index 56be8c9..29ae325 100644 --- a/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsBlock.java +++ b/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsBlock.java @@ -41,7 +41,7 @@ protected VersionPropsBlock( } @Override - protected List buildChildren() { + protected final List buildChildren() { List blocks = new ArrayList<>(); ASTNode child = myNode.getFirstChildNode(); while (child != null) { @@ -56,18 +56,18 @@ protected List buildChildren() { } @Override - public Indent getIndent() { + public final Indent getIndent() { return Indent.getNoneIndent(); } @Nullable @Override - public Spacing getSpacing(@Nullable Block child1, Block child2) { + public final Spacing getSpacing(@Nullable Block child1, Block child2) { return spacingBuilder.getSpacing(this, child1, child2); } @Override - public boolean isLeaf() { + public final boolean isLeaf() { return myNode.getFirstChildNode() == null; } } diff --git a/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsFormattingModelBuilder.java b/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsFormattingModelBuilder.java index b34ee11..9db66ce 100644 --- a/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsFormattingModelBuilder.java +++ b/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsFormattingModelBuilder.java @@ -42,7 +42,7 @@ private static SpacingBuilder createSpaceBuilder(CodeStyleSettings settings) { } @Override - public FormattingModel createModel(FormattingContext formattingContext) { + public final FormattingModel createModel(FormattingContext formattingContext) { final CodeStyleSettings codeStyleSettings = formattingContext.getCodeStyleSettings(); ASTNode root = formattingContext.getNode(); Block block = new VersionPropsBlock( From 8de92c196e9c7067e345de80af0214072c8e3fe1 Mon Sep 17 00:00:00 2001 From: svc-changelog Date: Mon, 23 Sep 2024 14:26:56 +0000 Subject: [PATCH 3/7] Add generated changelog entries --- changelog/@unreleased/pr-12.v2.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/@unreleased/pr-12.v2.yml diff --git a/changelog/@unreleased/pr-12.v2.yml b/changelog/@unreleased/pr-12.v2.yml new file mode 100644 index 0000000..11b1f45 --- /dev/null +++ b/changelog/@unreleased/pr-12.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: auto formatter + links: + - https://github.com/palantir/gradle-consistent-versions-idea-plugin/pull/12 From 83c3804470c58378e9d7ffe0f5b69f7f952a2655 Mon Sep 17 00:00:00 2001 From: Finlay Williams Date: Tue, 24 Sep 2024 09:26:17 +0100 Subject: [PATCH 4/7] formatting test --- .../intellij/VersionPropsCodeInsightTest.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/gradle-consistent-versions-idea-plugin/src/test/java/com/palantir/gradle/versions/intellij/VersionPropsCodeInsightTest.java b/gradle-consistent-versions-idea-plugin/src/test/java/com/palantir/gradle/versions/intellij/VersionPropsCodeInsightTest.java index dc928b8..69c475f 100644 --- a/gradle-consistent-versions-idea-plugin/src/test/java/com/palantir/gradle/versions/intellij/VersionPropsCodeInsightTest.java +++ b/gradle-consistent-versions-idea-plugin/src/test/java/com/palantir/gradle/versions/intellij/VersionPropsCodeInsightTest.java @@ -16,6 +16,8 @@ package com.palantir.gradle.versions.intellij; import com.intellij.codeInsight.completion.CompletionType; +import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.testFramework.UsefulTestCase; import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture; import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase5; @@ -76,4 +78,18 @@ public void test_other_file_names() throws Exception { List lookupElementStrings = fixture.getLookupElementStrings(); UsefulTestCase.assertEmpty(lookupElementStrings); } + + @Test + public void test_formatter() { + JavaCodeInsightTestFixture fixture = getFixture(); + fixture.configureByText( + "versions.props", + "groupPart1.groupPart2 : packageName = version \n groupPart1.groupPart2:packageName=version"); + + WriteCommandAction.writeCommandAction(fixture.getProject()).run(() -> CodeStyleManager.getInstance( + fixture.getProject()) + .reformatText(fixture.getFile(), List.of(fixture.getFile().getTextRange()))); + fixture.checkResult( + "groupPart1.groupPart2:packageName = version \ngroupPart1.groupPart2:packageName = version"); + } } From c1bcbefc8f6a657679c24e7caba9cfe12decea2a Mon Sep 17 00:00:00 2001 From: Finlay Williams Date: Thu, 26 Sep 2024 15:02:39 +0100 Subject: [PATCH 5/7] no space around dot --- .../intellij/VersionPropsFormattingModelBuilder.java | 2 ++ .../versions/intellij/VersionPropsCodeInsightTest.java | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsFormattingModelBuilder.java b/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsFormattingModelBuilder.java index 9db66ce..2c1ab9a 100644 --- a/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsFormattingModelBuilder.java +++ b/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsFormattingModelBuilder.java @@ -32,6 +32,8 @@ public class VersionPropsFormattingModelBuilder implements FormattingModelBuilder { private static SpacingBuilder createSpaceBuilder(CodeStyleSettings settings) { return new SpacingBuilder(settings, VersionPropsLanguage.INSTANCE) + .around(VersionPropsTypes.DOT) + .none() .around(VersionPropsTypes.COLON) .none() .around(VersionPropsTypes.EQUALS) diff --git a/gradle-consistent-versions-idea-plugin/src/test/java/com/palantir/gradle/versions/intellij/VersionPropsCodeInsightTest.java b/gradle-consistent-versions-idea-plugin/src/test/java/com/palantir/gradle/versions/intellij/VersionPropsCodeInsightTest.java index 5755431..de70569 100644 --- a/gradle-consistent-versions-idea-plugin/src/test/java/com/palantir/gradle/versions/intellij/VersionPropsCodeInsightTest.java +++ b/gradle-consistent-versions-idea-plugin/src/test/java/com/palantir/gradle/versions/intellij/VersionPropsCodeInsightTest.java @@ -18,18 +18,19 @@ import static org.assertj.core.api.Assertions.assertThat; import com.intellij.codeInsight.completion.CompletionType; -import com.intellij.openapi.command.WriteCommandAction; -import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.command.WriteCommandAction; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.TokenType; +import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.testFramework.UsefulTestCase; import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture; import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase5; import com.palantir.gradle.versions.intellij.psi.VersionPropsTypes; import java.util.List; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; public class VersionPropsCodeInsightTest extends LightJavaCodeInsightFixtureTestCase5 { From 421c16d53f66bdbbe306fdc26515ce40b7e2ec64 Mon Sep 17 00:00:00 2001 From: Finlay Williams Date: Thu, 26 Sep 2024 15:09:45 +0100 Subject: [PATCH 6/7] test was wrong --- .../gradle/versions/intellij/VersionPropsCodeInsightTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle-consistent-versions-idea-plugin/src/test/java/com/palantir/gradle/versions/intellij/VersionPropsCodeInsightTest.java b/gradle-consistent-versions-idea-plugin/src/test/java/com/palantir/gradle/versions/intellij/VersionPropsCodeInsightTest.java index de70569..231fa7c 100644 --- a/gradle-consistent-versions-idea-plugin/src/test/java/com/palantir/gradle/versions/intellij/VersionPropsCodeInsightTest.java +++ b/gradle-consistent-versions-idea-plugin/src/test/java/com/palantir/gradle/versions/intellij/VersionPropsCodeInsightTest.java @@ -186,7 +186,7 @@ public void test_formatter() { WriteCommandAction.writeCommandAction(fixture.getProject()).run(() -> CodeStyleManager.getInstance( fixture.getProject()) .reformatText(fixture.getFile(), List.of(fixture.getFile().getTextRange()))); - fixture.checkResult( - "groupPart1.groupPart2:packageName = version \ngroupPart1.groupPart2:packageName = version"); + System.out.println(fixture.getFile().getText()); + fixture.checkResult("groupPart1.groupPart2:packageName = version\ngroupPart1.groupPart2:packageName = version"); } } From d4a7b1ef01cda0d60d3dbf8e30fec7691ccc5f3c Mon Sep 17 00:00:00 2001 From: Finlay Williams Date: Tue, 8 Oct 2024 10:39:19 +0100 Subject: [PATCH 7/7] simplified --- .../versions/intellij/VersionPropsFormattingModelBuilder.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsFormattingModelBuilder.java b/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsFormattingModelBuilder.java index 2c1ab9a..95369cd 100644 --- a/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsFormattingModelBuilder.java +++ b/gradle-consistent-versions-idea-plugin/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsFormattingModelBuilder.java @@ -32,10 +32,6 @@ public class VersionPropsFormattingModelBuilder implements FormattingModelBuilder { private static SpacingBuilder createSpaceBuilder(CodeStyleSettings settings) { return new SpacingBuilder(settings, VersionPropsLanguage.INSTANCE) - .around(VersionPropsTypes.DOT) - .none() - .around(VersionPropsTypes.COLON) - .none() .around(VersionPropsTypes.EQUALS) .spaceIf(settings.getCommonSettings(VersionPropsLanguage.INSTANCE.getID()) .SPACE_AROUND_ASSIGNMENT_OPERATORS)