mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
extract method: avoid invalid elements (IDEA-112311)
This commit is contained in:
+10
-2
@@ -1239,8 +1239,16 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
|
||||
public boolean isDeclaredInside(PsiVariable variable) {
|
||||
if (variable instanceof ImplicitVariable) return false;
|
||||
int startOffset = myElements[0].getTextRange().getStartOffset();
|
||||
int endOffset = myElements[myElements.length - 1].getTextRange().getEndOffset();
|
||||
int startOffset;
|
||||
int endOffset;
|
||||
if (myExpression != null) {
|
||||
final TextRange range = myExpression.getTextRange();
|
||||
startOffset = range.getStartOffset();
|
||||
endOffset = range.getEndOffset();
|
||||
} else {
|
||||
startOffset = myElements[0].getTextRange().getStartOffset();
|
||||
endOffset = myElements[myElements.length - 1].getTextRange().getEndOffset();
|
||||
}
|
||||
PsiIdentifier nameIdentifier = variable.getNameIdentifier();
|
||||
if (nameIdentifier == null) return false;
|
||||
final TextRange range = nameIdentifier.getTextRange();
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
public class AnnotationArgConverter {
|
||||
public GrAnnotationMemberValue convert(PsiAnnotationMemberValue value) {
|
||||
final StringBuilder buffer = new StringBuilder();
|
||||
|
||||
buffer.append("@A(");
|
||||
|
||||
<selection>value.accept(new JavaElementVisitor() {
|
||||
@Override
|
||||
public void visitExpression(PsiExpression expression) {
|
||||
buffer.append(expression.getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitNewExpression(PsiNewExpression expression) {
|
||||
PsiArrayInitializerExpression arrayInitializer = expression.getArrayInitializer();
|
||||
if (arrayInitializer == null) {
|
||||
super.visitNewExpression(expression);
|
||||
}
|
||||
else {
|
||||
buffer.append(")");
|
||||
}
|
||||
}
|
||||
|
||||
})</selection>;
|
||||
|
||||
buffer.append(")");
|
||||
|
||||
try {
|
||||
return GroovyPsiElementFactory.getInstance(value.getProject()).createAnnotationFromText(buffer.toString());
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
public class AnnotationArgConverter {
|
||||
public GrAnnotationMemberValue convert(PsiAnnotationMemberValue value) {
|
||||
final StringBuilder buffer = new StringBuilder();
|
||||
|
||||
buffer.append("@A(");
|
||||
|
||||
newMethod(value, buffer);
|
||||
|
||||
buffer.append(")");
|
||||
|
||||
try {
|
||||
return GroovyPsiElementFactory.getInstance(value.getProject()).createAnnotationFromText(buffer.toString());
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void newMethod(PsiAnnotationMemberValue value, final StringBuilder buffer) {
|
||||
value.accept(new JavaElementVisitor() {
|
||||
@Override
|
||||
public void visitExpression(PsiExpression expression) {
|
||||
buffer.append(expression.getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitNewExpression(PsiNewExpression expression) {
|
||||
PsiArrayInitializerExpression arrayInitializer = expression.getArrayInitializer();
|
||||
if (arrayInitializer == null) {
|
||||
super.visitNewExpression(expression);
|
||||
}
|
||||
else {
|
||||
buffer.append(")");
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -591,6 +591,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testExpression() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTestDisabledParam() throws PrepareFailedException {
|
||||
final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
|
||||
settings.ELSE_ON_NEW_LINE = true;
|
||||
|
||||
Reference in New Issue
Block a user