Menu

#367 Missing indent for multiline comments

Closed
closed-fixed
None
5
2026-02-26
2024-02-20
pustekuchen
No

Option: Indend Comments active

  TSampleClass = class(TObject)
    procedure functionx(); { this is multiline comment
    ---- ----- ---- --------------------------------- -- -------
    ----------
    ----
    }
    procedure functiony();                   //test
  end;

Expected

  TSampleClass = class(TObject)
    procedure functionx(); { this is multiline comment
      ---- ----- ---- --------------------------------- -- -------
      ----------
      ----
      }
    procedure functiony();                   //test
  end;

Discussion

  • Thomas Mueller

    Thomas Mueller - 2024-02-25
    • assigned_to: Thomas Mueller
     
  • Thomas Mueller

    Thomas Mueller - 2024-02-25

    That's a really odd one. I don't use this option myself, but I just played a bit with it in the Code Formatter Config window. It definitely doesn't work as expected, e.g.

    type
      TClass = class(TObject)
      private
            { this is a comment
    -- with
      -------- multiple
      --------lines
      --
       }
      end;
    

    gets formatted as

    type
      TClass = class(TObject)
      private
        { this is a comment
    -- with
    -------- multiple
    --------lines
    --
    }
      end;
    

    And the more spaces I put in front of the first line, the less the following lines get indented. I'm not quite sure how it actually should work.

     

    Last edit: Thomas Mueller 2024-02-25
    • Thomas Mueller

      Thomas Mueller - 2024-02-25

      Actually, it might be working as designed:
      The formatter determines the indent of the 2nd and later lines relatively to the first comment line. So given this input:

      type
        TClass = class(TObject)
        private
              { this is a comment <-- Indent is 8
      -- with  // indent is 0, so the relative indent is -8
        -------- multiple // indent is 2, so the relative indent is -6
        --------lines // indent is 2, so the relative indent is -6
        -- // indent is 2, so the relative indent is -6
         }  // indent is 3, so the relative indent is -5
        end;
      

      Since the first line gets indented

      type
        TClass = class(TObject)
        private
          { this is a comment // new indent is 4
      -- with // new indent is 4-8 -> 0
      -------- multiple // new indent is 4-6 -> 0
      --------lines // new indent is 4-6 -> 0
      -- // new indent is 4-6 -> 0
      } // new indent is 4-5 -> 0
        end;
      

      So it works as designed for this, trying to keep the relative indentation inside the comment.

       
  • Thomas Mueller

    Thomas Mueller - 2024-02-25

    Different example:

    type
      TClass = class(TObject)
      private
     { this is a comment // indent is 1
              -- with // indent is 10, relative indent is 9
        -------- multiple // indent is 4, relative indent is 3
         --------lines // indent is 5, relative indent is 4
                 } // indent is 13, relative indent is 12
      end;
    

    Output:

    type
      TClass = class(TObject)
      private
        { this is a comment // new indent is 4
                 -- with // new indent is 4 + 9 = 13
           -------- multiple // new indent is 4 + 3 = 7
            --------lines // new indent is 4 + 4 = 8
                    } // new indent is 4 + 12 = 16
      end;
    

    Again, this looks just like it should

     
  • pustekuchen

    pustekuchen - 2024-02-25

    Would it be possible to create an option that ignores the relative indent for comments and puts everything on one level?

     
  • Thomas Mueller

    Thomas Mueller - 2024-02-25

    Example where the comment starts after a procedure like in the original post:

    type
      TClass = class(TObject)
      private
    procedure bla; { this is a comment // indent is 0 (the indent of the line!)
           -- with // indent is 7, relative indent is 7
     -------- multiple // indent is 1, relative indent is 1
      --------lines// indent is 2, relative indent is 2
              } // indent is 10, relative indent is 10
      end;
    

    Output:

    type
      TClass = class(TObject)
      private
        procedure bla; { this is a comment // new indent is 2 (the indent of the line!)
        -- with // new indent is 4 but should possibly be 2 + 7 = 9
    -------- multiple // new ident is 0 but should possybly be 2+1 = 3
    --------lines // new ident is 0 but should possibly be 2 + 2 = 4
           }
      end;
    

    This looks wrong. I'm not quite sure what to make of this. Maybe I'm interpreting it wrongly. (Remember: I didn't write this code, it was donated to GExperts from the original author Egbert van Nes, so I have to guess what he intended.)

     
    • Thomas Mueller

      Thomas Mueller - 2024-02-25

      Playing with it a bit further, it turns out that the comments are always indented relativ to the first full comment line, so:

      type
        TClass = class(TObject)
        private
      procedure bla; { this is a comment // indent des not matter
             -- with // first full comment line, indent is 7
       -------- multiple // indent is 1, relative indent is -6
        --------lines// indent is 2, relative indent is -5
                } // indent is 10, relative indent is 3
        end;
      

      Output:

      type
        TClass = class(TObject)
        private
          procedure bla; { this is a comment // indent des not matter
          -- with // new indent is 4
      -------- multiple // new ident is 0 because 4-6 < 0
      --------lines // new ident is 0 because 4-5 < 0
             } // new indent is 7 = 4 + 3
        end;
      

      another example, where it works:

      type
        TSampleClass = class(TObject)
          procedure functionx(); { this is multiline comment
         ---- ----- ---- --------------------------------- -- ------- //indent is 3
            ---------- // indent is 6, relative indent is 3
              ---- // indent is 8, relative indent is 5
          } // indent is 4, relative indent is 1
          procedure functiony();                   //test
        end;
      

      Output

      type
        TSampleClass = class(TObject)
          procedure functionx(); { this is multiline comment
          ---- ----- ---- --------------------------------- -- ------- // indent is 4
             ---------- // indent is 7 = 4 + 3
               ---- // indent is 9 = 4 + 5
           } // indent is 5 = 4 + 1
          procedure functiony(); //test
        end;
      

      So this seems to be how it is intended to work.

       
      • pustekuchen

        pustekuchen - 2024-02-25

        For

        type
          TClass = class(TObject)
          private
        procedure bla; { this is a comment // indent des not matter
               -- with // first full comment line, indent is 7
         -------- multiple // indent is 1, relative indent is -6
          --------lines// indent is 2, relative indent is -5
                  } // indent is 10, relative indent is 3
          end;
        

        i would expect.

        type
          TClass = class(TObject)
          private
            procedure bla; { this is a comment 
              -- with
              -------- multiple 
              --------lines
              } 
          end;
        
         

        Last edit: pustekuchen 2024-02-25
  • Thomas Mueller

    Thomas Mueller - 2024-02-25

    Please give some more examples on how you expect this to work. I'm not sure I understand your request. What is the purpose of the lines in your original report? Are they just place holders for some text? Why do you think the formatting is wrong?

     
    • pustekuchen

      pustekuchen - 2024-02-25

      The main problem in my eyes is that if you have a multiline comment that is not indented, it is visually more difficult to distinguish from the next line. In this case the line with "procedure functiony();".
      Each comment line (1+) of the multiline comment should start at the same indentation level.

       

      Last edit: pustekuchen 2024-02-25
  • Thomas Mueller

    Thomas Mueller - 2026-02-26
    • status: open --> closed-fixed
    • Group: New --> Closed
     
  • Thomas Mueller

    Thomas Mueller - 2026-02-26

    fixed in revision #5175

     

Log in to post a comment.

Auth0 Logo