Completion Provider with Custom Condition and Improper Handling of Special Characters

Answered

I had two miscellaneous questions regarding Completion Providers/Contributors if you don't mind:

1. I know you can filter the type of token to apply the Completion Provider to with the Completion Contributor extend method, but I was wondering if there was a way to have this contributor only apply when a certain condition is met (e.g., if the file has a specific name)?

2. In YAML files when I'm completing on a Scalar value in a Key-Value pair (e.g. “key: value” items) and I start with a special character (e.g. '.', ‘;’, etc.) and trigger a completion after, then the completion result list provides strings that do not contain that character, and triggering the completion inserts the string after the special character, instead of replacing it.

e.g. adding “this is a lookup” to CompletionResultSet, typing “key: ;”, putting the cursor after the “;” and triggering completion will bring up a result set including the “this is a lookup” string, and clicking on it will append the string after the “;”, instead of replacing it. Is there a way to change this behaviour so that either the lookup string is not in the completion set if on a special character, or the special character is replaced when the lookup element is selected?

0
2 comments

Hi Marcus,

1. I can't find a ready to use pattern to match the file name, but you can implement your own pattern:

val fileNameCondition = object : PatternCondition<PsiFile>("withFileName") {
  override fun accepts(psiFile: PsiFile, context: ProcessingContext): Boolean {
    return "FileName.kt" == psiFile.name
  }
}
val fileNamePattern = StandardPatterns.instanceOf(PsiFile::class.java).with(fileNameCondition)

There are many patterns in the platform, so I recommend checking their implementations if you need something more complex.

2. com.intellij.codeInsight.completion.InsertHandler allows for customizing what happens when the selected item is applied.

1

I'll look into both of those then, thank you so much again!

0

Please sign in to leave a comment.