Creating TextFieldWithAutoCompletion without having a reference to the project

Hello,

I'm having difficulties trying to create a TextFieldWithAutoCompletion for my plugin.

I was able to provide a create custom initialization with createUIComponents() but I cannot a reference to the project (none is open at plugin start time). I tried specifying null for project similar to EditorTextField (which accepts null just fine).

But for some reason, even though according to a jetbrains @Nullable annotation project can be passed as null - it fails with NPE trying to instantiate my TextFieldWithAutocompletion when trying to call ServiceManager getService with project specified as @NotNull: 

java.lang.IllegalArgumentException: Argument for @NotNull parameter 'project' of com/intellij/openapi/components/ServiceManager.getService must not be null
at com.intellij.openapi.components.ServiceManager.getService(ServiceManager.java)
at com.intellij.psi.PsiFileFactory.getInstance(PsiFileFactory.java:35)
at com.intellij.ui.LanguageTextField.createDocument(LanguageTextField.java:89)
at com.intellij.ui.LanguageTextField$SimpleDocumentCreator.createDocument(LanguageTextField.java:79)
at com.intellij.ui.LanguageTextField.<init>(LanguageTextField.java:63)
at com.intellij.util.textCompletion.TextFieldWithCompletion.<init>(TextFieldWithCompletion.java:48)
at com.intellij.ui.TextFieldWithAutoCompletion.<init>(TextFieldWithAutoCompletion.java:54)

That said I have the following questions:

- Is there a better way to create TextFieldWithAutoCompletion for a plugin?

- Is there a robust way to substitute the components once the project is available via providing a dummy project at start provided the answer to the first question is negative

- Out of curiosity: shouldn't there have been a compilation error for intellij platform when there's a @Nullable reference being passed to a @NotNull parameter?

0
3 comments

Hi, Alexander!

Sorry, you encountered a problem in our code. Actually there should be a @NotNull annotation in the constructor. Even if you could create a TextFieldWithAutoCompletion without a project, completion won't work in it, since it is installed on the PsiFile in the constructor that is not available without a project (see TextCompletionUtil.installProvider). In short, you need a project.

Also, TextFieldWithAutoCompletion does not do anything useful, it's better to use its ancestor TextFieldWithCompletion that accepts TextCompletionProvider which is more customisable and have several useful ancestors, such as ValuesCompletionProvider.

Answering your second question, we have a DefaultProject that is always available, you can use ProjectManager.getDefaultProject method to get it. It is used for storing default settings, for example see GithubSettingsPanel.createUiComponents() -- this is configuration panel for our GitHub plugin.

As for the question about a compilation error: no, this Nullable parameter is passed to a method without annotations=( And even with proper annotations, there only would be a warning.

Hope this helps!

--

Julia Beliaeva

1
Avatar
Permanently deleted user

Hello Julia,

Thanks for a thorough answer!

I'll give TextFieldWithCompletion a try.

Regarding the project I still don't understand where to get a proper one.

I tried getting it from ProjectManager.getInstance().getOpenProjects() but the array is empty by the time the platform builds my toolWindow's UI (where I have to construct the TextField) - which is weird by the way.

Or maybe I'm trying to obtain it from a wrong thread?

0

Alexander,

If ProjectManager.getInstance().getOpenProjects() returns an empty array, then there is no opened projects. But there are no tool windows either. Are you sure you are creating it the correct way?

You can check a section of our plugin development guide on creating tool windows. It suggests to use ToolWindowFactory interface, specifically to override a createToolWindowContent method. This method receives a project as parameter. You can find steps to follow at the bottom of the page, along with a link to a sample project.

--

Julia Beliaeva

0

Please sign in to leave a comment.