Code Insight Reference

From RAD Studio
Jump to: navigation, search

Go Up to The Code Editor Index (IDE Tutorial)


Code Insight refers to a subset of features embedded in the Code Editor (such as Code Completion, Parameter Completion, Code Hints, Tooltip Expression, Tooltip Insight, Go To Definition, Block Completion, Class Completion, and Error Insight) that aid in the code writing process. These features help identify common statements you want 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 following subsections.

To enable and configure Code Insight features, choose Tools > Options > User Interface > Editor > Source and click on Code Insight Options option.

Note:

Code Insight features are now defined per Language.

When using Delphi you can use the Language Server Protocol (LSP) for Code Insight (the default), or the Classic implementation.

For C++, the IDE automatically uses the Language Server Protocol (LSP) or Classic Code Insight providers depending on the compiler being used (Clang or Classic).

Code Insight using Delphi Language Server Protocol (LSP)

The Language Server Protocol (LSP) is a specification that provides language services through a separate server process that communicates with an IDE. As a result, Code Insight features are provided by a separate process(es), and the features are asynchronously provided.

Note:

Code Insight using the Language Server Protocol (LSP) can be used while debugging as well as normal code editing.

Files changed outside the IDE will be recognized by the Language Server Protocol (LSP) and re-scanned.

The Language Server Protocol (LSP) has the User Account Control (UAC) virtualization disabled.

Languages

Languages are defined in Tools > Options > Language setting pane, where you can see a list of all languages available. You can also create a new language.

Language LSP.png

Editor Options and Syntax Highlighting

The Editor options and Syntax Highlighting are defined per language. You can find them in Tools > Options > User Interface > Editor > Source and click on Options.

Source Editor Options.png

On this setting pane, you can choose a language. Then, for that language, there are the editor options, as well as the Syntax Highlighter to use for that language.

Syntax Highlighter.png

Changing Code Insight / LSP Manager

Delphi LSP is enabled by default. You can check if the IDE is using Delphi LSP by opening a Delphi project > Task Manager and look for a DelphiLSP.exe process.

The final tab for a language is Code Insight. It provides the User Editor Font and a set of Code Insight settings.

The Code Insight Manager provides Code Insight functionality for a language. You can set this to any manager listed there, even if that manager was not intended for the language.

Note:

The Code Insight Manager lists all managers registered with the IDE.

Delphi: you can choose the Code Insight Manager from either Classic or Language Server Protocol.

C++: you cannot change the Code Insight Manager. It uses the Classic or LSP automatically for the Classic or Clang compilers.
Attention: It is not recommended to change the Code Insight Manager to one that is not intended for the current language.

To change Delphi between using LSP and the Classic Code Insight implementation, choose:

  • Delphi (Language Server Protocol): the new LSP support.
  • Delphi (Classic Code Insight): the old implementation of Code Insight used in 10.3.3 and earlier. When you select this option, you turn the LSP off.

Code Insight.png

You can also create a New LSP Manager. Click on New, and a dialog appears allowing you to specify the LSP server executable and some options, including a friendly name, the language identifier, a timeout after which the server will be forcibly restarted, and any server-specific initialization options to be injected into the Initialize JSON RPC call.

You can use this option to add a server for Python or any other language.

Note: RAD Studio only supports servers that communicate over standard I/O (console I/O).

New LSP Server.png

Code Completion — Ctrl+Space

The Code Completion feature displays a drop-down list of all items that contain the typed string anywhere in an identifier.

To invoke Code Completion, press Ctrl+Space while using the Code Editor. Select the character and press Enter to insert the text in the code at the cursor location.

Code Completion is also automatically invoked at some code locations, such as when you type a period "." in Delphi, or a period ".", arrow "->", scope "::" or header "#include <" statement in C++.

You invoke Code Completion for your specific language in the following ways:

  • C++
  • Press Ctrl+Space (always invokes Code Completion).
  • Enter . or -> (only works when Auto Invoke is enabled on the Code Insight Options page).
  • To cancel a Code Completion request, press the Esc key.

Code Completion C.png

Note: For the Clang-enhanced compiler, this feature uses a Language Server Protocol server. It is asynchronous and non-blocking.
  • Delphi
  • Press Ctrl+Space (always invokes Code Completion).
  • Enter . (only works when Auto Invoke is enabled on the Code Insight Options page).
  • To cancel a Code Completion request, press the Esc key.

