• Stars
    star
    373
  • Rank 114,600 (Top 3 %)
  • Language
    Python
  • License
    Other
  • Created over 8 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

auto-formatter for modern fortran source code

fprettify

CI Coverage Status PyPI - License PyPI Code Climate

fprettify is an auto-formatter for modern Fortran code that imposes strict whitespace formatting, written in Python.

NOTE: I'm looking for help to maintain this repository, see #127.

Features

  • Auto-indentation.
  • Line continuations are aligned with the previous opening delimiter (, [ or (/ or with an assignment operator = or =>. If none of the above is present, a default hanging indent is applied.
  • Consistent amount of whitespace around operators and delimiters.
  • Removal of extraneous whitespace and consecutive blank lines.
  • Change letter case (upper case / lower case conventions) of intrinsics
  • Tested for editor integration.
  • By default, fprettify causes whitespace changes only and thus preserves revision history.
  • fprettify can handle cpp and fypp preprocessor directives.

Limitations

  • Works only for modern Fortran (Fortran 90 upwards).
  • Feature missing? Please create an issue.

Requirements

  • Python 3 (Python 2.7 no longer supported)
  • ConfigArgParse: optional, enables use of config file

Examples

Compare examples/*before.f90 (original Fortran files) with examples/*after.f90 (reformatted Fortran files) to see what fprettify does. A quick demonstration:

program demo
integer :: endif,if,elseif
integer,DIMENSION(2) :: function
endif=3;if=2
if(endif==2)then
endif=5
elseif=if+4*(endif+&
2**10)
elseif(endif==3)then
function(if)=endif/elseif
print*,endif
endif
end program

⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩ fprettify ⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩

program demo
   integer :: endif, if, elseif
   integer, DIMENSION(2) :: function
   endif = 3; if = 2
   if (endif == 2) then
      endif = 5
      elseif = if + 4*(endif + &
                       2**10)
   elseif (endif == 3) then
      function(if) = endif/elseif
      print *, endif
   endif
end program

Installation

The latest release can be installed using pip:

pip install --upgrade fprettify

Installation from source requires Python Setuptools:

pip install .

For local installation, use --user option.

If you use the Conda package manager, fprettify is available from the conda-forge channel:

conda install -c conda-forge fprettify

Command line tool

Autoformat file1, file2, ... inplace by

fprettify file1, file2, ...

The default indent is 3. If you prefer something else, use --indent n argument.

In order to apply fprettify recursively to an entire Fortran project instead of a single file, use the -r option.

For more options, read

fprettify -h

Editor integration

For editor integration, use

fprettify --silent

For instance, with Vim, use fprettify with gq by putting the following commands in your .vimrc:

autocmd Filetype fortran setlocal formatprg=fprettify\ --silent

Deactivation and manual formatting (experimental feature)

fprettify can be deactivated for selected lines: a single line followed by an inline comment starting with !& is not auto-formatted and consecutive lines that are enclosed between two comment lines !&< and !&> are not auto-formatted. This is useful for cases where manual alignment is preferred over auto-formatting. Furthermore, deactivation is necessary when non-standard Fortran syntax (such as advanced usage of preprocessor directives) prevents proper formatting. As an example, consider the following snippet of fprettify formatted code:

A = [-1, 10, 0, &
     0, 1000, 0, &
     0, -1, 1]

In order to manually align the columns, fprettify needs to be deactivated by

A = [-1,   10, 0, & !&
      0, 1000, 0, & !&
      0,   -1, 1]   !&

or, equivalently by

!&<
A = [-1,   10, 0, &
      0, 1000, 0, &
      0,   -1, 1]
!&>

Contributing / Testing

The testing mechanism allows you to easily test fprettify with any Fortran project of your choice. Simply clone or copy your entire project into fortran_tests/before and run python setup.py test. The directory fortran_tests/after contains the test output (reformatted Fortran files). If testing fails, please submit an issue!

More Repositories

1

stdlib

Fortran Standard Library
Fortran
1,066
star
2

fpm

Fortran Package Manager (fpm)
Fortran
879
star
3

fortls

fortls - Fortran Language Server
Python
249
star
4

vscode-fortran-support

Fortran language support for Visual Studio Code
TypeScript
212
star
5

fortran-lang.org

(deprecated) Fortran website
HTML
130
star
6

minpack

Modernized Minpack: for solving nonlinear equations and nonlinear least squares problems
Fortran
94
star
7

test-drive

The simple testing framework
Fortran
81
star
8

setup-fortran

GitHub action to setup Fortran compiler and toolchain
Shell
75
star
9

fftpack

Double precision version of fftpack
Fortran
67
star
10

http-client

http-client offers a user-friendly, high-level API to make HTTP requests in Fortran.
Fortran
58
star
11

webpage

New Fortran webpage
Python
47
star
12

fpm-registry

Centralized registry of fpm packages
Python
36
star
13

playground

An interactive Fortran playground
JavaScript
34
star
14

fpm-docs

Documentation repository for the Fortran package manager (fpm)
Fortran
34
star
15

setup-fpm

GitHub Action to setup the Fortran Package Manager for CI on Ubuntu, MacOS and Windows.
JavaScript
29
star
16

benchmarks

Fortran benchmarks
C
19
star
17

stdlib-docs

Documentation for https://github.com/fortran-lang/stdlib
HTML
18
star
18

homebrew-fortran

Homebrew tap for Fortran tooling and libraries
Ruby
13
star
19

talks

Repository for talks and presentations about fortran-lang projects
TeX
13
star
20

stdlib-cmake-example

Integration of the Fortran standard library in CMake projects
Fortran
13
star
21

fortran-forum-article-template

Article template for the ACM Fortran Forum
Shell
9
star
22

registry

Registry for Fortran package manager
JavaScript
8
star
23

fpm-metadata

Python model for Fortran package manifests
Python
6
star
24

fpm-on-wheels

Fortran package manager on wheels
Shell
4
star
25

assets

Assets, artwork and graphics for fortran-lang
HTML
3
star
26

fpm-haskell

The legacy Haskell implementation of fpm. No longer under active development.
Haskell
2
star
27

.github

Community-wide resources and default files for Fortran-lang
2
star