Get current auto-complete at cursor

I'm trying to get the list of possible auto-complete values at the current cursor location.

class CursorListener : CaretListener {
override fun caretPositionChanged(event: CaretEvent) {
val editor = event.getEditor()
val project = event.editor.project!!
val manager = FileEditorManager.getInstance(project);
val file = manager.selectedFiles[0]
val psiFile = PsiManager.getInstance(project).findFile(file)
val element = psiFile!!.findElementAt(event.caret!!.offset)
val res = LookupElementBuilder.createWithSmartPointer("", element)

I need to get a lookupElementList rather than build one. Another problem I'm seeing is element is the top of the file rather than the cursor location.


What exactly is your use-case?


Voice to text software for coding. I want to use autocomplete to help determine possible recognitions


See com.intellij.codeInsight.completion.CompletionService. Though items might be sorted/hidden/not available "yet"/too many items to show all at once, so I'm not sure how that would work in your case.


In `CompletionService.getCompletionService()` and `CompletionService.getCompletionService().currentCompletion` is there a way to actually get the results? All the methods are void. Debugging, I was able to see it has the correct number of elements but couldn't find the element values.

I also see there is `CompletionService.getCompletionService().performCompletion(` but not seeing how to get the results from that either.

>> Though items might be sorted/hidden/not available "yet"/too many items to show all at once

Should I be using a different listener?


You should receive the elements in "consumer" parameter for com.intellij.codeInsight.completion.CompletionService#getVariantsFromContributors(com.intellij.codeInsight.completion.CompletionParameters, com.intellij.codeInsight.completion.CompletionContributor, com.intellij.util.Consumer<? super com.intellij.codeInsight.completion.CompletionResult>)


My remark about items availability is a general remark, since completion does a lot of filtering and sorting and depends on user interaction to filter/show elements possibly. So you might or might not get the same results as a "real completion" interaction will yield.


Please sign in to leave a comment.