Sign in Join
Wiki copytemporary

IDE Window: Extract Procedure: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: IDE Window: Extract Procedure

OldNewLine
1+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.
2+
3+Basic example:
4+
5+``` pascal
6+ procedure DoSomething;
7+ begin
8+ CallSomething;
9+ end;
10+```
11+
12+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:
13+
14+``` pascal
15+ procedure NewProc;
16+ begin
17+ CallSomething;
18+ end;
19+
20+ procedure DoSomething;
21+ begin
22+ NewProc;
23+ end;
24+```
25+
26+You can see, that the new procedure "NewProc" was created with the selection as body and the old code was replaced by a call.
27+
28+Local Variables and Parameters:\
29+"Extract Proc" scans for used variables and automatically creates the parameter list and local variables. Example:
30+
31+``` pascal
32+ procedure TForm1.DoSomething(var Erni, Bert: integer);
33+ var
34+ i: Integer; // Comment
35+ begin
36+ Erni:=Erni+Bert;
37+ for i:=Erni to 5 do begin
38+ |
39+ end;
40+ end;
41+```
42+
43+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.
44+
45+Result:
46+
47+``` pascal
48+ procedure NewProc(const Erni: integer);
49+ var
50+ i: Integer; // Comment
51+ begin
52+ for i:=Erni to 5 do begin
53+ |
54+ end;
55+ end;
56+
57+ procedure TForm1.DoSomething(var Erni, Bert: integer);
58+ begin
59+ Erni:=Erni+Bert;
60+ NewProc(Erni);
61+ end;
62+```
63+
64+You can see "i" was moved to the new procedure (Note: including its comment) and Erni.
65+
66+Limitations:\
67+Pascal is a very powerful language, so don't expect it will work with every code. Current limits/ToDos:
68+
69+- check if selection bounds on statement bounds
70+- "with" statements
71+
72+---
73+*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](/wiki-contact).*

Back to history