[java] add chop/join actions for record components: IDEA-264997

GitOrigin-RevId: 6c832eaeeac6f65678ae8a8ec760cc4d57ab6151
This commit is contained in:
Roman Ivanov
2021-03-23 20:20:32 +00:00
committed by intellij-monorepo-bot
parent e258d8e96c
commit c97105f244
18 changed files with 188 additions and 1 deletions
@@ -2088,6 +2088,14 @@
<className>com.intellij.codeInsight.intention.impl.lists.JavaJoinArgumentsAction</className>
<category>Java/Other</category>
</intentionAction>
<intentionAction>
<className>com.intellij.codeInsight.intention.impl.lists.JavaChopRecordComponentsAction</className>
<category>Java/Other</category>
</intentionAction>
<intentionAction>
<className>com.intellij.codeInsight.intention.impl.lists.JavaJoinRecordComponentsAction</className>
<category>Java/Other</category>
</intentionAction>
<intentionAction>
<className>com.intellij.codeInsight.intention.impl.ConvertCompactConstructorToCanonicalAction</className>
<category>Java/Declaration</category>
@@ -0,0 +1,47 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.intention.impl.lists;
import com.intellij.java.JavaBundle;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.List;
public class JavaChopRecordComponentsAction extends AbstractJavaChopListAction<PsiRecordHeader, PsiRecordComponent> {
@Override
@Nullable("When failed") PsiRecordHeader extractList(@NotNull PsiElement element) {
return PsiTreeUtil.getParentOfType(element, PsiRecordHeader.class, false, PsiCodeBlock.class, PsiExpression.class);
}
@Override
@Nullable("When failed") List<PsiRecordComponent> getElements(@NotNull PsiRecordHeader list) {
return Arrays.asList(list.getRecordComponents());
}
@Override
boolean needTailBreak(@NotNull PsiRecordComponent last) {
return true;
}
@Override
boolean needHeadBreak(@NotNull PsiRecordComponent first) {
return true;
}
@Nls(capitalization = Nls.Capitalization.Sentence)
@NotNull
@Override
public String getFamilyName() {
return JavaBundle.message("intention.family.put.record.components.on.separate.lines");
}
@NotNull
@Override
public String getText() {
return getFamilyName();
}
}
@@ -0,0 +1,47 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.intention.impl.lists;
import com.intellij.java.JavaBundle;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.List;
public class JavaJoinRecordComponentsAction extends AbstractJavaJoinListAction<PsiRecordHeader, PsiRecordComponent> {
@Override
@Nullable("When failed") PsiRecordHeader extractList(@NotNull PsiElement element) {
return PsiTreeUtil.getParentOfType(element, PsiRecordHeader.class, false, PsiCodeBlock.class, PsiExpression.class);
}
@Override
@Nullable("When failed") List<PsiRecordComponent> getElements(@NotNull PsiRecordHeader list) {
return Arrays.asList(list.getRecordComponents());
}
@Override
boolean needTailBreak(@NotNull PsiRecordComponent last) {
return false;
}
@Override
boolean needHeadBreak(@NotNull PsiRecordComponent first) {
return false;
}
@Nls(capitalization = Nls.Capitalization.Sentence)
@NotNull
@Override
public String getFamilyName() {
return JavaBundle.message("intention.family.put.record.components.on.one.line");
}
@NotNull
@Override
public String getText() {
return getFamilyName();
}
}
@@ -44,7 +44,8 @@ final class JavaListUtils {
private static boolean isValidIntermediateElement(@NotNull PsiElement element) {
return element instanceof PsiWhiteSpace ||
(element instanceof PsiJavaToken && ((PsiJavaToken)element).getTokenType() == JavaTokenType.COMMA);
(element instanceof PsiJavaToken && ((PsiJavaToken)element).getTokenType() == JavaTokenType.COMMA) ||
(element instanceof PsiComment && ((PsiComment)element).getTokenType() == JavaTokenType.C_STYLE_COMMENT);
}
@Nullable
@@ -0,0 +1,4 @@
record R(<spot>
int i,
String s</spot>
) {}
@@ -0,0 +1 @@
record R(<spot>int i, String s</spot>) {}
@@ -0,0 +1,5 @@
<html>
<body>
Puts record components on separate lines.
</body>
</html>
@@ -0,0 +1 @@
record R(<spot>int i, String s</spot>) {}
@@ -0,0 +1,4 @@
record R(<spot>
int i,
String s</spot>
) {}
@@ -0,0 +1,5 @@
<html>
<body>
Puts record components on a single line.
</body>
</html>
@@ -0,0 +1,6 @@
// "Put record components on separate lines" "true"
record R(
int i,
String s
)
@@ -0,0 +1,3 @@
// "Put record components on separate lines" "true"
record R(int i, <caret>String s)
@@ -0,0 +1,6 @@
// "Put record components on separate lines" "false"
record A(
String s/**/,
int i<caret>
) {}
@@ -0,0 +1,3 @@
// "Put record components on one line" "true"
record R(int i, String s)
@@ -0,0 +1,6 @@
// "Put record components on one line" "true"
record R(
int i,
<caret>String s
)
@@ -0,0 +1,19 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.intention;
import com.intellij.codeInsight.daemon.LightIntentionActionTestCase;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
public class JavaChopRecordComponentsActionTest extends LightIntentionActionTestCase {
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/lists/chopRecordComponents";
}
@Override
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
return LightJavaCodeInsightFixtureTestCase.JAVA_16;
}
}
@@ -0,0 +1,19 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.intention;
import com.intellij.codeInsight.daemon.LightIntentionActionTestCase;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
public class JavaJoinRecordComponentsActionTest extends LightIntentionActionTestCase {
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/lists/joinRecordComponents";
}
@Override
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
return LightJavaCodeInsightFixtureTestCase.JAVA_16;
}
}
@@ -802,6 +802,8 @@ intention.family.put.arguments.on.one.line=Put arguments on one line
intention.family.put.arguments.on.separate.lines=Put arguments on separate lines
intention.family.put.parameters.on.one.line=Put parameters on one line
intention.family.put.parameters.on.separate.lines=Put parameters on separate lines
intention.family.put.record.components.on.one.line=Put record components on one line
intention.family.put.record.components.on.separate.lines=Put record components on separate lines
intention.family.replace.optional.ispresent.condition.with.functional.style.expression=Replace Optional.isPresent() condition with functional style expression
intention.family.sort.content=Sort content
intention.family.swap.if.statements=Swap 'if' statements