prioritize javadoc existing param description completion (IDEA-153083)

This commit is contained in:
peter
2016-04-19 12:33:20 +02:00
parent 1475442c3f
commit 2c3941fb1f
2 changed files with 36 additions and 19 deletions
@@ -69,6 +69,20 @@ public class JavaDocCompletionContributor extends CompletionContributor {
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.completion.JavaDocCompletionContributor");
private static final @NonNls String VALUE_TAG = "value";
private static final @NonNls String LINK_TAG = "link";
private static final InsertHandler<LookupElement> PARAM_DESCRIPTION_INSERT_HANDLER = (context, item) -> {
if (context.getCompletionChar() != Lookup.REPLACE_SELECT_CHAR) return;
context.commitDocument();
PsiDocTag docTag = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), PsiDocTag.class, false);
if (docTag != null) {
Document document = context.getDocument();
int tagEnd = DocTagSelectioner.getDocTagRange(docTag, document.getCharsSequence(), 0).getEndOffset();
int tail = context.getTailOffset();
if (tail < tagEnd) {
document.deleteString(tail, tagEnd);
}
}
};
public JavaDocCompletionContributor() {
extend(CompletionType.BASIC, PsiJavaPatterns.psiElement(JavaDocTokenType.DOC_TAG_NAME), new TagChooser());
@@ -265,23 +279,8 @@ public class JavaDocCompletionContributor extends CompletionContributor {
}
});
for (String description : descriptions) {
result.addElement(LookupElementBuilder.create(description).withInsertHandler(new InsertHandler<LookupElement>() {
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
if (context.getCompletionChar() != Lookup.REPLACE_SELECT_CHAR) return;
context.commitDocument();
PsiDocTag docTag = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), PsiDocTag.class, false);
if (docTag != null) {
Document document = context.getDocument();
int tagEnd = DocTagSelectioner.getDocTagRange(docTag, document.getCharsSequence(), 0).getEndOffset();
int tail = context.getTailOffset();
if (tail < tagEnd) {
document.deleteString(tail, tagEnd);
}
}
}
}));
result.addElement(PrioritizedLookupElement.withPriority(
LookupElementBuilder.create(description).withInsertHandler(PARAM_DESCRIPTION_INSERT_HANDLER), 1));
}
}
@@ -265,8 +265,7 @@ class Foo {
}
'''
myFixture.completeBasic()
myFixture.assertPreferredCompletionItems 0, 'some', 'some integer param'
myFixture.lookup.currentItem = myFixture.lookupElements[1]
myFixture.assertPreferredCompletionItems 0, 'some integer param', 'some'
myFixture.type('\t')
myFixture.checkResult '''
class Foo {
@@ -284,6 +283,25 @@ class Foo {
'''
}
public void "test suggest same param descriptions with no text after param name"() {
myFixture.configureByText "a.java", '''
class Foo {
/**
* @param intParam <caret>
* @throws Foo
*/
void foo2(int intParam, Object param2) { }
/**
* @param intParam some integer param
*/
void foo(int intParam, Object param2) { }
}
'''
myFixture.completeBasic()
myFixture.assertPreferredCompletionItems 0, 'some integer param'
}
public void "test see super class"() {
myFixture.addClass("package foo; public interface Foo {}")
myFixture.addClass("package bar; public class Bar {} ")