让sublime可以和visual studio一样自动在运算符前后添加空格的插件
来源:互联网 发布:linux grep指令 编辑:程序博客网 时间:2024/06/04 07:23
用过vs的人都知道,vs会自动在代码中运算符的前后加空格,比如 i=1; 换行后会自动变成i = 1;
开始觉得这个挺烦的,后来习惯了,发现这个功能还是挺好的,然代码更清晰.
最近换了sublimetext3,也想有类似功能,找了好久,没有发现很满意的,于是就自己动手了
代码很简单
使用方法(仅限于sublimetext3, 2的话没试过):
在sublimetext3根目录的Data/Packages目录下见一个叫InsertSpace的目录
InsertSpace目录下建一个文件InsertSpace.py, 把InsertSpace的代码复制下来在粘贴到这个文件
然后复制键绑定的代码,粘贴到键绑定文件中
OK
注意:这个键绑定只在c,c++,py,cs文件中起作用,我是用来写c的,所以只做了c运算符
要在其他文件中起作用,修改键绑定代码中:
{ “key”: “selector”, ……
行,加入相应的文件scope名即可,其他代码文件运算符可能不一样,要做相应修改
insertSpace.py
import sublime, sublime_pluginreq_single_op_all = '(([\+\-\*/%=<>\|&\^\?]|(<<)|(>>))(=)|(!=))|((?<=[\w ])((<<)|(>>)|(&&)|(\|\|)|[\+\-\*/%=<>\|&\^\?])(?=[\w ]))'#这个正则表达式匹配:#+ - * / % = < > | & ^ ? << >> && || != += -= *= /= %= &= ^= |= <<= >>= reg_tri_op = '\?[\w ]*:'#三目运算符?:,需要特殊处理,因为':'号在switc case语句中前后不需要加空格reg_comma = '(,|;)(?=[\w])'#','';'运算符,需要特殊处理,因为一般','';'前面不加空格,而后面加空格char_to_ins = ' 'class OpUnit: subplg = None subedit = None def __init__(self, regex, ins_func): self.regex = regex self.ins_func = ins_func #对于一般的运算符,前后都要加空格 @staticmethod def singleOpInsert(begin, end): view = OpUnit.subplg.view edit = OpUnit.subedit if view.substr(begin - 1) != char_to_ins: view.insert(edit, begin, char_to_ins) if view.substr(end + 1) != char_to_ins: view.insert(edit, end + 1, char_to_ins) elif view.substr(end) != char_to_ins: view.insert(edit, end, char_to_ins) #对于三目运算符,?号前后不加空格(因为已经在匹配普通运算符时加了空格),':'号前后才加空格 @staticmethod def triOpInsert(begin, end): view = OpUnit.subplg.view edit = OpUnit.subedit if view.substr(end - 2) != char_to_ins: view.insert(edit, end - 1, char_to_ins) if view.substr(end + 1) != char_to_ins: view.insert(edit, end + 1, char_to_ins) elif view.substr(end) != char_to_ins: view.insert(edit, end, char_to_ins) #对于','';'运算符,只在后面加空格 @staticmethod def commaOpInsert(begin, end): view = OpUnit.subplg.view edit = OpUnit.subedit if view.substr(end) != char_to_ins: view.insert(edit, end, char_to_ins)op_units = (OpUnit(req_single_op_all, OpUnit.singleOpInsert), OpUnit(reg_tri_op, OpUnit.triOpInsert), OpUnit(reg_comma, OpUnit.commaOpInsert))class InsertSpaceCommand(sublime_plugin.TextCommand): def run(self, edit): cur_region = self.view.sel()[0] (cur_row,cur_col) = self.view.rowcol(cur_region.end()) #获得当前光标所在位置的行列 OpUnit.subplg = self OpUnit.subedit = edit for op in op_units: regex = op.regex op_func = op.ins_func #find函数会找到start_pos后第一个匹配正则表达式的region, start_pos = self.view.text_point(cur_row , 0) matched_region = self.view.find(regex, start_pos) (row, col) = self.view.rowcol(matched_region.end()) while row == cur_row: #如果匹配到的文本已经不再当前行,则不再匹配 begin = matched_region.begin() end = matched_region.end() if begin < 0 or end < 0: break; op_func(begin,end) start_pos = end + 1 matched_region = self.view.find(regex, start_pos) (row, col) = self.view.rowcol(matched_region.end()) self.view.run_command("insert", {"characters": "\n"}) #最后需要插入一个换行符, 因为这是与enter键绑定的,它覆盖了默认的一个enter键绑定,所以要手动插入换行
按键绑定
{ "keys": ["enter"], "command": "insert_space", "context": [ { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }, { "key": "following_text", "operator": "not_regex_contains", "operand": "^\\}", "match_all": true }, { "key": "selector", "operator": "equal", "operand": "(source.c, source.c++, source.python, source.cs)", "match_all": true }, { "key": "panel_has_focus", "operator": "not_equal", "operand": true}, { "key": "auto_complete_visible", "operator": "equal", "operand": false}, ]},
转自https://www.oschina.net/code/snippet_1016548_56822
阅读全文
0 0
- 让sublime可以和visual studio一样自动在运算符前后添加空格的插件
- visual studio下自动添加注释的插件-VAssistX
- sublime写python3可以添加的插件
- 让MyEclipse具有Visual Studio一样强大的代码提示
- 让Visual Studio 自动添加头部注释信息
- 让你的Visual Studio 2010自动添加头部注释信息
- 如何在 Visual Studio 2015 中自动给 cpp 和 h 文件添加头部注释
- visual studio 2013 添加背景图片 让你在coding的同时有动力
- Visual Studio自动添加引用
- 如何让你的visual studio自动编译typescript
- 让你的MyEclipse像Visual Studio 2008一样拥有强大功能智能感知功能
- 去除字符串中的空格,字符串前后的和字符串中的都可以
- Visual Studio-自动添加定制的方法注释
- 去除NSString字符串的前后空格和换行符
- 去除NSString字符串的前后空格和换行符
- sublime 自动添加兼容前缀插件autoprefixer
- 让sublime可以在浏览器中预览
- 让studio 和 eclipe 一样快捷键
- 编程之战第一章 你好世界
- MySQL状态
- 异常与文件
- JDBC
- 用hibernate有时不能删除子表的数据
- 让sublime可以和visual studio一样自动在运算符前后添加空格的插件
- jqgrid 重新加载数据.
- 服务器性能分析工具
- charge-station HDU
- Golang教程目录
- 172. Factorial Trailing Zeroes
- (信源五)最小均方算法--LMS
- HDU 2389 Rain on your Parade(Hopcroft-Carp算法板子题)
- Android Fragment 真正的完全解析(上)