Menu

#378 Wrong Formatting with Anonymous Thread in "Code Formatter""

Closed
closed-fixed
formatter (28)
5
2026-01-17
2024-05-22
No

See this example below.

No configuration can solve this problem

The problem is that the Procedure "P_LoopActive" has no margins and is glued to the left

Procedure TForm_MenuPrincipal.P_ActiveUDPServer(Op: Integer);
Var PortaUDP: Integer;
Var fThread_UDPServer: TThread;
Begin

   fThread_UDPServer := TThread.CreateAnonymousThread(Procedure
Procedure P_LoopAtive;
Var I: Integer;
Begin
 PortaUDP := vPorta_Refresh;

 For I := 1 To Count_SessionWindows Do Begin
  Try
   Server_Refresh.Active := False;
   Server_Refresh.DefaultPort := PortaUDP;
   Server_Refresh.BroadcastEnabled := True;
   Server_Refresh.ThreadedEvent := False;
   Server_Refresh.Active := True;
   break;
  Except
   On E: Exception Do Begin             // Não dar erro aleatorios no Terminal Server
    Inc(PortaUDP);

    FreeAndNil(Server_Refresh);

    Server_Refresh := TIdUDPServer.Create(self);
    Server_Refresh.OnUDPRead := Server_RefreshUDPRead;

    P_LogException(E);
   End;
  End;
 End;
End;

   Begin
    If Op = 0 Then
     P_LoopAtive;

    If Op = 1 Then Begin
     Try
      Server_Refresh.Active := True;
     Except
      P_LoopAtive;
     End;
    End;
   End);
  fThread_UDPServer.FreeOnTerminate := True;
  fThread_UDPServer.Start;

End;

Discussion

  • Thomas Mueller

    Thomas Mueller - 2025-03-30

    I see where the problem is: You have got an anonymous procedure with a nested procedure inside. The formatter does not support that yet. It does support anonymous procedures without that.
    I wasn't even aware that the Compiler supports this kind of construct.

    A simplified example would be:

    Procedure Bla;
    Begin
       fThread_UDPServer := TThread.CreateAnonymousThread(procedure
         Procedure NestedProcedureInsideAnonymousProcedure;
         Begin
           CodeOfNestedProcedure;
         End;
    
       Begin
         CodeOfTheAnonymousProcedure;
       End);
      CodeOfProcedureBla;
    End;
    

    which gets formatted as:

    procedure Bla;
    begin
      fThread_UDPServer := TThread.CreateAnonymousThread(procedure
    
    procedure NestedProcedureInsideAnonymousProcedure;
    begin
      CodeOfNestedProcedure;
    end;
    
        begin
          CodeOfTheAnonymousProcedure;
        end);
      CodeOfProcedureBla;
    end;
    

    but should be formatted as:

    procedure Bla;
    begin
      fThread_UDPServer := TThread.CreateAnonymousThread(procedure
    
          procedure NestedProcedureInsideAnonymousProcedure;
          begin
            CodeOfNestedProcedure;
          end;
    
        begin
          CodeOfTheAnonymousProcedure;
        end);
      CodeOfProcedureBla;
    end;
    
     

    Last edit: Thomas Mueller 2025-03-30
  • Thomas Mueller

    Thomas Mueller - 2025-04-06

    Added test cases for formatter unit tests in revision #4502 (still no fix, sorry. This is really complex.)

     
  • Thomas Mueller

    Thomas Mueller - 2026-01-17
    • status: open --> closed-fixed
    • assigned_to: Thomas Mueller
    • Group: New --> Closed
     
  • Thomas Mueller

    Thomas Mueller - 2026-01-17

    Fixed in revision #4965 (Actually fixed by Claude Code without breaking any of the other tests.)

     

Log in to post a comment.

MongoDB Logo MongoDB