Skip to content

Instantly share code, notes, and snippets.

@MaverickChan
Created May 18, 2017 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaverickChan/dc6e2f5f371e39e9f40998a0f9f1377a to your computer and use it in GitHub Desktop.
Save MaverickChan/dc6e2f5f371e39e9f40998a0f9f1377a to your computer and use it in GitHub Desktop.
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键绑定,所以要手动插入换行
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment