Sign in Join
Wiki copytemporary

Lazarus IDE Tools: what changed

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

Title: Lazarus IDE Tools

OldNewLine
1+The **[[Lazarus Faq|Lazarus]] IDE Tools** is a library of [[Free Pascal]] source parsing and editing tools, called the "codetools".
2+
3+These tools provide features like *Find Declaration*, *Code Completion*, *Extraction*, *Moving Inserting* and *Beautifying Pascal* sources. These functions can save a lot of time and duplicated work. They are customizable, and every feature is available via shortcuts (see Editor Options).
4+
5+Because they work solely on Pascal sources and understand FPC, [[Delphi]] and [[Kylix]] code, they do not require compiled units or an installed Borland/Embarcadero compiler. Delphi and FPC code can be edited at the same time, with several Delphi and FPC versions. This makes porting Delphi code to FPC/Lazarus much easier.
6+
7+## Summary Table of IDE shortcuts
8+
9+| | |
10+|----|----|
11+| [[Lazarus IDE Tools|Declaration Jumping]] | <span class="kbd">Ctrl</span>+Click or <span class="kbd">Alt</span>+<span class="kbd">Up</span> (jump to declaration of type or variable) |
12+| [[Lazarus IDE Tools|Method Jumping]] | <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">Up</span> (toggle between definition and body) |
13+| [[Lazarus IDE Tools|Code Templates]] | <span class="kbd">Ctrl</span>+<span class="kbd">J</span> |
14+| [[Lazarus IDE Tools|Syncro Edit]] | <span class="kbd">Ctrl</span>+<span class="kbd">J</span> (while text is selected) |
15+| [[Lazarus IDE Tools|Code Completion]] (Class Completion) | <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">C</span>, <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">X</span> for creating class fields instead of local variables |
16+| [[Lazarus IDE Tools|Identifier Completion]] | <span class="kbd">Ctrl</span>+<span class="kbd">space</span> |
17+| [[Lazarus IDE Tools|Word Completion]] | <span class="kbd">Ctrl</span>+<span class="kbd">W</span> |
18+| [[Lazarus IDE Tools|Parameter Hints]] | <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">space</span> |
19+| [[Lazarus IDE Tools|Incremental Search]] | <span class="kbd">Ctrl</span>+<span class="kbd">E</span> |
20+| [[Lazarus IDE Tools|Rename Identifier]] | <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">E</span> |
21+
22+## Method Jumping
23+
24+To jump between a procedure body (begin..end) and the procedure definition (procedure Name;) use <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">Up</span>.
25+
26+For example:
27+
28+``` pascal
29+interface
30+
31+procedure DoSomething; // procedure definition
32+
33+implementation
34+
35+procedure DoSomething; // procedure body
36+begin
37+end;
38+```
39+
40+If the cursor is on the procedure body and <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">Up</span> is pressed, the cursor will jump to the definition. Pressing <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">Up</span> again will jump to the body, after 'begin'.
41+
42+This works between methods (procedures in classes) as well.
43+
44+Hints: 'Method Jumping' jumps to the same procedure with the same name and parameter list. If there is no exact procedure, it jumps to the best candidate and positions the cursor on the first difference. (For Delphians: Delphi can not do this).
45+
46+For example a procedure with different parameter types:
47+
48+``` pascal
49+interface
50+
51+procedure DoSomething(p: char); // procedure definition
52+
53+implementation
54+
55+procedure DoSomething(p: string); // procedure body
56+begin
57+end;
58+```
59+
60+Jumping from the definition to the body will position the cursor at the 'string' keyword. This can be used for renaming methods and/or changing the parameters.
61+
62+For example:
63+
64+You renamed 'DoSomething' to 'MakeIt':
65+
66+``` pascal
67+interface
68+
69+procedure MakeIt; // procedure definition
70+
71+implementation
72+
73+procedure DoSomething; // procedure body
74+begin
75+end;
76+```
77+
78+Then you jump from MakeIt to the body. The IDE searches for a fitting body, does not find one, and hence searches for a candidate. Since you renamed only one procedure there is exactly one body without definition (DoSomething) and so it will jump to DoSomething and position the cursor right on 'DoSomething'. Then you can simply rename it there too. This works for parameters as well.
79+
80+## Include Files
81+
82+Include files are files inserted into sources with the {\$I filename} or {\$INCLUDE filename} compiler directive. Lazarus and FPC often uses include files to reduce redundancy and avoid unreadable {\$IFDEF} constructs, needed to support different platforms.
83+
84+Contrary to Delphi, the Lazarus IDE has full support for include files. You can for example jump from the method in the .pas file to the method body in the include file. All codetools like code completion consider include files as special bounds.
85+
86+For instance: When code completion adds a new method body behind another method body, it keeps them both in the same file. This way you can put whole class implementations in include files, as the LCL does for nearly all controls.
87+
88+But there is a trap for newbies: If you open an include file for the first time and try method jumping or find declaration you will get an error. The IDE does not yet know to which unit the include file belongs. You must open the unit first.
89+
90+As soon as the IDE parses the unit, it will parse the include directives there and the IDE will remember this relationship. It saves this information on exit and on project save to ~/.lazarus/includelinks.xml. The next time you open this include file and jump or do a find declaration, the IDE will internally open the unit and the jump will work. You can also hint the IDE by putting `{%mainunit yourunit.pas}` at the top of yourinclude.inc.
91+
92+This mechanism has limits. Some include files are included twice or more. For example: lcl/include/winapih.inc.
93+
94+How you will jump from the procedure/method definitions in this include file to their bodies will depend on your last action. If you worked on lcl/lclintf.pp the IDE will jump to winapi.inc. If you worked on lcl/interfacebase.pp, then it will jump to lcl/include/interfacebase.inc (or one of the other include files). If you are working on both, then you can get confused. ;)
95+
96+## Code Templates
97+
98+Code Templates converts an identifier into a text or code fragment.
99+
100+Code Templates default short cut is <span class="kbd">Ctrl</span>+<span class="kbd">J</span>. You can type an identifier, press <span class="kbd">Ctrl</span>+<span class="kbd">J</span> and the identifier is replaced by the text defined for the identifier. Code Templates can be defined in Tools -\> Options -\> CodeTools.
101+
102+Example: Write the identifier 'classf', leave the cursor right behind the 'f' and press <span class="kbd">Ctrl</span>+<span class="kbd">J</span>. The 'classf' will be replaced by
103+
104+``` pascal
105+T = class(T)
106+private
107+
108+public
109+ constructor Create;
110+ destructor Destroy; override;
111+end;
112+```
113+
114+and the cursor is behind the 'T'.
115+
116+You can get the list of templates by positioning the cursor on space (not on an identifier) and pressing <span class="kbd">Ctrl</span>+<span class="kbd">J</span>. The list of code templates will pop up. Use the cursor keys or type some chars to choose one. Return creates the selected template and Escape closes the pop up.
117+
118+The biggest time savers are templates 'b'+<span class="kbd">Ctrl</span>+<span class="kbd">J</span> for begin..end.
119+
120+## Parameter Hints
121+
122+Parameter Hints shows a hint box with the parameter declarations for the current parameter list.
123+
124+For example: `Canvas.FillRect(|);`
125+
126+Place the cursor in the brackets and press <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">space</span>. A hint box will show up showing the parameters of FillRect.
127+
128+<figure>
129+<img src="Parameterhints1.png" title="Parameterhints1.png" />
130+<figcaption>Parameterhints1.png</figcaption>
131+</figure>
132+
133+Since 0.9.31 there is a button to the right of each declaration to insert the missing parameters.This will copy the parameter names from the chosen declaration to the cursor position.
134+
135+<figure>
136+<img src="Parameterhints2.png" title="Parameterhints2.png" />
137+<figcaption>Parameterhints2.png</figcaption>
138+</figure>
139+
140+Hint: Use the Variable Declaration Completion to declare the variables.
141+
142+> **Note:** The short cut's name is "Show code context".
143+
144+## Incremental Search
145+
146+Incremental Search changes the statusbar of the source editor. Type some characters and the editor will search and highlight immediately all occurrences in the text. Shortcut is <span class="kbd">Ctrl</span>+<span class="kbd">e</span>.
147+
148+- For example pressing <span class="kbd">e</span> will search and highlight all occurrences of 'e'.
149+- Then pressing <span class="kbd">t</span> will search and highlight all occurrences of 'et' and so forth.
150+- You can jump to the next with <span class="kbd">F3</span> (or <span class="kbd">Ctrl</span>+<span class="kbd">e</span> while in search) and the previous with <span class="kbd">Shift</span>+<span class="kbd">F3</span>.
151+- Backspace deletes the last character
152+- return stops the search without adding a new line in the editor.
153+- You can resume the last search by pressing <span class="kbd">Ctrl</span>+<span class="kbd">e</span> a second time, immediately after you started incr-search with <span class="kbd">Ctrl</span>+<span class="kbd">e</span>. that is while the search term is still empty.
154+- Paste <span class="kbd">Ctrl</span>+<span class="kbd">V</span> will append the text from the clipboard to the current search text (since lazarus 0.9.27 r19824).
155+
156+### Hint: Quick searching an identifier with incremental search
157+
158+- Place text cursor on identifier (do not select anything)
159+- Press <span class="kbd">Ctrl</span>+<span class="kbd">C</span>. The source editor will select the identifier and copy it to the clipboard
160+- Press <span class="kbd">Ctrl</span>+<span class="kbd">E</span> to start incremental search
161+- Press <span class="kbd">Ctrl</span>+<span class="kbd">V</span> to search for the identifier (since 0.9.27)
162+- Use <span class="kbd">F3</span> and <span class="kbd">Shift</span>+<span class="kbd">F3</span> to quickly jump to next/previous.
163+- Use any key (for example cursor left or right) to end the search
164+
165+## Syncro Edit
166+
167+Syncro Edit allows you to edit all occurrences of a word at the same time (synchronized). You simple edit the word in one place, and as you type, all other occurrences of the word are updated too.
168+
169+Syncro Edit works on all words in a selected area:
170+
171+- Select a block of text
172+- press <span class="kbd">Ctrl</span>+<span class="kbd">J</span> or click the icon in the gutter. (This only works, if there are any words that occur more than once in the selection.
173+ - From Lazarus 4.0 on the following are available:
174+ - <span class="kbd">Ctrl</span>+<span class="kbd">J</span> Words are grouped independent of the upper/lower casing.
175+ - <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">J</span> Words are only grouped if they have the same upper/lower casing.
176+ - <span class="kbd">Ctrl</span>+<span class="kbd">Alt</span>+<span class="kbd">J</span> Words are only grouped if they have the same context. Identifiers are different from the same word in a comment, or the same word as keyword or string.
177+ - <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">Alt</span>+<span class="kbd">J</span> Words are only grouped if they have the same context and same upper/lower casing.
178+- use the <span class="kbd">tab</span> key to select the word you want to edit (if several different words occurred more than once)
179+- Edit the word
180+- Press <span class="kbd">Esc</span> to finish
181+
182+See an animated example [[New IDE features since|here]]
183+
184+> **Note:** <span class="kbd">Ctrl</span>+<span class="kbd">J</span> is also used for template edit. It switches its meaning if you select some text.
185+
186+## Find next / previous word occurrence
187+
188+The two functions can be found in the popup menu of the source editor
189+
190+- Source editor / popup menu / Find / Find next word occurrence
191+- Source editor / popup menu / Find / Find previous word occurrence
192+
193+And you can assign them shortcuts in the editor options.
194+
195+## Code Completion
196+
197+Code Completion can be found in the IDE menu Edit -\> Complete Code and has as standard short cut <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">C</span>.
198+
199+For Delphians: Delphi calls "code completion" the function showing the list of identifiers at the current source position (<span class="kbd">Ctrl</span>+<span class="kbd">Space</span>). Under Lazarus this is called "Identifier completion".
200+
201+Code Completion combines several powerful functions. Examples:
202+
203+- Class Completion: completes properties, adds/updates method bodies, add private variables and private access methods
204+- Forward Procedure Completion: adds procedure bodies
205+- Event Assignment Completion: completes event assignments and adds method definition and body
206+- Variable Declaration Completion: adds local variable definitions
207+- Procedure Call Completion: adds a new procedure
208+- Reversed procedure completion: adds procedure declarations for procedure/function bodies
209+- Reversed Class Completion: adds method declarations for method bodies
210+
211+Which function is used, depends on the cursor position in the editor and will be explained below.
212+
213+Code Completion can be found in the IDE menu Edit -\> Complete Code and has as standard short cut <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">C</span>.
214+
215+### Class Completion
216+
217+The most powerful code completion feature is "Class Completion". You write a class, add the methods and properties and Code Completion will add the method bodies, the property access methods/variables and the private variables.
218+
219+For example: Create a class (see Code Templates to save you some type work):
220+
221+``` pascal
222+TExample = class(TObject)
223+public
224+ constructor Create;
225+ destructor Destroy; override;
226+end;
227+```
228+
229+Position the cursor somewhere in the class and press <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">C</span>. This will create the method missing bodies and move the cursor to the first created method body, so you can just start writing the class code:
230+
231+``` pascal
232+{ TExample }
233+
234+constructor TExample.Create;
235+begin
236+ |
237+end;
238+
239+destructor TExample.Destroy;
240+begin
241+ inherited Destroy;
242+end;
243+```
244+
245+> **Note:** The '\|' is the cursor and is not added.
246+
247+> **Tip:** You can jump between a method and its body with <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">Up</span>.
248+
249+You can see, that the IDE added the 'inherited Destroy' call too. This is done, if there is an 'override' keyword in the class definition.
250+
251+Now add a method DoSomething:
252+
253+``` pascal
254+TExample = class(TObject)
255+public
256+ constructor Create;
257+ procedure DoSomething(i: integer);
258+ destructor Destroy; override;
259+end;
260+```
261+
262+Then press <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">C</span> and the IDE will add
263+
264+``` pascal
265+procedure TExample.DoSomething(i: integer);
266+begin
267+ |
268+end;
269+```
270+
271+You can see, that the new method body is inserted between Create and Destroy, exactly as in the class definition. This way the bodies keep the same logical ordering as you define. You can define the insertion policy in Tools \> Options -\> Codetools -\> Code Creation.
272+
273+**Complete Properties**
274+
275+Add a property AnInteger:
276+
277+``` pascal
278+TExample = class(TObject)
279+public
280+ constructor Create;
281+ procedure DoSomething(i: integer);
282+ destructor Destroy; override;
283+ property AnInteger: Integer;
284+end;
285+```
286+
287+Press <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">C</span> and you will get:
288+
289+``` pascal
290+procedure TExample.SetAnInteger(const AValue: integer);
291+begin
292+ |if FAnInteger=AValue then exit;
293+ FAnInteger:=AValue;
294+end;
295+```
296+
297+The code completion has added a Write access modifier and added some common code. Jump to the class with <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">Up</span> to see the new class:
298+
299+``` pascal
300+TExample = class(TObject)
301+private
302+ FAnInteger: integer;
303+ procedure SetAnInteger(const AValue: integer);
304+public
305+ constructor Create;
306+ procedure DoSomething(i: integer);
307+ destructor Destroy; override;
308+ property AnInteger: integer read FAnInteger write SetAnInteger;
309+end;
310+```
311+
312+The property was extended by a Read and Write access modifier. The class got the new section 'private' with a Variable 'FAnInteger' and the method 'SetAnInteger'. It is a common Delphi style rule to prepend private variables with an 'F' and the write method with a 'Set'. If you don't like that, you can change this in Tools -\> Options \> Codetools -\> Code Creation.
313+
314+Creating a read only property:
315+
316+``` pascal
317+property PropName: PropType read;
318+```
319+
320+Will be expanded to
321+
322+``` pascal
323+property PropName: PropType read FPropName;
324+```
325+
326+Creating a write only property:
327+
328+``` pascal
329+property PropName: PropType write;
330+```
331+
332+Will be expanded to
333+
334+``` pascal
335+property PropName: PropType write SetPropName;
336+```
337+
338+Creating a read only property with a Read method:
339+
340+``` pascal
341+property PropName: PropType read GetPropName;
342+```
343+
344+Will be kept and a GetPropName function will be added:
345+
346+``` pascal
347+function GetpropName: PropType;
348+```
349+
350+Creating a property with a stored modifier:
351+
352+``` pascal
353+property PropName: PropType stored;
354+```
355+
356+Will be expanded to
357+
358+``` pascal
359+property PropName: PropType read FPropName write SetPropName stored PropNameIsStored;
360+```
361+
362+Because stored is used for streaming read and write modifiers are automatically added as well.
363+
364+Hint: Identifier completion also recognizes incomplete properties and will suggest the default names. For example:
365+
366+``` pascal
367+property PropName: PropType read |;
368+```
369+
370+Place the cursor one space behind the 'read' keyword and press <span class="kbd">Ctrl</span>+<span class="kbd">Space</span> for the identifier completion. It will present you the variable 'FPropName' and the procedure 'SetPropName'.
371+
372+### Forward Procedure Completion
373+
374+"Forward Procedure Completion" is part of the Code Completion and adds missing procedure bodies. It is invoked, when the cursor is on a forward defined procedure.
375+
376+For example: Add a new procedure to the interface section:
377+
378+``` pascal
379+procedure DoSomething;
380+```
381+
382+Place the cursor on it and press <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">C</span> for code completion. It will create in the implementation section:
383+
384+``` pascal
385+procedure DoSomething;
386+begin
387+ |
388+end;
389+```
390+
391+Hint: You can jump between a procedure definition and its body with <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">Up</span>.
392+
393+The new procedure body will be added in front of the class methods. If there are already some procedures in the interface the IDE tries to keep the ordering. For example:
394+
395+``` pascal
396+procedure Proc1;
397+procedure Proc2; // new proc
398+procedure Proc3;
399+```
400+
401+If the bodies of Proc1 and Proc3 already exists, then the Proc2 body will be inserted between the bodies of Proc1 and Proc3. This behaviour can be setup in Tools -\> Options -\> Codetools -\> Code Creation.
402+
403+Multiple procedures:
404+
405+``` pascal
406+procedure Proc1_Old; // body exists
407+procedure Proc2_New; // body does not exist
408+procedure Proc3_New; // "
409+procedure Proc4_New; // "
410+procedure Proc5_Old; // body exists
411+```
412+
413+Code Completion will add all 3 procedure bodies (Proc2_New, Proc3_New, Proc4_New).
414+
415+Why is it called "Forward Procedure Completion"?
416+
417+Because it does not only work for procedures defined in the interface, but for procedures with the "forward" modifier as well. And because the codetools treats procedures in the interface as having an implicit 'forward' modifier.
418+
419+### Event Assignment Completion
420+
421+"Event Assignment Completion" is part of the Code Completion and completes a single Event:=\| statement. It is invoked, when the cursor is behind an assignment to an event.
422+
423+For example: In a method, say the FormCreate event, add a line 'OnPaint:=':
424+
425+``` pascal
426+procedure TForm1.Form1Create(Sender: TObject);
427+begin
428+ OnPaint:=|
429+end;
430+```
431+
432+The '\|' is the cursor and should not be typed. Then press <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">C</span> for code completion. The statement will be completed to
433+
434+``` pascal
435+OnPaint:=@Form1Paint;
436+```
437+
438+A new method Form1Paint will be added to the TForm1 class. Then class completion is started and you get:
439+
440+``` pascal
441+procedure TForm1.Form1Paint(Sender: TObject);
442+begin
443+ |
444+end;
445+```
446+
447+This works just like adding methods in the object inspector.
448+
449+> **Note:** You must place the cursor just after the ':=' assignment operator. If you place the cursor on the identifier (e.g. OnPaint) code completion will invoke "Local Variable Completion", which fails, because OnPaint is already defined.
450+
451+Hints:
452+
453+- You can choose the default visibility of the new method in Tools / Options / Codetools / Class Completion / Default section of methods (since 1.8)
454+- You can define the new method name by yourself. For example: `OnPaint:=@ThePaintMethod;`.
455+
456+Since 0.9.31 Lazarus completes procedure parameters. For example
457+
458+``` pascal
459+procedure TForm1.FormCreate(Sender: TObject);
460+var
461+ List: TList;
462+begin
463+ List:=TList.Create;
464+ List.Sort(@MySortFunction|);
465+end;
466+```
467+
468+Place the cursor on 'MySortFunction' and press <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">C</span> for code completion. You get a new procedure:
469+
470+``` pascal
471+function MySortFunction(Item1, Item2: Pointer): Integer;
472+begin
473+ |
474+end;
475+
476+procedure TForm1.FormCreate(Sender: TObject);
477+var
478+ List: TList;
479+begin
480+ List:=TList.Create;
481+ List.Sort(@MySortFunction);
482+end;
483+```
484+
485+### Variable Declaration Completion
486+
487+"Variable Declaration Completion" is part of the Code Completion and adds a local variable definition for a Identifier:=Term; statement. It is invoked, when the cursor is on the identifier of an assignment or a parameter.
488+
489+For example:
490+
491+``` pascal
492+procedure TForm1.Form1Create(Sender: TObject);
493+begin
494+ i:=3;
495+end;
496+```
497+
498+Place the cursor on the 'i' or just behind it. Then press <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">C</span> for code completion and you will get:
499+
500+``` pascal
501+procedure TForm1.Form1Create(Sender: TObject);
502+var
503+ i: Integer;
504+begin
505+ i:=3;
506+end;
507+```
508+
509+The codetools first checks, if the identifier 'i' is already defined and if not it will add the declaration 'var i: integer;'. The type of the identifier is guessed from the term right to the assignment ':=' operator. Numbers like the 3 defaults to Integer.
510+
511+Another example:
512+
513+``` pascal
514+type
515+ TWhere = (Behind, Middle, InFront);
516+
517+ procedure TForm1.Form1Create(Sender: TObject);
518+ var
519+ a: array[TWhere] of char;
520+ begin
521+ for Where:=Low(a) to High(a) do writeln(a[Where]);
522+ end;
523+```
524+
525+Place the cursor on 'Where' and press <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">C</span> for code completion. You get:
526+
527+``` pascal
528+ procedure TForm1.Form1Create(Sender: TObject);
529+ var
530+ a: array[TWhere] of char;
531+ Where: TWhere;
532+ begin
533+ for Where:=Low(a) to High(a) do writeln(a[Where]);
534+ end;
535+```
536+
537+Since 0.9.11 Lazarus also completes parameters. For example
538+
539+``` pascal
540+ procedure TForm1.FormPaint(Sender: TObject);
541+ begin
542+ with Canvas do begin
543+ Line(x1,y1,x2,y2);
544+ end;
545+ end;
546+```
547+
548+Place the cursor on 'x1' and press <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">C</span> for code completion. You get:
549+
550+``` pascal
551+ procedure TForm1.FormPaint(Sender: TObject);
552+ var
553+ x1: integer;
554+ begin
555+ with Canvas do begin
556+ Line(x1,y1,x2,y2);
557+ end;
558+ end;
559+```
560+
561+Since 0.9.31 Lazarus completes pointer parameters. For example
562+
563+``` pascal
564+ procedure TForm1.FormCreate(Sender: TObject);
565+ begin
566+ CreateIconIndirect(@IconInfo);
567+ end;
568+```
569+
570+Place the cursor on 'IconInfo' and press <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">C</span> for code completion. You get:
571+
572+``` pascal
573+ procedure TForm1.FormCreate(Sender: TObject);
574+ var
575+ IconInfo: TIconInfo;
576+ begin
577+ CreateIconIndirect(@IconInfo);
578+ end;
579+```
580+
581+In all above examples you can use <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">X</span> to show a Code Creation dialog where you can set more options.
582+
583+### Procedure Call Completion
584+
585+Code completion can create a new procedure from a call statement itself.
586+
587+Assume you just wrote the statement "DoSomething(Width);"
588+
589+``` pascal
590+procedure SomeProcedure;
591+var
592+ Width: integer;
593+begin
594+ Width:=3;
595+ DoSomething(Width);
596+end;
597+```
598+
599+Position the cursor over the identifier "DoSomething" and press <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">C</span> to get:
600+
601+``` pascal
602+procedure DoSomething(aWidth: LongInt);
603+begin
604+
605+end;
606+
607+procedure SomeProcedure;
608+var
609+ Width: integer;
610+begin
611+ Width:=3;
612+ DoSomething(Width);
613+end;
614+```
615+
616+It does not yet create functions nor methods.
617+
618+### Reversed Class Completion
619+
620+"Reversed Class Completion" is part of the **Code Completion** and adds a private method declaration for the current method body. It is invoked, when the cursor is in a method body, not yet defined in the class. This feature is available since Lazarus 0.9.21.
621+
622+For example:
623+
624+``` pascal
625+ procedure TForm1.DoSomething(Sender: TObject);
626+ begin
627+ end;
628+```
629+
630+The method DoSomething is not yet declared in TForm1. Press <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">C</span> and the IDE will add "procedure DoSomething(Sender: TObject);" to the private methods of TForm1.
631+
632+For Delphians: Class completion works under Lazarus always in one way: From class interface to implementation or backwards/reversed from class implementation to interface. Delphi always invokes both directions. The Delphi way has the disadvantage, that if a typo will easily create a new method stub without noticing.
633+
634+### Comments and Code Completion
635+
636+Code completion tries to keep comments where they belong. For example:
637+
638+``` pascal
639+ FList: TList; // list of TComponent
640+ FInt: integer;
641+```
642+
643+When inserting a new variable between FList and FInt, the comment is kept in the FList line. Same is true for
644+
645+``` pascal
646+ FList: TList; { list of TComponent
647+ This is a comment over several lines, starting
648+ in the FList line, so codetools assumes it belongs
649+ to the FLIst line and will not break this
650+ relationship. Code is inserted behind the comment. }
651+ FInt: integer;
652+```
653+
654+If the comment starts in the next line, then it will be treated as if it belongs to the code below. For example:
655+
656+``` pascal
657+ FList: TList; // list of TComponent
658+ { This comment belongs to the statement below.
659+ New code is inserted above this comment and
660+ behind the comment of the FList line. }
661+ FInt: integer;
662+```
663+
664+### Method update
665+
666+Normally class completion will add all missing method bodies. (Since 0.9.27) But if exactly one method differ between class and bodies then the method body is updated. For example: You have a method *DoSomething*.
667+
668+``` pascal
669+ public
670+ procedure DoSomething;
671+ end;
672+
673+procedure TForm.DoSomething;
674+begin
675+end;
676+```
677+
678+Now add a parameter:
679+
680+``` pascal
681+ public
682+ procedure DoSomething(i: integer);
683+ end;
684+```
685+
686+and invoke Code Completion (<span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">C</span>). The method body will be updated and the new parameter will be copied:
687+
688+``` pascal
689+procedure TForm.DoSomething(i: integer);
690+begin
691+end;
692+```
693+
694+## Refactoring
695+
696+### Invert Assignments
697+
698+Abstract
699+
700+"Invert Assignments" takes some selected pascal statements and inverts all assignments from this code. This tool is usefull for transforming a "save" code to a "load" one and inverse operation.
701+
702+Example:
703+
704+``` pascal
705+procedure DoSomething;
706+begin
707+ AValueStudio:= BValueStudio;
708+ AValueAppartment :=BValueAppartment;
709+ AValueHouse:=BValueHouse;
710+end;
711+```
712+
713+Select the lines with assignments (between begin and end) and do Invert Assignments. All assignments will be inverted and identation will be add automatically. For example:
714+
715+Result:
716+
717+``` pascal
718+procedure DoSomething;
719+begin
720+ BValueStudio := AValueStudio;
721+ BValueAppartment := AValueAppartment;
722+ BValueHouse := AValueHouse;
723+end;
724+```
725+
726+### Enclose Selection
727+
728+Select some text and invoke it. A dialog will popup where you can select if the selection should be enclosed into **try..finally** or many other common blocks.
729+
730+### Rename Identifier
731+
732+Place the cursor on an identifier and invoke it. A dialog will appear, where you can setup the search scope and the new name.
733+
734+- It will rename all occurences and only those that actually use this declaration. That means it does not rename declarations with the same name.
735+- And it will first check for name conflicts.
736+- Limits: It only works on pascal sources, does not yet rename files nor adapt lfm/lrs files nor lazdoc files.
737+
738+### Find Identifier References
739+
740+Place the cursor on an identifier and invoke it. A dialog will appear, where you can setup the search scope. The IDE will then search for all occurences and only those that actually use this declaration. That means it does not show other declarations with the same name.
741+
742+### Show abstract methods
743+
744+This feature lists and auto completes virtual, abstracts methods that need to be implemented. Place the cursor on a class declaration and invoke it. If there are missing abstract methods a dialog will appear listing them. Select the methods to implement and the IDE creates the method stubs. Since Lazarus 1.3 it adds missing *class interface* methods too.
745+
746+### Extract Procedure
747+
748+See [[IDE Window: Extract Procedure|Extract Procedure]].
749+
750+## Find Declaration
751+
752+Position the cursor on an identifier and do 'Find Declaration'. Then it will search the declaration of this identifier, open the file and jump to it. If the cursor is already at a declaration it will jump to the previous declaration with the same name. This allows to find redefinitions and overrides.
753+
754+Every find declaration sets a Jump Point. That means you jump with find declaration to the declaration and easily jump back with Search -\> Jump back.
755+
756+There are some differences to Delphi: the codetools work on sources following the normal pascal rules, instead of using the compiler output. The compiler returns the final type. The codetools see the sources and all steps in between. For example:
757+
758+The *Visible* property is first defined in *TControl* (controls.pp), then redefined in TCustomForm and finally redefined in TForm. Invoking find declaration on Visible will you first bring to Visible in TForm. Then you can invoke Find Declaration again to jump to Visible in TCustomForm and again to jump to Visible in TControl.
759+
760+Same is true for types like *TColor*. For the compiler it is simply a 'longint'. But in the sources it is defined as
761+
762+``` pascal
763+TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;
764+TColor = TGraphicsColor;
765+```
766+
767+And the same for **forward defined classes**: for instance in *TControl*, there is a private variable
768+
769+``` pascal
770+FHostDockSite: TWinControl;
771+```
772+
773+Find declaration on TWinControl jumps to the forward definition
774+
775+``` pascal
776+TWinControl = class;
777+```
778+
779+And invoking it again jumps to the real implementation
780+
781+``` pascal
782+TWinControl = class(TControl)
783+```
784+
785+This way you can track down every identifier and find every overload.
786+
787+### Hints
788+
789+- ump back with <span class="kbd">Ctrl</span>+<span class="kbd">H</span>.
790+- view/navigate all visited locations via Menu: View -\> "jump history"
791+- With a 5 button Mouse the 2 extra buttons to go forward/backward between the visited points
792+
793+
794+using [[IDE Window: EditorMouseOptionsAdvanced|advanced mouse options]] the buttons can be remapped.
795+
796+## Identifier Completion
797+
798+"Identifier Completion" is invoked by <span class="kbd">Ctrl</span>+<span class="kbd">space</span>. It shows all identifiers in scope. For example:
799+
800+``` pascal
801+procedure TForm1.FormCreate(Sender: TObject);
802+begin
803+ |
804+end;
805+```
806+
807+Place the cursor between *begin* and *end* and press <span class="kbd">Ctrl</span>+<span class="kbd">space</span>. The IDE/CodeTools will now parse all reachable code and present you a list of all found identifiers. The CodeTools cache the results, so invoking it a second time will be much faster.
808+
809+> **Note:** for Delphians: Delphi calls it *Code completion*.
810+
811+Some identifiers like 'Write', 'ReadLn', 'Low', 'SetLength', 'Self', 'Result', 'Copy' are built into the compiler and are not defined anywhere in source. The identifier completion has a lot of these things built in as well. If you find one missing, just create a feature request in the bug tracker.
812+
813+*Identifier completion* does not complete all **keywords**. So you can not use it to complete 'repe' to 'repeat'. For these things use <span class="kbd">Ctrl</span>+<span class="kbd">W</span> Word Completion or <span class="kbd">Ctrl</span>+<span class="kbd">J</span> Code Templates. Since 0.9.27 *identifier completion* completes some keywords.
814+
815+Identifier completion shows even those identifiers, that are not compatible.
816+
817+### Matching only the first part of a word
818+
819+You can invoke identifier completion only on the first few characters in a word. Position the cursor within a word. *Only characters to the left of the cursor* will be used to look up identifiers. For example:
820+
821+``` pascal
822+procedure TForm1.FormCreate(Sender: TObject);
823+begin
824+ Ca|ption
825+end;
826+```
827+
828+The box will show you only identifiers beginning with 'Ca' ( \| indicates the cursor position).
829+
830+### Keys
831+
832+- Letter or number: add the character to the source editor and the current prefix. This will update the list.
833+- Backspace: remove the last character from source editor and prefix. Updates the list.
834+- Return: replace the whole word at cursor with the selected identifier and close the popup window.
835+- Shift+Return: as *Return*, but replaces only the prefix (left part) of the word at the cursor.
836+- Up/Down: move selection
837+- Escape: close popup without change
838+- Tab: completes the prefix to next choice. For example: The current prefix is 'But' and the identifier completion only shows 'Button1' and 'Button1Click'. Then pressing *Tab* will complete the prefix to 'Button1'.
839+- Else: as *Return* and add the character to the source editor
840+
841+### Methods
842+
843+When cursor is in a class definition and you identifier complete a method defined in an ancestor class the parameters and the override keyword will be added automatically. For example:
844+
845+``` pascal
846+TMainForm = class(TForm)
847+protected
848+ mous|
849+end;
850+```
851+
852+Completing **MouseDown** gives:
853+
854+``` pascal
855+TMainForm = class(TForm)
856+protected
857+ procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X,
858+ Y: Integer); override;
859+end;
860+```
861+
862+Note that in case there is a semicolon after the identifier
863+
864+``` pascal
865+TMainForm = class(TForm)
866+protected
867+ mous|;
868+end;
869+```
870+
871+then the default action is performed, which is to simply complete the identifier:
872+
873+``` pascal
874+TMainForm = class(TForm)
875+protected
876+ mousedown;
877+end;
878+```
879+
880+### Properties
881+
882+``` pascal
883+property MyInt: integer read |;
884+```
885+
886+Identifier completion will show **FMyInt** and **GetMyInt**.
887+
888+``` pascal
889+property MyInt: integer write |;
890+```
891+
892+Identifier completion will show **FMyInt** and **SetMyInt**.
893+
894+### Uses section / Unit names
895+
896+In uses sections the identifier completion will show the filenames of all units in the search path. These will show all lowercase (e.g. *avl_tree*), because most units have lowercase filenames. On completion it will insert the case of the unit (e.g. *AVL_Tree*).
897+
898+### Statements
899+
900+``` pascal
901+procedure TMainForm.Button1Click(Sender: TObject);
902+begin
903+ ModalRe|;
904+end;
905+```
906+
907+becomes:
908+
909+``` pascal
910+procedure TMainForm.Button1Click(Sender: TObject);
911+begin
912+ ModalResult:=|;
913+end;
914+```
915+
916+### Icons in completion window
917+
918+In Lazarus 1.9+, option exists to show icons instead of "types", for lines in the completion window. Picture shows these icons:
919+
920+<figure>
921+<img src="ide_completion_icons.png" title="ide_completion_icons.png" />
922+<figcaption>ide_completion_icons.png</figcaption>
923+</figure>
924+
925+## Word Completion
926+
927+*Word Completion* is invoked by <span class="kbd">Ctrl</span>+<span class="kbd">W</span>. It shows all words of all currently open editors and can therefore be used in non pascal sources, in comments and for keywords.
928+
929+Otherwise it works the same as identifier completion.
930+
931+## Unit / Identifier Dictionary (Cody)
932+
933+This dialog lets you search for identifiers in other units. The units do not yet have to be used by your code.
934+
935+This feature is part of the package "Cody". To activate the feature install the package.
936+
937+## Goto Include Directive
938+
939+"Goto Include Directive" in the search menu of the IDE jumps to {\$I filename} statement where the current include file is used.
940+
941+## Publish Project
942+
943+Creates a copy of the whole project. If you want to send someone just the sources and compiler settings of your code, this function is your friend.
944+
945+A normal project directory contains a lot of information. Most of it is not needed to be published: the .lpi file can contain session information (like caret position and bookmarks of closed units) and the project directory contains a lot of .ppu, .o files and the executable. To create a lpi file with only the base information and only the sources, along with all sub directories use "Publish Project".
946+
947+> **Note:** Since version 0.9.13 there is a new *Project Option* that allows you to store session information in a separate file from the normal .lpi file. This new file ends with the .lps extension and only contains session information, which will leave your .lpi file much cleaner.
948+
949+In the dialog you can setup a filter to include and exclude certain files; with the command after you can compress the output into one archive.
950+
951+## Hints from comments
952+
953+At several places the IDE shows hints for an identifier. For example when moving the mouse over an identifier in the source editor and waiting a few seconds. When the IDE shows a hint for an identifier it searches the declaration and all its ancestors and looks for comments and fpdoc files. There are many coding styles and many commenting styles. In order to support many of the common comment styles the IDE uses the following heuristics:
954+
955+### Comments shown in the hint
956+
957+Comments in front of a declaration, without empty line and not starting with the *\<* sign:
958+
959+``` pascal
960+var
961+ {Comment}
962+ Identifier: integer;
963+```
964+
965+Comments with the *\<* sign belong to the prior identifier.
966+
967+Comments behind an identifier on the same line:
968+
969+``` pascal
970+var
971+ identifier, // Comment
972+ other,
973+```
974+
975+Comments behind the definition on the same line:
976+
977+``` pascal
978+var
979+ identifier:
980+ char; // Comment
981+```
982+
983+An example for **\<** sign:
984+
985+``` pascal
986+const
987+ a = 1;
988+ //< comment for a
989+ b = 2;
990+ // comment for c
991+ c = 3;
992+```
993+
994+All three comment types are supported:
995+
996+``` pascal
997+ {Comment}(*Comment*)//Comment
998+ c = 1;
999+```
1000+
1001+### Comments not shown in the hint
1002+
1003+Comments starting with **\$** or **%** are ignored. For example *//% Hiddden*, *//\$ Hidden*, *(*\$ Hidden*)*.
1004+
1005+Comments in front separated with an empty line are treated as not specific to the following identifier. For example the following class header comment is not shown in the hint:
1006+
1007+``` pascal
1008+type
1009+ { TMyClass }
1010+
1011+ TMyClass = class
1012+```
1013+
1014+The class header comments are created on class completion. You can turn this off in the *Options / Codetools / Class completion / Header comment for class*. If you want to show the header comment in the hint, just remove the empty line.
1015+
1016+The following comment will be shown for GL_TRUE, but not for GL_FALSE:
1017+
1018+``` pascal
1019+ // Boolean
1020+ GL_TRUE = 1;
1021+ GL_FALSE = 0;
1022+```
1023+
1024+## Quick Fixes
1025+
1026+Quick Fixes are menu items for specific compiler messages. They help you to quickly fix the problem. Select a message in the *Messages* window and right click, or right click in the source editor on the icon to the left.
1027+
1028+- Unit not found: remove from uses section
1029+- Unit not found: find unit in loaded packages and allow to auto add package dependency
1030+- Constructing a class "\$1" with abstract method "\$2": show dialog to override all abstract methods
1031+- Local variable "\$1" not used: remove definition
1032+- Circular unit reference between \$1 and \$2: show Unit Dependencies dialog with full path between the two units
1033+- Identifier not found: search via Code Browser
1034+- Identifier not found: search via Cody Dictionary (needs package [[Cody]])
1035+- Identifier not found: add local variable
1036+- Recompiling \$1, checksum changed for \$2: show a dialog with search paths and other information
1037+- IDE warning: other sources path of package %s contains directory...: open package
1038+- any hint, note, warning: add IDE directive {%H-}
1039+- any hint, note, warning: add compiler directive {\$warn id off} (since 1.7)
1040+- any hint, note, warning: add compiler option -vm\<messageid\>
1041+- Local variable "i" does not seem to be initialized: insert assignment (since 1.5)
1042+- Inherited method is hidden: add modifier override, overload, or reintroduce (since 1.9)
1043+
1044+## Outline
1045+
1046+This option is located in the IDE Options dialog, Editor - Display - Markup and Matches - Outline (global).
1047+
1048+It makes highlighting of Pascal keywords together with their begin-end brackets. This allows to see nesting of big blocks. For example, for such procedure body:
1049+
1050+``` pascal
1051+ with ADockObject do
1052+ begin
1053+ if DropAlign = alNone then
1054+ begin
1055+ if DropOnControl <> nil then
1056+ DropAlign := DropOnControl.GetDockEdge(DropOnControl.ScreenToClient(DragPos))
1057+ else
1058+ DropAlign := Control.GetDockEdge(DragTargetPos);
1059+ end;
1060+ PositionDockRect(Control, DropOnControl, DropAlign, FDockRect);
1061+ end;
1062+```
1063+
1064+it highlights:
1065+
1066+- outer with-do-begin-end in orange
1067+- next if-then-begin-end in green
1068+- inner if-then-else in cyan
1069+
1070+<figure>
1071+<img src="ide_outline.png" title="ide_outline.png" />
1072+<figcaption>ide_outline.png</figcaption>
1073+</figure>
1074+
1075+---
1076+*From the Free Pascal wiki, page "Lazarus IDE Tools", as it stood on 2024-05-10 after a change by Michael, 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