Auto-Completing Code and Paths
This section covers various techniques of context-aware code completion that allow you to speed up the coding process.
Basic code completion. Completing names and keywords
Basic code completion helps you complete names of classes, methods, and keywords within the visibility scope. When you invoke code completion, PhpStorm analyses the context and suggests the choices that are reachable from the current caret position.
Code completion covers supported and custom file types. However, PhpStorm does not recognize the structure of custom file types and suggests completion options regardless of whether a specific type is appropriate in the current context.
If basic code completion is applied to part of a parameter, or a variable declaration, PhpStorm suggests a list of possible names depending on the item type.
To use basic code completion:
- Start typing a name.
- Press Ctrl+Space or choose Code | Completion | Basic from the main menu.
The image below shows a suggestion list containing a variable name:
The image below shows a suggestion list containing keywords:
- If necessary, press Ctrl+Space for the second time (this action produces the same effect as pressing Ctrl+Alt+Space).
Smart code completion. Completing code based on type information
Smart code completion filters the suggestions list and shows only the types applicable to the current context.
To use smart code completion:
Completing statements
The Smart Enter completion enables you to create syntactically correct code constructs.
To automatically complete a statement, start typing it and press N/A. The punctuation required in the current context is added and the caret moves to the next editing position.
- Completing code constructs: start typing a code construct and press N/A:
PhpStorm automatically completes the construct, and the caret is placed at the next editing position:
- : PhpStorm automatically encapsulates a method call when you directly type a new method call next to it. and then type the method call. When
|"test"
println
gets the focus in the suggestion list, select it with N/A: The resulting code will look like the following:System.out.println("test");
Completing tag names
PhpStorm automatically completes tags and attributes names and values in the following file types:
Automatic tag name completion is based on the DTD or Schema the file is associated with.
If there is no schema association, PhpStorm will use the file content (tag and attribute names and their values) to complete your input.
In XML/XSL files, completion for taglibs and namespaces is available.
Completing tag names
- Press < and start typing the tag name. PhpStorm displays the list of tag names appropriate in the current context. Use the ArrowUp and ArrowDown buttons to scroll through the list.
- Press Enter to accept a selection from the list.
PhpStorm automatically inserts the mandatory attributes according to the schema.
Hippie completion. Expanding words based on context
Hippie completion is a completion engine that analyses your text in the visible scope and draws its completion proposals from the current context. It helps you complete a word with a keyword, class name, method or variable.
To expand a string at caret to an existing word, do the following:
Postfix code completion
Postfix code completion helps you reduce backward caret jumps as you write code. It allows you to transform an already typed expression to a different one based on a postfix you type after a dot, the type of expression, and its context.
For example, the .if
postfix applied to an expression wraps it with an if
statement.
To enable/disable the postfix completion feature or separate templates, in the Settings / Preferences Dialog dialog, go to Editor | General | Postifx completion. You can also choose which key you want to use to expand postfix templates: Tab, Space, or Enter.
Postfix completion suggestions are shown as part of the basic completion suggestions list. To see a full list of postfix completions applicable in the current context, press Ctrl+J.
Completion tips and tricks
Narrow down the suggestions list
You can narrow down the suggestions list by typing any part of a word (even characters from somewhere in the middle), or invoking code completion after a dot separator. PhpStorm will show suggestions that include the characters you've entered in any positions.
This makes the use of wildcards unnecessary:
In case of CamelCase or snake_case names, type the initial letters only. PhpStorm automatically recognizes and matches the initial letters.
View reference information
- You can use the Quick Definition View by pressing Ctrl+Shift+I when you select an entry in the suggestions list:
- You can use the Quick Information View by pressing Ctrl+Q when you select an entry in the suggestions list:
Sort entries in the suggestions list
You can sort the suggestions list alphabetically or by relevance. To toggle between these modes, click or respectively in the lower-right corner of the list.
PhpStorm will remember your choice. You can change the default behavior in the Code Completion settings page.
View code hierarchy
You can view code hierarchy when you've selected an entry from the suggestions list:
Completing a path
This type of completion aims at speeding up selection of files and folders. The dialog used for that purpose is called rather often, for example, when you specify the installation folder of an external tool or a Version Control engine or configure a PHP interpreter.
- Open the Select Path dialog by choosing on the menu or by clicking the Browse button next to the field where a path is required.
- Make sure that the Path field is visible. If the field is hidden, click the Show path link.
- Start typing a path. PhpStorm suggests a pop-up list where you can quickly find the needed directory: As you type, the suggestion list shrinks to show the matching paths only: You can also press Ctrl+Space to show the suggestion list.
- Select the relevant suggestion from the list.