Sign in Join
Wiki copytemporary

Revision 1 of 1, by Trev on 12-27-2019. The page as it is now.

IDE Window: Extract Procedure

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:

pascal
 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:

pascal
 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:

pascal
 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:

pascal
 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:


From the Free Pascal wiki, page "IDE Window: Extract Procedure", as it stood on 2019-12-27 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.