IDEA-118988 completion could add 'value=' in annotation

This commit is contained in:
peter
2014-02-25 12:43:20 +01:00
parent 30a899f729
commit 0ecb335d3a
4 changed files with 36 additions and 2 deletions

View File

@@ -465,8 +465,10 @@ public class JavaCompletionContributor extends CompletionContributor {
if (!(method instanceof PsiAnnotationMethod)) continue;
final String attrName = method.getName();
for (PsiNameValuePair apair : existingPairs) {
if (Comparing.equal(apair.getName(), attrName)) continue methods;
for (PsiNameValuePair existingAttr : existingPairs) {
if (PsiTreeUtil.isAncestor(existingAttr, insertedElement, false)) break;
if (Comparing.equal(existingAttr.getName(), attrName) ||
PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME.equals(attrName) && existingAttr.getName() == null) continue methods;
}
LookupElementBuilder element = LookupElementBuilder.createWithIcon(method).withInsertHandler(new InsertHandler<LookupElement>() {
@Override
@@ -474,6 +476,15 @@ public class JavaCompletionContributor extends CompletionContributor {
final Editor editor = context.getEditor();
TailType.EQ.processTail(editor, editor.getCaretModel().getOffset());
context.setAddCompletionChar(false);
context.commitDocument();
PsiAnnotationParameterList paramList =
PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), PsiAnnotationParameterList.class, false);
if (paramList != null && paramList.getAttributes().length > 0 && paramList.getAttributes()[0].getName() == null) {
int valueOffset = paramList.getAttributes()[0].getTextRange().getStartOffset();
context.getDocument().insertString(valueOffset, PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME);
TailType.EQ.processTail(editor, valueOffset + PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME.length());
}
}
});

View File

@@ -0,0 +1,8 @@
@interface Anno {
String value();
String bar();
String goo();
}
@Anno("a", <caret>)
class Foo {}

View File

@@ -0,0 +1,8 @@
@interface Anno {
String value();
String bar();
String goo();
}
@Anno(value = "a", bar = <caret>)
class Foo {}

View File

@@ -756,6 +756,13 @@ public class ListUtils {
assertStringItems("bar", "foo");
}
public void testAddExplicitValueInAnnotation() throws Throwable {
configureByTestName()
assertStringItems("bar", "goo")
selectItem(myItems[0])
checkResult()
}
public void testUnnecessaryMethodMerging() throws Throwable {
configureByFile(getTestName(false) + ".java");
assertStringItems("fofoo", "fofoo");