4

Is there ANY way to get IntelliJ's code completion working when a file is not part of a project? Just writing a small test on the side and opened the file in Intellij but it's about as useful as nano.

The only thing it suggests for imports is * and for member functions it's suggesting absolutely nothing.

It's the only file I have currently opened.

Yes, it asked me to set a "Project SDK". I did.

No, I'm not in power saving mode.

Would appreciate your help.

User1291
  • 7,664
  • 8
  • 51
  • 108
  • I suspect that for many autocomplete suggestions IntelliJ would need information from the project, dependencies, etc. to make the suggestion. – Tim Biegeleisen Sep 26 '16 at 12:00
  • @TimBiegeleisen I very much doubt that due to the fact there ARE no dependencies. It's just a single .java file with a public static void method main. – User1291 Sep 26 '16 at 12:03
  • Can you build the file in IntelliJ? – Tim Biegeleisen Sep 26 '16 at 12:07
  • @TimBiegeleisen I can click the make project button, which does perfectly nothing except tell me that all files are up to date when I click it for a second time. – User1291 Sep 26 '16 at 12:10
  • I read something that said if your source file is not on the build path then autocomplete will not work. Add some garbage and build. Do you get errors? Fix it and build again. Did it build? – Tim Biegeleisen Sep 26 '16 at 12:11
  • @TimBiegeleisen Added some garbage and clicked the button. Again, no reaction. Removed the garbage and clicked the button again. Neither does build. – User1291 Sep 26 '16 at 12:13
  • 1
    Then the source file is not on the build path = no auto completion. Is there any reason why you can't just create a skeleton Maven project and add your source file there? – Tim Biegeleisen Sep 26 '16 at 12:13

2 Answers2

1

Based on this SO answer, it appears that a source file in IntelliJ needs to be bound to a build path in order for it get the required class definitions and resources needed for auto completion to work. Since you cannot build your project, my assumption is that the source file is not in a build path anywhere.

One quick fix would be to keep around a skeleton Maven project into which you can add the source files you want to modify. Autocompletion should be available there.

Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • So ... assuming most tests will have a main method, that means, in essence, you have to create a project for every little thing you want to test? Wow. No offence, but that's just an awful design. Thank you for your time, though. I'm going to keep this question open a bit longer and hope there's a more intelligent solution to this. – User1291 Sep 26 '16 at 12:29
  • 1
    @User1291 There probably is a better answer, but playing the devil's advocate, why would someone want to use IntelliJ without being able to build? Maybe they assume in the general case this is not too likely. – Tim Biegeleisen Sep 26 '16 at 12:34
  • Outside of toy code, writing a single-source application is actually a real thing (see [this](https://docs.oracle.com/en/java/javase/12/tools/java.html#GUID-3B1CE181-CD30-4178-9602-230B800D4FAE) and [that](https://openjdk.java.net/jeps/330)). Intellij needs to step up their game. It's a bit obnoxious to say the least that the no 1 Java editor can not assist me if I ask him to open a stand-alone Java source code file. It feels like a joke to be honest lol. – Martin Andersson Sep 14 '19 at 11:49
  • Also, just for the record.. I switched to [NetBeans](https://netbeans.apache.org/) for my single-source project which doesn't have any problems opening and supporting a single .java file =) – Martin Andersson Sep 14 '19 at 12:06
1

I had the same problem, to fix it i had to set the src folder as Source Root:

click File > Project structure > Modules, then select the src folder and click Sources.

Of course you also need to set the JDK in the project settings:

Project Structure > Platform Settings > SDKs

Also you need to set the project compile output path:

Project Structure > Project > Project compiler output and select the directory where compiled files should be generated

velocity
  • 1,630
  • 21
  • 24