Fully automatic indentation

From Free Pascal wiki
Jump to navigationJump to search

Overview

The source editor should auto indent on pressing return/enter, on paste and when inserting auto generated code. This depends on language and user preferences. Most other editors use either a fixed set of rules or a set of options to configure the rules. This is semi automatic indentation. These options are either too simple or too complex. And they only allow one set of rules, so editing sources with different policies is difficult.

Fully automatic indentation tries to guess the rules from the surrounding code.

Examples

Amount of indent

Let's keep it simple and assume one indent size. Open questions are:

  • How much indent
  • Use tabs
  • long lines
  • Special cases

Examples

Sometimes record and classes are aligned to the keywords instead of start of last line.

<Delphi> type TMyRecord = record

                i: integer;
                end;

</Delphi>

begin end

<Delphi> procedure Do; var

 i: integer; // indent after var

begin // no indent after procedure, no indent before procedure-begin, unindent after var section

 if expr then
 begin       // no indent after then, no indent before then-begin
   Code;     // indent after begin
 end;        // unindent before end

end; </Delphi>

<Delphi> procedure Do; begin

 if expr then begin  
   Code;             // indent after begin
 end;                // unindent before end

end; </Delphi>

<Delphi> procedure Do; begin

 if expr then
   begin   // indent after then and unindent after then-statement
   Code;   // no indent after begin
   end;    // no unindent before end

end; </Delphi>

<Delphi> procedure Do; begin

 if expr then
   begin     // indent after then and unindent after then-statement
     Code;   // indent after begin
   end;      // unindent before end

end; </Delphi>

repeat until

I never saw code other than this:

<Delphi> repeat

 Code; // indent after repeat

until ; // unindent before until </Delphi>

try finally

I never saw code other than this:

<Delphi> try

 Code;     // indent after try

finally // unindent before finally

 on e do ; // indent after finally

end; // unindent before finally-end </Delphi>