How to invoke code completion via code?

已回答

Hi,

I would like to invoke code completion, showing a drop down of some options to enter, from within my plugin. Is this possible and how might I do this?

Thanks

0
Avatar
Permanently deleted user

Thanks for the response. I've been trying to use the following code:

CompletionService.getCompletionService().getVariantsFromContributors(parameters, new JsonCompletion(), new Consumer<CompletionResult>());

However my issue is, this doesn't create a code completion drop down as I would like, it just supplies lookup elements for code completion when the user next begins to type. I would like to create a drop down offering some hard-coded code completion options, not dictated by whether or not the user is typing. What I would like is the equivalent of creating the code completion list, then calling .setVisible(true). As it is now, I create the list but it isn't visible until typing happens or ctrl + space, any ideas?

0
Avatar
Permanently deleted user

So I have ended up using the following code:

new CodeCompletionHandlerBase(CompletionType.BASIC, true, false, true)
.invokeCompletion(project, editor, 10, true, false);

This works as I would like. The only issue is with write safe context. If I package it in ApplicationManager.runWriteAction(), it fails with assertion error and says completion should not be invoked inside write action. If I package it in ApplicationManager.invokeLater(), it tells me write unsafe context, but performs completion as I would like. I've tried it with all ModalityState options but none seem to work. Is it possible to do what I'm doing without these errors? Thanks

0

Please explain your use-case in more detail: in what place? by explicit action from user? or automatically, if yes when and why?

0
Avatar
Permanently deleted user

I have code completion for certain phrases which can be entered in a json file. When the code completion for these phrases is triggered, it will insert a new json block. The issue is, I will need more information than just one phrase and the way I am solving that is by invoking more code completion drop downs, with the required information for the user to select. Once it is all selected, the new json block is inserted. This works except for the write unsafe context issue.

I've looked into using templates for this but they don't seem applicable, the reason being I use http calls to determine the json block and there are thousands of options it could be. As far as I know, there is now way to dynamically create a template like this.

0

Sorry for delay. I'm not quite sure how this works/behaves in editor as you described. Could you please share your full code? Alternatively, show a modal dialog with corresponding input fields using completion (com.intellij.ui.TextFieldWithAutoCompletion etc) ?

0

请先登录再写评论。