Skip to content

Commit 295f34c

Browse files
committed
Merge branch 'code-editors'
2 parents de33889 + 5889517 commit 295f34c

File tree

4 files changed

+277
-0
lines changed

4 files changed

+277
-0
lines changed

_data/sidebar_tree.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ tree:
8787
tree:
8888
- url: /programming/sr/vision/markers
8989
title: Markers
90+
- url: /programming/editors
91+
title: Code Editors
92+
tree:
93+
- url: /programming/editors/vscode
94+
title: VSCode
9095
- url: /programming/git_repositories
9196
title: Git Repositories
9297
- url: /rules/

_sass/docs.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@
108108
}
109109
}
110110

111+
kbd {
112+
color: #555;
113+
border: 1px solid #ddd;
114+
border-radius: 3px;
115+
box-shadow: 0 1px #ddd;
116+
font-size: 0.9em;
117+
padding: 1px 3px;
118+
}
119+
111120
ul, ol {
112121
margin: 1.2em 0 1.2em 40px;
113122

programming/editors/index.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
layout: page
3+
title: Code Editors
4+
---
5+
6+
# Code Editors
7+
8+
When developing code to run on our robotics kit, we recommend that you start by
9+
using our hosted IDE. The hosted IDE provides an easy way to get going, as well
10+
as ensuring that you have a history of your code which is available from
11+
anywhere and your mentor can access. We appreciate that you may wish to use your
12+
own editor instead, which you are free to do.
13+
14+
When developing code to run in the simulator, you will need to have both the
15+
simulator and your own editor set up.
16+
17+
<div class="info">
18+
However you are working on your code, you are encouraged to ensure you have a backup!
19+
</div>
20+
21+
## Choosing an Editor
22+
23+
There are lots of code editors to choose from. Several are freely available for
24+
Python and support features which will make it easier to develop your code.
25+
26+
Some features to look for are:
27+
28+
- [Syntax highlighting][syntax-highlighting] makes it easier to see the overall
29+
structure of a program without needing to read every word.
30+
31+
- [Code completion][code-completion] offers suggestions of what you might want
32+
to type and can offer a way to explore what features an API offers.
33+
34+
- Interactive debugging allows stepping through code one statement at a time,
35+
examining the results as you go.
36+
37+
- Integration with tools known as "linters" which can help check that your code
38+
is well formed without running it (a process formally known as "static analysis").
39+
40+
[syntax-highlighting]: https://en.wikipedia.org/wiki/Syntax_highlighting
41+
[code-completion]: https://en.wikipedia.org/wiki/Autocomplete#In_source_code_editors
42+
43+
<div class="info">
44+
While you can use a plain text editor like Notepad to edit your code, we would
45+
recommend that you choose an editor which supports at least syntax highlighting.
46+
</div>
47+
48+
### Common Editors
49+
50+
Here are some common Python editors:
51+
52+
- [VSCode](https://code.visualstudio.com/) with the [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python)
53+
- [PyCharm](https://www.jetbrains.com/pycharm/)
54+
- [Sublime Text](https://www.sublimetext.com/)
55+
- [Atom](https://atom.io/)

programming/editors/vscode.md

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
---
2+
layout: page
3+
title: VSCode
4+
---
5+
6+
# Visual Studio Code
7+
8+
[Visual Studio Code][vscode] (known commonly as VSCode) is a free code editor
9+
developed by Microsoft. As such it has [extensive documentation][vscode-docs]
10+
about its general use, which we will not repeat here. Instead this page will
11+
focus on the ways to get the most out of VSCode when developing code for Student
12+
Robotics.
13+
14+
<!-- We link to the homepage as that has a big install button --->
15+
[vscode]: https://code.visualstudio.com
16+
<!-- but we also want to link directly to their docs so people have a starting point --->
17+
[vscode-docs]: https://code.visualstudio.com/docs
18+
19+
## Python Extension
20+
21+
While VSCode has some built-in support for developing Python, we recommend that
22+
you additionally install the official [Python extension][ms-python.python]. This
23+
brings more comprehensive support and enables everything else we will document
24+
here.
25+
26+
[ms-python.python]: https://marketplace.visualstudio.com/items?itemName=ms-python.python
27+
28+
## Code Completions
29+
30+
In order for VSCode to pick up the `sr.robot` library and offer [completions][code-completion]
31+
you'll need to tell it where to find the library files:
32+
33+
1. Open the workspace containing your code.
34+
2. Open your [workspace settings][workspace-settings] file:
35+
* On Windows/Linux - **File** > **Preferences** > **Settings**.
36+
* On macOS - **Code** > **Preferences** > **Settings**.
37+
38+
3. Select **Workspace** (rather then **User**).
39+
4. Search for `python.autoComplete.extraPaths`.
40+
5. Click "Edit in settings.json".
41+
6. Add the path to the directory which contains the `sr` directory, within
42+
double quotes. The path can be either relative to your project or absolute.
43+
44+
For example if developing code for the simulator and you have extracted the
45+
simulator directory next to your code:
46+
47+
```
48+
.
49+
├── competition-simulator-<version>
50+
│ ├── ...
51+
│ ├─ modules
52+
│ │ └── sr
53+
│ │ └── robot
54+
│ │ └── ...
55+
│ └─ worlds
56+
│ └── Arena.wbt
57+
└── robot.py
58+
```
59+
60+
then the path you should add is `"competition-simulator-<version>/modules/"`.
61+
62+
[code-completion]: https://en.wikipedia.org/wiki/Autocomplete#In_source_code_editors
63+
[workspace-settings]: https://code.visualstudio.com/docs/getstarted/settings#_creating-user-and-workspace-settings
64+
65+
## Interactive Debugging
66+
67+
<div class="info">
68+
This section refers to a feature which is only available within the simulator.
69+
<!--
70+
Pedantic note: yes, you can actually make this work on the kits too (very easily
71+
it turns out), however the steps to set this up on the kits are a bit different
72+
so for now we just document the simulator version.
73+
-->
74+
</div>
75+
76+
Interactive debugging is a great way to inspect what your code is doing whilst
77+
the simulator is actually running. It will allow you to inspect the values of
78+
variables throughout the code and even re-run sections to understand how they
79+
interact.
80+
81+
There are two initial setup steps (installing `debugpy` and configuring VSCode)
82+
and then two steps each time you want to debug your code.
83+
84+
### Install `debugpy`
85+
86+
In order for VSCode to be able to debug your code as it runs in the simulator
87+
you will need to install the [`debugpy`][debugpy] Python package. You will use
88+
this package in your Python code to signal that it is ready for VSCode to attach
89+
to it, so it needs to be installed into the same Python environment that Webots
90+
is configured to use.
91+
92+
``` shell
93+
python -m pip install debugpy
94+
```
95+
96+
You may need to use the full path to your `python`, or on some platforms it may
97+
be called `python3`.
98+
99+
On Windows, if specifying a full path to `python.exe`, you should use the basic
100+
Command Prompt (and not the terminal within VSCode or PowerShell).
101+
102+
### Configure VSCode
103+
104+
Next, we are going to create a [debug configuration][debug-config] so that
105+
VSCode knows what you want it to debug.
106+
107+
<!--
108+
If the user hasn't activated their Python extension in the current editor
109+
session then it won't yet have registered itself. Additionally if the currently
110+
focused file is not a Python file then they will be asked for the environment
111+
they want to debug. Avoid both of these by instructing the user to open their
112+
`robot.py` before attempting to configure debugging.
113+
-->
114+
115+
1. Open the workspace containing your code.
116+
2. Open your `robot.py` file.
117+
3. Select the Run view in the sidebar (or press
118+
<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>D</kbd> /
119+
<kbd>⌘</kbd> + <kbd>⇧</kbd> + <kbd>D</kbd> on macOS)
120+
4. Click "create a launch.json file".
121+
5. In the menu which appears, if you are asked to select an environment select **Python**.
122+
6. When asked for a Debug Configuration, select **Remote Attach**.
123+
7. Leave the subsequent values at their defaults (press <kbd>Enter</kbd> twice).
124+
125+
This will create a new file in your project which contains the configuration.
126+
127+
Depending on the location of your robot code is within your project, you may
128+
need to adjust the configuration further.
129+
130+
If your code is in the root of your workspace:
131+
132+
```
133+
.
134+
└── robot.py
135+
```
136+
137+
then no further changes are needed.
138+
139+
If your code is within a folder, perhaps because your workspace includes the
140+
simulator folder and your code is in `zone-0` or `zone-1`:
141+
142+
```
143+
.
144+
├── competition-simulator-<version>
145+
│ ├── ...
146+
│ └─ worlds
147+
│ └── Arena.wbt
148+
└── zone-1
149+
└── robot.py
150+
```
151+
152+
then you will need to modify the `pathMappings` key within the configuration.
153+
The change you'll need to make is to alter the `localRoot` key such that it
154+
accounts for the sub-directory containing the robot code.
155+
156+
In the above example, the change would be to update the line:
157+
158+
``` json
159+
"localRoot": "${workspaceFolder}",
160+
```
161+
162+
to instead be:
163+
164+
``` json
165+
"localRoot": "${workspaceFolder}/zone-1",
166+
```
167+
168+
[debugpy]: https://pypi.org/project/debugpy/
169+
[debug-config]: https://code.visualstudio.com/docs/python/debugging
170+
171+
### Configure your code
172+
173+
To debug a given part of your code, you will need to insert into your code a
174+
statement which will launch the debugger.
175+
176+
At the place in your code that you would like the debugger to start, insert the
177+
following snippet:
178+
179+
``` python
180+
import debugpy
181+
debugpy.listen(5678)
182+
debugpy.wait_for_client()
183+
breakpoint()
184+
```
185+
186+
This code (specifically the `wait_for_client` call) will block until the
187+
debugger is attached.
188+
189+
### Run and debug
190+
191+
You can now launch your code in the simulator exactly as you would normally.
192+
When your code stops running (the simulation will also stop processing) change
193+
to VSCode and select **Run** > **Start Debugging** or press <kbd>F5</kbd>.
194+
195+
VSCode will attach to your code, paused at the `breakpoint()` line.
196+
197+
A full tutorial of [debugging in VSCode][vscode-debugging] is beyond the scope
198+
of this article, though the most common commands (all available from the **Run**
199+
menu) are:
200+
201+
* Step Over (<kbd>F10</kbd>)
202+
* Step Into (<kbd>F11</kbd>)
203+
* Step Out (<kbd>Shift</kbd> + <kbd>F11</kbd>)
204+
* Continue (<kbd>F5</kbd>)
205+
206+
You can also inspect the values of variables in your code by hovering over them.
207+
208+
[vscode-debugging]: https://code.visualstudio.com/docs/editor/debugging

0 commit comments

Comments
 (0)