Completion refactoring: got rid of JetPackagesContributor
This commit is contained in:
@@ -223,14 +223,9 @@
|
||||
<codeInsight.gotoSuper language="jet" implementationClass="org.jetbrains.jet.plugin.codeInsight.GotoSuperActionHandler"/>
|
||||
<typeDeclarationProvider implementation="org.jetbrains.jet.plugin.codeInsight.JetTypeDeclarationProvider"/>
|
||||
|
||||
<completion.contributor language="jet"
|
||||
id="JetPackagesContributor"
|
||||
order="first"
|
||||
implementationClass="org.jetbrains.jet.plugin.completion.JetPackagesContributor"/>
|
||||
|
||||
<completion.contributor language="jet"
|
||||
id="JetCompletionContributor"
|
||||
order="after JetPackagesContributor"
|
||||
order="first"
|
||||
implementationClass="org.jetbrains.jet.plugin.completion.JetCompletionContributor"/>
|
||||
|
||||
<completion.contributor language="jet"
|
||||
|
||||
@@ -60,7 +60,7 @@ public class JetCompletionContributor : CompletionContributor() {
|
||||
val dummyIdentifier = when {
|
||||
context.getCompletionType() == CompletionType.SMART -> CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED + "$" // add '$' to ignore context after the caret
|
||||
|
||||
JetPackagesContributor.ACTIVATION_PATTERN.accepts(tokenBefore) -> JetPackagesContributor.DUMMY_IDENTIFIER
|
||||
PackageDirectiveCompletion.ACTIVATION_PATTERN.accepts(tokenBefore) -> PackageDirectiveCompletion.DUMMY_IDENTIFIER
|
||||
|
||||
EXTENSION_RECEIVER_TYPE_ACTIVATION_PATTERN.accepts(tokenBefore) -> EXTENSION_RECEIVER_TYPE_DUMMY_IDENTIFIER
|
||||
|
||||
@@ -117,6 +117,11 @@ public class JetCompletionContributor : CompletionContributor() {
|
||||
return
|
||||
}
|
||||
|
||||
if (PackageDirectiveCompletion.perform(parameters, result)) {
|
||||
result.stopHere()
|
||||
return
|
||||
}
|
||||
|
||||
val jetReference = getJetReference(parameters)
|
||||
if (jetReference != null) {
|
||||
try {
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2014 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.jet.plugin.completion
|
||||
|
||||
import com.intellij.codeInsight.completion.*
|
||||
import com.intellij.openapi.progress.ProcessCanceledException
|
||||
import com.intellij.patterns.ElementPattern
|
||||
import com.intellij.patterns.PlatformPatterns
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.util.ProcessingContext
|
||||
import org.jetbrains.jet.lang.psi.JetFile
|
||||
import org.jetbrains.jet.lang.psi.JetPackageDirective
|
||||
import org.jetbrains.jet.plugin.caches.resolve.*
|
||||
import org.jetbrains.jet.plugin.codeInsight.TipsManager
|
||||
import org.jetbrains.jet.plugin.references.JetSimpleNameReference
|
||||
|
||||
/**
|
||||
* Performs completion in package directive. Should suggest only packages and avoid showing fake package produced by
|
||||
* DUMMY_IDENTIFIER.
|
||||
*/
|
||||
public class JetPackagesContributor : CompletionContributor() {
|
||||
class object {
|
||||
val DUMMY_IDENTIFIER = "___package___"
|
||||
|
||||
val ACTIVATION_PATTERN = PlatformPatterns.psiElement().inside(javaClass<JetPackageDirective>())
|
||||
}
|
||||
|
||||
{
|
||||
extend(CompletionType.BASIC, ACTIVATION_PATTERN, object : CompletionProvider<CompletionParameters>() {
|
||||
override fun addCompletions(parameters: CompletionParameters, context: ProcessingContext, result: CompletionResultSet) {
|
||||
val file = parameters.getPosition().getContainingFile() as JetFile
|
||||
|
||||
val ref = file.findReferenceAt(parameters.getOffset())
|
||||
|
||||
if (ref is JetSimpleNameReference) {
|
||||
val name = ref.expression.getText()!!
|
||||
|
||||
try {
|
||||
val prefixLength = parameters.getOffset() - ref.expression.getTextOffset()
|
||||
var result = result.withPrefixMatcher(PlainPrefixMatcher(name.substring(0, prefixLength)))
|
||||
|
||||
val resolveSession = ref.expression.getLazyResolveSession()
|
||||
val bindingContext = resolveSession.resolveToElement(ref.expression)
|
||||
|
||||
val variants = TipsManager.getPackageReferenceVariants(ref.expression, bindingContext)
|
||||
for (variant in variants) {
|
||||
val lookupElement = DescriptorLookupConverter.createLookupElement(resolveSession, variant)
|
||||
if (!lookupElement.getLookupString().contains(DUMMY_IDENTIFIER)) {
|
||||
result.addElement(lookupElement)
|
||||
}
|
||||
}
|
||||
|
||||
result.stopHere()
|
||||
}
|
||||
catch (e: ProcessCanceledException) {
|
||||
throw rethrowWithCancelIndicator(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2010-2014 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.jet.plugin.completion
|
||||
|
||||
import com.intellij.codeInsight.completion.*
|
||||
import com.intellij.openapi.progress.ProcessCanceledException
|
||||
import com.intellij.patterns.ElementPattern
|
||||
import com.intellij.patterns.PlatformPatterns
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.util.ProcessingContext
|
||||
import org.jetbrains.jet.lang.psi.JetFile
|
||||
import org.jetbrains.jet.lang.psi.JetPackageDirective
|
||||
import org.jetbrains.jet.plugin.caches.resolve.*
|
||||
import org.jetbrains.jet.plugin.codeInsight.TipsManager
|
||||
import org.jetbrains.jet.plugin.references.JetSimpleNameReference
|
||||
|
||||
/**
|
||||
* Performs completion in package directive. Should suggest only packages and avoid showing fake package produced by
|
||||
* DUMMY_IDENTIFIER.
|
||||
*/
|
||||
object PackageDirectiveCompletion {
|
||||
val DUMMY_IDENTIFIER = "___package___"
|
||||
val ACTIVATION_PATTERN = PlatformPatterns.psiElement().inside(javaClass<JetPackageDirective>())
|
||||
|
||||
fun perform(parameters: CompletionParameters, result: CompletionResultSet): Boolean {
|
||||
val position = parameters.getPosition()
|
||||
if (!ACTIVATION_PATTERN.accepts(position)) return false
|
||||
|
||||
val file = position.getContainingFile() as JetFile
|
||||
|
||||
val ref = file.findReferenceAt(parameters.getOffset()) as? JetSimpleNameReference ?: return false
|
||||
val name = ref.expression.getText()!!
|
||||
|
||||
try {
|
||||
val prefixLength = parameters.getOffset() - ref.expression.getTextOffset()
|
||||
var result = result.withPrefixMatcher(PlainPrefixMatcher(name.substring(0, prefixLength)))
|
||||
|
||||
val resolveSession = ref.expression.getLazyResolveSession()
|
||||
val bindingContext = resolveSession.resolveToElement(ref.expression)
|
||||
|
||||
val variants = TipsManager.getPackageReferenceVariants(ref.expression, bindingContext)
|
||||
for (variant in variants) {
|
||||
val lookupElement = DescriptorLookupConverter.createLookupElement(resolveSession, variant)
|
||||
if (!lookupElement.getLookupString().contains(DUMMY_IDENTIFIER)) {
|
||||
result.addElement(lookupElement)
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
catch (e: ProcessCanceledException) {
|
||||
throw rethrowWithCancelIndicator(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user