fprettify icon indicating copy to clipboard operation
fprettify copied to clipboard

Can comments starting in column 1 be indented?

Open Pmoloney3415 opened this issue 2 years ago • 2 comments
trafficstars

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

Pmoloney3415 avatar Feb 10 '23 17:02 Pmoloney3415

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

gmmfarrow avatar Mar 20 '24 11:03 gmmfarrow

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}

nbehrnd avatar Mar 21 '24 08:03 nbehrnd