mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-65998 Groovy: Introduce Variable intention should work on application statement. (Method call without brackets)
This commit is contained in:
-20
@@ -153,26 +153,6 @@ public class GroovyInsertHandler implements InsertHandler<LookupElement> {
|
||||
addTailType(item).processTail(context.getEditor(), context.getTailOffset());
|
||||
}
|
||||
|
||||
/*
|
||||
private static boolean isExpressionStatement(PsiFile psiFile, int offset) {
|
||||
PsiElement elementAt = psiFile.findElementAt(offset);
|
||||
if (elementAt == null) return false;
|
||||
GrExpression expr = PsiTreeUtil.getParentOfType(elementAt, GrExpression.class);
|
||||
if (expr == null) return false;
|
||||
final PsiElement parent = expr.getParent();
|
||||
if (parent instanceof GrControlFlowOwner) return true;
|
||||
if (parent instanceof GrExpression ||
|
||||
parent instanceof GrArgumentList ||
|
||||
parent instanceof GrReturnStatement ||
|
||||
parent instanceof GrAssertStatement ||
|
||||
parent instanceof GrThrowStatement ||
|
||||
parent instanceof GrSwitchStatement) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
|
||||
private static void handleOverwrite(final int offset, final Document document) {
|
||||
final CharSequence sequence = document.getCharsSequence();
|
||||
int i = offset;
|
||||
|
||||
@@ -50,14 +50,14 @@ import org.jetbrains.plugins.groovy.lang.psi.*;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifier;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTopLevelDefinition;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.*;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrAssertStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.*;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty;
|
||||
@@ -1097,4 +1097,19 @@ public class PsiUtil {
|
||||
}
|
||||
return qualifier == null;
|
||||
}
|
||||
|
||||
public static boolean isExpressionStatement(GrExpression expr) {
|
||||
final PsiElement parent = expr.getParent();
|
||||
if (parent instanceof GrControlFlowOwner) return true;
|
||||
if (parent instanceof GrExpression ||
|
||||
parent instanceof GrArgumentList ||
|
||||
parent instanceof GrReturnStatement ||
|
||||
parent instanceof GrAssertStatement ||
|
||||
parent instanceof GrThrowStatement ||
|
||||
parent instanceof GrSwitchStatement) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+24
-21
@@ -89,22 +89,31 @@ public abstract class GrIntroduceHandlerBase<Settings extends GrIntroduceSetting
|
||||
for (GrExpression expression = PsiTreeUtil.getParentOfType(elementAtCaret, GrExpression.class);
|
||||
expression != null;
|
||||
expression = PsiTreeUtil.getParentOfType(expression, GrExpression.class)) {
|
||||
if (expressions.contains(expression) || expression instanceof GrParenthesizedExpression) continue;
|
||||
if (expression instanceof GrSuperReferenceExpression || expression.getType() == PsiType.VOID) continue;
|
||||
|
||||
if (expression instanceof GrApplicationStatement) continue;
|
||||
if (expression instanceof GrReferenceExpression &&
|
||||
(expression.getParent() instanceof GrMethodCall && ((GrReferenceExpression)expression).resolve() instanceof PsiMethod ||
|
||||
((GrReferenceExpression)expression).resolve() instanceof PsiClass)) {
|
||||
continue;
|
||||
}
|
||||
if (expression instanceof GrAssignmentExpression) continue;
|
||||
if (expressions.contains(expression)) continue;
|
||||
if (expressionIsNotCorrect(expression)) continue;
|
||||
|
||||
expressions.add(expression);
|
||||
}
|
||||
return expressions;
|
||||
}
|
||||
|
||||
private static boolean expressionIsNotCorrect(GrExpression expression) {
|
||||
if (expression instanceof GrParenthesizedExpression) return true;
|
||||
if (expression instanceof GrSuperReferenceExpression) return true;
|
||||
if (expression.getType() == PsiType.VOID) return true;
|
||||
if (expression instanceof GrAssignmentExpression) return true;
|
||||
if (expression instanceof GrReferenceExpression && expression.getParent() instanceof GrCall) {
|
||||
final PsiElement resolved = ((GrReferenceExpression)expression).resolve();
|
||||
return resolved instanceof PsiMethod || resolved instanceof PsiClass;
|
||||
}
|
||||
if (expression instanceof GrApplicationStatement) {
|
||||
return !PsiUtil.isExpressionStatement(expression);
|
||||
}
|
||||
if (expression instanceof GrClosableBlock && expression.getParent() instanceof GrStringInjection) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static int correctOffset(Editor editor, int offset) {
|
||||
Document document = editor.getDocument();
|
||||
CharSequence text = document.getCharsSequence();
|
||||
@@ -290,23 +299,17 @@ public abstract class GrIntroduceHandlerBase<Settings extends GrIntroduceSetting
|
||||
public static GrExpression findExpression(GroovyFileBase file, int startOffset, int endOffset) {
|
||||
GrExpression selectedExpr = GroovyRefactoringUtil.findElementInRange(file, startOffset, endOffset, GrExpression.class);
|
||||
if (selectedExpr == null) return null;
|
||||
if (selectedExpr instanceof GrClosableBlock && selectedExpr.getParent() instanceof GrStringInjection) {
|
||||
throw new GrIntroduceRefactoringError(GroovyRefactoringBundle.message("selected.block.should.represent.an.expression"));
|
||||
}
|
||||
|
||||
if (selectedExpr instanceof GrReferenceExpression &&
|
||||
selectedExpr.getParent() instanceof GrMethodCall &&
|
||||
(((GrMethodCall)selectedExpr.getParent()).isCommandExpression() || selectedExpr.getParent() instanceof GrApplicationStatement) ||
|
||||
selectedExpr instanceof GrApplicationStatement) {
|
||||
throw new GrIntroduceRefactoringError(GroovyRefactoringBundle.message("selected.expression.in.command.expression"));
|
||||
}
|
||||
|
||||
PsiType type = selectedExpr.getType();
|
||||
if (type != null) type = TypeConversionUtil.erasure(type);
|
||||
|
||||
if (PsiType.VOID.equals(type)) {
|
||||
throw new GrIntroduceRefactoringError(GroovyRefactoringBundle.message("selected.expression.has.void.type"));
|
||||
}
|
||||
|
||||
if (expressionIsNotCorrect(selectedExpr)) {
|
||||
throw new GrIntroduceRefactoringError(GroovyRefactoringBundle.message("selected.block.should.represent.an.expression"));
|
||||
}
|
||||
|
||||
return selectedExpr;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user