Sublime(mac/windows) with cpplint check——code-style of C++

sublime是一个轻量级的编辑器,是现在使用非常广泛的一个编辑器。可以渲染编辑很多种语言。本博客讲解使用sublime加载cpplint插件,加入代码风格的检查功能。
这篇博客是在同事的onenote的基础上写成的,感谢他。


首先安装sublime3

sublime跨平台,支持mac和windows,下载相应的版本即可
sublime

安装python2.7

这个插件需要python2.7的支持, 注意必须是2.7版本,亲测3.x版本不支持
windows直接下载对应的版本即可,mac应该自带,没有的话,也可以下载对应的版本安装即可。

python2.7

安装python的包管理器pip

pip 是python的第三方包管理器,可以方便的实现包管理,包括安装和卸载
直接运行下边连接的get-pip.py脚本即可完成安装

python get-pip.py

get-pip.py

安装sublime的插件管理器

copy below code in your sublime console, or click the below link, do with that struction.
for sublime3 with below code in console which can accessed via ctrl/cmd+`

import urllib.request,os,hashlib; h = '6f4c264a24d933ce70df5dedcf1dcaee' + 'ebe013ee18cced0ef93d5f746d80ef60'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)

package Control Installation

pip安装cpplint模块

pip install cpplint

如果出现安装错误,尤其是在企业环境下,需要设置代理,如下

设置代理

  1. windows
    我的电脑右键->属性->左侧高级系统设置->环境变量->在系统环境变量一栏,
    如下图处置
    代理变量设置
  2. mac
    修改~/.bashrc文件(对当前用户有效),/etc/profile(对所有用户有效)添加下边的行,截图如下:
    mac环境变量配置

sublime使用包管理器安装两个插件

安装的插件名字-SublimeLinter, SublimeLinter-cpplint
sublime安装插件的命令ctrl+shift+p for windows, cmd+shift+p for mac

Install package
SublimeLinter
SublimeLinter-cpplint

将起实际作用的脚本放到指定目录下

cpplint.py
download the cpplint file from my github to your local folder,
1. for windows
把上述的文件包装在bat文件里面,比如将文件

For example, I put this cpplint.py to C:\code, then make cpplint.bat in the same folder.
cpplint.bat:
python C:\code\cpplint.py %*

cpplint.bat
local file folder

配置sublime sublimeLinter

可以直接使用下边连接的模板文件

{
    "debug": true,
    "delay": 0.3,
    "gutter_theme": "Default",
    "highlights.time_to_idle": 1.5,
    "highlights.demote_while_editing": "none",
    "highlights.demote_scope": "",
    "lint_mode": "load_save",

    "linters": {
        "cpplint": {
            "@disable": false,
            "args": [],
            "disable": false,
            "excludes": [],
            "filter": "",
            "executable": ["c:\\code\\cpplint.bat"],
        }
    },

    "no_column_highlights_line": false,
    "show_hover_line_report": true,
    "show_hover_region_report": true,
    "show_marks_in_minimap": true,
    "show_panel_on_save": "window",
    "statusbar.counters_template": "W: {warning} E: {error}",
    "statusbar.messages_template": "{message}",

    "styles": [
        {
            "mark_style": "stippled_underline",
            "priority": 1,
            "scope": "source.js meta.function-call.method.js support.function.dom.js",
            "icon": "pointer",
            "types": [
                "warning"
            ]
        },
        {
            "mark_style": "stippled_underline",
            "priority": 1,
            "scope": "source.js meta.block.js keyword.operator.assignment.js",
            "icon": "pointer",
            "types": [
                "error"
            ]
        }
    ],

    "syntax_map": {
        "C": "cpplint",
        "C++": "cpplint",
        "html (django)": "html",
        "html (rails)": "html",
        "html 5": "html",
        "javascript (babel)": "javascript",
        "magicpython": "python",
        "php": "html",
        "python django": "python",
        "pythonimproved": "python"
    }
}

文件的位置如下图:
setting1
setting2

注意修改可执行文件的路径为自己的路径

setting3

  1. for mac
    mac不需要包装py文件,直接使用py文件,将可执行路径更改为mac下路径即可

    • 可能出现文件找不到,原因是从windows拷贝的文件换行符的差异,使用dos2unix转换一下格式即可。
    • 可能没有权限,为python文件加上可执行权限

    chmod a+x cpplint.py

重启sublime


当你ctrl+s保存文件的时候,会在sublime的底部控制台出现检查结果

enjoy it.

This article is based on the original one which wirte by colleague, who find this way, and make it work. Great Thanks to him.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值