[java] IDEA-296138 Join lines: join case 1:/case 2: into case 1,2:

GitOrigin-RevId: 0503a15ffcbaa37fb4c12aa769d709b8522f5020
This commit is contained in:
Tagir Valeev
2022-06-17 17:21:03 +00:00
committed by intellij-monorepo-bot
parent e7fd4f11f6
commit 8e02b90e66
7 changed files with 86 additions and 0 deletions
@@ -1189,6 +1189,7 @@
<completion.ml.model implementation="com.intellij.codeInsight.completion.ml.JavaMLRankingProvider"/>
<joinLinesHandler implementation="com.intellij.codeInsight.editorActions.NestedIfJoinLinesHandler"/>
<joinLinesHandler implementation="com.intellij.codeInsight.editorActions.BlockJoinLinesHandler"/>
<joinLinesHandler implementation="com.intellij.codeInsight.editorActions.CaseJoinLinesHandler"/>
<joinLinesHandler implementation="com.intellij.codeInsight.editorActions.ChainCallJoinLinesHandler"/>
<joinLinesHandler implementation="com.intellij.codeInsight.editorActions.AssignmentSequenceJoinLinesHandler"/>
<joinLinesHandler implementation="com.intellij.codeInsight.editorActions.DeclarationJoinLinesHandler"/>
@@ -0,0 +1,37 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.editorActions;
import com.intellij.application.options.CodeStyle;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightingFeature;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.editor.Document;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.NotNull;
public class CaseJoinLinesHandler implements JoinLinesHandlerDelegate {
@Override
public int tryJoinLines(@NotNull final Document document, @NotNull final PsiFile psiFile, final int start, final int end) {
if (!HighlightingFeature.ENHANCED_SWITCH.isAvailable(psiFile)) return -1;
PsiElement elementAtStartLineEnd = psiFile.findElementAt(start);
PsiElement elementAtNextLineStart = psiFile.findElementAt(end);
if (elementAtStartLineEnd == null || elementAtNextLineStart == null) return -1;
if (!PsiUtil.isJavaToken(elementAtStartLineEnd, JavaTokenType.COLON)) return -1;
if (!(elementAtNextLineStart instanceof PsiKeyword) ||
((PsiKeyword)elementAtNextLineStart).getTokenType() != JavaTokenType.CASE_KEYWORD) {
return -1;
}
PsiSwitchLabelStatement firstLabel = PsiTreeUtil.getParentOfType(elementAtStartLineEnd, PsiSwitchLabelStatement.class);
if (firstLabel == null) return -1;
PsiSwitchLabelStatement secondLabel = PsiTreeUtil.getParentOfType(elementAtNextLineStart, PsiSwitchLabelStatement.class);
if (secondLabel == null || secondLabel == firstLabel) return -1;
PsiElement nextToken = PsiTreeUtil.skipWhitespacesForward(elementAtNextLineStart);
if (nextToken == null) return -1;
int replaceStart = elementAtStartLineEnd.getTextRange().getStartOffset();
int replaceEnd = nextToken.getTextRange().getStartOffset();
boolean spaceAfterComma = CodeStyle.getSettings(psiFile).getCommonSettings(JavaLanguage.INSTANCE).SPACE_AFTER_COMMA;
document.replaceString(replaceStart, replaceEnd, "," + (spaceAfterComma ? " " : ""));
return replaceStart;
}
}
@@ -0,0 +1,12 @@
class A {
void test(int x) {
switch (x) {
<selection>case 0:
case 1:
case 2:</selection>
case 3:
System.out.println("hello");
default:
}
}
}
@@ -0,0 +1,10 @@
class A {
void test(int x) {
switch (x) {
<caret>case 0:
case /*hello*/ 1,2,3:
System.out.println("hello");
default:
}
}
}
@@ -0,0 +1,9 @@
class A {
void test(int x) {
switch (x) {
case 0,/*hello*/ 1,2,3:
System.out.println("hello");
default:
}
}
}
@@ -0,0 +1,10 @@
class A {
void test(int x) {
switch (x) {
case 0, 1, 2:
case 3:
System.out.println("hello");
default:
}
}
}
@@ -290,6 +290,13 @@ public class JoinLinesTest extends LightJavaCodeInsightTestCase {
public void testJoinFields2() {doTest(3);}
public void testJoinFields3() {doTest();}
public void testCaseLabels() {doTest();}
public void testCaseLabels2() {
CommonCodeStyleSettings settings = getJavaSettings();
settings.SPACE_AFTER_COMMA = false;
doTest();
}
private void doTest() {
doTest(".java");
}