Changeset 6023


Ignore:
Timestamp:
Jan 13, 2026, 2:43:17 PM (4 days ago)
Author:
evignon
Message:

update du script format_code pour avoir des codes lmdz bien indentes
et avec un format "propre"

File:
1 edited

Legend:

Unmodified
Added
Removed
  • LMDZ6/trunk/tools/format_code.sh

    r6010 r6023  
    44# a common and unified format
    55# it works in 1 or 2 steps:
    6 # 1/an optional preprocessing step with findent
     6# 1/an optional preprocessing step with findent (if_findent)
    77# (more robust for code structuration)
    88# 2/a formatting step with fprettify using a .fprettify format file
     
    1010# if findent or fprettify displays an error or warning message,
    1111# the file is not processed
     12# how to use:
     13# ./format_code.sh "file1 file2 file3"
    1214# v1: November 2025
    1315#-----------------------------------------------------------------
     
    1618# List of files to process
    1719#------------------------------------------------------------------
    18 files_to_process=( $(find ../libf/*/* -maxdepth 0 -type f ! -xtype l \( -name "*.f90" -o -name "*.F90" \)) )
     20#files_to_process=( $(find ../libf/*/*blowing* -maxdepth 0 -type f ! -xtype l \( -name "*.f90" -o -name "*.F90" \)) )
     21
     22files_to_process="$1"
    1923
    2024# Check installation of findent and fprettify
     
    4549    echo "the file $FILENAME already exists"
    4650else
     51echo "I create the file $FILENAME "
     52
    4753# create the file
    4854cat << 'EOF' > "$FILENAME"
    49 # --- Indentation ---
    50 indent = 4                  # 4-space indentation (standard practice)
    51 indent_cont = 6             # continuation lines more indented for clarity
    52 tabindent = false           # always use spaces, never tabs
     55# =========================
     56# fprettify configuration
     57# =========================
    5358
    54 # --- Line formatting ---
    55 max_line_length = 200       # good default for modern screens
    56 whitespace = true           # trim trailing whitespace
    57 whitespace_statements = true
    58 whitespace_operators = true # enforce spaces around operators
    59 compact = false             # do not compact everything (more readable)
     59# Indentation (number of spaces)
     60indent = 3
    6061
    61 # --- Keywords ---
    62 case_keywords = lower       # lowercase keywords (if, do, end, module)
    63 case_intrinsics = lower     # lowercase intrinsics (sin, cos, abs)
    64 case_types = preserve       # preserve user-defined type casing
     62# Maximum line length
     63line-length = 200
    6564
    66 # --- Logical formatting ---
    67 split_multiline_statements = true     # break long statements
    68 align_assignments = true              # align "=" signs vertically
    69 align_continuation = true
     65# Indent internal blocks (do, if, select case…)
     66indent-blocks = true
    7067
    71 # --- Operators ---
    72 space_in_percents = false   # Fortran % operator (component), no spaces
    73 space_in_power = false      # no spaces around exponent (**)
    74 space_in_colons = false     # array slices remain compact (i:j)
    75 space_in_parentheses = false
     68# Add whitespace around operators (=, +, -, *, /, etc.)
     69whitespace = true
    7670
    77 # --- Comments ---
    78 keep_comments = true
    79 align_comments = true       # align trailing comments
    80 comment_indent = 2
     71# Keyword normalization (lowercase or uppercase)
     72case = [2,2,2,2]
    8173
    82 # --- Preprocessing ---
    83 cpp_keywords = true         # format #ifdef / #define blocks
    84 preserve_cpp_indentation = true
     74# Reformat declarations (type :: variable)
     75reformat-declarations = true
    8576
    86 # --- Safety ---
    87 strict_indent = true         # fail on inconsistent indentation
    88 ignore_old_style = false     # do not preserve ugly fixed-format legacy stuff
     77# Align variable declarations
     78align-declarations = true
    8979
     80# Align consecutive assignments
     81align-assignments = true
     82
     83# Align arguments in multi-line calls
     84align-arguments = true
     85
     86# Preserve comments as-is
     87preserve-comments = true
     88
     89# Do not modify string literals
     90preserve-strings = true
     91
     92# Reformat line continuations (&)
     93reformat-continuations = true
     94
     95# Reformat USE statements
     96reformat-use-statements = true
     97
     98# Allow inline formatting control
     99# (via ! fprettify: off / on)
     100enable-inline-control = true
    90101EOF
    91102fi
     
    104115    tmp=$(mktemp) || return 1
    105116
    106     local patterns="warning|error|failed|deprecated"
     117    local patterns="error"
    107118    local err
    108119    local ret
     
    110121    cp "$f" "$tmp"
    111122
    112 #    # Step 1: findent
    113 #    rm "$tmp"
    114 #    err=$(findent < "$f" 2>&1)
    115 #    ret=$?
    116 #    if [ $ret -ne 0 ] || echo "$err" | grep -Eqi "$patterns"; then
    117 #        echo "findent detected WARNING/ERROR in $f"
    118 #        rm -f "$tmp"
    119 #        return 1
    120 #    fi
    121 #    findent < "$f" > "$tmp" 2>/dev/null
     123# Step 1: findent
     124    if_findent=0
     125    if [ "$if_findent" -eq 1 ]; then
     126       rm "$tmp"
     127       err=$(findent < "$f" 2>&1)
     128       ret=$?
     129       if [ $ret -ne 0 ] || echo "$err" | grep -Eqi "$patterns"; then
     130          echo "findent detected ERROR in $f"
     131          echo $err
     132          #rm -f "$tmp"
     133          return 1
     134       fi
     135       findent < "$f" > "$tmp" 2>/dev/null
     136    fi
    122137
    123 
    124     # Step 2: fprettify
    125     err=$(fprettify "$tmp" 2>&1)
     138# Step 2: fprettify
     139    err=$(fprettify  "$tmp" 2>&1)
    126140    ret=$?
    127141    if [ $ret -ne 0 ] || echo "$err" | grep -Eqi "$patterns"; then
    128         echo "fprettify detected WARNING/ERROR in $f"
    129         rm -f "$tmp"
     142        echo "fprettify detected ERROR in $f"
     143        echo "$err"   
     144        #rm -f "$tmp"
    130145        return 1
    131146    fi
Note: See TracChangeset for help on using the changeset viewer.