Skip to end of metadata
Go to start of metadata

Redirection Notice

Icon
This page will redirect to https://www.jetbrains.com/help/phpstorm/using-code-editor.html in about 2 seconds.

 

One of the things that happen a lot in an IDE is writing code. We do this in the editor. In this tutorial, we will explore the editor and some of the basic actions and navigation features available.

Left and right Gutter

When opening a file containing code, whether HTML, JavaScript, PHP or any of the other languages supported by PhpStorm, a new editor tab will be opened. In each tab, there are a few logical areas in which specific information is available or where certain actions can be performed.

On the left, there's a gutter which provides additional information about our code using symbols for breakpoints, bookmarks, code folding, navigation and so on.

The right-hand side of the editor may display warnings and errors about our code. A green square would indicate no warnings or errors have been found in this class by PhpStorm's code inspections. Yellow would mean one or more warnings have been found. Red would mean there are errors in the file.

Automatic Code Inspection and Quick Fixes

The IDE inspects our code while we're writing it using several rules, or so-called Inspections. PhpStorm contains many inspections for all supported languages.

Warnings and errors are shown in the right gutter. We can hover the colored markers and see which inspection triggered the warning and why.

In this case, we're using an undefined class Person, and we also declared a variable $x which is not used yet. We can see the warning is visible in our code as well: the undefined class is marked in yellow, the missing variable is greyed out.

Some inspections also provide quick fixes: pressing Alt+Enter when the curor is placed on the relevant piece of code may show a list of possible solutions to the warning or error, and provide an automatic means of fixing it.

Icon

Not all inspections are enabled by default. Maybe we want to disable a certain inspection rule for our application. We can view and enable or disable inspections through the Project Settings | Inspections.

Working with Tabs

Each file is opened in a separate tab. When multiple tabs are opened, we can pin tabs and make sure they are not automatically closed by the IDE when a lot of them are open.

Closing a tab can be done by clicking the tiny x on each tab or by using the keyboard shortcut Ctrl+F4 (or CMD+F4 on Mac OS X).

We can switch between tabs by clicking them or pressing Alt+Left / Right (or Ctrl+Left / Right on Mac OS X). The tab switcher (Ctrl+Tab) lets us switch easily between tabs and tool windows.

Icon

We can customize the behavior of editor tabs in IDE Settings | Editor | Editor Tabs. For example, we can configure how many tabs can be opened at a time, as well as their appearance, position, closing policy and other options.

Splitting and Detaching Editor Tabs

Some application types require working on several files at once. We may want to have all files visible at the same time, too. Using the context menu, we can split editors horizontally and/or vertically.

When we have a big screen to work with, or multiple screens, we can also detach editors. To detach an editor tab, drag it outside of the main window. The contents of the editor tab is opened in a separate frame. To attach an editor tab, drag it from its frame and drop into the main IDE frame until the tab name appears again.

Icon

Tabs can be dragged between split panes as well.

Favorite Tabs

Editors can be added to a favorites list so they can easily be opened again. This is very handy when there are several files that we have to open a lot during our development. Adding a tab to the favorites list has to be done using the context menu. We can jump to the favorites tool window pressing Alt+2 (or CMD+2 on Mac OS X).

Navigating between Tabs

