Exclude keywords from completion if the corresponding feature is unsupported by the language level selected for the module
This commit is contained in:
@@ -20,7 +20,7 @@ import com.intellij.codeInsight.completion.InsertHandler
|
||||
import com.intellij.codeInsight.completion.InsertionContext
|
||||
import com.intellij.codeInsight.lookup.LookupElement
|
||||
import com.intellij.codeInsight.lookup.LookupElementBuilder
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.module.ModuleUtilCore
|
||||
import com.intellij.psi.PsiComment
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiErrorElement
|
||||
@@ -30,10 +30,13 @@ import com.intellij.psi.filters.position.LeftNeighbour
|
||||
import com.intellij.psi.filters.position.PositionElementFilter
|
||||
import com.intellij.psi.tree.IElementType
|
||||
import com.intellij.psi.tree.TokenSet
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.config.LanguageVersionSettingsImpl
|
||||
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
|
||||
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget.*
|
||||
import org.jetbrains.kotlin.idea.completion.handlers.WithTailInsertHandler
|
||||
import org.jetbrains.kotlin.idea.completion.handlers.createKeywordConstructLookupElement
|
||||
import org.jetbrains.kotlin.idea.project.languageVersionSettings
|
||||
import org.jetbrains.kotlin.lexer.KtKeywordToken
|
||||
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
@@ -255,7 +258,7 @@ object KeywordCompletion {
|
||||
position: PsiElement): (KtKeywordToken) -> Boolean {
|
||||
val offset = position.getStartOffsetInAncestor(contextElement)
|
||||
val truncatedContext = contextElement.text!!.substring(0, offset)
|
||||
return buildFilterByText(prefixText + truncatedContext, contextElement.project)
|
||||
return buildFilterByText(prefixText + truncatedContext, position)
|
||||
}
|
||||
|
||||
private fun buildFilterWithReducedContext(prefixText: String,
|
||||
@@ -263,12 +266,12 @@ object KeywordCompletion {
|
||||
position: PsiElement): (KtKeywordToken) -> Boolean {
|
||||
val builder = StringBuilder()
|
||||
buildReducedContextBefore(builder, position, contextElement)
|
||||
return buildFilterByText(prefixText + builder.toString(), position.project)
|
||||
return buildFilterByText(prefixText + builder.toString(), position)
|
||||
}
|
||||
|
||||
|
||||
private fun buildFilterByText(prefixText: String, project: Project): (KtKeywordToken) -> Boolean {
|
||||
val psiFactory = KtPsiFactory(project)
|
||||
private fun buildFilterByText(prefixText: String, position: PsiElement): (KtKeywordToken) -> Boolean {
|
||||
val psiFactory = KtPsiFactory(position.project)
|
||||
return fun (keywordTokenType): Boolean {
|
||||
val postfix = if (prefixText.endsWith("@")) ":X Y.Z" else " X"
|
||||
val file = psiFactory.createFile(prefixText + keywordTokenType.value + postfix)
|
||||
@@ -281,6 +284,8 @@ object KeywordCompletion {
|
||||
|
||||
isErrorElementBefore(elementAt) -> return false
|
||||
|
||||
!isSupportedAtLanguageLevel(keywordTokenType, position) -> return false
|
||||
|
||||
keywordTokenType !is KtModifierKeywordToken -> return true
|
||||
|
||||
else -> {
|
||||
@@ -356,6 +361,17 @@ object KeywordCompletion {
|
||||
}
|
||||
}
|
||||
|
||||
private fun isSupportedAtLanguageLevel(keyword: KtKeywordToken, position: PsiElement): Boolean {
|
||||
val languageVersionSettings = ModuleUtilCore.findModuleForPsiElement(position)?.languageVersionSettings ?: LanguageVersionSettingsImpl.DEFAULT
|
||||
val feature = when (keyword) {
|
||||
KtTokens.TYPE_ALIAS_KEYWORD -> LanguageFeature.TypeAliases
|
||||
KtTokens.HEADER_KEYWORD, KtTokens.IMPL_KEYWORD -> LanguageFeature.MultiPlatformProjects
|
||||
KtTokens.SUSPEND_KEYWORD -> LanguageFeature.Coroutines
|
||||
else -> return true
|
||||
}
|
||||
return languageVersionSettings.supportsFeature(feature)
|
||||
}
|
||||
|
||||
// builds text within scope (or from the start of the file) before position element excluding almost all declarations
|
||||
private fun buildReducedContextBefore(builder: StringBuilder, position: PsiElement, scope: PsiElement?) {
|
||||
if (position == scope) return
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
class A {
|
||||
fun foo() {
|
||||
bar()
|
||||
}
|
||||
}
|
||||
|
||||
class B {
|
||||
fun bar() {
|
||||
foo()
|
||||
}
|
||||
}
|
||||
|
||||
<caret>
|
||||
|
||||
// EXIST: abstract
|
||||
// EXIST: class
|
||||
// EXIST: enum class
|
||||
// EXIST: final
|
||||
// EXIST: fun
|
||||
// EXIST: internal
|
||||
// EXIST: object
|
||||
// EXIST: open
|
||||
// EXIST: private
|
||||
// EXIST: public
|
||||
// EXIST: interface
|
||||
// EXIST: val
|
||||
// EXIST: var
|
||||
// EXIST: operator
|
||||
// EXIST: infix
|
||||
// EXIST: sealed
|
||||
// EXIST: data
|
||||
// EXIST: inline
|
||||
// EXIST: tailrec
|
||||
// EXIST: external
|
||||
// EXIST: annotation class
|
||||
// EXIST: const
|
||||
// NOTHING_ELSE
|
||||
+7
-1
@@ -20,6 +20,7 @@ import com.intellij.codeInsight.completion.CompletionType
|
||||
import com.intellij.codeInsight.lookup.LookupElement
|
||||
import org.jetbrains.kotlin.idea.completion.KeywordLookupObject
|
||||
import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor
|
||||
import org.jetbrains.kotlin.idea.test.KotlinProjectDescriptorWithFacet
|
||||
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform
|
||||
|
||||
abstract class AbstractKeywordCompletionTest : KotlinFixtureCompletionBaseTestCase() {
|
||||
@@ -32,7 +33,12 @@ abstract class AbstractKeywordCompletionTest : KotlinFixtureCompletionBaseTestCa
|
||||
return items.filter { it.`object` is KeywordLookupObject }.toTypedArray()
|
||||
}
|
||||
|
||||
override fun getProjectDescriptor() = KotlinLightProjectDescriptor.INSTANCE
|
||||
override fun getProjectDescriptor(): KotlinLightProjectDescriptor {
|
||||
if ("LangLevel10" in fileName()) {
|
||||
return KotlinProjectDescriptorWithFacet.KOTLIN_10
|
||||
}
|
||||
return KotlinLightProjectDescriptor.INSTANCE
|
||||
}
|
||||
|
||||
override fun defaultInvocationCount() = 1
|
||||
}
|
||||
+6
@@ -44,6 +44,12 @@ public class KeywordCompletionTestGenerated extends AbstractKeywordCompletionTes
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("AfterClasses_LangLevel10.kt")
|
||||
public void testAfterClasses_LangLevel10() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/keywords/AfterClasses_LangLevel10.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("AfterDot.kt")
|
||||
public void testAfterDot() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/keywords/AfterDot.kt");
|
||||
|
||||
Reference in New Issue
Block a user