用过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_plugin req_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