用過vs的人都知道,vs會自動在代碼中運算符的前后加空格,比如 i=1; 換行后會自動變成i = 1;
開始覺得這個挺煩的,后來習(xí)慣了,發(fā)現(xiàn)這個功能還是挺好的,然代碼更清晰.
最近換了sublimetext3,也想有類似功能,找了好久,沒有發(fā)現(xiàn)很滿意的,于是就自己動手了
代碼很簡單
使用方法(僅限于sublimetext3, 2的話沒試過):
在sublimetext3根目錄的Data/Packages目錄下見一個叫InsertSpace的目錄
InsertSpace目錄下建一個文件InsertSpace.py, 把InsertSpace的代碼復(fù)制下來在粘貼到這個文件
然后復(fù)制鍵綁定的代碼,粘貼到鍵綁定文件中
OK
注意:這個鍵綁定只在c,c++,py,cs文件中起作用,我是用來寫c的,所以只做了c運算符
要在其他文件中起作用,修改鍵綁定代碼中:
{ "key": "selector", ......
行,加入相應(yīng)的文件scope名即可,其他代碼文件運算符可能不一樣,要做相應(yīng)修改
insertSpace.py
import sublime, sublime_plugin
req_single_op_all = '(([\+\-\*/%=<>\|&\^\?]|(<<)|(>>))(=)|(!=))|((?<=[\w ])((<<)|(>>)|(&&)|(\|\|)|[\+\-\*/%=<>\|&\^\?])(?=[\w ]))'
#這個正則表達(dá)式匹配:
#+ - * / % = < > | & ^ ? << >> && || != += -= *= /= %= &= ^= |= <<= >>=
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)
#對于三目運算符,?號前后不加空格(因為已經(jīng)在匹配普通運算符時加了空格),':'號前后才加空格
@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()) #獲得當(dāng)前光標(biāo)所在位置的行列
OpUnit.subplg = self
OpUnit.subedit = edit
for op in op_units:
regex = op.regex
op_func = op.ins_func
#find函數(shù)會找到start_pos后第一個匹配正則表達(dá)式的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: #如果匹配到的文本已經(jīng)不再當(dāng)前行,則不再匹配
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鍵綁定的,它覆蓋了默認(rèn)的一個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},
]
},