Skip to content

Commit c74b54f

Browse files
committed
new file: _posts/2018-01-06-cpplint-config.md
1 parent 4aa541e commit c74b54f

File tree

6 files changed

+110
-2
lines changed

6 files changed

+110
-2
lines changed

_posts/2018-01-06-cpplint-config.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
title: vim 配置 cpplint
3+
tags: cpplint Syntastic C++ Vim
4+
---
5+
6+
[cpplint][cpplint] 是 Google C++ 代码规范的静态检查工具,是一个简单的 Python 脚本。
7+
借助 [Syntastic][syntastic] 工具,可以配置为 C++ 文件的检查工具。
8+
本文只介绍 C++ Linting 的配置,关于代码所进、注释、自动补全、括号匹配等
9+
配置方式请参考 [这篇文章](/2015/07/18/vim-cpp.html)。先上图:
10+
11+
![vim-cpplint](/assets/img/blog/vim/cpplint-vim@2x.png)
12+
13+
<!--more-->
14+
15+
# 安装 Syntastic
16+
17+
Syntastic 是 Vim 中的语法检查框架,可以对不同的文件应用不同的外部检查工具。
18+
并将所有检查错误聚合并显示在 Vim 中。如果你在使用 [Vundle][vundle]
19+
安装 Syntastic 很方便,在 `~/.vimrc` 加一行配置:
20+
21+
```vim
22+
Plugin 'scrooloose/syntastic'
23+
```
24+
25+
在 Vim 中执行 `:PluginInstall` 即可安装成功。
26+
如果你本地安装有 `gcc` 就已经有 C++ 语法检查功能了,打开一个 C++ 文件。
27+
左侧会显示所有语法错误的位置,光标所在行的错误信息在状态栏给出。
28+
29+
![syntastic cpp](/assets/img/blog/vim/syntastic-cpp@2x.png)
30+
31+
现在给出的错误是通过一次编译得到**语法错误**,不是**代码风格问题**
32+
如果没有 gcc,你可以把你的编译器设置上去:
33+
34+
```vim
35+
let g:syntastic_cpp_compiler = 'clang++'
36+
```
37+
38+
参考文档:<https://github.com/vim-syntastic/syntastic/wiki/(v3.1.0)---C--:---gcc>
39+
40+
# 安装 cpplint
41+
42+
除了**语法检查**外,我们还想进行**代码风格检查**,比如空格、缩进、命名等问题。
43+
这时我们就需要 [cpplint][cpplint],这时一个外部程序。先安装这个工具(可能需要管理员权限):
44+
45+
```bash
46+
pip install cpplint
47+
```
48+
49+
cpplint 装好之后就立即可用了,下图中我执行了 `cpplint /tmp/hello-world.cpp`
50+
它说我没有 Copyright 文件头,不应该使用 `using namespace std;`
51+
调用 `string` 构造时多一个空格。
52+
53+
![syntastic cpp](/assets/img/blog/vim/cpplint@2x.png)
54+
55+
> 上图中的 `[5]` 表示错误级别是 5,可以通过这个级别进行错误过滤,见下文。
56+
57+
[Harttle](/) 有个需求,我就是要写 `using namespace std;`,还有不要 Copyright 声明。
58+
可以在项目根目录中加入 `CPPLINT.cfg` 文件,其内容如下:
59+
60+
```
61+
filter=-build/namespace,-legal/copyright
62+
```
63+
64+
多个 filter 以逗号分隔,更多参数请参考 `cpplint --help`
65+
66+
# 配置到 Vim
67+
68+
现在 cpplint 已经可以用了,我们把这个外部工具配置到 Syntastic,
69+
用作 C++ 文件类型的检查就大功告成了。
70+
71+
`~/.vimrc` 中,除了上述的语法检查(gcc)之外,我们再定义一个 `cpplint`
72+
把它加到 `checkers` 中。
73+
74+
```vim
75+
let g:syntastic_cpp_cpplint_exec = 'cpplint'
76+
let g:syntastic_cpp_checkers = ['cpplint', 'gcc']
77+
" 设置 cpplint 的错误级别阈值(默认是 5),级别低于这一设置的不会显示
78+
let g:syntastic_cpp_cpplint_thres = 1
79+
```
80+
81+
注意需要设置 *错误聚合*,才能同时显示两个 checker 的错误:
82+
83+
```vim
84+
let syntastic_aggregate_errors = 1
85+
```
86+
87+
参考文档:<https://github.com/vim-syntastic/syntastic/wiki/(v3.1.0)---C--:---cpplint>
88+
89+
# 最终效果
90+
91+
至此,Harttle 的 Vim 中已经可以同时显示语法错误和代码风格错误了。
92+
语法错误显示为 "✗",代码风格错误显示为 "!":
93+
94+
![vim-cpplint](/assets/img/blog/vim/cpplint-vim@2x.png)
95+
96+
有没有 Harttle 的 Vim 左侧的错误标识和你的不一样?这些字符可以设置的,
97+
下面的设置分别是语法错误标识、语法警告标识、风格错误标识、风格警告标识:
98+
99+
```vim
100+
let g:syntastic_error_symbol = "✗"
101+
let g:syntastic_warning_symbol = "⚠"
102+
let g:syntastic_style_error_symbol = '!'
103+
let g:syntastic_style_warning_symbol = '?'
104+
```
105+
106+
[cpplint]: https://github.com/google/styleguide/tree/gh-pages/cpplint
107+
[syntastic]: https://github.com/vim-syntastic/syntastic
108+
[vundle]: https://github.com/gmarik/vundle#about

_vim-practice/2015-07-18-vim-cpp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: 在VIM下写C++能有多爽?
3-
tags: C++ NERDCommenter Vim Vundle YCM 快捷键 代码风格
3+
tags: C++ NERDCommenter Vim YCM 快捷键 代码风格
44
---
55

66
我是坚定的Vim党,因为和命令行的集成如此之好,Vim可以解决任何问题。然而在这个世界上,即便是最好用的工具也是需要配置的。

_vim-practice/2015-11-22-vim-frontend.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: 打造前端开发的 Vim 环境
3-
tags: CSS HTML JavaScript Vim YCM 快捷键 Syntastic 代码风格
3+
tags: CSS HTML JavaScript Vim YCM Syntastic
44
---
55

66
前不久harttle着手搭建了[类似IDE的Vim环境][vim-ide],然而对于前端开发者这还远远不够。
146 KB
Loading

assets/img/blog/vim/cpplint@2x.png

171 KB
Loading
146 KB
Loading

0 commit comments

Comments
 (0)