fortran-lang / fprettify

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

Problem with --enable-replacements --c-relations #47

Closed meteokid closed 3 years ago

meteokid commented 5 years ago

I have some code that is badly reformatted when I specify --enable-replacements --c-relations

The code snippet:

$ cat test.F90
     if (G_yglob_8(1)  .lt. -near_pole_8 .or. yglob(1)  .lt. -near_pole_8 .or. &
          G_yglob_8(gnj).gt.  near_pole_8 .or. yglob(gnj).gt.  near_pole_8 ) then

Here are the results with and w/o the problematic options.

$ fprettify.py -s test.F90
     if (G_yglob_8(1) .lt. -near_pole_8 .or. yglob(1) .lt. -near_pole_8 .or. &
         G_yglob_8(gnj) .gt. near_pole_8 .or. yglob(gnj) .gt. near_pole_8) then

With the option set I get a False "line too long" warning and a "mangled" source code

$ fprettify.py -s --enable-replacements --c-relations test.F90
WARNING: File test.F90, line 3
    auto indentation failed due to chars limit, line should be split (limit: 132)

     if (G_yglob_8(1) < -near_pole_8 .or. yglob(1) < -near_pole_8 .or. G_yglob_8(gn &
                                                                                j) > near_pole_8 .or. yglob(gnj) > near_pole_8) then

With -l 512 the warning is gone but the source code is still "mangled"

$ fprettify.py -s -l 512 --enable-replacements --c-relations test.F90

     if (G_yglob_8(1) < -near_pole_8 .or. yglob(1) < -near_pole_8 .or. G_yglob_8(gn &
                                                                                 j) > near_pole_8 .or. yglob(gnj) > near_pole_8) then
pseewald commented 5 years ago

the reason for this is that continuation lines are joined into one fortran line for in-line formatting. Since the replacement changes the number of characters of the operator, it should also change the position of the line break that is inserted after all formatting has been applied. This should be easy to fix and I will have a look.

meteokid commented 5 years ago

I committed a patch for this. Pull request https://github.com/pseewald/fprettify/pull/48

pseewald commented 3 years ago

Resolved by #48