IntelliJ Code Completion not working for new Java

2019-01-18 20:27发布

问题:

IntelliJ IDEA 13 has started exhibiting a very weird behavior in my local setup.

Namely, in any new Java class added to an existing project, Code Completion does not work. So, after declaring an object variable of any type in the new class and then typing the name of that variable, followed by the dot (.), no suggestions come up for any of the methods of the corresponding object.

For example, after declaring

File f = new File("/home");

typing f. does not bring up a list of all the methods in the File class, to select one from. As a matter of fact, when typing the dot (.), no suggestions appear and at the bottom left (in the status bar) of the IDE window, the message "Identifier expected. Identifier expected." is displayed. Sometimes, a long list of totally irrelevant methods from irrelevant components or libraries are proposed.

Strangely, Code Completion works as expected if the above declaration happens in any of the existing classes.

This behavior persists after many combinations of machine restart, IDE restart, project re-importing, closing/reopening, or rebuilding.

Any ideas?

回答1:

For followers, one thing that helped me (auto complete only showed cast field instanceof etc.) was that I had opened a maven module that didn't link correctly to its children (it had a commented out submodules section), so IntelliJ just showed the maven java files as basically "text" files with a tiny "j" in their icon in the project pane. Fixing up the root maven pom.xml file to include the children dirs as modules and voila, they are treated as real java files again, woot!



回答2:

Make sure you mark all your Source directories as such in File -> Project Structure -> Modules.



回答3:

Ensure that you are not in Power Save Mode while running IntelliJ (File [menu] -> Power Save Mode checked or not). If Power Save Mode is enabled, IntelliJ will not run background tasks, including code completion.



回答4:

I am using IntelliJ Ultimate 2017.1, and I encountered similar issue. Maven project, was trying to using selenium APIs, but the intellisense didn't pop up relative methods for me.

Here is the way I solved my issue:

Right click on your project src folder
Choose Mark Directory As Then select Sources Root



回答5:

There could be 2 reasons for your problem

  1. Either the the source folder is not properly configured in the your build java build path. If the source folder is not properly configured the autocomplete wont get the required class definitions and autocomplete would fail for your project.
  2. Restoring the default options in 'Windows > Preferences > Java > Editor > Content Assist > Advanced'


回答6:

Right click on project -> Add framework support -> Select Maven -> Click OK



回答7:

The only thing that worked for me was killing IntelliJ (on Ubuntu) and starting it again.



回答8:

Cut the folder .idea and the files *.iml in the root folder and paste somewhere else out of project. when you re-open the project , auto import will work again magically.



回答9:

I had this same problem and it was caused by me not having a folder marked as sources root.

Right click on your src directory and Mark Directory As > Sources Root.



回答10:

I was facing this issue while coding for eclipse-che client extension. Eclipse che modules are all of maven type. My client extension project was an independent module which can be compiled independent of the parent project. When I imported the same structure to Intellij my client-side extension project was not giving suggestions. After I include the client-extension project to the parent module (adding it to pom.xml) the suggestions started showing up.



回答11:

Remove your package from the Settings>Editor>General>Auto Import>Java>Exclude from Import and Completion

I had run into this problem. None of the solutions from this post worked, but I was able to fix it because I accidentally put my project's package in the "Exclude from Import and Completion" settings.



回答12:

I had a groovy project where somehow the jdk was not set as a dependency, so jdk classes would not autocomplete.

Fix:

  1. project structure
  2. modules
  3. (your module)
  4. tab Dependencies.
  5. In the field Module SDK, add a jdk.


回答13:

This is not IntelliJ 13 but a more recent 2017.1.1 Community version. Adding this answer here so that others benefit from a cause that has its roots in the Gradle Configurations.

Now, in my case too the auto complete, Generate Override methods and other features were not working. What I figured out is that in my project settings, I had multiple configurations for the gradle modules. The moment I cleaned up the rest of the modules and retained the only one (apart from test) I needed for that project, all behaviors were back to normal.

Also in such cases of Gradle projects, remember to mark all your sourceSet root folders ad the Source folders.

Edit: If you do not want to create multiple configurations per sourceSet and you are importing a gradle module, on the import screen, you should also disable the Create a module for per sourceSet flag. Without this, I was having to remove the multiple modules after importing the module.



回答14:

I had similar problem, nothing in this topic helped.

So finally I made it work, I clear cache of IntelliJ and restart.

File > Invalidate Caches / Restart.. > Invalidate Cache and Restart (button)