fortran-lang / fprettify

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

indentation fails at multiple subroutine definitions in preprocessor directives #156

Open LukasvdWiel opened 1 year ago

LukasvdWiel commented 1 year ago

Let me have unformatted code:

#ifdef foo
subroutine bla(x)
#else
subroutine bla(x,y)
#endif

[do something]

end subroutine

subroutine bar

I would expect [do something] to be indented once, and both subroutine calls to be equally indented:

#ifdef foo
subroutine bla(x)
#else
subroutine bla(x,y)
#endif

    [do something]

end subroutine

subroutine bar

but what happens is that the second subroutine definition of bla is already indented. Worse then that, all code below takes the extra indent level, and subsequent subroutines, such as bar in this example, is indented more than it should.

#ifdef foo
subroutine bla(x)
#else
    subroutine bla(x,y)
#endif

        [do something]

    end subroutine

    subroutine bar

I hope you could find time to fix this.

Have an excellent day!

Lukas

nbehrnd commented 1 year ago

@LukasvdWiel I would like to add a suggestion. It is unrelated to the content, but to the presentation of the content: If you would like to share a snippet of code in (Gruber's) Markdown, you either use four explicit spaces, or a tab of indentation. This is why [do something] above currently is displayed differently.

Alternatively, GitHub's flavour of Markdown additionally allows (optionally annotated) fenced blocks you likely aimed to use. Note however, this approach usually does not use single quotes (your post in the present form), but back ticks (known in French as accent grave) to enclose such a snippet. (Link to a compilation of hints about/around markdown.)

In addition to an easier copy-paste of such a snippet *en bloc**, the annotation can enable syntax highlighting. Optional (mutually exclusive) keywords I know to be supported here on GitHub are python

for i in range(5):
    print(i)

awk

#!/usr/bin/awk -f
BEGIN{
string = "this is an example";

split(string, array)

for (i = 1; i<=length(array); i++){};
print array[i]}

or f90 / fortran

program test
  implicit none
  integer :: i

  do i = 1, 5
    write (*, "(2(I2, 1X))") i, i**2
  end do
end program test

* This copy-paste then equally preserves the indentation (useful if it is functionally relevant, e.g. for Python).

clip

LukasvdWiel commented 1 year ago

@nbehrnd Thank you very much, I have applied your wisdom to my original message... It made the issue a lot prettier.

nbehrnd commented 1 year ago

@LukasvdWiel A revisit of your post, which is conceptual. That is to say, share of a specific minimal working example could illustrate, hence substantiate your point. If your observation could be replicated with source code in a single file, one option is to add an additional .txt extension; and while running an instance of GitHub in a browser, to attach it below one of the frames edited by you (an example below).

Else, if you already split your program into a main.f90 and a separate e.g. my_module.f90 about procedures or/and a minimal set of raw data, deposit within a .zip archive (because .zip equally is one of the file extensions permitted here) equally is at your hands.

Briefly (because of the leading number signs) I thought your examples could be an example of an accidental code-switching to Python. It is a term in natural languages (e.g. Franglais in lieu of French (Français), or English (Anglais)) when elements of one language enter an other without representing e.g. a loanword. This impression however can be an error on my side, too.

example.f90.txt example.f90.txt.zip