fortran-lang / fprettify

auto-formatter for modern fortran source code
https://pypi.python.org/pypi/fprettify
Other
370 stars 76 forks source link

Line continuation indentation is not idempotent #93

Closed dev-zero closed 3 years ago

dev-zero commented 3 years ago

in the following example from CP2K an extra space is inserted before each continued line every time fprettify runs on the file:

MODULE cp_lbfgs
CONTAINS
   SUBROUTINE prn1lb()
      FORMAT('RUNNING THE L-BFGS-B CODE', /, /,                          &
      & 'it    = iteration number', /,                                    &
      & 'Machine precision =', 1p, d10.3)

200   FORMAT('RUNNING THE L-BFGS-B CODE', /, /,                          &
      & 'it    = iteration number', /,                                    &
      & 'Machine precision =', 1p, d10.3)

2001             FORMAT('RUNNING THE L-BFGS-B CODE', /, /,                          &
      & 'it    = iteration number', /,                                    &
      & 'Machine precision =', 1p, d10.3)

!&<
2001 FORMAT('RUNNING THE L-BFGS-B CODE', /, /,                          &
         & 'it    = iteration number', /,                                    &
         & 'Machine precision =', 1p, d10.3)
!&>
   END SUBROUTINE prn1lb
END MODULE
dev-zero commented 3 years ago

the problem are the label numbers: removing them makes fprettify behave consistently again, and the number of spaces added for indentation depend on the number of spaces between the label number and FORMAT.

dev-zero commented 3 years ago

ok, that's what I got so far:

After the first pass of reformat_ffile_combined we get for the labeled lines:

200 FORMAT('RUNNING THE L-BFGS-B CODE', /, /,                          &

in the second pass the difference of spaces inserted between the label and the format also get added to the line continuations, while the present spaces are not stripped since we are in the auto_align = False branch.

From what I see there are multiple solutions:

pseewald commented 3 years ago

Thanks for investigating this. The problem occurs if the label numbers are wider than the logical indentation width (which causes an offset in the continuation lines). Should be fixed soon.

pseewald commented 3 years ago

Hm there are still cases in CP2K triggering this or a similar bug, having a look again...