Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto formatter #12

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-12.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: auto formatter
links:
- https://github.com/palantir/gradle-consistent-versions-idea-plugin/pull/12
Original file line number Diff line number Diff line change
@@ -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 final List<Block> buildChildren() {
List<Block> 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 final Indent getIndent() {
return Indent.getNoneIndent();
}

@Nullable
@Override
public final Spacing getSpacing(@Nullable Block child1, Block child2) {
return spacingBuilder.getSpacing(this, child1, child2);
}

@Override
public final boolean isLeaf() {
return myNode.getFirstChildNode() == null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* (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.EQUALS)
.spaceIf(settings.getCommonSettings(VersionPropsLanguage.INSTANCE.getID())
.SPACE_AROUND_ASSIGNMENT_OPERATORS)
.before(VersionPropsTypes.PROPERTY)
.none();
}

@Override
public final 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<lang.parserDefinition language="VersionProps" implementationClass="com.palantir.gradle.versions.intellij.VersionPropsParserDefinition"/>
<lang.syntaxHighlighterFactory language="VersionProps"
implementationClass="com.palantir.gradle.versions.intellij.VersionPropsSyntaxHighlighterFactory"/>
<lang.formatter language="VersionProps" implementationClass="com.palantir.gradle.versions.intellij.VersionPropsFormattingModelBuilder"/>
<completion.contributor language="VersionProps"
implementationClass="com.palantir.gradle.versions.intellij.VersionCompletionContributor" order="last"/>
<completion.contributor language="VersionProps"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
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.testFramework.UsefulTestCase;
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase5;
Expand Down Expand Up @@ -77,4 +79,18 @@ public void test_other_file_names() throws Exception {
List<String> lookupElementStrings = fixture.getLookupElementStrings();
UsefulTestCase.assertEmpty(lookupElementStrings);
}

@Test
public void test_formatter() {
JavaCodeInsightTestFixture fixture = getFixture();
fixture.configureByText(
"versions.props",
"groupPart1.groupPart2:packageName = version \ngroupPart1.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");
}
}