Skip to main content Link Search Menu Expand Document (external link)

Theme: auto

Formatting Your Code

We’ve tried to get your development environment configured so that your source files are automatically formatted when the file is saved. However, on occasion this does not work for a variety of reasons. In this case, you can still get the IDE to help format the files.

Visual Studio 2022 (PC)

Assuming the source file you want to format is the current file in your view, you can have Visual Studio 2022 format it in two different ways:

  • In the menu at the top, go to Edit>Advanced>Format Document
  • Press the hotkey for the “Format Document” command, which defaults to Ctrl+K, Ctrl+D. (The easiest way to do this keystroke is hold down Ctrl, press and release K, press and release D, release Ctrl.)

If you want to change the hotkey for format document, you can go to Tools>Options… and then under Environment>Keyboard search for the “Edit.FormatDocument” command. You can then reassign it to a new shortcut.

Reformatting an Entire Lab at Once

If you have lots of source files in your lab which ended up with messed up formatting, you can also reformat them all at once.

  1. In Windows Explorer, open up your repo root folder. Click on the address bar and copy the path.

  2. In the Start Menu, search for “x64 Native Tools Command Prompt for VS 2022” and run it.

  3. In the command prompt, type cd followed by a space, and then right click to paste the path. Hit enter.

  4. Run the following command, replacing the XX with the lab number (for example 02 for Lab 2 or 10 for Lab 10):

    Scripts\run-clang-format.bat LabXX
    

Visual Studio Code (Mac)

Assuming the source file you want to format is the current file in your view, you can have Visual Studio Code format it in two different ways:

  • In the menu at the top, go to View>Command Palette… Type “Format Document” and hit enter
  • Press the hotkey for the “Format Document” command, which defaults to Shift+Option+F

If you want to change the hotkey for format document, you can go to Code>Settings>Keyboard Shortcuts and search for “Format Document” to change it (it’s editor.action.formatDocument).

Reformatting an Entire Lab at Once

If you have lots of source files in your lab which ended up with messed up formatting, you can also reformat them all at once.

Prerequisites

You only need to do these steps the very first time you want to reformat a lab with the script.

  1. Open a terminal. Install clang-format with:
    brew install clang-format
    

Reformatting

  1. In terminal, cd to your repo directory

  2. Run the following command, replacing the XX with the lab number (for example 02 for Lab 2 or 10 for Lab 10):

    Scripts/mac-clang-format.sh LabXX