No completion auto-popup where parameter name can be

This commit is contained in:
Valentin Kipyatkov
2014-12-22 21:33:48 +03:00
parent 000cde3f7f
commit ec1541a692
18 changed files with 154 additions and 41 deletions
@@ -46,7 +46,7 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import com.intellij.patterns.StandardPatterns
import com.intellij.util.ProcessingContext
import com.intellij.patterns.PatternCondition
import org.jetbrains.jet.lang.psi.psiUtil.isAncestor
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
class CompletionSessionConfiguration(
val completeNonImportedDeclarations: Boolean,
@@ -216,17 +216,60 @@ class BasicCompletionSession(configuration: CompletionSessionConfiguration,
resultSet: CompletionResultSet)
: CompletionSessionBase(configuration, parameters, resultSet) {
public enum class CompletionKind {
KEYWORDS_ONLY
NAMED_PARAMETERS_ONLY
ALL
TYPES
ANNOTATION_TYPES
ANNOTATION_TYPES_OR_PARAMETER_NAME
}
public val completionKind: CompletionKind = calcCompletionKind()
private fun calcCompletionKind(): CompletionKind {
if (NamedParametersCompletion.isOnlyNamedParameterExpected(position)) {
return CompletionKind.NAMED_PARAMETERS_ONLY
}
if (reference == null) {
return CompletionKind.KEYWORDS_ONLY
}
val annotationEntry = position.getStrictParentOfType<JetAnnotationEntry>()
if (annotationEntry != null) {
val valueArgList = position.getStrictParentOfType<JetValueArgumentList>()
if (valueArgList == null || !annotationEntry.isAncestor(valueArgList)) {
val parent = annotationEntry.getParent()
if (parent is JetDeclarationModifierList && parent.getParent() is JetParameter) {
return CompletionKind.ANNOTATION_TYPES_OR_PARAMETER_NAME
}
return CompletionKind.ANNOTATION_TYPES
}
}
// Check that completion in the type annotation context and if there's a qualified
// expression we are at first of it
val typeReference = position.getStrictParentOfType<JetTypeReference>()
if (typeReference != null) {
val firstPartReference = PsiTreeUtil.findChildOfType(typeReference, javaClass<JetSimpleNameExpression>())
if (firstPartReference == reference.expression) {
return CompletionKind.TYPES
}
}
return CompletionKind.ALL
}
override fun doComplete() {
assert(parameters.getCompletionType() == CompletionType.BASIC)
val completionKind = completionKind()
if (completionKind != CompletionKind.NAMED_PARAMETERS_ONLY) {
val kindFilter = when (completionKind) {
CompletionKind.TYPES ->
DescriptorKindFilter(DescriptorKindFilter.CLASSIFIERS_MASK or DescriptorKindFilter.PACKAGES_MASK) exclude DescriptorKindExclude.EnumEntry
CompletionKind.ANNOTATION_TYPES ->
CompletionKind.ANNOTATION_TYPES, CompletionKind.ANNOTATION_TYPES_OR_PARAMETER_NAME ->
DescriptorKindFilter(DescriptorKindFilter.NON_SINGLETON_CLASSIFIERS_MASK or DescriptorKindFilter.PACKAGES_MASK) exclude NonAnnotationClassifierExclude
else ->
@@ -296,7 +339,7 @@ class BasicCompletionSession(configuration: CompletionSessionConfiguration,
private fun addNonImported(completionKind: CompletionKind) {
if (shouldRunTopLevelCompletion()) {
addAllClasses {
if (completionKind != CompletionKind.ANNOTATION_TYPES)
if (completionKind != CompletionKind.ANNOTATION_TYPES && completionKind != CompletionKind.ANNOTATION_TYPES_OR_PARAMETER_NAME)
it != ClassKind.ENUM_ENTRY
else
it == ClassKind.ANNOTATION_CLASS
@@ -312,40 +355,6 @@ class BasicCompletionSession(configuration: CompletionSessionConfiguration,
}
}
private enum class CompletionKind {
KEYWORDS_ONLY
NAMED_PARAMETERS_ONLY
ALL
TYPES
ANNOTATION_TYPES
}
private fun completionKind(): CompletionKind {
if (NamedParametersCompletion.isOnlyNamedParameterExpected(position)) return CompletionKind.NAMED_PARAMETERS_ONLY
if (reference == null) return CompletionKind.KEYWORDS_ONLY
val modifierList = position.getStrictParentOfType<JetModifierList>()
if (modifierList != null) {
val valueArgList = position.getStrictParentOfType<JetValueArgumentList>()
if (valueArgList == null || !modifierList.isAncestor(valueArgList)) {
return CompletionKind.ANNOTATION_TYPES
}
}
// Check that completion in the type annotation context and if there's a qualified
// expression we are at first of it
val typeReference = position.getStrictParentOfType<JetTypeReference>()
if (typeReference != null) {
val firstPartReference = PsiTreeUtil.findChildOfType(typeReference, javaClass<JetSimpleNameExpression>())
if (firstPartReference == reference.expression) {
return CompletionKind.TYPES
}
}
return CompletionKind.ALL
}
private fun addReferenceVariants(kindFilter: DescriptorKindFilter, shouldCastToRuntimeType: Boolean) {
collector.addDescriptorElements(
getReferenceVariants(kindFilter, shouldCastToRuntimeType),
@@ -207,7 +207,14 @@ public class KotlinCompletionContributor : CompletionContributor() {
val configuration = CompletionSessionConfiguration(parameters)
if (parameters.getCompletionType() == CompletionType.BASIC) {
val somethingAdded = BasicCompletionSession(configuration, parameters, result).complete()
val session = BasicCompletionSession(configuration, parameters, result)
if (session.completionKind == BasicCompletionSession.CompletionKind.ANNOTATION_TYPES_OR_PARAMETER_NAME && parameters.isAutoPopup()) {
result.stopHere()
return
}
val somethingAdded = session.complete()
if (!somethingAdded && parameters.getInvocationCount() < 2) {
// Rerun completion if nothing was found
val newConfiguration = CompletionSessionConfiguration(completeNonImportedDeclarations = true,
@@ -0,0 +1,4 @@
fun foo(i<caret>) { }
// INVOCATION_COUNT: 0
// NUMBER: 0
@@ -0,0 +1,4 @@
fun foo([inlineOptions] i<caret>) { }
// INVOCATION_COUNT: 0
// NUMBER: 0
@@ -1,7 +1,10 @@
annotation class Hello
val v = 1
fun foo(<caret>) { }
// INVOCATION_COUNT: 1
// EXIST: Hello
// EXIST: inlineOptions
// ABSENT: String
// ABSENT: v
@@ -1,3 +1,7 @@
annotation class iHello
fun foo(i<caret>) { }
// INVOCATION_COUNT: 1
// EXIST: iHello
// EXIST: inlineOptions
@@ -1,7 +1,10 @@
annotation class Hello
val v = 1
fun foo(p: String, <caret>) { }
// INVOCATION_COUNT: 1
// EXIST: Hello
// EXIST: inlineOptions
// ABSENT: String
// ABSENT: v
@@ -1,7 +1,10 @@
annotation class Hello
val v = 1
fun foo(volatile <caret>) { }
// INVOCATION_COUNT: 1
// EXIST: Hello
// EXIST: inlineOptions
// ABSENT: String
// ABSENT: v
@@ -1,7 +1,10 @@
annotation class Hello
val v = 1
fun foo(<caret>
// INVOCATION_COUNT: 1
// EXIST: Hello
// EXIST: inlineOptions
// ABSENT: String
// ABSENT: v
@@ -2,6 +2,7 @@ val v = 1
fun foo(inlineOptions(InlineOp<caret>) { }
// INVOCATION_COUNT: 1
// EXIST: InlineOption
// ABSENT: String
// ABSENT: v
@@ -1,4 +1,5 @@
fun foo(koltin.<caret>) { }
fun foo(kotlin.<caret>) { }
// INVOCATION_COUNT: 1
// EXIST: inlineOptions
// ABSENT: String
@@ -1,7 +1,10 @@
annotation class Hello
val v = 1
fun foo([<caret>) { }
// INVOCATION_COUNT: 1
// EXIST: Hello
// EXIST: inlineOptions
// ABSENT: String
// ABSENT: v
@@ -1,7 +1,10 @@
annotation class iHello
val v = 1
fun foo([i<caret>) { }
// INVOCATION_COUNT: 1
// EXIST: iHello
// EXIST: inlineOptions
// ABSENT: String
// ABSENT: v
@@ -2,6 +2,7 @@ val v = 1
fun foo(inlineOptions(<caret>) { }
// INVOCATION_COUNT: 1
// EXIST: InlineOption
// EXIST: String
// EXIST: v
@@ -0,0 +1,8 @@
val v = 1
fun foo([i<caret>) { }
// INVOCATION_COUNT: 0
// EXIST: inlineOptions
// ABSENT: String
// ABSENT: v
@@ -0,0 +1,8 @@
val v = 1
fun foo([volatile i<caret>) { }
// INVOCATION_COUNT: 0
// EXIST: inlineOptions
// ABSENT: String
// ABSENT: v
@@ -900,6 +900,18 @@ public class JSBasicCompletionTestGenerated extends AbstractJSBasicCompletionTes
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/completion/basic/common/annotations"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("NoParameterAnnotationAutoPopup1.kt")
public void testNoParameterAnnotationAutoPopup1() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/common/annotations/NoParameterAnnotationAutoPopup1.kt");
doTest(fileName);
}
@TestMetadata("NoParameterAnnotationAutoPopup2.kt")
public void testNoParameterAnnotationAutoPopup2() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/common/annotations/NoParameterAnnotationAutoPopup2.kt");
doTest(fileName);
}
@TestMetadata("ParameterAnnotation1.kt")
public void testParameterAnnotation1() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/common/annotations/ParameterAnnotation1.kt");
@@ -953,6 +965,18 @@ public class JSBasicCompletionTestGenerated extends AbstractJSBasicCompletionTes
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/common/annotations/ParameterAnnotationArgs.kt");
doTest(fileName);
}
@TestMetadata("ParameterAnnotationAutoPopup1.kt")
public void testParameterAnnotationAutoPopup1() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/common/annotations/ParameterAnnotationAutoPopup1.kt");
doTest(fileName);
}
@TestMetadata("ParameterAnnotationAutoPopup2.kt")
public void testParameterAnnotationAutoPopup2() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/common/annotations/ParameterAnnotationAutoPopup2.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/completion/basic/common/extensions")
@@ -900,6 +900,18 @@ public class JvmBasicCompletionTestGenerated extends AbstractJvmBasicCompletionT
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/completion/basic/common/annotations"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("NoParameterAnnotationAutoPopup1.kt")
public void testNoParameterAnnotationAutoPopup1() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/common/annotations/NoParameterAnnotationAutoPopup1.kt");
doTest(fileName);
}
@TestMetadata("NoParameterAnnotationAutoPopup2.kt")
public void testNoParameterAnnotationAutoPopup2() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/common/annotations/NoParameterAnnotationAutoPopup2.kt");
doTest(fileName);
}
@TestMetadata("ParameterAnnotation1.kt")
public void testParameterAnnotation1() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/common/annotations/ParameterAnnotation1.kt");
@@ -953,6 +965,18 @@ public class JvmBasicCompletionTestGenerated extends AbstractJvmBasicCompletionT
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/common/annotations/ParameterAnnotationArgs.kt");
doTest(fileName);
}
@TestMetadata("ParameterAnnotationAutoPopup1.kt")
public void testParameterAnnotationAutoPopup1() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/common/annotations/ParameterAnnotationAutoPopup1.kt");
doTest(fileName);
}
@TestMetadata("ParameterAnnotationAutoPopup2.kt")
public void testParameterAnnotationAutoPopup2() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/basic/common/annotations/ParameterAnnotationAutoPopup2.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/completion/basic/common/extensions")