166

Intellij IDEA automatically chain packages together if the intermediate ones are otherwise empty. It is a nice feature in general. However, sometimes you don't want them to be chained, especially when you are in the middle of creating new package structures for your new project. I might have come across the setting for disabling this feature for a certain package, but I can't find it where now. So, does anyone know how to control this feature? Thank you very much.

BTW, in case you need clarification, what I mean by package chaining is as follows. Suppose you have this package structure:

$ tree com
com
└── company
    └── project
        └── some
            └── feature

Since there are really nothing else inside the intermediate folders, Intellij IDEA will automatically display it like

com.company.project.some.feature

in the project explorer, which looks like the packages are chained together.

JBT
  • 8,498
  • 18
  • 65
  • 104
  • 9
    The answer below has the real answer, but I thought I'd chime in with one thing I recently learned. If you have collapsed package structures, if you add a new package that has the same root namespace then intellij will be smart enough to expand things out. i.e if i have `com.foo.devshorts.model` and then i add `com.foo.devshorts.controllers` it'll expand out `com.foo.devshorts`. – devshorts Feb 24 '14 at 23:24
  • 3
    @devshorts That is actually one important reason why I asked. In your example, if I want create `controllers` after creating `com.foo.devshorts.model`, I will have to prefix `controller` with `com.foo.devshorts`, which is annoying. – JBT Feb 25 '14 at 00:38
  • Yeah, I feel the same way. Though I've gotten to just doing it the way I suggested since I prefer compacted packages more often than not. – devshorts Feb 25 '14 at 01:43
  • 6
    JBT & @devshorts An easier way - if you want to keep the compact packages view - is to use the navigation bar (Alt+Home). Open it, arrow over (and down) to the desired parent package, and then create a new sub-package (or prefixed class). Another pseudo workaround, since deleting is easier than typing, is to put your cursor on the long package (com.foo.devshorts.model in your example) in the project view and do a "Copy Reference" (Ctrl+Alt+Shift+C, menu Edit > Copy Reference, or in right click context menu). Then when you create your package, paste the package you copied and delete as needed. – Javaru Feb 25 '14 at 14:55

5 Answers5

305

Uncheck "Hide Empty Middle Packages" under the Project View settings dropdown:

Hide Empty Middle Packages

Petrus Theron
  • 27,855
  • 36
  • 153
  • 287
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
28

As another solution of how to create another file in the intermediate package:

Use expanded hierarchy of packages on the top of the window as shown below As shown below

Khojiakbar
  • 559
  • 5
  • 14
4

Or you just can right-click on left-sided project tab and pick "Compact Middle Packages"

Sonners
  • 41
  • 2
1

Just simply uncheck flatten modules, flatten package on right-click on PACKAGE drop-down.

0

As @Shreyash Choudhary said, uncheck Flatten Package in Project settings worked for me on v. 2021.3.2.

IntelliJ example