fortran-lang / fprettify

auto-formatter for modern fortran source code

Home Page:https://pypi.python.org/pypi/fprettify

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can comments starting in column 1 be indented?

Pmoloney3415 opened this issue · comments

Is it possible to add an option to do this within the programme? Thanks for the great project!

This would be a really useful feature for a project I'm working on!

For the time till then, you might search and replace the occurrences with e.g., AWK.

  • for comments starting in column 1:

    $ awk '{ if ($0 ~ /^!/) print "   "$0; else print $0}' my_file.f90
  • for comment lines starting with some white space then followed by a comment

    $ awk '{ if ($0 ~ /^\s!/) print "   "$0; else print $0}' my_file.f90
  • to apply both conditions simultaneously, either use awk's logical or

    $ awk '{if (($0 ~ /^!/) || ($0 ~ /\s!/)) print "   "$0; else print $0}' my_file.f90

    or a regex clause for zero, or any number of white spaces

    $ awk '{ if ($0 ~ /^\s*!/) print "   "$0; else print $0}' my_file.f90

It works fine in AWK as implemented as GNU Awk 5.2.1. Or put it in a script e.g.,

#!/usr/bin/gawk -f

# name   : indent.awk
# purpose: indent comment lines starting on column 1 by three additional leading spaces

# either run the script by
#
# ```
# $ awk -f ./indent.awk my_code.f90
# $ ./indent.awk my_code.f90  # after provision of the executable bit to the script
# ```

{ if ($0 ~ /^!/) print "   "$0; else print $0}