Add Local Variable

The Add Local Variable refactoring is used to declare a local variable while you’re entering code in a method or procedure. The refactoring picks up the word at cursor position and determines the variable’s type based on history and context. An Add Local Variable dialog is used to optionally change the suggested type.

An example

Suppose you have a method TSample.DoSomething like this and need a local variable S: string.


Rather than manually adding the local variable, press Ctrl+L (default keyboard shortcut) to invoke the Add Local Variable refactoring:

Add Local Variable Dialog

The Add Local Variable dialog shows the picked up identifier “S” and “string” type (here based on history).
Just hitting the enter key is enough to accept the suggestion and actually add the variable declaration
without scrolling.


In effect you need just two keystrokes to add a local variable: Ctrl+L to invoke the dialog and Enter to accept the suggested type.

If you need to change the suggested type, the type selector has incremental search and just
pressing the “i” key changes the type to “Integer”, pressing the “b” key changes the type to “Boolean”.
The presets in the type selector are customizable.

Advanced Variable Detection

Rather than just looking for the word at cursor position, Add Local Var looks for assignment statements Identifier := Initializer on a cursor line.
It then picks up the variable name as the identifier that is being assigned to.
For example, if you type any of the following:


and invoke Add Local Variable, the identifier “List” is suggested as variable, rather than the word at the cursor.

This saves moving the cursor back to the variable before invoking Add Local Variable.
Only if there’s a single identifier before the := (assignment) this is assumed the variable.
In all other cases the word at cursor position is used as variable name.

In addition, the for loop is also recognized:


Pressing Ctrl+L anywhere in this line suggests to add the loop control variable “I”.

In situations like this


“IsFound” will be suggested as local variable, regardless of the cursor position because it’s left of the assignment := .

This makes it impossible to add a local “Index” To avoid this, there’s an escape: if the selection is non-empty, the selection is used as variable name rather than the identifier before the assignment.

How the suggested type is determined

Context

The current line is scanned for code like “typename.Create” and “as typename” to suggest a type.
For example, invoked on “Stream” in the example below…


Add Local Variable auto suggests “TMemoryStream” which results in:


String literals and True|False are also recognized. For example:


Hungarian Type Prefix

If enabled, a user definable Hungarian type prefix lookup list is used
to suggest a type based on case sensitive prefix. For example, with a prefix lookup defined as:


… a variable “sCaption” would be suggested as string, szCaption as PChar and dValue as Double etc.

History

If you – like many others – always use the same name for a certain type of
variable such as S: string; or Doc: TXMLDocument, that name will be suggested next time you add a local variable.