Keyword completion: do not suggest const except inside top level or object

#KT-23026 Fixed
#KT-29840 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-02-15 18:48:24 +09:00
committed by Alexander Podkhalyuzin
parent 7d809bc1d5
commit 8cdf901538
14 changed files with 28 additions and 12 deletions
@@ -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
@@ -0,0 +1,4 @@
class Foo <caret> {
}
// EXIST: constructor
@@ -39,7 +39,6 @@ class MouseMovedEventArgs
// EXIST: tailrec
// EXIST: external
// EXIST: annotation class
// EXIST: const val
// EXIST: suspend
// EXIST: typealias
// EXIST: expect
-1
View File
@@ -38,7 +38,6 @@ class A {
// EXIST: tailrec
// EXIST: external
// EXIST: annotation class
// EXIST: const val
// EXIST: suspend
// EXIST: typealias
// EXIST: expect
@@ -24,7 +24,6 @@ annotation class Test {
// EXIST: tailrec
// EXIST: external
// EXIST: annotation class
// EXIST: const val
// EXIST: suspend
// EXIST: typealias
// EXIST: expect
@@ -36,7 +36,6 @@ public class Test {
// EXIST: tailrec
// EXIST: external
// EXIST: annotation class
// EXIST: const val
// EXIST: suspend
// EXIST: typealias
// EXIST: expect
@@ -30,7 +30,6 @@ class TestClass {
// EXIST: tailrec
// EXIST: external
// EXIST: annotation class
// EXIST: const val
// EXIST: suspend
// EXIST: typealias
// EXIST: expect
@@ -24,7 +24,6 @@ enum class Test {
// EXIST: tailrec
// EXIST: external
// EXIST: annotation class
// EXIST: const val
// EXIST: suspend
// EXIST: fun
// EXIST: typealias
@@ -26,7 +26,6 @@ interface Test {
// EXIST: tailrec
// EXIST: external
// EXIST: annotation class
// EXIST: const val
// EXIST: suspend
// EXIST: typealias
// EXIST: expect
@@ -17,6 +17,5 @@ class TestSample(<caret>)
// EXIST: actual
/* TODO: keywords below should not be here*/
// EXIST: abstract
// EXIST: const val
// EXIST: lateinit var
// NOTHING_ELSE
@@ -38,7 +38,6 @@ class Some {
// EXIST: tailrec
// EXIST: external
// EXIST: annotation class
// EXIST: const val
// EXIST: suspend
// EXIST: typealias
// EXIST: expect
@@ -38,7 +38,6 @@ class Some {
// EXIST: tailrec
// EXIST: external
// EXIST: annotation class
// EXIST: const val
// EXIST: suspend
// EXIST: typealias
// EXIST: expect
@@ -36,7 +36,6 @@ class Some {
// EXIST: tailrec
// EXIST: external
// EXIST: annotation class
// EXIST: const val
// EXIST: suspend
// EXIST: typealias
// EXIST: expect
@@ -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");