disable introduce variable from part of literal expression which is not a string

This commit is contained in:
anna
2013-12-05 10:20:57 +01:00
parent 34787065ae
commit d1985af4a7
3 changed files with 24 additions and 4 deletions
@@ -37,10 +37,7 @@ import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pass;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.*;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.wm.WindowManager;
@@ -414,6 +411,12 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase {
final PsiExpression toBeExpression = createReplacement(fakeInitializer, project, prefix, suffix, parent, rangeMarker, refIdx);
toBeExpression.accept(errorsVisitor);
if (hasErrors[0]) return null;
if (literalExpression != null) {
PsiType type = toBeExpression.getType();
if (type != null && !type.equals(literalExpression.getType())) {
return null;
}
}
final PsiReferenceExpression refExpr = PsiTreeUtil.getParentOfType(toBeExpression.findElementAt(refIdx[0]), PsiReferenceExpression.class);
if (refExpr == null) return null;
@@ -0,0 +1,5 @@
class A {
public void test() {
int i = <selection>1</selection>23 + 123;
}
}
@@ -224,6 +224,18 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase {
doTest(new MockIntroduceVariableHandler("str", false, false, false, "java.lang.String"));
}
public void testSubLiteralFailure() throws Exception {
try {
doTest(new MockIntroduceVariableHandler("str", false, false, false, "int"));
}
catch (Exception e) {
assertEquals(e.getMessage(), "Error message:Cannot perform refactoring.\n" +
"Selected block should represent an expression");
return;
}
fail("Should not be able to perform refactoring");
}
public void testSubLiteralFromExpression() throws Exception {
doTest(new MockIntroduceVariableHandler("str", false, false, false, "java.lang.String"));
}