fortran-lang / fprettify

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

Allow only single indentation width for function/subroutine calls that span multiple lines #159

Open aerosayan opened 11 months ago

aerosayan commented 11 months ago

Hi, I recently started using fprettify and I'm liking it very much.

Feature Request:

Currently, fprettify indents function/subroutine arguments to the opening of the bracket ... like this.

    call ImportSu2Mesh("onera-m6.su2", nodes, cells, &
                       numNodes, numCells)

Issue: Aesthetically, I like it, but in practice, this creates lot of git diff noise when we change the function name during development. Since Fortran function calls can be long, and span multiple lines, this shifts and changes multiple lines at once.

I would like it to be like this by indenting by 4 spaces ...

    call ImportSu2Mesh("onera-m6.su2", nodes, cells, &
         numNodes, numCells)

or like this by indenting by 8 spaces ...

    call ImportSu2Mesh("onera-m6.su2", nodes, cells, &
             numNodes, numCells)

The indentation width should be customizable by the user.

clang-format supports this for C/C++ with ...

  1. AlignAfterOpenBracket: https://clang.llvm.org/docs/ClangFormatStyleOptions.html#alignafteropenbracket
  2. ContinuationIndentWidth: https://clang.llvm.org/docs/ClangFormatStyleOptions.html#continuationindentwidth

Essentially, if a line is continued to the next line, it is indented by the values of ContinuationIndentWidth. We can set it to 2space, 4 spaces, or 8 spaces. It's customized by the user for their preference.

Some line continuation indentation examples clang shows are ...


// Example 1
someLongFunction(argument1,
    argument2);

// Example 2
int i =         //  VeryVeryVeryVeryVeryLongComment
    longFunction( // Again a long comment
    arg);

Do note that when a subroutine or one or more functions are called at once, then the indention rule would apply based on the last line, and context i.e if last line starts at column n, the next line would start at column n+indentationWidth.

call ImportSu2Mesh("onera-m6.su2", nodes, cells, &
    numNodes, numCells, numDeadCells, numBoundaryCells, &
    FunctionToCalculateRandomThing(a, b, c, d,  &  ! <<< New function called
        e, f, g, h, i , j, k, l) &  ! <<< Line is indented right
    backToOriginalIndentation, endOfSubroutineArguments) ! <<< Line is indented left

Since a new function is continued to the next line, the next line is further indented to the right, and when the function argument list is over, the line is indented left.

Would it not be possible to develop a feature like this?

Currently fprettify does shift everything to the right, in the feature I'm proposing, the shift would just be constant value of 4 spaces or 8 spaces, or as customized by the user.

Thanks

aerosayan commented 11 months ago

Also, since modern Fortran has a limit of 132 characters per line, and has a bad history of having very short and famously cryptic names like gemm, axpy, etc ...

Fortran formatters indenting the code to the right so much, is effectively punishing developers for writing long descriptive names.

I know many users like the default option, but to me, same indentation width being used everywhere seems more consistent, and useful.

I will also try to learn more about your code so I can hopefully develop features I need quickly.

Thanks

aerosayan commented 11 months ago

Upon more careful review, I found a way to make it work somehow ...

    call ImportAsciiStlMesh( & ! <<< Nice workaround!
      "f35.stl", &
      mesh%cells, mesh%nodes, mesh%numCells, &
      echo)

As shown above, if we use an & to break the line before passing the first argument to the subroutine/function, then fprettify indents everything only by a single indentation level.

This will work for now.

Thanks