Brief Summary
This article provides a comprehensive overview of code editing and navigation features available in the Visual Studio Code C/C++ extension. It covers various aspects such as identifying header files, code formatting using clang-format and vc_format, semantic colorization, quick info, Doxygen and Markdown comments, and source code navigation tools like symbol search, call hierarchy, peek, and go to definition/declaration/references/type definition.
- Identifying header files and managing include paths.
- Code formatting options using clang-format and vc_format.
- Enhanced semantic colorization for improved code readability.
- Navigation features for efficient code exploration and understanding.
Editing C++ Code
The C/C++ extension in Visual Studio Code offers powerful source code editing features for formatting and understanding codebases. It helps in identifying header files by searching the current source directory, subdirectories, and platform-specific locations. If a header file is missing, a red squiggle appears under the #include
directive, prompting users to add additional include directories via the extension's settings. The editor also provides a list of members when typing a member access symbol, which filters in real-time as more letters are typed.
Code Formatting
The C/C++ extension supports source code formatting using clang-format
and vc_format
, with clang-format
as the default. Formatting can be applied to an entire file or a selection via the right-click context menu, and can be triggered on saving, typing, or pasting through editor settings. By default, clang-format
uses a .clang-format
file if present, or falls back to the Visual Studio style. Users can specify a different clang-format
version by updating the C_Cpp.clang_format_path
setting. Alternatively, the Visual C++ formatting engine (vc_format
) can be used, especially if an .editorconfig
file is present, or by setting the C_Cpp.formatting
setting to vc_format
.
Enhanced Semantic Colorization and Quick Info
The Visual Studio Code C/C++ extension supports semantic colorization when IntelliSense is enabled, allowing customization of colors for classes, functions, and variables. Quick Info provides an inline view of a symbol's definition when hovering over it.
Doxygen and Markdown Comments
The extension supports Doxygen comments for generating documentation from source code, with features like generating a Doxygen comment block by typing /**
and pressing Enter. Supported tags include @brief
, @tparam
, @param
, @return
, @exception
, @deprecated
, @note
, @attention
, and @pre
. The C++ extension also supports a subset of Markdown in comments by default, with options to enable all Markdown, keep the subset, or disable Markdown support via the Markdown in Comments setting.
Navigate Source Code
The source code navigation features in the C/C++ extension help improve understanding of the codebase by enabling quick searches for symbols, navigation to definitions, and finding references. These features are powered by a local database of symbol information that is created and updated whenever a folder containing C++ source code files is opened or changed.
Search for Symbols
Users can search for symbols in the current file or workspace to navigate code quickly. To search within the current file, press ⇧⌘O
and enter the symbol name. To search the entire workspace, press ⌘T
and enter the symbol name. The list of potential matches filters as you type, and selecting a match navigates to its location, opening the file if necessary. These commands can also be accessed via the Command Palette using @
for the current file and #
for the workspace.
Call Hierarchy
The Call Hierarchy view displays all calls to or from a function, helping users understand complex calling relationships. To view the call hierarchy, right-click a function and choose "Show Call Hierarchy," or use the keyboard shortcut or Command Palette. The call tree in the side bar shows all functions called by the selected function, and can be toggled to show incoming calls (functions referencing the selected function). Nested calls can be explored by right-clicking functions in the call tree.
Peek
The Peek feature displays a few lines of code inside a peek window, allowing users to quickly understand the context of a symbol without navigating away. To open a peek window, right-click and select "Peek," then choose to peek at a symbol's definition, declaration, type definition, or references. Users can browse the results and navigate to a specific location by selecting the result or double-clicking in the source code displayed in the peek window.
Go to Definition
The "Go to Definition" feature allows quick navigation to where a symbol is defined in the source code. Select a symbol, press F12
, or right-click and choose "Go to Definition." If there is only one definition, it navigates directly to its location; otherwise, competing definitions are displayed in a peek window. If no definitions are found, the extension searches for a declaration of the symbol.
Go to Declaration
The "Go to Declaration" feature navigates to the location where a symbol is declared in the source code. This feature functions similarly to "Go to Definition." Select a symbol, right-click, and choose "Go to Declaration" from the context menu to navigate to the symbol's declaration.
Go to References
The "Go to References" feature helps understand how often and where a symbol is referenced in the source code. Select a symbol and press ⇧F12
or right-click and choose "Go to References." If references are found, they are displayed in a peek window.
Go to Type Definition
The "Go to Type Definition" feature jumps to where a type is defined in the source code. Select a type, right-click to open the context menu, and choose "Go to Type Definition" to navigate to the type's definition.