From eecab463613d6732777950dff85e005769a12f5d Mon Sep 17 00:00:00 2001 From: changsh726 Date: Wed, 2 Sep 2020 20:55:18 +0800 Subject: [PATCH] Docs: update how_to_lint_and_format.md --- docs/howto/how_to_lint_and_format.md | 38 ++++++++++++++++++++++++++++ scripts/apollo_format.sh | 11 +++++++- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 docs/howto/how_to_lint_and_format.md diff --git a/docs/howto/how_to_lint_and_format.md b/docs/howto/how_to_lint_and_format.md new file mode 100644 index 0000000000..c31228a0b4 --- /dev/null +++ b/docs/howto/how_to_lint_and_format.md @@ -0,0 +1,38 @@ +# How to Lint and Format + +## Coding style + +- **C/C++ coding style**: Apollo adopted the + [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html). + +## Linters provided in apollo + +Apollo uses the following lint tools to check the code style. **Note:** As for +now, ci will only check the style of C++ codes and it may do so in the future +for codes in other languages. + +| tool | file type | usage (from Apollo root dir) | +| :--------: | :-----------------------------------: | :---------------------------------------------: | +| cpplint | .h .c .hpp .cpp .hh .cc .hxx .cxx .cu | ./apollo.sh lint cpp | +| flake8 | .py | ./apollo.sh lint py | +| shellcheck | .sh .bash .bashrc | ./apollo.sh lint sh | + +To make sure your code conforms to the style guide, you can use command +`bash apollo.sh lint all` to check if your code has any style problems and then +fix them manually. Another option is to use formatters to fix style problems +automatically. + +## Formatters provided in apollo + +Apollo integrated a set of formatting tools that cover many types of files: + +| tool | file type | usage (from Apollo root dir) | configuration file | +| :----------: | :-----------------------------------: | :----------------------------------------------: | :----------------: | +| clang-format | .h .c .hpp .cpp .hh .cc .hxx .cxx .cu | ./apollo.sh format -c | .clang-format | +| autopep8 | .py | ./apollo.sh format -p | tox.ini | +| buildifier | BUILD .BUILD .bzl .bazel | ./apollo.sh format -b | NA | +| shfmt | .sh .bash .bashrc | ./apollo.sh format -s | .editorconfig | +| prettier | .md .json .yaml | ./apollo.sh format -m | .prettier.json | + +For ease of use, you can format all the files with types listed above using +command `./apollo.sh format -a ` diff --git a/scripts/apollo_format.sh b/scripts/apollo_format.sh index 1f0beb16ed..7ae9358c47 100755 --- a/scripts/apollo_format.sh +++ b/scripts/apollo_format.sh @@ -33,7 +33,8 @@ function print_usage() { ${BLUE}-b|--bazel ${NO_COLOR}Format Bazel code ${BLUE}-c|--cpp ${NO_COLOR}Format cpp code ${BLUE}-s|--shell ${NO_COLOR}Format Shell code - ${BLUE}-a|--all ${NO_COLOR}Format all (C++/Python/Bazel/Shell) + ${BLUE}-m|--markdown ${NO_COLOR}Format Markdown file + ${BLUE}-a|--all ${NO_COLOR}Format all ${BLUE}-h|--help ${NO_COLOR}Show this message" } @@ -53,11 +54,16 @@ function run_shfmt() { bash "${TOP_DIR}/scripts/shfmt.sh" "$@" } +function run_prettier() { + bash "${TOP_DIR}/scripts/mdfmt.sh" "$@" +} + function run_format_all() { run_clang_format "$@" run_buildifier "$@" run_autopep8 "$@" run_shfmt "$@" + run_prettier "$@" } function main() { @@ -81,6 +87,9 @@ function main() { -s | --shell) run_shfmt "$@" ;; + -m | --markdown) + run_prettier "$@" + ;; -a | --all) run_format_all "$@" ;; -- GitLab