Guides for SE student projects »

Caution: VS Code guides section is a work-in-progress, containing content contributed by students.

VS Code - Setting up Checkstyle

Given below are the steps to set up a Checkstyle plugin in VS Code so that VS Code can alert you about code style problems as you write code.

  • Verify prerequisites:
    • Ensure you followed the section Preparing VS Code for Java projects above.
    • The two Checkstyle config files (checkstyle.xml and suppressions.xml) should be in the ./config/checkstyle directory, as mentioned here.
  • Open VS Code.
  • Download the Checkstyle for Java extension by author ShengChen using Extensions -> Search Checkstyle for Java -> Install (the first entry).
    install checkstyle extension in VS Code
  • Open the project directory in VS Code using File -> Open Folder....
  • Set up the extension to use the project's Checkstyle files as follows:
    • Check if the ./.vscode/settings.json file exists. If not, create a folder .vscode in the project root directory and add a file settings.json within the .vscode folder. This file contains configuration settings for projects in VS Code.
    • Add these configuration settings to the settings.json file. These settings ensure that the Checkstyle extension uses the correct configuration files in the ./config/checkstyle directory:
      settings.json
       {
         "java.checkstyle.configuration": "${workspaceFolder}/config/checkstyle/checkstyle.xml",
         "java.checkstyle.properties": {
           "config_loc": "${workspaceFolder}/config/checkstyle"
         }
       }
      
  • Add .vscode to your .gitignore if you haven't done so by adding these few lines to the end of .gitignore:
    .gitignore
    # VS Code
    /.vscode/
    
  • Now you should be able to edit your code with Checkstyle violations being detected as you edit them in the Problems tab in VS Code. For example, after changing the code to add a wildcard import, you can see that the wildcard import on line 11 has been detected by the Checkstyle extension:

Contributors: Brendon Koh (@brein62)