From b98aa76325514c6e90f3cf7c4c91a20c105c6abe Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Tue, 7 Jul 2015 13:53:01 +0300 Subject: [PATCH] Added completion action to exclude classes/packages. #KT-2413 fixed --- ...cludeFromCompletionLookupActionProvider.kt | 68 +++++++++++++++++++ idea/src/META-INF/plugin.xml | 1 + 2 files changed, 69 insertions(+) create mode 100644 idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/KotlinExcludeFromCompletionLookupActionProvider.kt diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/KotlinExcludeFromCompletionLookupActionProvider.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/KotlinExcludeFromCompletionLookupActionProvider.kt new file mode 100644 index 00000000000..657d949850b --- /dev/null +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/KotlinExcludeFromCompletionLookupActionProvider.kt @@ -0,0 +1,68 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.kotlin.idea.completion + +import com.intellij.codeInsight.completion.ExcludeFromCompletionLookupActionProvider +import com.intellij.codeInsight.daemon.impl.actions.AddImportAction +import com.intellij.codeInsight.lookup.Lookup +import com.intellij.codeInsight.lookup.LookupActionProvider +import com.intellij.codeInsight.lookup.LookupElement +import com.intellij.codeInsight.lookup.LookupElementAction +import com.intellij.openapi.project.Project +import com.intellij.psi.* +import com.intellij.psi.util.PsiUtil +import com.intellij.util.Consumer +import org.jetbrains.kotlin.idea.core.completion.DeclarationLookupObject +import org.jetbrains.kotlin.idea.imports.importableFqName +import org.jetbrains.kotlin.name.FqName + +public class KotlinExcludeFromCompletionLookupActionProvider : LookupActionProvider { + override fun fillActions(element: LookupElement, lookup: Lookup, consumer: Consumer) { + val lookupObject = element.getObject() as? DeclarationLookupObject ?: return + + lookupObject.descriptor?.let { + it.importableFqName?.let { + addExcludes(consumer, lookup.getPsiFile().getProject(), it) + return + } + } + + lookupObject.psiElement?.let { + val fakeLookupElement = object : LookupElement() { + override fun getLookupString() = "" + override fun getObject() = it + } + + ExcludeFromCompletionLookupActionProvider().fillActions(fakeLookupElement, lookup, consumer) + } + } + + private fun addExcludes(consumer: Consumer, project: Project, fqName: FqName) { + for (s in AddImportAction.getAllExcludableStrings(fqName.asString())) { + consumer.consume(ExcludeFromCompletionAction(project, s)) + } + } + + private class ExcludeFromCompletionAction( + private val project: Project, + private val exclude: String + ) : LookupElementAction(null, "Exclude '$exclude' from completion") { + override fun performLookupAction(): LookupElementAction.Result { + AddImportAction.excludeFromImport(project, exclude) + return LookupElementAction.Result.HIDE_LOOKUP + } + } +} diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 75d442a17af..9c6af2334ef 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -322,6 +322,7 @@ +