Code Completion

From RAD Studio
Jump to: navigation, search

Go Up to Getting Started with RAD Studio


Code Completion (Ctrl+Space) is a Code Insight feature available in the Code Editor. Code Completion displays a resizable "hint" window that lists valid elements that you can select to add to your code. You can control the sorting of items in the Code Completion hint window by right-clicking the box and choosing Sort by Name or Sort by Scope.

Different items appear in different colors and various formatting in the Code Completion list. For example, by default, keywords are dark blue, and procedure, function and property names are black and boldfaced.

Invoking Code Completion

Automatic code completion is on by default, and options for enabling and disabling Code Completion are located on the Tools > Options > Editor Options > Code Insight dialog box.

  • When Auto Invoke is enabled for Code Completion, typing a period (.) invokes Code Completion for both Delphi and C++.
  • You can always use Ctrl+Space to invoke Code Completion in Delphi and C++ even if the Auto Invoke option is disabled on Code Insight.
  • If the Delay option is set on Code Insight, a delay timer runs (the cursor spins) before Code Completion is invoked.

Code Completion for C++ on OS X Requires Initial Compile: Code Completion requires that you have compiled at least once your C++ desktop project for the OS X target platform. Before compiling the project, ensure that your environment meets all requirements, as described in Setting Up Your Development Environment for OS X.

When you compile the project, RAD Studio creates the precompiled header file (such as myProject.pch) in the C:\Users\<user>\Documents\Embarcadero\Studio\Projects\OSX32 folder. If you erroneously deleted the projectname.pch file, Code Completion is unavailable for your project until you compile the project again.

Code Completion Window

This section illustrates the Code Completion window for a small code block. Let's say you have a collection of buttons.

var
  GViewButtons: TObjectList<TToolButton>;

In your application, you need to do a for loop through the buttons in the collection, and you do not know how many buttons are in the collection. Your code block is similar to the following:

begin
  { ... }

  if Assigned(GViewButtons) then
    for I := 0 to GViewButtons. - 1 do
    begin
      { something }
    end;

  { ... }
end;

Move your cursor beyond the . (period character) after the GViewButtons statement, and press Ctrl+Space. The Code Completion window appears, as follows:

CodeCompletion.png

Tip: You can resize the code completion window with the mouse, by dragging the lower right corner as you would normally resize standard windows.

Locate the Count property and hit ENTER. Your code will be completed to:

begin
  { ... }

  if Assigned(GViewButtons) then
    for I := 0 to GViewButtons.Count - 1 do
    begin
      { something }
    end;

  { ... }
end;

Using Code Completion

The following are specific ways to use Code Completion in the IDE:

  • To display the properties, methods, and events available in a class, press Ctrl+Space after the name of a variable that represents either a class instance or a pointer to a class instance.
  • To invoke code completion for a pointer type, the pointer must first be dereferenced. For instance:
    • In C++, type: this->
    • In Delphi, type: Self.
  • Type an arrow (->) for a pointer to an object.
  • You can also type the name of non-pointer types followed by a period (.) to see the list of inherited and virtual properties, methods, and events. For instance:
    • In C++ type: TRect test;test.
    • In Delphi, type: var test: TRect; begin test.
  • Type an assignment operator or the beginning of an assignment statement, and press Ctrl+Space to display a list of possible values for the variable.
  • In C++, type the scope operator (::).
  • Type a procedure, function, or method call and press Ctrl+Space to display a list of arguments that are valid for assignment to the variable entered. Select a list item followed by an ellipsis (...) to open a second list of related arguments compatible with the variable entered in the assignment statement.
  • Type a record (in Delphi) or a structure (in C++) to display a list of fields.
  • Type an array property (not a genuine array), and press Ctrl+Space to display an index expression.
  • In C++, you can also press Ctrl+Space on a blank statement line to display symbols from additional RTL units even if they are not used by the current unit.
  • In Delphi, the reserved words appear in the Code Completion window if you have enabled Show reserved words on the Tools > Options > Editor Options > Code Insight dialog box. The words that appear are determined by the context when you invoke Code Completion. The current list of Delphi reserved words is available in the Delphi Language Guide (Fundamental Syntactic Elements).

Canceling Code Completion or Dismissing the Code Completion Window

When you invoke Code Completion, the cursor becomes an hour glass, indicating that the IDE is processing your request.

  • To cancel a Code Completion request before the request completes, press the Esc key.
  • To dismiss the Code Completion window, press the Esc key (or simply click elsewhere).

Browsing to a Declaration

When the Code Completion window is displayed, you can hold down Ctrl and click any identifier in the list to browse to its declaration.

Also, if you hover the mouse pointer over the identifier in the Code Editor, a hint window tells where the identifier is declared. You can press Ctrl, point to the identifier in the code (it changes to blue underline, by default, and the insertion point changes to a hand pointing), and then click (with the mouse) to move to its declaration.

Note: Code Insight works only in the compilation unit. Code Completion supports WM_xxx, CM_xxx, and CN_xxx message methods based on like named constants from all units in the uses clause.

Note: For C++, Code Completion features work best when you have already built your application and have created a precompiled header. Otherwise, you need to wait for the compiler to generate the required information. It is recommended that you set the PCH Usage option on the Project > Options > C++ Compiler Precompiled Headers dialog box. Choose "Generate and use".

See Also