Refactoring in Photran

Analysis & Refactoring

Photran 4.0 is the first release to incorporate basic refactoring support as well as program analysis-based features such as content assist, a Fortran Declaration view, and an Open Declaration action. Currently, only two refactorings are supported, but more are in development.

The instructions on this page are for Photran 4.0 Beta 4. The instructions for prior versions were slightly different.

On this page...

  1. Preparing to Use Analysis Features and Refactoring Support
  2. General Instructions for Using Refactorings
  3. The Rename Refactoring
  4. The Introduce Implicit None Refactoring

Preparing to Use Analysis Features and Refactoring Support

Setting Up the Indexer

In order to perform refactorings or to use the Open Declaration action, content assist, or the Fortran Declaration view, Photran must keep track of what modules, subprograms, etc. are declared in every file in your project. This information is updated every time you save a file. Since this part of Photran is experimental and has not been optimized for speed, it is disabled unless you explicitly enable it in your project.

  1. Right-click on your project's folder in the Fortran Projects view
  2. Click on Properties
  3. Expand Fortran General in the list on the left, and click on Analysis/Refactoring
  4. Check the "Enable Fortran analysis/refactoring" check box
  5. If you want to enable content assist or the Fortran Declaration view, check those boxes as well
  6. Click OK

Setting Up Module and Include Paths

If your source code contains INCLUDE lines, or USE lines referencing modules in other files, Photran needs to know where to look in order to find these. It will not figure this out automatically. For each project in which you plan to use refactoring support,

  1. Right-click on your project's folder in the Fortran Projects view
  2. Click on Properties
  3. Expand Fortran General in the list on the left, and click on Analysis/Refactoring
  4. List the folders in which Photran should search for INCLUDE files and modules when refactoring. They will be searched in order from the first folder listed to the last. Subfolders are not searched automatically; you must include them explicitly.
  5. Click OK

General Instructions for Using Refactorings

In the Fortran editor (for free-format source code), select (highlight) the name of a variable, subprogram, etc. Then right-click on the selected name, choose Refactoring from the context menu, and click on the name of the appropriate refactoring. Enter any information requested, then click Preview to see what changes it will make. It is important to verify that they are what you expect! Then click OK to make the changes, or Cancel to avoid making any changes.

Where you click on is extremely important. Consider this program, for example.

1 ! Print hello to the screen
2 program p
3     character(len=*) :: hello = 'hello'
4     print *, hello
5 contains
6     subroutine s
7         character(len=*) :: hello = 'This is a different hello'
8     end subroutine
9 end program

If you click on hello in the comment or the string literal (lines 1 and 3), the refactoring will not work, because hello is not a variable in that context; it is just a piece of text. If you click on the variable hello in lines 3 or 4, refactoring will succeed. Note, however, that the variable hello in line 7 is completely different. So if you rename the occurrence of hello on line 3 and 4, those occurrences will both change, but the hello variable on line 7 will remain the same. Similarly, you can rename hello on line 7 without changing the variable on lines 3 and 4.

The Rename Refactoring

  • Purpose: Rename essentially acts as a "smart" search and replace, allowing the names of the following entities to be changed. It correctly observes scoping and shadowing rules and renames module entities across files.
  • Applies To:
    1. Local variables1,2
    2. Subprograms3
    3. Derived types
    4. Module entities
    5. Main programs
    6. Namelists
    7. Common blocks
    8. Block data
    1. Dummy subprogram arguments cannot be renamed
    2. Components of derived types cannot be renamed
    3. Subprograms cannot be renamed if they are declared External or Intrinsic

The Introduce Implicit None Refactoring

  • Purpose. Introduce Implicit None adds an implicit none statement and adds explicit declarations for all variables that were previously declared implicitly.
  • Applies To:
    1. Main programs
    2. Subprograms
    3. Modules