Code Completion D.png

When you type characters, the selection/match behavior as you type considers the following situations:

  • When there is an exact match between what is typed and an identifier, it is selected.
  • Otherwise, the first identifier that starts with what was typed is selected.
  • Otherwise, the first identifier that contains what was typed is selected.

For example, when you type MyControl in the editor, invoke code completion, and then when you type rect, not only the properties or methods beginning with Rect are shown, but also ClientRect, BoundsRect, etc. Anything containing rect inside the identifier string are shown.

Code Completion works correctly, including correct results, in situations including:

  • A new file that does not yet exist on disk.
  • A new project where neither the project nor any units exist on disk.
  • A modified unit, not yet saved, where the current in-IDE code has changes that affect completion results.
  • Results are correct (and changed correctly) when changing target platforms or other project options.
Note:
  • Matching is case-insensitive.
  • Code Completion will work when Use MSBuild externally to compile is turned ON.

Code Completion Settings

When using Code Completion, there are some code completion filters that you can configure in the Insight Options tab. Go to Tools > Options > User Interface > Editor > Source and click on Insight Options.

Code Completion Settings.png

Use these filters according to your needs:

  • List all symbols that start with the filter first: symbols that start with what you typed (the 'filter') are sorted ahead of symbols that contain what you typed. If this is not checked, symbols are sorted alphabetically; if it is turned on, symbols that begin with the filter text are moved to the top of the list.
  • Select shortest matching symbol: when the code completion list is shown, an entry is always auto selected, or instead opens with the closest symbol in scope to where your coding is selected. This setting is checked by default (set to select the shortest) but turning it off will give you a closer behavior to classic code completion.
  • Filter text is underlined: when checked, the code completion dropdown underlines text that matches what you type.
  • Show symbols that contain filter: this controls what symbols are included in the list. By default (checked), all symbols that contain the text you typed are listed. If the setting is turned off, the code completion list shows only symbols that start with what you typed.

To get a closer behavior to classic code completion:

  • List all symbols that start with the filter first: checked
  • Select shortest matching symbol: off
  • Filter text is underlined: off
  • Show symbols that contain filter: off

Parameter Completion — Ctrl+Shift+Space

The Parameter Completion is auto-invoked after opening a left bracket of a function call, or pressing Ctrl+Shift+Space while using the Code Editor. A popup window displays a hint containing argument names and types for method calls. You can type characters to match those in the selection and press Return to insert the text in the code at the cursor location.

Examples:

  • C++

Parameter Completion C.png

Note: For the Clang-enhanced compiler, this feature uses a Language Server Protocol server. It is asynchronous and non-blocking.
  • Delphi

Parameter Completion D.png

Code Hints

The Code Hints displays a hint containing information about the symbol such as type, file, and line number, where declared. 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 the Help Insight feature, uncheck Tooltip Help Insight on the Tools > Options > Editor > Code Insight dialog box.

Example:

Code Hints D.png

Tooltip Expression Evaluation

The Tooltip Expression Evaluation displays the current value of a variable when you position the cursor over it. This feature is available when the program execution is paused during a debugging session.

Examples:

  • C++

Tooltip Expresion Evaluation C.png

  • Delphi

Tooltip Expresion Evaluation D.png

Tooltip Insight

The Tooltip Insight displays information in a popup window when the mouse hovers above a symbol.

Tooltip Symbol Insight

The Tooltip Symbol Insight displays declaration information in a tooltip window for any identifier by passing the cursor over a symbol in the Code Editor.

Examples:

  • C++

Tooltip Symbol Insight C.png

  • Delphi

Tooltip Symbol Insight D.png

Tooltip Help Insight — Ctrl+Shift+H

The Tooltip Help Insight displays a hint containing information about the symbol such as type, file, line number where declared, and any XML documentation associated with the symbol (if available).

Invoke the Tooltip Help Insight by hovering the mouse over an identifier in your code, while working in the Code Editor. You can also invoke it by pressing Ctrl+Shift+H.

Examples:

  • C++

Tooltip Help Insight C.png

Note: Only available when using the Classic Compiler
  • Delphi

Tooltip Help Insight D.png

Find Declaration — Ctrl+Click

Find Declaration navigates to the location where a type or variable is defined.

