Skip to content

Instantly share code, notes, and snippets.

@bgourlie
Created June 12, 2016 15:43
Show Gist options
  • Save bgourlie/da6fe8aed93fa88c52c265d49e1e3aa4 to your computer and use it in GitHub Desktop.
Save bgourlie/da6fe8aed93fa88c52c265d49e1e3aa4 to your computer and use it in GitHub Desktop.
package org.rust.ide
import com.intellij.codeInsight.completion.*
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.patterns.PlatformPatterns
import com.intellij.util.ProcessingContext
import org.rust.lang.RustLanguage
import org.rust.lang.core.psi.RustOuterAttrElement
class DeriveCompletionContributor : CompletionContributor() {
init {
extend(CompletionType.BASIC, PlatformPatterns.psiElement(RustOuterAttrElement::class.java).withLanguage(RustLanguage), object : CompletionProvider<CompletionParameters>() {
override fun addCompletions(parameters: CompletionParameters, context: ProcessingContext?, result: CompletionResultSet) {
result.addElement(LookupElementBuilder.create("Clone"));
result.addElement(LookupElementBuilder.create("Copy"));
}
})
}
}
@bgourlie
Copy link
Author

bgourlie commented Jun 12, 2016

To preface: I'm new both to Kotlin and IntelliJ plugin development, so please excuse any naivety!

I'm following the example here for creating a CompletionContributor. I'm having trouble getting this simple completion to trigger, presumably because I'm not passing the correct parameters to PlatformPatterns.psiElement().

Ultimately, we'll want completion to trigger when the following conditions are met:

  1. It's an attribute element
  2. It's a derive attribute, specifically
  3. The carat is within the parens, eg: #[derive(C<carat>)]

Right now, I'm just trying to accomplish step one, which is to correctly identify the element.

FWIW, I've gotten this completion to trigger on RustLiteral.Text elements, simply to rule out any issues with getting the completion contributor registered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment