Code Editor
Go Up to Getting Started with RAD Studio
The Code Editor occupies the center pane of the IDE window. The Code Editor is a full-featured, customizable, UTF8 editor that provides syntax highlighting, multiple undo capability, and context-sensitive Help for language elements.
As you design the user interface for your application, RAD Studio generates the underlying code. When you modify object properties, your changes are automatically reflected in the source files.
Because all of your programs share common characteristics, RAD Studio auto-generates code to get you started. You can think of the auto-generated code as an outline that you can examine to create your program.
The context menu commands for the Code Editor are described in Code Editor Context Menu.
The video RAD Studio 2010 Editor, by Mike Rozlog provides good hints and tips.
To help you write code, the Code Editor provides the features described in this section.
Contents |
Change Bars
The left margin of the Code Editor displays a green change bar to indicate lines that have not been changed in the current editing session. A yellow change bar indicates that changes have been made since the last File > Save operation.
You can, however, customize the change bars to display in colors other than the default green and yellow. Select Tools > Options > Environment Options > Editor Options.
Indenting Code
You can use the Tab key to indent the current line of code or a block of code in the Code Editor. The number of spaces that the Tab key indents is determined by the Block indent option on the Tools > Options > Editor Options > Source Options dialog box.
- To indent a line of code, place the cursor at the beginning of the line and press Tab.
- To indent an entire block of code, highlight the code block and press Tab.
- To move text to the left ("outdent"), use Shift + Tab.
Formatting Code
Editing Delphi or C++ code in the Code Editor, you can apply the Format Source context menu command to format the source code. You can set the formatter options for Indentation, Spaces, Line Breaks, and Capitalization groups in the Options dialog box (Tools > Options) under the Formatter group. Notice that the Format Source command implements automatic formatting of your code. It takes into account only formatting options specified in the the Options dialog and totally overwrites all your manual formatting implemented in the Code Editor. For example, it ignores your manual code indenting. You can select a block of code and call Format Source command, then only this block will be formatted.
The source code formatter is used for the best source code presentation. It is useful to analyze program flow, search for bugs and present code. After many changes during iterative bug fixing, initial source code usually looks very strange. Each method or block is formatted with its own style, a mix of tabs and spaces for indentation. Different tab size settings used by authors leads to the fact that instead of program structure explanation, indentation only confuses the user. Correct source formatting can help to find bugs quickly and to understand the program logic.
Code Insight
Code Insight refers to a subset of features embedded in the Code Editor (such as Code Parameter Hints, Code Hints, Help Insight, Code Completion, Class Completion, Block Completion, and Code Browsing) that aid in the code writing process. These features help identify common statements you wish to insert into your code, and assist you in the selection of properties and methods. Some of these features are described in more detail in the sub-sections below.
To invoke Code Insight, press Ctrl+Space while using the Code Editor. A pop-up window displays a list of symbols that are valid at the cursor location.
To enable and configure Code Insight features, choose Tools > Options > Editor Options and click Code Insight.
When you're using the Delphi Language, the pop-up window filters out all interface method declarations that are referred to by property read or write clauses. The window displays only properties and stand-alone methods declared in the interface type. Code insight supports WM_xxx, CM_xxx, and CN_xxx message methods based on like-named constants from all units in the uses clause.
Code Parameter Hints
Displays a hint containing argument names and types for method calls. Available between the parenthesis of a call, i.e., ShowMessage ( | );
You can invoke Code Parameter Hints by pressing Ctrl+Shift+Space.
Code Hints
Displays a hint containing information about the symbol such as type, file and line # declared at.
You can display Code Hints by hovering the mouse over an identifier in your code, while working in the Code Editor.
Note: Code Hints only work for Delphi when you have disabled the Help Insight feature. To disable Help Insight, uncheck Tooltip help insight on the Tools > Options > Editor Options > Code Insight dialog box.
Help Insight
Help Insight displays a hint containing information about the symbol such as type, file, line # declared at, and any XML documentation associated with the symbol (if available).
Invoke Help Insight by hovering the mouse over an identifier in your code, while working in the Code Editor. You can also invoke Help Insight by pressing Ctrl+Shift+H.
Code Completion
The Code Completion feature displays a drop-down list of available symbols at the current cursor location. You invoke Code Completion for your specific language in the following way:
- In Delphi:
- Press
Ctrl+Space(always invokes Code Completion). - Enter
.(only works when Auto Invoke is enabled on Code Insight page).
- Press
- In C++:
- Press
Ctrl+Space(always invokes Code Completion). - Enter
.or->(only works when Auto Invoke is enabled on Code Insight page).
- Press
To cancel a Code Completion request, press the Esc key.
Class Completion
Class completion simplifies the process of defining and implementing new classes by generating skeleton code for the class members that you declare. By positioning the cursor within a class declaration in the interface section of a unit and pressing CTRL+SHIFT+C, any unfinished property declarations are completed. For any methods that require an implementation, empty methods are added to the implementation section. Class completion is also a command ("Complete class at cursor") on the Code Editor context menu.
Block Completion
When you press ENTER while working in the Code Editor and there is a block of code that is incorrectly closed, the Code Editor enters the closing block token at the next available empty line after the current cursor position. For instance, if you are using the Code Editor with the Delphi language, and you type the token begin and then press ENTER, the Code Editor automatically completes the statement so that you now have: begin end. This feature also works for the C++ language.
Code Browsing (Ctrl-Click)
While using the Code Editor to edit an application such as a VCL Forms application, you can use CTRL-click to automatically "jump to" the code that defines an identifier. To browse code, hold down the CTRL key while passing the mouse over the name of any class, variable, property, method, or other identifier. After the mouse pointer turns into a hand and the identifier is highlighted and underlined, click on the highlighted identifier, and the Code Editor jumps to the declaration of the identifier, opening the source file, if necessary.
You can do the same thing by right-clicking an identifier and choosing Find Declaration from the context menu.
Code browsing can find and open only the units that exist in the project Search path or in the global Browsing path. Directories are searched in the following order:
- Either the project-specific Search path for Delphi (Project > Options > Delphi Compiler) or the Include path for C++ (Project > Options > Directories and Conditionals).
- The global Browsing path (for Delphi: Tools > Options > Environment Options > Delphi Options > Library - Win32 , or for C++: Tools > Options > Environment Options > C++ Options > Paths and Directories).
The sections below describe features that you can use to navigate your code while you are using the Code Editor.
Method Hopping
You can navigate between methods using a series of editor hotkeys. You can also lock the hopping to occur only within the methods of the current class. For example, if class lock is enabled and you are in a method of TComponent, then hopping is only available within the methods of TComponent.
The keyboard shortcuts for Method Hopping are as follows:
- CTRL+Q^L - toggles class lock.
- CTRL+ALT+UP - moves to the top of the current method, or the previous method.
- CTRL+ALT+DOWN - moves to the next method.
- CTRL+ALT+HOME - first method in source.
- CTRL+ALT+END - last method in source.
- CTRL+ALT+MOUSE_WHEEL - scrolls through methods.
Finding Classes
Allows you to find classes. Use the Search > Find Class command to see a list of available classes that you can select. After you choose one, the IDE navigates to its declaration.
Finding Units
If you are programming in the Delphi language, you can use a refactoring feature to locate namespaces or units. Use the Find Unit command to locate and add units to your code file.
Live Templates
Live Templates allow you to have a dictionary of pre-written code that can be inserted into your programs while you're working with the Code Editor. This reduces the amount of typing that you must do.
Use the links at the end of this topic to learn more about creating and using Live Templates.
Code Folding
Code folding lets you collapse or expand regions or blocks of code. Collapsing your code creates a hierarchical view of the code and makes it easier to read and navigate. The collapsed code is not deleted, but hidden from view until you expand it. Code folding is on by default.
To use code folding, use the plus (+) and minus (-) signs located on the lefthand edge of the Code Editor:
- Click the plus (+) sign to expand the associated region of code.
- Click the minus (-) sign to collapse the associated region of code.
To enable/disable code folding:
- Use the Code Folding checkbox on the Tools > Options > Editor Options dialog box.
- Use the keyboard shortcut Ctrl+Shift K+O.
For more information about code folding, including how to create code folding regions, see Using Code Folding.
Refactoring
Refactoring is the process of improving your code without changing its external functionality. For example, you can turn a selected code fragment into a method by using the extract method refactoring. The IDE moves the extracted code outside of the current method, determines the needed parameters, generates local variables if necessary, determines the return type, and replaces the code fragment with a call to the new method. Several other refactoring methods, such as renaming a symbol and declaring a variable, are also available.
SyncEdit
New video for 2010! Video: Sync Editing in RAD Studio 2010, by Mike Rozlog
The Sync Edit feature lets you simultaneously edit identical identifiers in code. As you change the first identifier, the same change is performed automatically on the other identifiers. You can also set jump points to navigate to specific sections of your code.
To-Do Lists
A To-Do List records tasks that need to be completed for a project. After you add a task to the To-Do List, you can edit the task, add it to your code as a comment, indicate that it has been completed, and then remove it from the list. You can filter the list to display only those tasks that interest you.
Keystroke Macros
You can record a series of keystrokes as a macro while editing code. After you record a macro, you can play it back to repeat the keystrokes during the current IDE session. Recording a macro replaces the previously recorded macro.
Bookmarks
Bookmarks provide a convenient way to navigate long files. You can mark a location in your code with a bookmark and jump to that location from anywhere in the file.
When you set a bookmark, a book icon
is displayed in the left gutter of the Code Editor. You can use up to ten bookmarks, numbered 0
through 9
, within a file.
You can drag-and-drop bookmark icons in the gutter of the Code Editor, and a moved bookmark has the number of the original bookmark.
Block Comments
You can comment-out a section of code by selecting the code in the Code Editor and pressing Ctrl+/ (slash). Each line of the selected code is prefixed with // and is ignored by the compiler. Pressing Ctrl+/ adds or removes the slashes, based on whether the first line of the code is prefixed with //. When using the Visual Studio or Visual Basic key mappings, use Ctrl+K+C to add and remove comment slashes.
See Also
- Video: RAD Studio 2010 Editor, by Mike Rozlog
- Video: Sync Editing in RAD Studio 2010, by Mike Rozlog
- Code Editor Context Menu
- Customizing the Code Editor
- Formatting Source Code
- Code Completion
- Using Code Insight
- Using Class Completion
- Using Live Templates
- Creating Live Templates
- Using Sync Edit
- Using Code Folding
- Using To-Do Lists
- Recording a Keystroke Macro
- Using Bookmarks