Created
July 11, 2013 18:25
-
-
Save dkandalov/5977888 to your computer and use it in GitHub Desktop.
Example of adding completion contributor at runtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.intellij.codeInsight.completion.* | |
import com.intellij.codeInsight.lookup.LookupElementBuilder | |
import com.intellij.openapi.extensions.Extensions | |
import com.intellij.openapi.extensions.PluginDescriptor | |
import com.intellij.openapi.util.KeyedExtensionCollector | |
import com.intellij.patterns.ElementPattern | |
import com.intellij.patterns.PlatformPatterns | |
import com.intellij.util.ProcessingContext | |
import org.jetbrains.annotations.NotNull | |
import org.picocontainer.PicoContainer | |
import com.intellij.openapi.extensions.DefaultPluginDescriptor | |
import static liveplugin.PluginUtil.changeGlobalVar | |
import static liveplugin.PluginUtil.show | |
if (isIdeStartup) return | |
static resetCompletionContributorCache() { | |
// couldn't find any other way to replace extensions in cache, therefore clearing using reflection | |
// (see com.intellij.openapi.util.KeyedExtensionCollector#forKey) | |
def field = KeyedExtensionCollector.class.getDeclaredField("myCache") | |
field.accessible = true | |
field.get(CompletionContributor.MyExtensionPointManager.INSTANCE).clear() | |
} | |
def createContributorEP(CompletionType completionType, ElementPattern elementPattern, Closure callback) { | |
def contributor = new CompletionContributor(){}.with{ | |
extend(completionType, elementPattern, new CompletionProvider<CompletionParameters>() { | |
@Override protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) { | |
callback(parameters, context, result) | |
} | |
}) | |
it | |
} | |
def contributorEP = new CompletionContributorEP() { | |
@Override CompletionContributor getInstance() { contributor } | |
@Override String getKey() { "any" } | |
@Override protected Object instantiateExtension(String implementationClass, PicoContainer picoContainer) { | |
contributor | |
} | |
} | |
contributorEP.pluginDescriptor = new DefaultPluginDescriptor("liveplugin") | |
contributorEP | |
} | |
def registerCompletionContributor(String id, CompletionType completionType, ElementPattern elementPattern, Closure callback) { | |
resetCompletionContributorCache() | |
def extensionPoint = Extensions.rootArea.getExtensionPoint("com.intellij.completion.contributor") | |
CompletionContributorEP completionContributorEP = changeGlobalVar(id) { lastContributorEP -> | |
if (lastContributorEP != null && extensionPoint.hasExtension(lastContributorEP)) { | |
extensionPoint.unregisterExtension(lastContributorEP) | |
} | |
createContributorEP(completionType, elementPattern, callback) | |
} | |
extensionPoint.registerExtension(completionContributorEP) | |
} | |
def id = "myComletionContributor" | |
registerCompletionContributor(id, CompletionType.BASIC, PlatformPatterns.psiElement()) { CompletionParameters parameters, ProcessingContext context, CompletionResultSet result -> | |
result.addAllElements([LookupElementBuilder.create("this is auto-complete suggestion")]) | |
} | |
show("registered completion contributor") |
Hi. I think this example is up-to-date and works https://github.com/dkandalov/live-plugin/wiki/Google-auto-complete (it's in Groovy though). I should probably delete this gist to avoid confusion 🤔
Ah, I somehow missed that one, thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, this is pretty old and does not work. Would it be possible for you to update and convert this to Kotlin? I'd appreciate it. Thanks!