From 0bc997eb54c9f55cbe5995702e29d5b0065c9e03 Mon Sep 17 00:00:00 2001 From: Thibaut de Saivre <thibaut.de-saivre@polytechnique.edu> Date: Mon, 16 Oct 2023 15:10:20 +0200 Subject: [PATCH] add settings and extensions for c++ --- cpp/.vscode/extensions.json | 9 +++++ cpp/.vscode/settings.json | 22 ++++++++++++ cpp/README.md | 69 ++++++++----------------------------- 3 files changed, 46 insertions(+), 54 deletions(-) create mode 100644 cpp/.vscode/extensions.json create mode 100644 cpp/.vscode/settings.json diff --git a/cpp/.vscode/extensions.json b/cpp/.vscode/extensions.json new file mode 100644 index 0000000..3775419 --- /dev/null +++ b/cpp/.vscode/extensions.json @@ -0,0 +1,9 @@ +{ + "recommendations": [ + "ms-vscode.cpptools-extension-pack", // C++ language support + "llvm-vs-code-extensions.vscode-clangd", // Better C++ language server + "xaver.clang-format", // C++ formatter + "twxs.cmake", // CMake language support (for CMakeLists.txt files for c++ project configurations) + "ms-vscode.makefile-tools" // Makefile support + ] +} diff --git a/cpp/.vscode/settings.json b/cpp/.vscode/settings.json new file mode 100644 index 0000000..53059da --- /dev/null +++ b/cpp/.vscode/settings.json @@ -0,0 +1,22 @@ +{ + // Generic settings : + "editor.formatOnSave": true, // Format files on save + "formatFiles.runOrganizeImports": true, // Sort imports when formatting + "editor.codeActionsOnSave": { + // Organize imports on save + "source.organizeImports": true + }, + + // C++ + "clangd.path": "PATH_TO_YOUR_CLANGD_EXECUTABLE", + + // Possibility for Clang format style (opinionated) + "clang-format.style": "{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: true, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4, NamespaceIndentation: All, FixNamespaceComments: false }", + "C_Cpp.intelliSenseEngine": "disabled", // Disable the standard extension intellisense + "[cpp]": { + "editor.defaultFormatter": "xaver.clang-format" + }, + "[c]": { + "editor.defaultFormatter": "xaver.clang-format" + } +} diff --git a/cpp/README.md b/cpp/README.md index ab9e544..a23bd47 100644 --- a/cpp/README.md +++ b/cpp/README.md @@ -8,83 +8,44 @@ In order to ensure that everyone uses the same linting and formatting settings, we duplicate the relevant settings in [`.vscode/settings.json`](.vscode/settings.json). -Relevant settings with python : +Relevant settings with c++ : - format on save -- strict type checking -- format using **Black** +- using `clangd` language server over the standard one +- using `clang-format` c++ formatter -## Formatter - Black +## Formatter - ClangFormat -[Black](https://github.com/psf/black) is a popular python formatter which is strict and opinionated. +[ClangFormat](https://clang.llvm.org/docs/ClangFormat.html) is a good c++ formatter. Using a formatter ensures that the code you and your team write is consistent. You should always use a formatter. -Additional configuration for the formatter can be added to the [`pyproject.toml`](./pyproject.toml) file, -although the default settings are already pretty good. +You can edit the formatter style using the `clang-format.style` setting in [`settings.json`](.vscode/settings.json). -## Linter - Ruff +## Language Server - Clangd -[Ruff](https://github.com/astral-sh/ruff) is a good python linter, in the sense that it is strict enough, and coded in rust (blazingly fast). -It is the fastest python linter available, which is nice when you want reactive linting during typing. -Using a linter will help you write cleaner code, and avoid common mistakes. -You should always use a linter. - -Additional configuration for the formatter can be added to the [`pyproject.toml`](./pyproject.toml) file, -although the default settings are already pretty good. +The [Clangd](https://clangd.llvm.org/) language server is a better language server than the default one used by VSCode C++ extensions. It also provides inlay hints. In order for it to work, you must install the Clangd extension, and disable the default C++ extension's intellisense. ## Environment and reproducibility +TODO: linux + In order to make it easier for other developers to contribute to your project, and to enable deploying your application easily, you must indicate which python version and dependencies you are using. You typically indicate the python version in your [`pyproject.toml`](./pyproject.toml) or (`README.md`)(./README.md) file. -### Create a virtual environment - -If you do not use `conda`, you may create a virtual environment. Virtual environments are often contained in a `venv` folder. -Virtual environment enable you to have separate python versions and dependencies for each project. - -```bash -python --version # Ensure that your current python version is the one you want to use. Use `pyenv` to change it if needed (see wikibr). -python -m venv venv # Creates a virtual environment in the `venv` folder. - -# LINUX / MACOS : activate the virtual environment -source venv/bin/activate - -# WINDOWS : activate the virtual environment -venv\Scripts\activate -``` - -You must always activate your virtual environment before running your code or installing dependencies (else it will use your global python interpreter). - -### Install and Freeze dependencies - -Python depdendencies are typically indicated in a [`requirements.txt`](./requirements.txt) file. - -Install dependencies : - -```bash -pip install --upgrade pip # Update pip (python package manager) -pip install -r requirements.txt # Install dependencies read from the requirements file -``` - -Freeze dependencies : -You can write your dependencies by hand in the `requirements.txt` file, or you can "freeze" your current environment. - -```bash -pip freeze > requirements.txt # Write the current dependencies to the requirements file -``` - ### Git -Some common gitignores for a python project : +Some common gitignores for a C++ project : -- `__pycache__` : python cache, generated at runtime. -- `venv` : virtual environment folder. +- `build/` : the build output folder. +- `.cache/` : clangd cache folder. ## Python Clean Code +TODO : code rules + Some often-overlooked python clean code rules : ### Docstrings -- GitLab