We can navigation to the next/previous tab by right-clicking the current editor tab and using the Select Next/Previous Tab from the context menu, or by pressing Alt+Right / Ctrl+Right (CMD+Shift+] on Mac OS X) or Alt+Left / Ctrl+Left (CMD+Shift+[ on Mac OS X). The focus will move to the next editor tab to the right / to the left of the active one.

Working with Bookmarks

Where #Favorite Tabs list a group of files and allows for easy navigation, bookmarks can be used to naigate quickly to a given line of code.

Pressing F11 when the cursor is on a line of code toggles it as a bookmark. The left gutter will display if a specific line of code is bookmarked or not.

Using Shift+F11, we can navigate between the bookmarks and get a preview of where the line of code is in the file.

An unlimited amount of anonymous bookmarks can be created. We can also assign a mnemonic to a bookmark: a number or literal can be assigned to it on the left gutter. Ctrl+F11 (or the Edit | Toggle Bookmark With Mnemonic menu) lets us assign a number or literal (0-9 or A-Z).

To navigate between numbered bookmarks within a project, press Ctrl+Number to navigate to the desired bookmark.

Navigating through a Project

One of the things where PhpStorm shines is navigating through code bases. Because the IDE works based on a project and knows about all files, types and symbols, it's easy for the IDE to provide a way to navigate between them.

Icon

Have a look at the Navigating through a PhpStorm Project tutorial for more advanced techniques of navigating through a project and codebase.

Navigating in the Editor

In the editor, we can hover over any variable or class and Ctrl+Click (or CMD+Click on Mac OS X) to navigate to the declaration of the construct. The same can be achieved without the mouse, too: place the cursor on the usage of the class and press _*Ctrl+B (or CMD+B on Mac OS X).

We can jump between functions in a file using the Alt+Down / Alt+Up (or Ctrl+Down / Ctrl+Up on Mac OS X) shortcuts. Navigate to the Last Edit Location using Ctrl+Shift+Backspace (Shift+CMD+Delete on Mac OS X).

When warnings or errors are found in our file, we can navigate between them using the F2 (next) and Shift+F2 (previous) keyboard shortcuts.

Navigating to Class, File, Symbol or Everywhere

We can quickly navigate to classes, files and other symbols using several keyboard shortcuts:

  • Navigate to Class by pressing Ctrl+N (CMD+O on Mac OS X)
  • Navigate to File by pressing Ctrl+Shift+N (Shift+Cmd+O on Mac OS X)
  • Navigate to Symbol by pressing Ctrl+Shift+Alt+N (Alt+CMD+O on Mac OS X)

These all bring up a similar search window in which we can type (part of) the item we are looking for. Using the arrow keys and the Enter key, we can navigate to the selected item.

The text to search for can be a full name, part of a name, can contain wildcards (*) and CamelHumps (DC will search for *D*emo*C*ontroller, for example).

Search Everywhere (Double Shift) works similar, but as its name implies: it searches everywhere, including classes, files, symbols and even menu items from the IDE.

Icon

Many other navigation actions are available, do check the Navigate menu to explore the options.

The Project Tool Window and the Navigation Bar

All files and libraries used by our project will be shown in the Project Tool Window (Alt+1 or CMD+1 on Mac OS X). We can navigate through it using the arrow keys, or by starting to type to search through it.

The Navigation bar will show us the full path where a file is located within our project. Alt+Home (CMD+Up on Mac OS X) will bring us there. Using the arrow keys, we can quickly navigate elsewhere in the project structure, too.

Working with Code

Creating an application holds a fair amount of coding. What was the name of that class or function again? The IDE offers code completion to find out. Often we're creating repetitive code that could easily be generated. Or worse, we're using a copy-paste-adapt approach where in some cases, we forget to adapt the code resulting in an overlooked bug. And what if our code structure isn't what we want? Do we copy-paste again? The answer to all this: generating code and refactoring, two things PhpStorm provides us.

Code Completion

While writing code, PhpStorm provides us with completion information for classes, files, functions and even database table names and columns if we make use of the database tools. We can start typing and press Ctrl+Space to get completion.

Icon

Due to PHP's dynamic nature, sometimes the IDE will be unable to figure out exact type information. Using PHPDoc comments, we can hint the IDE with this information. For example, the following will tell PhpStorm that the $animal variable holds an Elephant type:

Of course, we could also annotate the getAnimal method:

Generating Code

Generating code can be done in several ways. First, we can use the context menu or Alt+Insert (CMD+N on Mac OS X) in the Project Tool Window or the Navigation Bar to generate a file from a template. Note the available templates can be modified through IDE Settings | File Templates. Some of the templates, like PHP Class will open a dialog where we can specify some additional code generation options.

We can also generate code within the editor. Pressing Alt+Insert (CMD+N on Mac OS X) in the editor will give us a list of all available code generation options.

Let's generate getters and setters. We can select the fields we want to generate them for.

When clicking OK, PhpStorm will generate the code for us, including PHPDoc blocks and so on.

Icon

Try the other code generation options, too.

Refactoring Code

Copy-paste and find-replace are great tools to move code around. But since the IDE knows about the types in our codebase, how they relate to each other and what every line of code means, PhpStorm can do a better and faster job for many refactoring scenarios.

For example, let's extract the hard-coded string here to a function determineViewName. We can select the text and use the Refactor context menu to do this, as well as any of the refactoring keyboard shortcuts.

Depending on the chosen refactoring, we will be able to provide some options. We can specify the function's visibility (private, protected, public), the name and parameters and so on. Depending on the refactorig and its complexity, a preview button will be available too. And if a refactoring does not make sense or will potentially break our code, the IDE will ask us politely f we want to carry on and warn about occurences that should be checked manually.

Clicking the Refactor button will carry out the refactoring. Code will be updated. In this case, in only one location, but when doing a rename of a function or class, for example, the IDE will update all references across the project.

Icon

Use the Refactor context menu to explore the various refactorings that are available.

 

  • No labels