expand annotation to normal form should be available always when pair doesn't have name; initial tests (IDEA-100746)

This commit is contained in:
anna
2013-02-11 12:31:28 +01:00
parent e832d87131
commit e6c1d564ff
8 changed files with 79 additions and 55 deletions
@@ -30,7 +30,7 @@ public class ExpandToNormalAnnotationIntention extends MutablyNamedIntention {
@Override
protected String getTextForElement(PsiElement element) {
final PsiAnnotation annotation = (PsiAnnotation)element;
final PsiNameValuePair annotation = (PsiNameValuePair)element;
final String text = buildReplacementText(annotation);
return IntentionPowerPackBundle.message(
"expand.to.normal.annotation.name", text);
@@ -42,51 +42,26 @@ public class ExpandToNormalAnnotationIntention extends MutablyNamedIntention {
return new ExpandToNormalAnnotationPredicate();
}
public static String buildReplacementText(PsiAnnotation annotation) {
final StringBuilder text = new StringBuilder("@");
final PsiAnnotationParameterList parameterList =
annotation.getParameterList();
if (parameterList.getChildren().length == 0) {
final PsiJavaCodeReferenceElement nameReferenceElement =
annotation.getNameReferenceElement();
if (nameReferenceElement != null) {
text.append(nameReferenceElement.getText());
}
text.append("()");
}
else {
final PsiNameValuePair[] attributes = parameterList.getAttributes();
final PsiNameValuePair attribute = attributes[0];
final PsiAnnotationMemberValue value = attribute.getValue();
final PsiJavaCodeReferenceElement nameReferenceElement =
annotation.getNameReferenceElement();
if (nameReferenceElement != null) {
text.append(nameReferenceElement.getText());
}
text.append("(value = ");
if (value != null) {
text.append(value.getText());
}
text.append(')');
public static String buildReplacementText(PsiNameValuePair attribute) {
final StringBuilder text = new StringBuilder();
final PsiAnnotationMemberValue value = attribute.getValue();
text.append("value = ");
if (value != null) {
text.append(value.getText());
}
return text.toString();
}
@Override
protected void processIntention(@NotNull PsiElement element)
throws IncorrectOperationException {
final PsiAnnotation annotation = (PsiAnnotation)element;
final int textOffset = annotation.getTextOffset();
final Project project = annotation.getProject();
final String text = buildReplacementText(annotation);
final PsiElementFactory factory =
JavaPsiFacade.getElementFactory(project);
final PsiAnnotation newAnnotation =
factory.createAnnotationFromText(
text, annotation);
annotation.replace(newAnnotation);
final FileEditorManager editorManager =
FileEditorManager.getInstance(project);
protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
final PsiNameValuePair attribute = (PsiNameValuePair)element;
final int textOffset = attribute.getTextOffset();
final Project project = attribute.getProject();
final String text = buildReplacementText(attribute);
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
final PsiAnnotation newAnnotation = factory.createAnnotationFromText("@A(" + text +" )", attribute);
attribute.replace(newAnnotation.getParameterList().getAttributes()[0]);
final FileEditorManager editorManager = FileEditorManager.getInstance(project);
final Editor editor = editorManager.getSelectedTextEditor();
if (editor == null) {
return;
@@ -21,25 +21,17 @@ import com.siyeh.ipp.base.PsiElementPredicate;
class ExpandToNormalAnnotationPredicate implements PsiElementPredicate {
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof PsiAnnotation)) {
if (!(element instanceof PsiNameValuePair)) {
return false;
}
final PsiAnnotation annotation = (PsiAnnotation)element;
final PsiAnnotationParameterList parameterList =
annotation.getParameterList();
if (parameterList.getChildren().length == 0) {
return true;
}
final PsiNameValuePair[] attributes = parameterList.getAttributes();
if (attributes.length != 1) {
return false;
}
final PsiNameValuePair attribute = attributes[0];
final PsiNameValuePair attribute = (PsiNameValuePair)element;
if (attribute.getName() != null) return false;
final PsiAnnotationMemberValue value = attribute.getValue();
if (value == null) {
return false;
}
final String name = attribute.getName();
return name == null;
return true;
}
}
@@ -0,0 +1,3 @@
@SuppressWarnings(value = "<caret>foo")
public class Main {
}
@@ -0,0 +1,3 @@
@SuppressWarnings("<caret>foo", f = "bar")
public class Main {
}
@@ -0,0 +1,3 @@
@SuppressWarnings(value = "foo", f = "bar")
public class Main {
}
@@ -0,0 +1,3 @@
@SuppressWarnings("<caret>foo")
public class Main {
}
@@ -0,0 +1,3 @@
@SuppressWarnings(value = "foo")
public class Main {
}
@@ -0,0 +1,42 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ipp.annotation;
import com.siyeh.ipp.IPPTestCase;
public class ExpandToNormalAnnotationIntentionTest extends IPPTestCase {
public void testOneAttr() throws Exception {
doTest();
}
public void testMultiAttr() throws Exception {
doTest();
}
public void testAlreadyHasName() throws Exception {
assertIntentionNotAvailable();
}
@Override
protected String getIntentionName() {
return "Expand to 'value = \"foo\"'";
}
@Override
protected String getRelativePath() {
return "expandToNormal";
}
}