diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/KeywordCompletion.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/KeywordCompletion.kt index 27a9ffae3e3..3bdb2a3d785 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/KeywordCompletion.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/KeywordCompletion.kt @@ -407,11 +407,27 @@ object KeywordCompletion { is KtObjectDeclaration -> if (ownerDeclaration.isObjectLiteral()) KotlinTarget.OBJECT_LITERAL else KotlinTarget.OBJECT - else -> return true + else -> return keywordTokenType != CONST_KEYWORD } if (!ModifierCheckerCore.isPossibleParentTarget(keywordTokenType, parentTarget, languageVersionSettings)) return false + if (keywordTokenType == CONST_KEYWORD) { + return when (parentTarget) { + KotlinTarget.OBJECT -> true + KotlinTarget.FILE -> { + val prevSiblings = elementAt.parent.siblings(withItself = false, forward = false) + val hasLineBreak = prevSiblings + .takeWhile { it is PsiWhiteSpace || it.isSemicolon() } + .firstOrNull { it.text.contains("\n") || it.isSemicolon() } != null + hasLineBreak || prevSiblings.none { + it !is PsiWhiteSpace && !it.isSemicolon() && it !is KtImportList && it !is KtPackageDirective + } + } + else -> false + } + } + return true } } @@ -423,6 +439,8 @@ object KeywordCompletion { } } + private fun PsiElement.isSemicolon() = node.elementType == KtTokens.SEMICOLON + private fun isErrorElementBefore(token: PsiElement): Boolean { for (leaf in token.prevLeafs) { if (leaf is PsiWhiteSpace || leaf is PsiComment) continue diff --git a/idea/idea-completion/testData/keywords/AfterClassName.kt b/idea/idea-completion/testData/keywords/AfterClassName.kt new file mode 100644 index 00000000000..038d40b4a21 --- /dev/null +++ b/idea/idea-completion/testData/keywords/AfterClassName.kt @@ -0,0 +1,4 @@ +class Foo { +} + +// EXIST: constructor \ No newline at end of file diff --git a/idea/idea-completion/testData/keywords/AfterClassProperty.kt b/idea/idea-completion/testData/keywords/AfterClassProperty.kt index 3b91f33681d..5856072d801 100644 --- a/idea/idea-completion/testData/keywords/AfterClassProperty.kt +++ b/idea/idea-completion/testData/keywords/AfterClassProperty.kt @@ -39,7 +39,6 @@ class MouseMovedEventArgs // EXIST: tailrec // EXIST: external // EXIST: annotation class -// EXIST: const val // EXIST: suspend // EXIST: typealias // EXIST: expect diff --git a/idea/idea-completion/testData/keywords/AfterFuns.kt b/idea/idea-completion/testData/keywords/AfterFuns.kt index 72d1124b00c..71ae3369f5a 100644 --- a/idea/idea-completion/testData/keywords/AfterFuns.kt +++ b/idea/idea-completion/testData/keywords/AfterFuns.kt @@ -38,7 +38,6 @@ class A { // EXIST: tailrec // EXIST: external // EXIST: annotation class -// EXIST: const val // EXIST: suspend // EXIST: typealias // EXIST: expect diff --git a/idea/idea-completion/testData/keywords/InAnnotationClassScope.kt b/idea/idea-completion/testData/keywords/InAnnotationClassScope.kt index 587565318c3..233b136bc12 100644 --- a/idea/idea-completion/testData/keywords/InAnnotationClassScope.kt +++ b/idea/idea-completion/testData/keywords/InAnnotationClassScope.kt @@ -24,7 +24,6 @@ annotation class Test { // EXIST: tailrec // EXIST: external // EXIST: annotation class -// EXIST: const val // EXIST: suspend // EXIST: typealias // EXIST: expect diff --git a/idea/idea-completion/testData/keywords/InClassBeforeFun.kt b/idea/idea-completion/testData/keywords/InClassBeforeFun.kt index dd2fc9bc8c0..543a77758fc 100644 --- a/idea/idea-completion/testData/keywords/InClassBeforeFun.kt +++ b/idea/idea-completion/testData/keywords/InClassBeforeFun.kt @@ -36,7 +36,6 @@ public class Test { // EXIST: tailrec // EXIST: external // EXIST: annotation class -// EXIST: const val // EXIST: suspend // EXIST: typealias // EXIST: expect diff --git a/idea/idea-completion/testData/keywords/InClassScope.kt b/idea/idea-completion/testData/keywords/InClassScope.kt index 95243e84462..149a4288503 100644 --- a/idea/idea-completion/testData/keywords/InClassScope.kt +++ b/idea/idea-completion/testData/keywords/InClassScope.kt @@ -30,7 +30,6 @@ class TestClass { // EXIST: tailrec // EXIST: external // EXIST: annotation class -// EXIST: const val // EXIST: suspend // EXIST: typealias // EXIST: expect diff --git a/idea/idea-completion/testData/keywords/InEnumScope2.kt b/idea/idea-completion/testData/keywords/InEnumScope2.kt index 1abd8c62bd7..c16bd5561a8 100644 --- a/idea/idea-completion/testData/keywords/InEnumScope2.kt +++ b/idea/idea-completion/testData/keywords/InEnumScope2.kt @@ -24,7 +24,6 @@ enum class Test { // EXIST: tailrec // EXIST: external // EXIST: annotation class -// EXIST: const val // EXIST: suspend // EXIST: fun // EXIST: typealias diff --git a/idea/idea-completion/testData/keywords/InInterfaceScope.kt b/idea/idea-completion/testData/keywords/InInterfaceScope.kt index 2ad6488304f..a9df0092ed6 100644 --- a/idea/idea-completion/testData/keywords/InInterfaceScope.kt +++ b/idea/idea-completion/testData/keywords/InInterfaceScope.kt @@ -26,7 +26,6 @@ interface Test { // EXIST: tailrec // EXIST: external // EXIST: annotation class -// EXIST: const val // EXIST: suspend // EXIST: typealias // EXIST: expect diff --git a/idea/idea-completion/testData/keywords/InPrimaryConstructorParametersList.kt b/idea/idea-completion/testData/keywords/InPrimaryConstructorParametersList.kt index a36922410e3..d7d688ebc53 100644 --- a/idea/idea-completion/testData/keywords/InPrimaryConstructorParametersList.kt +++ b/idea/idea-completion/testData/keywords/InPrimaryConstructorParametersList.kt @@ -17,6 +17,5 @@ class TestSample() // EXIST: actual /* TODO: keywords below should not be here*/ // EXIST: abstract -// EXIST: const val // EXIST: lateinit var // NOTHING_ELSE diff --git a/idea/idea-completion/testData/keywords/PropertyAccessors.kt b/idea/idea-completion/testData/keywords/PropertyAccessors.kt index d1758778c9c..dde3ad27985 100644 --- a/idea/idea-completion/testData/keywords/PropertyAccessors.kt +++ b/idea/idea-completion/testData/keywords/PropertyAccessors.kt @@ -38,7 +38,6 @@ class Some { // EXIST: tailrec // EXIST: external // EXIST: annotation class -// EXIST: const val // EXIST: suspend // EXIST: typealias // EXIST: expect diff --git a/idea/idea-completion/testData/keywords/PropertyAccessors2.kt b/idea/idea-completion/testData/keywords/PropertyAccessors2.kt index 9f01eddbc37..64161276a48 100644 --- a/idea/idea-completion/testData/keywords/PropertyAccessors2.kt +++ b/idea/idea-completion/testData/keywords/PropertyAccessors2.kt @@ -38,7 +38,6 @@ class Some { // EXIST: tailrec // EXIST: external // EXIST: annotation class -// EXIST: const val // EXIST: suspend // EXIST: typealias // EXIST: expect diff --git a/idea/idea-completion/testData/keywords/PropertySetter.kt b/idea/idea-completion/testData/keywords/PropertySetter.kt index ce9344613a1..0c4e31519e2 100644 --- a/idea/idea-completion/testData/keywords/PropertySetter.kt +++ b/idea/idea-completion/testData/keywords/PropertySetter.kt @@ -36,7 +36,6 @@ class Some { // EXIST: tailrec // EXIST: external // EXIST: annotation class -// EXIST: const val // EXIST: suspend // EXIST: typealias // EXIST: expect diff --git a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/KeywordCompletionTestGenerated.java b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/KeywordCompletionTestGenerated.java index 948b2d18bd6..8712745ee99 100644 --- a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/KeywordCompletionTestGenerated.java +++ b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/KeywordCompletionTestGenerated.java @@ -25,6 +25,11 @@ public class KeywordCompletionTestGenerated extends AbstractKeywordCompletionTes KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); } + @TestMetadata("AfterClassName.kt") + public void testAfterClassName() throws Exception { + runTest("idea/idea-completion/testData/keywords/AfterClassName.kt"); + } + @TestMetadata("AfterClassProperty.kt") public void testAfterClassProperty() throws Exception { runTest("idea/idea-completion/testData/keywords/AfterClassProperty.kt");