fprettify icon indicating copy to clipboard operation
fprettify copied to clipboard

Precompiler directives break indentation

Open flokno opened this issue 3 years ago • 1 comments
trafficstars

Hi,

precompiler directives such as #ifdef can break indentation, see for example this piece of code after reformatting with fprettify:

    module pure function lo_frobnorm(m) result(nrm)
        real(flyt), dimension(:, :), intent(in) :: m
        real(flyt) :: nrm
    end function
#ifdef AGRESSIVE_SANITY
    module function lo_trace(m) result(tr)
#else
        module pure function lo_trace(m) result(tr)
#endif
            real(flyt), dimension(:, :), intent(in) :: m
            real(flyt) :: tr
        end function
        module pure function lo_sqnorm(a) result(nrm)
            real(flyt), dimension(3), intent(in) :: a
            real(flyt) :: nrm
        end function

After the #ifdef block, everything is shifted by 1 level.

I'm on fprettify v0.3.7

flokno avatar Sep 16 '22 13:09 flokno

Comment: Maybe it's not the preprocessor per se, since apparently there are some checks: https://github.com/pseewald/fprettify/blob/master/fprettify/fparse_utils.py#L35-L46

flokno avatar Sep 16 '22 13:09 flokno

I don't think its related to the directives directly. it is just that when you find a subroutine/module opening you indent afterwards.

we would need to be able to avoid indentation if a new 'region' is open with the same type

GSS79 avatar Jan 24 '23 08:01 GSS79

I don't think its related to the directives directly. it is just that when you find a subroutine/module opening you indent afterwards.

I confirm.

we would need to be able to avoid indentation if a new 'region' is open with the same type

I don't think this is general enough, as some of the 'regions' can be nested (e.g. Do, If). I don't see any other way to support this than to parse preprocessor statements, which is not worth it just to get these corner cases right, IMO.

@flokno If you really want to use the preprocessor in this way, and still want you code to be correctly formatted by fprettify, I suggest the following workaround:

#ifdef AGRESSIVE_SANITY
    module function lo_trace(m) result(tr)
#else
        module pure function lo_trace(m) result(tr)
#endif
            real(flyt), dimension(:, :), intent(in) :: m
            real(flyt) :: tr
#ifdef AGRESSIVE_SANITY
        end function
#else
    end function
#endif

pseewald avatar Jan 24 '23 21:01 pseewald

ok thanks @pseewald for the workaround

flokno avatar Jan 26 '23 09:01 flokno