|
| 1 | +# VSCode Tips |
| 2 | + |
| 3 | +Here are some tips for getting up to speed with VSCode as you progress through this course. |
| 4 | + |
| 5 | +## Some useful extensions |
| 6 | + |
| 7 | +VSCode can be extended with _extensions_. There are two that we recommend you install from day 1. |
| 8 | + |
| 9 | +**Spell Checker**. We fully understand that you guys sometimes have difficulty with the correct English spelling when choosing names for variables and functions in your JavaScript programs. That is nothing to be ashamed of, but why not get some help from a handy extension? |
| 10 | + |
| 11 | +**ESLint**. This extension can check your JavaScript code for obvious errors, such as undefined variables, unused variables, etc. |
| 12 | + |
| 13 | +### Installation instructions |
| 14 | + |
| 15 | +1. Start up VSCode. |
| 16 | + |
| 17 | +2. Press the last button in the area in left margin (called the Activity Bar), shown below: |
| 18 | + <br /> |
| 19 | + |
| 20 | +3. In the input field in the upper left corner, type `code spellchecker` as pictured here: |
| 21 | + |
| 22 | +  |
| 23 | + |
| 24 | + (The indication 122K in this picture means that this extension has been downloaded 122,000 times. The five star rating indicates users like it a lot.) |
| 25 | + |
| 26 | +4. Press the green `install` button of **Code Spellchecker**. |
| 27 | +5. When the installation has finished, install the ESLint extension in a similar fashion (1.2 million downloads!): |
| 28 | + |
| 29 | +  |
| 30 | + |
| 31 | +6. When this second extension has finished installing you will notice that the green `install` button changes to a blue `reload` button. But no need to press this button at this time (no harm done if you did). |
| 32 | +7. You now need to install a global Node package to support ESLint. Open a terminal window in VSCode by selecting **View**, **Integrated Terminal** from the menu bar. |
| 33 | +8. A terminal window opens in the lower half of the VSCode window. In this window, type the command below (on Linux and MacOS systems you may need to prefix this command with `sudo`, e.g. `sudo npm ...`): |
| 34 | + |
| 35 | + ``` |
| 36 | + npm install -g eslint |
| 37 | + ``` |
| 38 | + |
| 39 | + |
| 40 | +9. Once the installation has finished close VSCode for now. |
| 41 | + |
| 42 | +## Using VSCode for your homework |
| 43 | + |
| 44 | +You’ll get the most out of VSCode if you organise your work in folders, say a folder for each week in the JavaScript module. |
| 45 | + |
| 46 | +(Later in the course you will be “cloning” Git repositories into local folders as the basis for your homework or projects.) |
| 47 | + |
| 48 | +To start work with VSCode in particular folder, start VSCode and open the relevant folder: from the menu, select **File**, **Open Folder**. VSCode will now open this folder to be your "project folder", until you close VSCode or open another folder. |
| 49 | + |
| 50 | +Assuming that you will use the folder for JavaScript work, you need to create a special file inside of it as required by ESlint. |
| 51 | + |
| 52 | +From the menu, select **View**, **Command Palette…** and type `create` as show in the picture below. Select: **Create** '.eslintrc.json' File. |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +The `.eslintrc.json` file will now be created in the project folder. No need to touch this file at this time, it just needs to sit in your project folder. Later in the course you may wish to add additional "rules" to this file for more stringent code checking. |
| 57 | + |
| 58 | +## Creating your JavaScript file |
| 59 | + |
| 60 | +You are now ready to start adding your first JavaScript file. |
| 61 | + |
| 62 | +1. From the menu, select **File**, **New File**. This will create a new, empty file named "Untitled-1". |
| 63 | +2. Select **File**, **Save** from the menu and give your file a an appropriate name. Make sure that you give it an extension ".js" to make it a JavaScript file. |
| 64 | +3. Start entering your JavaScript code in the new file. Be on the watch out for green squirly underlines. These are warnings from either ESLint or the Spell Checker that something might be wrong. |
| 65 | +4. If you see green squirly underlines, hover your mouse pointer over the underlined text and a tooltip will appear that explains what might be wrong. |
| 66 | +5. You can also open the "problem" panel by selecting **View**, **Problems** from the menu to see any problems identified. |
| 67 | +6. Pay attention also to the lower left part of the VSCode window, i.e. the status bar. It gives an indication of the number of errors and warnings issued. In the picture below there are zero errors, 7 warnings (usually from ESLint) and 14 informational messages (usually from the spell checker). |
| 68 | + |
| 69 | +  |
| 70 | + |
| 71 | +## Some useful short-cut commands |
| 72 | + |
| 73 | +In the previous section we frequently referred you to the menu bar to select commands. As you get more proficient with VSCode you may want to inspect these menus a little closer and take note of the short-cut commands listed in their right margin. For example, the short-cut command for **File**, **New** is listed as Ctrl+N (press `Ctrl` and `N` keys simultaneously) on a Windows or Linux PC and ⌘N on a Mac. |
| 74 | + |
| 75 | +Here are some short-cut commands that you will use many times a day and that we recommend you familiarise yourself with from day 1: |
| 76 | + |
| 77 | +| Operation | Windows | Mac | Linux | |
| 78 | +| --------- | ------- | ----- | ----- | |
| 79 | +| **Format Document** (make it pretty) | Shift‑Alt‑F | ⇧⌥F| Ctrl‑Shift‑I | |
| 80 | +| **Search** (Find)| Ctrl+F | ⌘F | Ctrl+F | |
| 81 | +| **Replace** (Find and replace) | Ctrl+H | ⌥⌘F | Ctrl+H | |
| 82 | +| **Rename Symbol** (change all names in file to a different name) | F2 | F2 | F2 | |
| 83 | +| Open an **Integrated Terminal** window in VSCode | Ctrl+' | ⌃\` | Ctrl+' | |
| 84 | + |
| 85 | +- **Format Document**. This command reformats your JavaScript file in a generally accepted standard format, using proper indenting, proper use of spaces, placing of curly braces and more. A neatly formatted document helps you to better understand your own code and your teachers, mentors and fellow students will love your for it too when they review your work. |
| 86 | + |
| 87 | + *With VSCode at your finger tips there is no longer any excuse for submitting poorly formatted homework!* |
| 88 | + |
| 89 | +- **Search**. Search for specified text. |
| 90 | +- **Replace**. Replace specified text by some other text. |
| 91 | + |
| 92 | + In the figure below the **Replace** pop-up window is shown. The **Search** pop-up is similar, but with one input field only. |
| 93 | + |
| 94 | +  |
| 95 | + |
| 96 | + - The `Aa` button activates the **Match Case** option. |
| 97 | + - The `Ab|` button matches **Whole Words Only**. |
| 98 | + - The `.*` button allow you to search using _regular expressions_, which you may encounter in later modules as an advanced JavaScript programming topic. |
| 99 | + - The `c-b` button next to the second input field replaces the next occurrence of the matched text. |
| 100 | + - The `ab-ac` button replaces **all** occurrences of the matched text. |
| 101 | + - The left and right arrows move the cursor to the previous and next match. |
| 102 | + - To get rid of the pop-up press `Esc` or press the `x` button. |
| 103 | + |
| 104 | +- **Rename Symbol**. This command renames all occurrences of a JavaScript variable or function name. To do so, move the text cursor to the variable or function name and press F2. A small pop-up window will appear in which you can type a new name. Press Enter to finalise the change or Esc to cancel it. |
| 105 | + |
| 106 | +- **Open an Integrated Terminal window**. We already covered this when we mentioned the **View**, **Integrated Terminal** menu command. |
| 107 | + |
| 108 | +## Running and debugging your code with the VSCode Node debugger |
| 109 | + |
| 110 | +If your JavaScript program is contained in a single file the easiest way to run and examine your code is the start straight in VSCode. Before you can run it you need to do a small preparation. This needs to be done only once per project folder. |
| 111 | + |
| 112 | +1. As illustrated in the figure below, press the **Debug** button in the left margin as indicated by the red triangle. |
| 113 | + |
| 114 | +  |
| 115 | + |
| 116 | +2. You'll see the Debugger Pane in the left hand side of the screen as shown below. Press the "cog" button as indicated by the red triangle. |
| 117 | + |
| 118 | +  |
| 119 | + |
| 120 | +3. Next you will prompted to select an environment. Choose `Node.js`. This produces a JSON file, displayed in a new editor tab with a contents as shown below. |
| 121 | + |
| 122 | + ``` |
| 123 | + { |
| 124 | + // Use IntelliSense to learn about possible Node.js debug attributes. |
| 125 | + // Hover to view descriptions of existing attributes. |
| 126 | + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 |
| 127 | + "version": "0.2.0", |
| 128 | + "configurations": [ |
| 129 | + { |
| 130 | + "type": "node", |
| 131 | + "request": "launch", |
| 132 | + "name": "Launch Program", |
| 133 | + "program": "${file}" |
| 134 | + } |
| 135 | + ] |
| 136 | + } |
| 137 | + ``` |
| 138 | + |
| 139 | + At this his time no changes are necessary to this file. You can just close the editor tab. |
| 140 | + |
| 141 | +4. Next, press the **Explorer** button in the left margin as indicated by the red triangle in the figure below. |
| 142 | + |
| 143 | +  |
| 144 | + |
| 145 | +5. Observe that VSCode has created a `.vscode` subfolder in your project folder where it keeps the JSON file just created and potentially other VSCode settings. |
| 146 | +6. You can now run your code in the debugger. Switch to the editor tab contains your program and press the green triangular button or, alternatively, press F5. |
| 147 | +7. Note that when your program finishes execution the debugger is still active. You can stop it by pressing the square red button. |
| 148 | + |
| 149 | +If you do not really need to debug your program (e.g., by placing breakpoints) etc, you may prefer to run your program in a terminal window. You can open an Integrated Terminal as explained earlier and type: |
| 150 | + |
| 151 | +``` |
| 152 | +node prog.js |
| 153 | +``` |
| 154 | + |
| 155 | +Replace `prog.js` with the actual file name of your program. |
| 156 | + |
| 157 | +If you want to run your program in terminal window independent of VSCode you should make sure you are in the same directory as the program you want to run. |
| 158 | + |
| 159 | + |
| 160 | +### Placing break points and inspecting variables |
| 161 | + |
| 162 | +This is covered in class. |
| 163 | + |
| 164 | +### Further information |
| 165 | + |
| 166 | +Please note that VSCode is actively being developed. At present there is a monthly release cycle, so don't be surprised when you are prompted once a month to update to the latest version. We advise you to update when prompted (naturally, not when you are in the middle something that you don't want interrupted). |
| 167 | + |
| 168 | +You can find detailed information about VSCode at the [VSCode web site](https://code.visualstudio.com/docs). |
0 commit comments