Skip to content

seaube/vscode-clang-format

 
 

Repository files navigation

ClangFormat for Visual Studio Code

This is a fork of xaverh/vscode-clang-format which hasn't published a new version since 2019 at the time of writing this. See issue: xaverh/vscode-clang-format#113. Most of the changes are cleanup/modernizing the codebase.

Notable differences:

This extension allows you to format your code with Clang-Format. It can be configured with a config file named .clang-format within the working folder or a parent folder. Configuration see: http://clang.llvm.org/docs/ClangFormatStyleOptions.html

Usage

Files can be formatted on-demand by right clicking in the document and selecting "Format Document", or by using the associated keyboard shortcut (usually Ctrl++F on Windows, Ctrl++I on Linux, and ++F on macOS).

To automatically format a file on save, add the following to your vscode settings.json file:

{
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "seaube.clangformat"
}

If you would like to only use ClangFormat for a specify language use Language Specific Editor Settings.

{
  "editor.formatOnSave": true,
  "[cpp]": {
    "editor.defaultFormatter": "seaube.clangformat"
  }
}

Specifying the location of clang-format

This extension will attempt to find clang-format on your PATH. Alternatively, the clang-format executable can be specified in your vscode settings.json file:

{
  "clangFormat.executable": "/absolute/path/to/clang-format"
}

Additionally you may specify a different path for different platforms.

{
  "clangFormat.executable.windows": "/absolute/path/to/clang-format.exe",
  "clangFormat.executable.linux": "/absolute/path/to/clang-format",
  "clangFormat.executable.macos": "/absolute/path/to/clang-format",
}

Substitutions can also be used in the clang-format.executable value. The following substitutions are supported:

  • ${workspaceFolder} - replaced by the absolute path of the current vscode workspace. In case of outside-workspace files ${workspaceFolder} expands to the absolute path of the first available workspace.
  • ${cwd} - replaced by the current working directory of vscode.
  • ${env.VAR} - replaced by the environment variable VAR, e.g. ${env.HOME} will be replaced by $HOME, your home directory.

Substitutions are also supported in clang-format.assumeFilename. The supported substitutions are ${file}, ${fileNoExtension}, ${fileBasename}, ${fileBasenameNoExtension}, and ${fileExtname}, with the same meaning as the predefined variables in other configuration files.

For example:

  • ${fileNoExtension}.cpp - /home/src/foo.h will be formatted with -assume-filename /home/src/foo.cpp.

Source code

Available on github: https://github.com/seaube/vscode-clang-format

About

A Visual Studio Code extension that provides formatting with the use of ClangFormat

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 100.0%