mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 04:51:24 +07:00
[java-inspections] IDEA-126383 Shift+Ctrl+j should join multiple declarations of the same type
GitOrigin-RevId: f10e524166d16fbc55f73ed9a0a19fccd74e4739
This commit is contained in:
committed by
intellij-monorepo-bot
parent
bf8c1eb5a2
commit
61d7024bfe
@@ -1197,6 +1197,7 @@
|
||||
<joinLinesHandler implementation="com.intellij.codeInsight.editorActions.ChainCallJoinLinesHandler"/>
|
||||
<joinLinesHandler implementation="com.intellij.codeInsight.editorActions.AssignmentSequenceJoinLinesHandler"/>
|
||||
<joinLinesHandler implementation="com.intellij.codeInsight.editorActions.DeclarationJoinLinesHandler"/>
|
||||
<joinLinesHandler implementation="com.intellij.codeInsight.editorActions.VariableJoinLinesHandler"/>
|
||||
<joinLinesHandler implementation="com.intellij.codeInsight.editorActions.LiteralJoinLinesHandler"/>
|
||||
<editorSmartKeysConfigurable instance="com.intellij.application.options.JavadocOptionsProvider" id="editor.preferences.javadocOptions"/>
|
||||
<wordBoundaryFilter language="JAVA" implementationClass="com.intellij.codeInsight.editorActions.JavaWordBoundaryFilter"/>
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
// 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.codeInsight.PsiEquivalenceUtil;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class VariableJoinLinesHandler implements JoinLinesHandlerDelegate {
|
||||
@Override
|
||||
public int tryJoinLines(@NotNull Document document, @NotNull PsiFile file, int start, int end) {
|
||||
PsiElement elementAtStartLineEnd = file.findElementAt(start);
|
||||
PsiElement elementAtNextLineStart = file.findElementAt(end);
|
||||
if (elementAtStartLineEnd == null || elementAtNextLineStart == null) return -1;
|
||||
if (!PsiUtil.isJavaToken(elementAtStartLineEnd, JavaTokenType.SEMICOLON)) return -1;
|
||||
PsiLocalVariable firstVar = ObjectUtils.tryCast(elementAtStartLineEnd.getParent(), PsiLocalVariable.class);
|
||||
if (firstVar == null) return -1;
|
||||
PsiDeclarationStatement firstDeclaration = ObjectUtils.tryCast(firstVar.getParent(), PsiDeclarationStatement.class);
|
||||
if (firstDeclaration == null) return -1;
|
||||
PsiLocalVariable leftMostVar = (PsiLocalVariable)firstDeclaration.getDeclaredElements()[0];
|
||||
PsiTypeElement firstTypeElement = leftMostVar.getTypeElement();
|
||||
if (firstTypeElement.isInferredType()) return -1;
|
||||
PsiLocalVariable secondVar = PsiTreeUtil.getParentOfType(elementAtNextLineStart, PsiLocalVariable.class);
|
||||
if (secondVar == null || secondVar == firstVar) return -1;
|
||||
PsiDeclarationStatement secondDeclaration = ObjectUtils.tryCast(secondVar.getParent(), PsiDeclarationStatement.class);
|
||||
if (secondDeclaration == null) return -1;
|
||||
if (PsiTreeUtil.skipWhitespacesForward(firstDeclaration) != secondDeclaration) return -1;
|
||||
PsiTypeElement secondTypeElement = secondVar.getTypeElement();
|
||||
if (secondTypeElement.isInferredType()) return -1;
|
||||
if (!PsiEquivalenceUtil.areElementsEquivalent(firstTypeElement, secondTypeElement)) return -1;
|
||||
PsiModifierList firstModifiers = leftMostVar.getModifierList();
|
||||
PsiModifierList secondModifiers = secondVar.getModifierList();
|
||||
if (firstModifiers == null) {
|
||||
if (secondModifiers != null) return -1;
|
||||
} else {
|
||||
if (secondModifiers == null || !PsiEquivalenceUtil.areElementsEquivalent(firstModifiers, secondModifiers)) return -1;
|
||||
}
|
||||
|
||||
int startOffset = elementAtStartLineEnd.getTextRange().getStartOffset();
|
||||
int endOffset = secondTypeElement.getTextRange().getEndOffset();
|
||||
document.replaceString(startOffset, endOffset, ",");
|
||||
return startOffset;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class A {
|
||||
public void foo() {
|
||||
int a;<caret>
|
||||
int b;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class A {
|
||||
public void foo() {
|
||||
int a, b;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class A {
|
||||
public void foo() {
|
||||
final int a = 1;<caret>
|
||||
final int b = 2, c = 3;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class A {
|
||||
public void foo() {
|
||||
final int a = 1, b = 2, c = 3;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class A {
|
||||
public void foo() {
|
||||
@X final int a = 1, d = 2;<caret>
|
||||
@X final int /*+*/ b = 2, c = 3;
|
||||
}
|
||||
}
|
||||
@interface X {}
|
||||
@@ -0,0 +1,6 @@
|
||||
class A {
|
||||
public void foo() {
|
||||
@X final int a = 1, d = 2<caret>, /*+*/ b = 2, c = 3;
|
||||
}
|
||||
}
|
||||
@interface X {}
|
||||
@@ -0,0 +1,7 @@
|
||||
class A {
|
||||
public void foo() {
|
||||
int a;<caret>
|
||||
@X int b;
|
||||
}
|
||||
}
|
||||
@interface X {}
|
||||
@@ -0,0 +1,6 @@
|
||||
class A {
|
||||
public void foo() {
|
||||
int a;@X int b;
|
||||
}
|
||||
}
|
||||
@interface X {}
|
||||
@@ -0,0 +1,7 @@
|
||||
class A {
|
||||
public void foo() {
|
||||
var a = 1;<caret>
|
||||
var b = 2;
|
||||
}
|
||||
}
|
||||
@interface X {}
|
||||
@@ -0,0 +1,6 @@
|
||||
class A {
|
||||
public void foo() {
|
||||
var a = 1;var b = 2;
|
||||
}
|
||||
}
|
||||
@interface X {}
|
||||
@@ -0,0 +1,7 @@
|
||||
class A {
|
||||
public void foo() {
|
||||
int a;<caret>
|
||||
/*x*/ int b;
|
||||
}
|
||||
}
|
||||
@interface X {}
|
||||
@@ -0,0 +1,6 @@
|
||||
class A {
|
||||
public void foo() {
|
||||
int a; /*x*/ int b;
|
||||
}
|
||||
}
|
||||
@interface X {}
|
||||
@@ -280,6 +280,13 @@ public class JoinLinesTest extends LightJavaCodeInsightTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testJoinLocals1() {doTest();}
|
||||
public void testJoinLocals2() {doTest();}
|
||||
public void testJoinLocals3() {doTest();}
|
||||
public void testJoinLocals4() {doTest();}
|
||||
public void testJoinLocals5() {doTest();}
|
||||
public void testJoinLocals6() {doTest();}
|
||||
|
||||
private void doTest() {
|
||||
doTest(".java");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user