Skip to content

Instantly share code, notes, and snippets.

@dkandalov
Created July 11, 2013 18:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkandalov/5977888 to your computer and use it in GitHub Desktop.
Save dkandalov/5977888 to your computer and use it in GitHub Desktop.
Example of adding completion contributor at runtime
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")
@DasGandlaf
Copy link

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!

@dkandalov
Copy link
Author

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 🤔

@DasGandlaf
Copy link

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