While using the Code Editor you can use Ctrl+Click to automatically "jump to" the code that defines an identifier. To browse code, hold down the Ctrl key while hovering the mouse over the name of any class, variable, property, method, or other identifier.

The mouse pointer turns into a hand, and the identifier appears highlighted and underlined. Click 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. Pressing Alt+Left Arrow returns you to where you browsed from.

Find Declaration can find and open only units in the project Search Path or Source Path, or in the product Browsing Path or Library Path. Directories are searched in the following order:

  1. The project Search Path (Delphi) or Include Path (C++)
  2. The project Source Path, defined as the directory in which the project was saved
  3. The global Browsing Path
  4. The global Library Path
  5. The Library Path, which is searched only if there is no project open in the IDE

These paths can be modified by editing the corresponding values:

Examples:

  • C++

Find Declaration C.png

  • Delphi

Find Declaration D.png

Block Completion — Enter key

Block Completion happens when you are missing code in the following situations:

  • In C++, the block considered for completion is the code that is inside curly braces {}.
  • In Delphi, the block considered for completion is the code that is inside a begin-end.

When you press Enter while working in the Code Editor and there is a code that is incorrectly closed, the Code Editor enters the closing token at the next available empty line after the current cursor position.

The drop-down menu sets the behavior of completion when you surround existing statements with symbols, as follows:

  • Without new line - Positions the cursor after the code you just typed.
  • With new line - Positions the cursor inside the code you just typed.
  • New blocks only - Invokes completion only if you start a new block.

Class Completion — Ctrl+Shift+C

Class Completion simplifies the process of defining and implementing new classes by generating skeleton code for the class members that you declare.

Position the cursor within a class declaration in the interface section of a unit and press 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 can also be achieved by choosing the option Complete class at cursor from the Code Editor context menu.

Error Insight

The Error Insight feature underlines invalid code in red. Positioning the cursor over invalid text displays a tooltip window containing the probable cause of the error.

Also, the list of errors generated by the expression appears in the Errors pane of the Structure View.

Examples:

  • C++

Error Insight C.png

  • Delphi

Error Insight D.png

Index for C++ Insights

The Index for C++ Insights provides index for the Tooltip Insight and Find Declaration features.

It is the Language Server Protocol (LSP) server (cquery) that runs through the project and builds a database. It uses CPU, so it can be turned off through the Index for C++ Insights checkbox.

Note:

This setting applies only for C++ and Clang-enhanced compilers.

For the Clang-enhanced compiler, this feature uses a Language Server Protocol (LSP) server. It is asynchronous and non-blocking.

When working with Clang-enhanced compilers and the Index for C++ Insights feature is enabled, the project that you open is opened indexed. It also creates a database that is used for the Tooltip Insight and Find Declaration features. It uses CPU and it can be turned off by unchecking the Index for C++ Insights checkbox.

Tip: Keep in mind, if this option is unchecked, both the Tooltip Insight and Find Declaration features are non-functional. However, Code Completion and Parameter Completion are still functional whether or not Indexing is turned ON.

Advanced Options

Tools API Support

Code Insight support is mainly focused around the Language Server Protocol, and there is a generic LSP support, where you can create a New LSP server using the above dialog. However, the IDE implements two things: generic asynchronous code insight; and then LSP support, which is a specific implementation of asynchronous code insight.

The ToolsAPI defines a number of new types and interfaces for async code insight. The LSP uses these interfaces, but you can write a code insight provider that is asynchronous with any implementation you wish.

The interfaces are located in ToolsAPI.pas:

  • IOTAAsyncCodeInsightManager is the main interface for a generic async code insight manager implementation. It uses a number of callbacks, which are defined above it.
  • IOTACodeInsightUIOverride is used to override specific UI behavior.

Filing Bugs and Log Files

To enable log files, open the registry:

  • Create a key:

HKEY_CURRENT_USER\Software\Embarcadero\BDS\21.0\LSP

  • Create a DWORD value called DelphiLSPLog with value hex $ff or decimal 255

Log files will be located in C:\Users\<User>\AppData\Local\Temp\DelphiLSP.

Make sure you include the log files with every bug report in the Quality Portal. They are extremely helpful to track down the cause of issues.

Note: Log files include source code when a unit is modified in the IDE.

Next

Refactoring