1.. _module-pw_ide-guide-vscode: 2 3================== 4Visual Studio Code 5================== 6.. pigweed-module-subpage:: 7 :name: pw_ide 8 9.. toctree:: 10 :maxdepth: 1 11 :hidden: 12 13 code_intelligence 14 extension_enforcement 15 troubleshooting 16 legacy 17 development 18 19Pigweed provides rich and robust support for development in `Visual Studio Code <https://code.visualstudio.com/>`_, 20including: 21 22* High-quality C/C++ code intelligence for embedded systems projects using `clangd <https://clangd.llvm.org/>`_ 23 integrated directly with your project's Bazel build graph 24 25* Bundled core Bazel tools, letting you get started immediately without the need 26 to install global system dependencies 27 28* Interactive browsing, building, and running Bazel targets 29 30.. note:: 31 32 Currently, this document only applies to :ref:`Bazel projects<module-pw_ide-design-projects-bazel>`. 33 We're working on adding support for :ref:`bootstrap projects<module-pw_ide-design-projects-bootstrap>`. 34 In the meantime, bootstrap projects can use the :ref:`command-line interface<module-pw_ide-guide-cli>` 35 with the :ref:`legacy support for Visual Studio Code<module-pw_ide-guide-vscode-legacy>`. 36 37--------------- 38Getting started 39--------------- 40.. _Pigweed extension: https://marketplace.visualstudio.com/items?itemName=pigweed.pigweed 41 42All you need to do is install the `Pigweed extension`_ from the extension 43marketplace. If you start your project from one of Pigweed's quickstart or 44showcase example projects, you will be prompted to install the extension as soon 45as you open the project. 46 47Once installed, the Pigweed extension will do a few things for you automatically 48when you open :ref:`Pigweed projects<module-pw_ide-design-projects>`: 49 50* The Bazel extension will discover all of the targets in your project 51 52* The Pigweed extension will generate `compilation databases <https://clangd.llvm.org/design/compile-commands>`_ 53 for the :ref:`target groups<module-pw_ide-design-cpp-target-groups>` 54 in your project. 55 56You can now select a target group from the status bar item at the bottom 57of your window or by running the ``Pigweed: Select Code Analysis Target`` 58command. 59 60Once you select a target group, the ``clangd`` extension will be automatically 61configured to use the ``clang`` toolchain in the Bazel environment and the 62compilation database associated with the selected target group. 63 64What this gives you 65=================== 66Here's a non-exhaustive list of cool features you can now enjoy: 67 68* Code navigation, including routing through facades to the correct backend 69 70* Code completion, including correct class members and function signatures 71 72* Tooltips with docs, inferred types for ``auto``, inferred values for ``constexpr``, 73 data type sizes, etc. 74 75* Compiler errors and warnings as you write your code 76 77* Code formatting via the standard ``Format ...`` commands, including 78 Starlark files 79 80* Linting and debugging for Starlark files 81 82* A tree view of all Bazel targets, allowing you to build or run them directly 83 84----------------- 85Code intelligence 86----------------- 87Learn more about using and configuring code intelligence :ref:`here<module-pw_ide-guide-vscode-code-intelligence>`. 88 89---------------- 90Project settings 91---------------- 92Pigweed manipulates some editor and ``clangd`` settings to support features like 93the ones described above. For example, when you select a code analysis target, 94Pigweed sets the ``clangd`` extensions settings in ``.vscode/settings.json`` to 95configure ``clangd`` to use the selected target. Likewise, when using the 96:ref:`feature to disable code intelligence for source files not in the target's build graph<module-pw_ide-guide-vscode-settings-disable-inactive-file-code-intelligence>`, 97Pigweed will manipulate the ``.clangd`` `configuration file <https://clangd.llvm.org/config#files>`_. 98 99These files shouldn't be committed to the project repository, because they 100contain state that is specific to what an individual developer is working on. 101Nonetheless, most projects will want to commit certain shared settings to their 102repository to help create a more consistent development environment. 103 104Pigweed provides a mechanism to achieve that through additional settings files. 105 106Visual Studio Code settings 107=========================== 108The ``.vscode/settings.json`` file should be excluded from source control. 109Instead, add your shared project settings to ``.vscode/settings.shared.json`` 110and commit *that* file to source control. 111 112The Pigweed extension watches the shared settings file and automatically applies 113those settings to you local settings file. So shared project settings can be 114committed to ``.vscode/settings.shared.json``, and your current editor state, as 115well as your personal configuration preferences, can be stored in 116``.vscode/settings.json``. 117 118The automatic sync process will respect any settings you have in your personal 119settings file. In other words, if a conflicting setting appears in the shared 120settings file, the automatic sync will not override your personal setting. 121 122At some point, you may wish to *fully* synchronize with the shared settings, 123overriding any conflicting settings you may already have in your personal 124settings file. You can accomplish that by running the 125:ref:`settings sync command<module-pw_ide-guide-vscode-commands-sync-settings>`. 126 127``clangd`` settings 128=================== 129Additional configuration for ``clangd`` can be stored in ``.clangd.shared``, 130following the `YAML configuration format <https://clangd.llvm.org/config>`_. 131The Pigweed extension watches this file and automatically applies its settings 132to a ``.clangd`` file that *should not* be committed to source control. That 133file will *also* be used to configure ``clangd`` in ways that are specific to 134your selected analysis target and the state of your code tree. 135 136-------- 137Commands 138-------- 139Access commands by opening the command palette :kbd:`Ctrl+Shift+P` 140(:kbd:`Cmd+Shift+P` on Mac). 141 142.. describe:: Pigweed: Check Extensions 143 144 The Pigweed extension lets development teams maintain a consistent 145 development environment for all members of the team by ensuring that the 146 recommendations in ``extensions.json`` are enforced. Learn more at 147 :ref:`extension enforcement<module-pw_ide-guide-vscode-extension-enforcement>`. 148 149.. describe:: Pigweed: File Bug 150 151 Found a problem in the Pigweed Visual Studio Code extension, other Pigweed 152 tools, or Pigweed itself? Add a bug to our bug tracker to help us fix it. 153 154.. _module-pw_ide-guide-vscode-commands-sync-settings: 155 156.. describe:: Pigweed: Sync Settings 157 158 Pigweed automatically syncronizes shared Visual Studio Code settings from 159 ``.vscode/settings.shared.json`` to ``.vscode/settings.json``, but in the 160 case of conflicts, the automatic process will preserve the value in 161 ``.vscode/settings.json``. If you want to do a full sync of the shared 162 settings to your personal settings, including overriding conflicting values, 163 run this command. 164 165.. _module-pw_ide-guide-vscode-commands-open-output-panel: 166 167.. describe:: Pigweed: Open Output Panel 168 169 Opens the Pigweed output panel, which contains diagnostic output generated by 170 the Pigweed extension. This is a good first place to look if things go wrong. 171 172.. _module-pw_ide-guide-vscode-commands-refresh-compile-commands: 173 174.. describe:: Pigweed: Refresh Compile Commands 175 176 Manually trigger a refresh of the compilation databases used for C/C++ code 177 intelligence. Normally, the databases are refreshed automatically when build 178 files are changed, but if you have 179 :ref:`automatic refreshing disabled<module-pw_ide-guide-vscode-settings-disable-compile-commands-file-watcher>` 180 or need to refresh outside of the automatic cycle, this command will refresh 181 manually. 182 183.. describe:: Pigweed: Refresh Compile Commands and Set Code Analysis Target 184 185 This is the same as the :ref:`Pigweed: Refresh Compile Commands<module-pw_ide-guide-vscode-commands-refresh-compile-commands>`, 186 except that it also triggers :ref:`Pigweed: Select Code Analysis Target<module-pw_ide-guide-vscode-commands-select-target>` 187 after the refresh is complete. 188 189.. _module-pw_ide-guide-vscode-commands-select-target: 190 191.. describe:: Pigweed: Select Code Analysis Target 192 193 Select the target group that ``clangd`` should use for code analysis. 194 195 .. tip:: 196 197 You might notice that the currently-selected code analysis target is 198 stored in the ``pigweed.codeAnalysisTarget`` setting. If you edit this 199 value manually, don't worry! The Pigweed extension will immediately do 200 everything it would have done if you had run this command. 201 202.. describe:: Pigweed: Disable Inactive File Code Intelligence 203 204 See :ref:`the associated setting<module-pw_ide-guide-vscode-settings-disable-inactive-file-code-intelligence>`. 205 206.. describe:: Pigweed: Enable Inactive File Code Intelligence 207 208 See :ref:`the associated setting<module-pw_ide-guide-vscode-settings-disable-inactive-file-code-intelligence>`. 209 210.. describe:: Pigweed: Set Bazel Recommended Settings 211 212 Configure Visual Studio Code to use Pigweed's recommended Bazel settings. 213 Note that these settings are only applied to the *project* settings, so they 214 don't affect any other project's settings, or your user settings. 215 216 * Sets the Buildifier path to the version bundled with the Pigweed extension, enabling Starlark code intelligence 217 218 * Enables Bazel CodeLens support, allowing you to build and run targets directly from Bazel files 219 220.. describe:: Pigweed: Set Bazelisk Path 221 222 Pigweed recommends using `Bazelisk <https://github.com/bazelbuild/bazelisk>`_ 223 instead of plain Bazel to ensure that the right version of Bazel is used. 224 This command allows you to set the path to Bazelisk, selecting from versions 225 installed on your system or the version bundled with the Pigweed extension. 226 227.. describe:: Pigweed: Activate Bazelisk in Terminal 228 229 This will change the `$PATH` of your active integrated terminal to include 230 the path to Bazelisk configured in your editor settings. This allows you to 231 run Bazel actions via Visual Studio Code commands or via `bazelisk ...` 232 invocations in the integrated terminal, while working in the same Bazel 233 environment. 234 235--------------------- 236Configuration options 237--------------------- 238.. py:data:: pigweed.activateBazeliskInNewTerminals 239 :type: boolean 240 :value: true 241 242 When enabled, the path to Bazelisk will be added to the integrated terminal when launched 243 244.. py:data:: pigweed.codeAnalysisTarget 245 :type: string 246 247 The build target to use for editor code intelligence 248 249.. warning:: 250 251 You should only set this value by running the :ref:`Pigweed\: Select Code Analysis Target<module-pw_ide-guide-vscode-commands-select-target>`. 252 The command has other configuration side-effects that won't be triggered if 253 you manually set the value in ``settings.json``. 254 255.. py:data:: pigweed.disableBazelSettingsRecommendation 256 :type: boolean 257 :value: false 258 259 Disable reminders to use Pigweed's Bazel settings recommendations 260 261.. py:data:: pigweed.disableBazeliskCheck 262 :type: boolean 263 :value: false 264 265 Disable the recommendation to use Bazelisk 266 267.. _module-pw_ide-guide-vscode-settings-disable-compile-commands-file-watcher: 268 269.. py:data:: pigweed.disableCompileCommandsFileWatcher 270 :type: boolean 271 :value: false 272 273 Disable automatically refreshing compile commands 274 275.. _module-pw_ide-guide-vscode-settings-disable-inactive-file-code-intelligence: 276 277.. py:data:: pigweed.disableInactiveFileCodeIntelligence 278 :type: boolean 279 :value: true 280 281 When you select a target for code analysis, some source files in the project 282 may not appear in the compilation database for that target because they are 283 not part of the build graph for the target. By default, ``clangd`` will 284 attempt to provide code intelligence for those files anyway by inferring 285 compile commands from similar files in the build graph, but this code 286 intelligence is incorrect and meaningless, as the file won't actually be 287 compiled for that target. 288 289 Enabling this option will configure ``clangd`` to suppress all diagnostics 290 for any source files that are *not* part of the build graph for the currently 291 selected target. 292 293.. _module-pw_ide-guide-vscode-settings-enforce-extension-recommendations: 294 295.. py:data:: pigweed.enforceExtensionRecommendations 296 :type: boolean 297 :value: false 298 299 Require installing and disabling extensions recommended in ``extensions.json`` 300 301.. _module-pw_ide-guide-vscode-settings-hide-inactive-file-indicators: 302 303.. py:data:: pigweed.hideInactiveFileIndicators 304 :type: boolean 305 :value: false 306 307 When code intelligence is enabled for all files, hide indicators for inactive 308 and orphaned files. Note that changing this setting requires you to reload 309 Visual Studio Code to take effect. 310 311.. py:data:: pigweed.preserveBazelPath 312 :type: boolean 313 :value: false 314 315 When enabled, this extension won't override the specified Bazel path under 316 any circumstances. 317 318.. _module-pw_ide-guide-vscode-settings-project-root: 319 320.. py:data:: pigweed.projectRoot 321 :type: string 322 323 The root of the Pigweed project source directory 324 325.. _module-pw_ide-guide-vscode-settings-project-type: 326 327.. py:data:: pigweed.projectType 328 :type: bootstrap or bazel 329 330 The type of Pigweed project, either bootstrap or Bazel 331 332.. _module-pw_ide-guide-vscode-settings-refresh-compile-commands-target: 333 334.. py:data:: pigweed.refreshCompileCommandsTarget 335 :type: string 336 :value: //:refresh_compile_commands 337 338 The Bazel target to run to refresh compile commands 339