Difference between revisions of "Fully automatic indentation"

From Free Pascal wiki
Jump to navigationJump to search
Line 1: Line 1:
 
=Overview=
 
=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.
+
Since 0.9.29 the lazarus source editor has a new algorithm for automatic indentation on pressing Enter or on pasting code from clipboard. This new algorithm works only for pascal and works different to many other editors you may know. 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 the other editors 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. It does that by searching for similar code and copying the indent.
+
'''Fully automatic indentation''' tries to guess the rules from the surrounding code. It does that by searching for similar code and copying the indent.
  
You can define
+
Because it works fully automatic there are only a few options. You can disable it, you can setup the search scope and you can provide an example code: [[IDE_Window:_Codetools_Options#Indentation|Codetools Options: Indentation]]. You do not need to learn rules, just write pascal.
 
 
[[IDE_Window:_Codetools_Options#Indentation|Codetools Options: Indentation]]
 
  
 
=Examples=
 
=Examples=

Revision as of 14:10, 14 November 2009

Overview

Since 0.9.29 the lazarus source editor has a new algorithm for automatic indentation on pressing Enter or on pasting code from clipboard. This new algorithm works only for pascal and works different to many other editors you may know. 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 the other editors 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. It does that by searching for similar code and copying the indent.

Because it works fully automatic there are only a few options. You can disable it, you can setup the search scope and you can provide an example code: Codetools Options: Indentation. You do not need to learn rules, just write pascal.

Examples

Amount of indent

Let's keep it simple and assume one tab 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>

case of end

<Delphi> case expr of 1: ; // no indent after case-of 2:

 Code;    // indent after case-colon

const:

 begin    // indent after case-colon
   Code;  // indent after case-colon-begin
 end;     // unindent before case-colon-end, unindent after case-colon-end

else // no indent case-else

 Code;    // indent after case-else

end; // unindent after case-else statements </Delphi>

<Delphi> case expr of

          // empty lines => not supported
 1: ; // indent after case-of
 2: 
 begin    // no indent after case-colon
 end;

else // no indent before case-else end; // no indent before case-end </Delphi>

if then else

<Delphi> if expr then

 Code

else

 Code;

</delphi>

<Delphi> if expr or

 expr or
 expr then
 Code

else

 Code;

</delphi>

<Delphi> if expr

 or expr

then

 Code

else

 Code;

</delphi>

<Delphi> if SrcEdit.SelectionAvailable and SrcEdit.CaretInSelection(CaretPos) then Expression := SrcEdit.GetText(True) else Expression := Identifier; if not DebugBoss.Evaluate(Expression, DebugEval) or (DebugEval = ) then DebugEval := '???'; </delphi>