fortran-lang / fprettify

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

inconsistent indentation with #:else fypp directives #89

Closed dev-zero closed 3 years ago

dev-zero commented 3 years ago
#:if a
program main
#:else
module main
#:endif

gets prettified to

#:if a
program main
#:else
   module main
#:endif

likewise

#:if worktype
      ${worktype}$, &
#:else
      ${type}$, &
#:endif
         DIMENSION(${arr_exp}$), &
         POINTER :: work

becomes

#:if worktype
      ${worktype}$, &
         #:else
         ${type}$, &
         #:endif
         DIMENSION(${arr_exp}$), &
         POINTER :: work
pseewald commented 3 years ago

first example: fprettify assumes that preprocessors do not change the logic of a program, otherwise it would need to parse preprocessor languages which I'd like to avoid. You can workaround this by defining a fypp macro (in an external file included via #:include) that returns program or module.

second example: I can not reproduce this with the newest version of fprettify, can you confirm that it works?

dev-zero commented 3 years ago

first example: fprettify assumes that preprocessors do not change the logic of a program, otherwise it would need to parse preprocessor languages which I'd like to avoid. You can workaround this by defining a fypp macro (in an external file included via #:include) that returns program or module.

Hmm, I don't think I can follow. In my understanding the only way to prettify Fortran-preprocessor-interleaved code is by tracking the indentation level for each of them separately, no?

second example: I can not reproduce this with the newest version of fprettify, can you confirm that it works?

Yes, it seems CP2K's fprettify needs to be updated. Interestingly though it seems that fprettify now leaves it as it is, rather than formatting it properly. Meaning that

#:if worktype
      ${worktype}$, &
         #:else
         ${type}$, &
         #:endif
         DIMENSION(${arr_exp}$), &
         POINTER :: work

doesn't get changed. You can try it with src/common/memory_utilities.F, which should be reformatted by fprettify (but the CP2K extension should not be applied to it since it contains Fypp directives).

pseewald commented 3 years ago

Hmm, I don't think I can follow. In my understanding the only way to prettify Fortran-preprocessor-interleaved code is by tracking the indentation level for each of them separately, no?

Well preprocessor statements are basically ignored so what you end up with is

program main
module main

So module main is nested into program main. That is unfortunate but can not be easily solved I think. If you have a suggestion, let me know.

Interestingly though it seems that fprettify now leaves it as it is.

Yes, this is the current behaviour: fprettify leaves preprocessor statements as they are (and the weird formatting was caused by a previous version of fprettify).

dev-zero commented 3 years ago

Hmm, I don't think I can follow. In my understanding the only way to prettify Fortran-preprocessor-interleaved code is by tracking the indentation level for each of them separately, no?

Well preprocessor statements are basically ignored so what you end up with is

program main
module main

So module main is nested into program main. That is unfortunate but can not be easily solved I think. If you have a suggestion, let me know.

Now I see, thanks. And yes, for being able to prettify that properly you would have to parse the fypp conditionals, then prettify each blocks separately with the same starting indentation level, backing up and restoring the original indentation level as you encounter start and ends of fypp conditional blocks.

Interestingly though it seems that fprettify now leaves it as it is.

Yes, this is the current behaviour: fprettify leaves preprocessor statements as they are (and the weird formatting was caused by a previous version of fprettify).

Ok, great, I've created https://github.com/cp2k/cp2k/pull/1160 to update fprettify in CP2K, I guess this can be closed then

pseewald commented 3 years ago

Ok, great, I've created cp2k/cp2k#1160 to update fprettify in CP2K, I guess this can be closed then

There are just a few updates to fprettify that I'm going to wrap up today or tomorrow - could you wait for a release?

dev-zero commented 3 years ago

sure :)