Your project has structure. Guaranteed. You might not always have complete control over the structure, but it's there, and you have to work with it.
In this chapter, we cover the visual components that Visual Studio Code has to help you navigate through your files and folders. This includes a detailed look at the Explorer, search functionality, and how you can tailor these to your liking.
The purpose of the Explorer is to let you view and navigate through a tree view that contains the files and folders that make up your project. It looks similar to Figure 3-1.
To make the Explorer visible, you can click the Explorer icon in the Activity Bar (on the left of Figure 3-1), use the View ➪ Explorer menu option, or use a keyboard shortcut (defaulting to Ctrl+Shift+E or Cmd+Shift+E).
The Explorer consists of three sections. At the top is a list of open editors and editor groups. In the middle are the files and folders of your project. At the bottom, an outline of the currently active editor is displayed (assuming that the current editor supports the concept of an outline). The sections can be hidden or displayed by right‐clicking at the top of the Explorer and checking (or unchecking) the context item related to the section. Because both the Open Editors and Outline sections of the Explorer deal with editing files, they are described in Chapter 4, “Editing Code in the Language of Your Choice.”
The middle section of the Explorer (shown in Figure 3-2) contains a view of the files and folders that make up the current project. For the most part, the functionality found in the Project View mimics similar views in other editing tools. Folders are shown in a hierarchical manner. Clicking a folder opens it, displaying the files that are found inside. A second click on the folder closes the tree node, hiding all of the contained files.
To preview a file, click it. This opens the file in a tab within the currently active editor group. If you click a second file, it replaces the first file in the same tab. In other words, one tab is used to preview any file. That tab can be repositioned within the editor group, just like any other tab. If you've made a change to the first file, clicking a second file will create a different tab to be used to preview files.
The Project View area also provides a number of both common and uncommon features for a file and folder view. First, the common features:
These are features that are to be expected from almost any file and folder view. Where Visual Studio Code starts to go beyond the common features starts with a couple of settings that can modify the flow of some of these features to make them more to your liking. The Explorer: Enable Drag and Drop setting determines whether you can move files or folders by dragging them. The Explorer: Confirm Drag and Drop setting determines whether you are prompted for a confirmation before moving the files or folders, and the Explorer: Confirm Delete setting is used to specify whether a deletion requires confirmation before deleting the selection.
You can select one or more files from the Project View and drag them onto an editor group. It will add editor tabs for those files in the group. If any of the files are already in the group, nothing happens to that tab.
From the context menu shown in Figure 3-3, you can choose from the following options.
Open to the Side—With one or more files selected, you can open them to the side. This will create a new editor group to the right of the currently active editor group and add the files to that new group.
Reveal in Explorer—With one or more files selected, this option opens your local file system explorer to the directory in which the file(s) are found. If the selected files are in more than one directory, multiple instances of the explorer will open, one per directory.
Open In Terminal—With one or more files selected, this option launches the Terminal window with the initial directory set to the directory from the last selected file. The Terminal window is covered in detail in the “Terminal Window” section in Chapter 2, “Exploring the User Interface.”
From the Explorer, it's possible to compare two files. The options that appear on the context menu are related directly to the number of files that you have selected. But to put the options into context, realize that the intent is to allow you to choose two files and compare them. The options are:
Select for Compare—This option is used to choose the first file in the comparison. If you have not selected a file for comparison previously and have only one file selected, it will be the only option available. Selecting this option chooses the selected file as the first file in the comparison. If there is already a first file, then this option replaces that file with the selected file.
Compare with Selected—This option is used to choose the second file in the comparison and to start the comparison. The option is available when you have one file selected and a first file in the comparison has already been chosen. It is also available when you have two files selected, in which case the comparison is performed on the selected files regardless of whether you had previously specified a file for comparison.
If you have more than two files selected, the context menu shows both the Select For Compare and Compare With Selected options. Choosing Compare With Selected will start the comparison between the file that had previously been selected for comparison and the first of the selected files. The Select For Compare option compares the last of the selected files to the file that had previously been selected.
That's a lot of different options and a lot of different ways that things work within these two context menu options. As was mentioned at the outset, keep in mind that the goal of the file comparison is to have you choose two files to compare. That viewpoint helps a great deal with answering any questions that arise.
The context menu has a number of other useful features. To add files and folders to your project, select an existing folder and right‐click. At the top of the context menu are two options: New File and New Folder (see Figure 3-4). If you select a file, neither of these options will be available.
For those readers who might be familiar with adding new items in Visual Studio, the Visual Studio Code experience is quite different. Mostly, it just consists of entering a filename.
At the location where you selected the folder, a new item is added and a text box is visible where you can enter the name of the new item (see Figure 3-5). You just type the name of the new item, including the file extension.
The file extension is necessary because you don't specify the file type at any point in this process. There's no wizard that presents you with a list of possible file types and lets you select one. It's the file extension that is used to determine the file type and, of greater importance, the editor that will be used to edit the file. Because useful functionality like syntax coloring and IntelliSense are tied to the file editor, the file extension matters.
Along with impacting the editor for the file, the file extension also drives suggestions from Visual Studio Code. After you've added a new file, Visual Studio Code checks to see if you have installed any extensions to support the file type. If you haven't, Visual Studio Code checks the Marketplace for extensions that might be available. If one (or more) is found, a message indicating such appears at the bottom right of the interface (see Figure 3-6).
Naturally, you don't need to install the extension to be able to use the file. The most common file types already have editors that ship with Visual Studio Code. However, a lot of useful features are provided by the extensions, so it might be worth a look, especially for more esoteric file types. You'll find a more detailed look at how to use Extensions in Chapter 9, “Working with Extensions.”
Along with the context menu, the Explorer also has a collection of icons at the right of the title area (see Figure 3-7). These icons are only visible when you mouse over the heading bar.
The four icons that are visible give quick access to four common functions. The two on the left are used to create a new folder and a new file, respectively. They are used if you are trying to create a file or folder at the top level of the folder you have open. The third icon (the arrowed circle) causes the Explorer to be refreshed based on the current files in the folder. The last button (at the right) is used to collapse all the nodes in the Explorer tree.
Speaking of the files and folders that appear in the Explorer, there is a setting that controls which ones are visible. The Files: Exclude setting is a collection of glob patterns. If a file matches one of the patterns, it is not visible in the Explorer. You can find a description of glob patterns in the “Problems Window” section in Chapter 2.
Aside from evoking the impression of curtains, blinds, and knick‐knacks, decorations in the Visual Studio Code Explorer provide a useful notification role. As you edit code, the files themselves enter different states—they could contain errors, they could have been changed since the last commit, or they might be untracked. Through decorations, Explorer can notify the developer of these conditions.
Figure 3-8 shows the Explorer with a number of different decorations visible on the right side. In general, the decorations come in two varieties. The circles are found at the folder level and indicate that, inside the folder, there is at least one file that is of interest. The color of the circle indicates the type of issue: pale red indicates an error, light gold means that files have been modified, and green is for files that are not currently being tracked by source control.
In Figure 3-8, the .vscode
folder contains an untracked file. Both the src
and actions
folders contain a file with an error, and the containers
folder contains modified files. Note that the actions
folder also contains modified files, but the circle's color is pale red. That's because if there are multiple reasons for decorations in a folder, the color is provided by the most serious of the reasons.
Next to the settings.json
file, there is a green “U”. This indicates that the file is not currently being tracked by source control. Naturally, this decoration only appears if your workspace is currently associated with source control of some kind.
The authenticate.js
file is a source for a couple of decorations. The numeric value (a “1” in this example) indicates the number of problems found in the file. This value (and the associated color) only appears if the offending file is open in one of the editors. The “M” at the end of the line means that the file has been modified and saved. You can see that a number of other files in the actions
folder have also been modified, but have no errors in them. If you want to see a description of the reason for the decoration, hover over the file or folder and a tooltip appears (Figure 3-9).
Although it hasn't been talked about much to this point, when you are operating within the Explorer, you are working in a workspace. This is the name given to the collection of files and folders that are being worked on. It's not necessary that you are even aware of workspaces—for many developers, just opening a file or a folder and making changes is all there is to it. However, there are benefits to using workspaces, especially if you're working with multiple folders simultaneously.
Although every opened folder is part of a workspace, none of the benefits start accruing until you save the workspace. To save the workspace, choose the File ➪ Save Workspace As menu option. In the dialog that appears, you determine where you want to save a file with an extension of .code‐workspace
.
The .code‐workspace
file is not a project file or a solution file—it's just another configuration file. The schema for the configuration file is pretty straightforward, as shown in the following code snippet:
{
"folders": [
{
"name": "PWA",
"path": "aos-pwa"
},
{
"name": "API",
"path": "AosPwa.WebAPI"
}
],
"settings": {
"workbench.quickOpen.closeOnFocusLost": false
}
}
At the top level, there are two attributes in the example. The first, named folders
, is an array of the folders in the workspace. The path
attribute defines the path to the folder to be opened. The path is relative to the location of the .code‐workspace
file. The name
attribute provides a different title that gets displayed. Figure 3-10 illustrates what the Explorer looks like with the name
attribute set.
As you can see, the name (PWA or API) appears at the top of each folder. If you don't include a name
attribute, the name of the folder is used. That name
attribute is really there to provide a more user‐friendly version of the folder name. As well, the name given to the .code‐workspace
file is the name of the workspace and it appears at the top of the files and folders tree. In Figure 3-10, the name of the workspace is “AOS‐MOBILE.”
You can add folders to a workspace even before you have saved the workspace. You do this through the File ➪ Add Folder To Workspace menu option. The standard Open File dialog appears, and you choose a folder to add.
Also, Visual Studio Code supports adding a folder by dragging it from your local file explorer and dropping it into the Explorer area. You can add multiple folders by selecting multiple folders from the Open File dialog, or by dragging multiple folders into the Explorer.
If you have not yet saved your workspace, the name of the workspace is “UNTITLED WORKSPACE.” This will change as soon as you save the workspace under a different name.
You might have noticed that the workspace configuration file also includes a settings
attribute. Those are the workspace‐level settings. But it's also possible to define settings that apply not just to the user and workspace, but also to the individual folders. The Settings editor includes this capability. Figure 3-11 illustrates the user interface for the AOS‐MOBILE workspace.
To access the folder‐level settings, select the folder from the drop‐down list and edit the settings as usual.
One of the considerations from folder‐level settings is how to handle collisions. That is, how does Visual Studio Code deal with the same setting being given different values by a user, workspace, or folder? For many settings, it's not a problem. The folder takes precedence. However, there are cases where this isn't possible. In particular, if there are settings that impact the entire development environment (such as the editor layout or zoom level), the folder‐level setting is ignored. Settings that are ignored are grayed out when you are editing from within Visual Studio Code.
The file and folder search functionality in Visual Studio Code is available through a pane that, by default, replaces the Explorer in the interface. You launch search using a couple of different mechanisms. You can click the Search icon in the Activity Bar, you can use the Ctrl+Shift+F keyboard shortcut, or you can use the View ➪ Search menu option. The basic search interface is shown in Figure 3-12.
The functionality is relatively straightforward: type the term you want to search for into the text box and hit Enter. The list that appears below the text box (shown in Figure 3-13) shows the results.
The results are structured as a tree. Each file that contains at least one match appears as a node. The name of the file appears, along with the containing folder, relative to the top folder in your project. To the far right of the node, a number indicates the number of times the search term was found in the file. If you want to remove a file from the results, hover your mouse over the node and the number of results is replaced with an X. Clicking that X dismisses the results found in that file from the display.
The children for each file are the lines within the file where the search term is found. The tooltip for the line (visible on a mouse hover) is the entire line of code—actually, just the first 80 characters or so, but enough so that you can tell the details of the line.
Each line can be used to open the corresponding file in the editor and position the cursor on that line. A setting called Workbench ➪ List: Open Mode controls what you need to do to accomplish this. Two options are available. With Single Click, which is the default value, all that is required to open the file is to click it once. The other choice, Double Click, means that a double‐click is required to open the file. This setting controls not only how the action on the Search results lines works, but also how actions on any tree or list work.
The functionality of the Search pane is not unlike many other searches. But Visual Studio Code also includes some advanced features, which are quite handy as you're trying to reduce the number of lines returned, or if you're just looking for a different combination of terms.
On the right side of the Search text box are three icons. The icon on the left is used to control whether the search is performed in a case‐sensitive or case‐insensitive manner. The default is case insensitive. When case sensitivity is selected, the icon is outlined by a box. Clicking the icon toggles the sensitivity on and off.
The second icon is used to toggle whole word matching. When selected (the icon is outlined in a box), only instances where the search term is an entire word are included. The default is not to use whole words, and clicking the icon toggles the setting.
The third icon toggles on and off the regular expression pattern‐matching functionality. The details are described in the next section.
You can do more than just match the term you enter into the Search text box. You can provide a regular expression that is then processed against each line in each file. To turn this functionality on, click the icon to the extreme right of the Search text box. At this point, the entered term must be a valid regular expression in order for the search to function. Consider the examples in Table 3-1 of how regular expressions can be a powerful addition to your toolset.
TABLE 3-1: Regular Expression Examples
EXPRESSION | RESULT |
Status ?Code | Matches either “Status Code” or “StatusCode,” as the space is marked as an option character. |
Stat.*Code | The “.*” combination matches one or more characters, so this covers StatCode and StatusCode. |
[sS]tatusCode | Matches both the Pascal and Camel case versions of StatusCode. |
You have available to you the full range of regular expression control characters. Though it can be difficult to learn, the ability to utilize regular expressions makes searching even more powerful.
By default, all of the files in your project are included in the search. To be a little more precise, there are two settings that control the files that are searched. The Files: Exclude setting (described in the “Project View” section earlier in this chapter) controls the files that appear in your project by identifying (using a collection of glob patterns) the files and folders that are excluded. As you might expect, these files are not included in the search.
However, there is also a setting called Search: Exclude that defines (also through a collection of glob patterns) the set of files that, while they are part of the project, you do not want to be part of any search results. An example of the kinds of files you'd like to exclude from your search would be the node_modules
directory or the bower_components
directory. These would be third‐party components that are part of your project but are not really something you are likely to want to search.
Both of those settings are part of the default functionality of the Search pane. But you can modify the files being searched without modifying the settings. If you click the ellipsis immediately below the Regular Expression toggle icon, a couple of additional text boxes become visible, as shown in Figure 3-14.
The top text box is used to specify the files that you'd like to include in your search results. For instance, if you enter the word reduce, only files or folders that contain the text “reduce” appear in the search results.
Aside from entering text, some special characters can be used to perform pattern matching. Table 3-2 contains these characters and their purpose.
TABLE 3-2: File Include Patterns
PATTERN | DESCRIPTION |
* | Matches one or more characters in the filename or path. |
? | Matches a single character in the filename or path. |
** | Matches any number of path segments in folder path |
{} | Contains a comma‐separated list of patterns to match. |
[] | Defines a range of alphanumeric characters to match. |
The second text box is used to define the files and folders to be excluded from the search. The same rules as defined for the Files To Include text box apply. The difference is what Visual Studio Code does with the information. Here, files and folders that match the pattern are not included in the search results, even though they might normally be based on the Search settings. The gear icon at the right of the text box is used to toggle the exclusion pattern matching on and off.
It's possible to replace text through the Search pane. To start the process, click the triangle to the left of the Search text box to reveal a second text box (see Figure 3-15).
In the second text box, enter the text that you'd like to replace the text in the first text box with. As you type, you can see the replacement take place in the search results. Figure 3-16 illustrates what this would look like.
Hitting Enter at this point performs the replacement on the first instance. Clicking the icon to the right of the second text box performs the replacement on all of the instances throughout all of the files in the search result.
A number of additional settings impact where and how the results can be viewed, as well as the results that actually appear:
.gitignore
and .ignore
, that are regularly used to specify files that are, well, ignored by different processes. This setting determines if the Search will also ignore those files.The folders and files that make up your project are an important part of the development process. In this chapter, you saw how you can use the Explorer and Search panes in Visual Studio Code to your advantage as you navigate through your application. Using the tools that are available to you can help improve your flow. Finding code, replacing text, and easily recognizing the changes you made are important parts of that flow.
18.117.186.92