Completion inside a string literal
Answered
Hi, I'm trying to add some features on top of Protobuf plugin: to add completion of some custom options
class DimensionCompletionContributor : CompletionContributor() {
init {
extend(CompletionType.BASIC, Patterns.protoDimensionString, DimensionCompletionProvider())
}
}
class DimensionCompletionProvider() : CompletionProvider<CompletionParameters>()
{
override fun addCompletions(parameters: CompletionParameters, context: ProcessingContext, result: CompletionResultSet) {
result.addElement(LookupElementBuilder.create("is_test"))
result.addElement(LookupElementBuilder.create("is_test1"))
result.addElement(LookupElementBuilder.create("is_test2"))
result.addElement(LookupElementBuilder.create("is_test3"))
}
}
Pattern looks like this:
val protoDimensionString by lazy {
pp.psiElement().inside(pp.psiElement()
.withElementType(ProtoParserDefinition.token(ProtoParser.STRING_VALUE))
)
}
It works fine when I'm typing in the beginning of the literal (first attachment), however it doesn't work at any other part of the sting (see second picture).
Could anyone advise me how to get it to work?


Please sign in to leave a comment.
Looks like your
does not match a middle of the string.
or prefix matcher is not `is_` in such case, but `is_moderator, is_` what is not looks like a good place for your completions.
Thanks, Alexandr, you were right! Once I rewrote the prefix, it started working as expected (the code below).
One question, it there anything I can do with the pattern to match a word inside string literal? Just curious.
It depends on how literals are parsed. If whole string content is a single token, then probably not. If there are some literal chunks tokens, you should use them in pattern.
negram, you can take a look at LookupElementDecorator abstract class.
It lets you manipulate the lookup string and what is inserted.
You want the lookup string to be prefixed with `is_moderator, is_` but choices presented to the user would be `is_..` only.
I used it to create a WrappingDecorator with options to add/remove prefix/suffix in lookup string or the inserted string. I use it in some cases to present "user friendly" choices while inserting strings which are messy because of their context, just like you have in your example.
It is a bit of hacked class I wrote a while back through trial and error. Using it is similarly trial and error since after getting it working I never went back to clean it up to be easier to use but it does solve the issue for my Markdown Navigator plugin, where completions can be done inside other completable elements, which the IDE does not really expect in a real language. For example a heading is a named element which can be referenced so the IDE treats it as the target of completion, but it can contain links and emoji shortcuts which are also completable, somewhere inside the heading text. Creates a similar issue to what you have.
I am not sure you can use the class directly but it may give you ideas on your own implementation. Let me know if you want me to post the source here, it is in Kotlin.