mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-07 22:09:38 +07:00
IDEA-172368 'Join lines' could handle increment with 0 nicer
This commit is contained in:
@@ -25,8 +25,10 @@ import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import com.siyeh.ig.psiutils.ParenthesesUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class DeclarationJoinLinesHandler implements JoinLinesHandlerDelegate {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.editorActions.DeclarationJoinLinesHandler");
|
||||
@@ -106,17 +108,25 @@ public class DeclarationJoinLinesHandler implements JoinLinesHandlerDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an updated initializer after joining with given assignment
|
||||
* @param var variable which initializer should be updated
|
||||
* @param assignment assignment to merge into the initializer
|
||||
* @return updated initializer or null if operation cannot be performed (e.g. code is incomplete)
|
||||
*/
|
||||
@Nullable
|
||||
public static PsiExpression getInitializerExpression(PsiLocalVariable var,
|
||||
PsiAssignmentExpression assignment) {
|
||||
return getInitializerExpression(var.getInitializer(),
|
||||
assignment);
|
||||
return getInitializerExpression(var.getInitializer(), assignment);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiExpression getInitializerExpression(PsiExpression initializer,
|
||||
PsiAssignmentExpression assignment) {
|
||||
PsiExpression initializerExpression;
|
||||
final IElementType originalOpSign = assignment.getOperationTokenType();
|
||||
final PsiExpression rExpression = assignment.getRExpression();
|
||||
if (rExpression == null) return null;
|
||||
if (originalOpSign == JavaTokenType.EQ) {
|
||||
initializerExpression = rExpression;
|
||||
}
|
||||
@@ -167,6 +177,10 @@ public class DeclarationJoinLinesHandler implements JoinLinesHandlerDelegate {
|
||||
else {
|
||||
initializerText += rightText;
|
||||
}
|
||||
if ("+".equals(opSign) && ExpressionUtils.isZero(initializer) ||
|
||||
"*".equals(opSign) && ExpressionUtils.isOne(initializer)) {
|
||||
initializerText = rightText;
|
||||
}
|
||||
initializerExpression = JavaPsiFacade.getElementFactory(project).createExpressionFromText(initializerText, assignment);
|
||||
initializerExpression = (PsiExpression)CodeStyleManager.getInstance(project).reformat(initializerExpression);
|
||||
}
|
||||
|
||||
@@ -120,8 +120,10 @@ public class JoinDeclarationAndAssignmentAction extends PsiElementBaseIntentionA
|
||||
}
|
||||
WriteAction.run(() -> {
|
||||
final PsiExpression initializerExpression = DeclarationJoinLinesHandler.getInitializerExpression(variable, assignmentExpression);
|
||||
variable.setInitializer(initializerExpression);
|
||||
assignmentExpression.delete();
|
||||
if (initializerExpression != null) {
|
||||
variable.setInitializer(initializerExpression);
|
||||
assignmentExpression.delete();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
{
|
||||
int <caret>x = 1;
|
||||
x += 2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo {
|
||||
{
|
||||
int x = 1 + 2;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
class Foo {
|
||||
{
|
||||
int x = 0 + 2;<caret>
|
||||
int x = 2;
|
||||
}
|
||||
}
|
||||
@@ -175,14 +175,9 @@ public class JoinLinesTest extends LightCodeInsightTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected CommonCodeStyleSettings getJavaSettings() {
|
||||
return CodeStyleSettingsManager.getSettings(getProject()).getCommonSettings(JavaLanguage.INSTANCE);
|
||||
}
|
||||
public void testAssignmentExpression() { doTest(); }
|
||||
public void testAssignmentExpression2() { doTest(); }
|
||||
|
||||
public void testAssignmentExpression() {
|
||||
doTest();
|
||||
}
|
||||
public void testReformatInsertsNewlines() {
|
||||
CommonCodeStyleSettings settings = getJavaSettings();
|
||||
final Element root = new Element("fake");
|
||||
@@ -274,4 +269,9 @@ public class JoinLinesTest extends LightCodeInsightTestCase {
|
||||
|
||||
actionHandler.execute(getEditor(), DataManager.getInstance().getDataContext());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static CommonCodeStyleSettings getJavaSettings() {
|
||||
return CodeStyleSettingsManager.getSettings(getProject()).getCommonSettings(JavaLanguage.INSTANCE);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user