Search code examples
phpstorm

Show current file's block hierarchy


Is there a way in PhpStorm to show the hierarchy of where I am in the code? I'll often be placed on line 800 of some crappy top-down file and have no idea what block of the file I'm in.

For example, if my code looked like:

if (a) {
  // ... 200 lines of code ...
  if (unrelated) {
     // ... 200 lines of code ...
  }
  if (b) { 
     // ... 200 lines of code ...
  } else {
     // ... 200 lines of code ...
     if (c) {
        switch ($var) {
            case 'a':
               [MY CURRENT CURSOR LOCATION]
        }
     } else {
       // ... 200 lines of code ...
     }
  }
}

I'd want to see a summary something like:

if (a) {
  if (b) {} else {
    if (c) {
      switch case 'a':

Solution

  • That is not available for PHP (and I'm not sure if it's present for any other supported "coding" language like JS/Java/Ruby/Python/etc).

    For PHP it shows max 3 levels: Namespace, Class and Method. It does not include control/logic flow directives (if/else/for/switch)

    For hierarchical/markup languages like HTML/XML/JSON/YAML/CSS (mainly pre-processors that allow nesting) and alike it will show more nodes due to their nature.


    The best I can suggest right now that will help a bit (excluding refactoring such code as it's out of the scope and may not always be possible):

    • CodeGlance plugin (will show scrollable small code overview on a side like Sublime has).
    • Use code folding to collapse not-needed-at-the-moment nodes (can be standard nodes or custom block of text).
    • If you hover over closing collapsing mark (that plus/minus clickable element on the left editor edge that expands/collapses the node) IDE will show you top 5 or so lines of the opening block via temp popup at the top -- good to identify what is the closest "branch" you are in.

    Partially related existing tickets:

    Feel free to submit your own Feature Request ticket at PhpStorm's Issue Tracker.

    The possible issue with implementing this might be the expected complexity of the "if" conditions (most obvious case) -- quite often it will consist of 2+ conditions that might be quite lengthy (e.g. occupies 2+ lines). Problem arise with "how to display it in that small breadcrumbs area that is available".