Skip to main content

Introduction

Codee offers automatic formatting for Fortran source code using the codee format command.

Installation

Please refer to the Codee Installation Guide for platform-specific instructions. Note that no license is required to use codee format.

Standalone tool

Usage example

The codee format command formats Fortran files with the following extensions:

  • Free-form: .f90, .f95, .f03, .f08, .f18.
  • Fixed-form: .f, .for, .f77, .ftn, fpp.

The extension matching is case-insensitive; e.g., both .f90 and .F90 are recognized. Additionaly, note that fixed-form support is still experimental.

Consider the following Fortran code:

subroutine grid_pressure_velocity_calc(p,v)
implicit none
integer i, j
real(kind=8) :: dx, dy, p(:,:), v(:,:)

dx = 1.0d-3
dy = 1.0d-3

do j = 1, size(p, 1)
do i = 1,size(p, 2)
p(i,j)=dx*dy*real(i*j,kind=8)
v(i,j)=p(i,j)/(dx+dy)
end do
enddo

call update_boundary_conditions(p,v)
endsubroutine

By simply running the following command:

$ codee format code.f90

Codee will adjust the code indentation, spacing, and other style elements, such as keywords or the :: separator:

subroutine grid_pressure_velocity_calc(p, v)
implicit none
integer :: i, j
real(kind=8) :: dx, dy, p(:, :), v(:, :)

dx = 1.0d-3
dy = 1.0d-3

do j = 1, size(p, 1)
do i = 1, size(p, 2)
p(i, j) = dx * dy * real(i * j, kind=8)
v(i, j) = p(i, j) / (dx + dy)
end do
end do

call update_boundary_conditions(p, v)
end subroutine grid_pressure_velocity_calc

Codee can also format entire directories and all contained files by specifying the directory:

$ codee format my-directory

Configuring the style

You can customize the formatting style of codee format using the --style option, or by creating a .codee-format file and using the --config option.

The --style option will take precedence over the configuration file:

$ codee format --style "{IndentSize: 2, ColumnLimit: 80}" code.f90

A simple way to create a custom .codee-format file for your project is to start with the default configuration:

$ codee format --dump-preset-config default --with-docs > .codee-format

The configuration file uses a YAML format. It presents a list of configurable options, each accepting different values:

# The style used for all options not specifically set in the configuration.
# Possible values are:
# - `Codee` -> A style recommended by Codee.
# - `InheritParentConfig` -> Use the `.codee-format` from parent directories.
# If no such file is found falls back to `Codee`.
# - `Preserve` -> Preserve the original source code style. It can be
# used as a base configuration to create a specific
# style.

# If true, free-form end-of-line continuation ampersands are always placed at
# the column limit instead of right after the end of the line.
# This option doesn't have any effect when there is no `ColumnLimit` set.
# Defaults to **`false`**.
AlignAmpersandToColumnLimit: false

# When breaking lines according to `ColumnLimit`, preferentially break at
# operators inside assignments. Possible values are: `true` or `false`.
# Defaults to **`false`**.
AlignAssignmentOperators: true
...

For a complete list of available options, refer to the Style Options Documentation.

Disable format

Codee's automatic formatting can be disabled for a specific section of the source code by enclosing it between the directives codee format off and codee format on. This allows to preserve the given segment of the code without changes.

A common use example is matrix initialization using literals, often a highly ad-hoc formatting style:

! codee format off

<Code block>

! codee format on

Additional help

Don't forget to run codee format --help to explore all the options available!

Integrations with other tools

You can find detailed step-by-step guides for integrating Codee format with various tools at the Integrations sections.