Difference between revisions of "IDE Window: Extract Procedure"

From Lazarus wiki
m (Text replace - "pascal>" to "syntaxhighlight>")
Line 4: Line 4:
  
 
Basic example:
 
Basic example:
<pascal>
+
<syntaxhighlight>
 
  procedure DoSomething;
 
  procedure DoSomething;
 
  begin
 
  begin
 
   CallSomething;
 
   CallSomething;
 
  end;
 
  end;
</pascal>
+
</syntaxhighlight>
 
Select the line "CallSomething;" and do Extract Proc. A dialog pop ups and
 
Select the line "CallSomething;" and do Extract Proc. A dialog pop ups and
 
you can select the type and name of the procedure to create. For example:
 
you can select the type and name of the procedure to create. For example:
 
procedure, "NewProc". Result:
 
procedure, "NewProc". Result:
  
<pascal>
+
<syntaxhighlight>
 
  procedure NewProc;
 
  procedure NewProc;
 
  begin
 
  begin
Line 24: Line 24:
 
   NewProc;
 
   NewProc;
 
  end;
 
  end;
</pascal>
+
</syntaxhighlight>
  
 
You can see, that the new procedure "NewProc" was created with the selection
 
You can see, that the new procedure "NewProc" was created with the selection
Line 33: Line 33:
 
parameter list and local variables. Example:
 
parameter list and local variables. Example:
  
<pascal>
+
<syntaxhighlight>
 
  procedure TForm1.DoSomething(var Erni, Bert: integer);
 
  procedure TForm1.DoSomething(var Erni, Bert: integer);
 
  var
 
  var
Line 43: Line 43:
 
   end;
 
   end;
 
  end;
 
  end;
</pascal>
+
</syntaxhighlight>
  
 
Select the for loop and create a new Procedure "NewProc". The local variable i is only used in the selection, so it will be moved to the new procedure. Erni is also used in the remaining code, so it will become a parameter.
 
Select the for loop and create a new Procedure "NewProc". The local variable i is only used in the selection, so it will be moved to the new procedure. Erni is also used in the remaining code, so it will become a parameter.
Line 49: Line 49:
 
Result:
 
Result:
  
<pascal>
+
<syntaxhighlight>
 
  procedure NewProc(const Erni: integer);
 
  procedure NewProc(const Erni: integer);
 
  var
 
  var
Line 64: Line 64:
 
   NewProc(Erni);
 
   NewProc(Erni);
 
  end;
 
  end;
</pascal>
+
</syntaxhighlight>
  
 
You can see "i" was moved to the new procedure (Note: including its comment) and Erni.
 
You can see "i" was moved to the new procedure (Note: including its comment) and Erni.

Revision as of 00:50, 25 March 2012

Deutsch (de) English (en) español (es) suomi (fi) français (fr) русский (ru) slovenčina (sk) 中文(中国大陆)‎ (zh_CN)

Abstract: "Extract Procedure" takes some selected pascal statements and creates a new procedure/method from this code. This tool is useful to split big procedures or to easily create a new procedure from some code.

Basic example:

 procedure DoSomething;
 begin
   CallSomething;
 end;

Select the line "CallSomething;" and do Extract Proc. A dialog pop ups and you can select the type and name of the procedure to create. For example: procedure, "NewProc". Result:

 procedure NewProc;
 begin
   CallSomething;
 end;
 
 procedure DoSomething;
 begin
   NewProc;
 end;

You can see, that the new procedure "NewProc" was created with the selection as body and the old code was replaced by a call.

Local Variables and Parameters:
"Extract Proc" scans for used variables and automatically creates the parameter list and local variables. Example:

 procedure TForm1.DoSomething(var Erni, Bert: integer);
 var
   i: Integer; // Comment
 begin
   Erni:=Erni+Bert;
   for i:=Erni to 5 do begin
   |
   end;
 end;

Select the for loop and create a new Procedure "NewProc". The local variable i is only used in the selection, so it will be moved to the new procedure. Erni is also used in the remaining code, so it will become a parameter.

Result:

 procedure NewProc(const Erni: integer);
 var
   i: Integer; // Comment
 begin
   for i:=Erni to 5 do begin
   |
   end;
 end;
 
 procedure TForm1.DoSomething(var Erni, Bert: integer);
 begin
   Erni:=Erni+Bert;
   NewProc(Erni);
 end;

You can see "i" was moved to the new procedure (Note: including its comment) and Erni.

Limitations:
Pascal is a very powerful language, so don't expect it will work with every code. Current limits/ToDos:

  • check if selection bounds on statement bounds
  • "with" statements