ptop doesn't format "else if" well lazarus / 62-General ---------------------------------------------------------------------- anokim · 2025-01-02 08:47 I'm using ptop to autoformat my code but it behaves strangely. Let's take this ptop-formatted code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   n := 2;  if n < 0 then    WriteLn('negative')  else if n > 0 then         WriteLn('positive')  else    WriteLn('zero');  Line 5 is indented too much. The problem is that it is aligned to "if", similarly to line 3, but it should be aligned to "else if". For ptop, you can generate its config file with "ptop -g local.cfg". Then, you can tweak it and use the tweaked version: "ptop -c local.cfg in.pas out.pas". However, I couldn't figure out how to tweak it. ---------------------------------------------------------------------- anokim · 2025-01-02 11:27 I came up with a script that you can run after ptop: Code: Python  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i #!/usr/bin/env python3 import sys INDENT = 2  def fix_elseif_indentation(input_lines):    fixed_lines = []    i = 0    while i < len(input_lines):        line = input_lines[i]        fixed_lines.append(line)         # Look for "else if" pattern        stripped = line.lower().lstrip()        if stripped.startswith("else if"):            indents = len(line) - len(stripped)            good_indents = indents + INDENT            first_line_inside = True          while True:                i += 1                next_line = input_lines[i]                stripped_next_line = next_line.lstrip()                indents_next_line = len(next_line) - len(stripped_next_line)                if indents_next_line == indents:                    fixed_lines.append(next_line)                  break                # else:                if first_line_inside:                    indents_to_remove = indents_next_line - good_indents                #                fixed_lines.append(next_line[indents_to_remove:])                first_line_inside = False            #        #        i += 1    #    return fixed_lines  def main():    if len(sys.argv) != 3:        print("Usage: python script.py input.pas output.pas")        sys.exit(1)     input_file = sys.argv[1]    output_file = sys.argv[2]     try:        # Read input file        with open(input_file, "r") as f:            lines = f.readlines()         # Fix indentation        fixed_lines = fix_elseif_indentation(lines)       # Write output file        with open(output_file, "w") as f:          f.writelines(fixed_lines)     except FileNotFoundError:        print(f"Error: Could not find file {input_file}")        sys.exit(1)  except Exception as e:        print(f"Error: {str(e)}")        sys.exit(1)  if __name__ == "__main__":    main()  It's not battle-tested, just a first draft. Example usage: $ ./post_correction.py main.pas out.pas Content of out.pas: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   n := 2;  If n < 0 Then    WriteLn('negative')  Else If n > 0 Then    WriteLn('positive')  Else    WriteLn('zero');  Also works if you have a begin-end block under else-if. ---------------------------------------------------------------------- Zvoni · 2025-01-02 12:10 https://www.freepascal.org/tools/ptop.html At a guess (see example of manpage): Code: [Select]else=crbefore,dindonkey,inbytab [else]=if,then,else You need the "dindonkey"-option with the follow-up line "[else]" EDIT: Nope. i guessed wrong. Just played around with it. Looks like some precedence-collision ---------------------------------------------------------------------- rgranger · 2026-01-24 21:36 Ahh, I bet the same is going on with the on...do in exceptions. I also looked at the pascal-format build and it chokes on the compiler directives, so I guess ptop is it. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       If Resp = mrYes Then        Begin          Try            If Not ForceDirectories(MountDir) Then              Begin                Log(Format('Failed to create mount point "%s".', [MountDir]));              PlayFailureSound;                Result := False;              Exit;              End;          Except            on ECreate: Exception Do                        Begin                          Log(Format('Error creating mount point "%s": %s',                            [MountDir, ECreate.Message]));                          PlayFailureSound;                          Result := False;                        Exit;                        End;        End;