Skip to content

Commit ad0aa1c

Browse files
authored
Merge pull request #91 from QuantEcon/vscode-edits
edits for VScode lecture
2 parents 56ed467 + 339731f commit ad0aa1c

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

lectures/software_engineering/tools_editors.md

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@ kernelspec:
2626

2727
While Jupyter notebooks are a great way to get started with the language, eventually you will want to use more powerful tools. Visual Studio Code (VS Code) in particular, is the most popular open source editor for programming - with a huge set of extensions and strong industry support.
2828

29-
While you can use source code control, run terminals and the REPL ("Read-Evaluate-Print Loop"), without VS Code, we will concentrate on using it as a full IDE for all of these features.
29+
While you can use source code control, run terminals and the REPL ("Read-Evaluate-Print Loop") without VS Code, we will concentrate on using it as a full IDE for all of these features.
3030

3131
## Installing VS Code
3232

3333
To install VS Code and the Julia Extension,
3434

3535
1. Follow the instructions for setting up Julia {ref}`on your local computer <jl_jupyterlocal>`.
3636
2. Install [VS Code](https://code.visualstudio.com/) for your platform and open it
37+
- On Windows, during install under `Select Additional Tasks`, choose all options that begin with `Add "Open with Code" action`. This lets you open VS Code from inside File Explorer folders directly.
3738
3. Install the [VS Code Julia](https://marketplace.visualstudio.com/items?itemName=julialang.language-julia) extension
3839
- After installation of VS Code, you should be able to choose `Install` on the webpage of any extensions and it will open on your desktop.
39-
- Otherwise, open the extensions with `<Ctrl-Shift-X>` or selecting extensions in the left-hand side of the VS Code window. Then search for `Julia` in the Marketplace
40+
- Otherwise, open the extensions with `<Ctrl-Shift-X>` or selecting extensions in the left-hand side of the VS Code window. Then search for `Julia` in the Marketplace.
4041

4142
See the [Julia VS Code Documentation](https://www.julia-vscode.org/docs/dev/gettingstarted/#Installation-and-Configuration-1) for more details.
4243

@@ -60,7 +61,7 @@ denote opening the command palette and searching for a command with things like
6061
your most recent and common commands.
6162
### Optional Extensions and Settings
6263

63-
Open the settings with `> Preferences: User Settings` (see above for opening the command palette with `<Ctrl-Shift-P>`).
64+
Open the settings with `> Preferences: Open User Settings` (see above for opening the command palette with `<Ctrl-Shift-P>`).
6465

6566
As a few optional suggestions for working with the settings,
6667

@@ -83,7 +84,7 @@ A key benefit of VS Code is that you can use terminals and command-line interfac
8384

8485
Furthermore, in the case of julia (and other languages such as python) this will activate the project automatically. See the [documentation](https://code.visualstudio.com/docs/editor/integrated-terminal) for more details.
8586

86-
To start a terminal, you can use the `View > Terminal` in the menus, type ``Ctrl+` ``, or click on the bottom bar in VS Code.
87+
You can open the terminal panel with `> View: Toggle Terminal` , typing ``Ctrl+` ``, or by clicking on the list of warnings and errors bottom bar in VS Code. If no existing terminal exists, it will create a new one.
8788

8889

8990
(vscode)=
@@ -118,7 +119,7 @@ Type some code as such `f(x) = x + 1` into the file, into the `.jl` file, save i
118119
:width: 100%
119120
```
120121

121-
At this point, the function is available for use within either the code or the REPL. You can get inline results by adding more code to the file and executing each line with `<Shift=Enter>`.
122+
At this point, the function is available for use within either the code or the REPL. You can get inline results by adding more code to the file and executing each line with `<Shift-Enter>`.
122123

123124
```{figure} /_static/figures/vscode_jl_function_2.png
124125
:width: 100%
@@ -138,6 +139,12 @@ Because the REPL and the files are synchronized, you can modify functions and si
138139

139140
Next we will go through simple use of the plotting and package management.
140141

142+
```{note}
143+
VS Code typically activates the current project correctly. However, when choosing to enter the package mode, if the prompt changes to `(@v1.6) pkg>` rather than `(hello_world) pkg >` then you will need to manually activate the project. In that case, ensure that you are in the correct location and choose `] activate .`.
144+
145+
You can always see the current package location and details with `] st`. See [Julia Environments](jl_packages) for more details.
146+
```
147+
141148
The REPL. First, type `]` to enter the package management mode, then `add Plots`. Depending on whether you have done similar operations before, this may download a lot of dependencies. See below for an example
142149

143150
```{figure} /_static/figures/vscode_package_added.png
@@ -152,6 +159,8 @@ Add code in the `.jl` file for a simple plot, and it will be shown on a separate
152159
:width: 100%
153160
```
154161

162+
To exit package management mode and return to the REPL, type `Ctrl+C`. To then go from the REPL back to the VS Code terminal, type `Ctrl+D`.
163+
155164
### Executing Files
156165

157166
First we will reorganize our file so that it is a set of functions with a call at the end rather than a script. Replace the code with
@@ -180,7 +189,7 @@ You can execute a `.jl` file in several ways.
180189

181190
Within a terminal, you can provide the path to the file. For example,
182191
```{code-block} none
183-
julia --threads --project auto hello.jl
192+
julia --threads auto --project hello.jl
184193
```
185194

186195
See the [REPL](repl_main) section for more details on the commandline options.
@@ -193,12 +202,16 @@ Alternatively, within VS Code itself you can use the `<Ctrl-F5>` to run the new
193202

194203
To debug your function, first click to the left of a line of code to create a breakpoint (seen as a red dot).
195204

196-
Next, use `<Ctrl-Shift-D>` or select the bug tab on the left in Julia to see
205+
Next, use `<Ctrl-Shift-D>` or select the run and debug tab on the left in Julia to see
197206
```{figure} /_static/figures/debugger_1.png
198207
:width: 100%
199208
```
200209

201-
Then choose the `Run and Debug` option and it will execute `plot_results()` at the bottom of the file, and then stop inside at the breakpoint.
210+
Then choose the `Run and Debug` option and it will execute `plot_results()` at the bottom of the file, and then stop inside at the breakpoint.
211+
212+
```{note}
213+
Some users may see other options like `Run active Julia file` instead of `Run and Debug` in the run and debug tab.
214+
```
202215

203216
```{figure} /_static/figures/debugger_2.png
204217
:width: 100%
@@ -226,9 +239,13 @@ There are several ways to start the REPL.
226239

227240
The command line options for starting Julia are set to decent defaults for terminals running within VS Code, but you will want to set them yourself if starting the REPL otherwise.
228241

229-
One common choice is to choose the number of threads that Julia should have available. By default it is only a single thread, while `--threads auto` will tell Julia to create one for each processor on your machine. VS Code uses `--threads auto` by default.
242+
As an example, the argument `--threads` determines the number of threads that Julia starts with. If starting `julia` on the command line without specifying any arguments, it will default to 1 (or check an environment variable). To have Julia automatically choose the number of threads based on the number of processors for your machine, pass the `--threads auto` argument.
243+
244+
```{note}
245+
VS Code sets the number of threads automatically based on the number of cores on your machine, but the value can be modified in its `> Preferences: Open User Settings` and then search for `Julia: Num Threads`.
246+
```
230247

231-
The most important choice is the `--project` toggle which determines whether you want to activate an existing project (or create a new one) when starting the interpreter. Since a key feature of Julia is to have fully reproducible environments, you will want to do this whenever possible.
248+
The most important choice is the `--project` toggle which determines whether you want to activate an existing project (or create a new one) when starting the interpreter. Since a key feature of Julia is to have fully reproducible environments, you will want to do this whenever possible.
232249

233250
```{note}
234251
A key difference between Julia and some other package managers is that it is capable of having different versions of each package for different projects - which ensures all projects are fully reproducible by you, your future self, and any other collaborators. While there is a global set of packages available (e.g. `IJulia.jl` to ensure Jupyter support) you should try to keep the packages in different projects separated. See the documentation on [environments](https://docs.julialang.org/en/v1/manual/code-loading/#Environments-1) and the [package manager](https://pkgdocs.julialang.org/v1/getting-started/) for more.
@@ -279,10 +296,10 @@ As we saw before, `]` brings you into package mode. Some of the key choices are
279296

280297
* `] instantiate` (or `using Pkg; Pkg.instantiate()` in the normal julia mode) will check if you have all of the packages and versions mentioned in the `Project.toml` and `Manifest.toml` files, and install as required.
281298
- This feature will let you reproduce the entire environment and, if a `Manifest.toml` is available, the exact package versions used for a project. For example, these lecture notes use [Project.toml](https://github.com/QuantEcon/lecture-julia.notebooks/blob/main/Project.toml) and [Manifest.toml](https://github.com/QuantEcon/lecture-julia.notebooks/blob/main/Manifest.toml) - which you likely instantiated during installation after downloading these notebooks.
282-
* `] add Expectations` will add a package (here, `Expectations.jl`) from the activated project file (or the global environment if none is activated)
299+
* `] add Expectations` will add a package (here, `Expectations.jl`) to the activated project file (or the global environment if none is activated).
283300
* Likewise, `] rm Expectations` will remove that package.
284301
* `] st` will show you a snapshot of what you have installed.
285-
* `] up` will upgrade versions of your packages to the latest versions possible given the graph of compatibility used in each
302+
* `] up` will upgrade versions of your packages to the latest versions possible given the graph of compatibility used in each.
286303

287304
```{note}
288305
On some operating systems (such as OSX) REPL pasting may not work for package mode, and you will need to access it in the standard way (i.e., hit `]` first and then run your commands).

0 commit comments

Comments
 (0)