sublime text 给选中项插入编号

 1 #coding=utf-8
 2 import datetime, getpass
 3 import sublime, sublime_plugin
 4 import re
 5 
 6 # 插数字
 7 class InsertNumberCommand(sublime_plugin.TextCommand):
 8     def run(self, edit):
 9         self.view.window().show_input_panel("input start_num and step:", "start:1, step:1", lambda text: self.accumulate(text, edit), None, None)
10     def accumulate(self, text, edit):
11         text =  re.sub(r"[^d+-]*([+-]?d+)[,s	]+[^d+-]*([+-]?d+)", r"1 2", text)
12         numbers = text.split(" ")
13         start_num   = int(numbers[0])
14         diff_num    = int(numbers[1])
15         for region in self.view.sel():
16             #(row,col) = self.view.rowcol(region.begin())
17             self.view.insert(edit, region.end(), "%d" %start_num)
18             start_num += diff_num

Sublime Text 3 版本运行会报错  ValueError: Edit objects may not be used after the TextCommand's run method has returned ,需要把 callback 改成一个独立的 command

 1 import sublime
 2 import sublime_plugin
 3 import datetime, getpass
 4 import re
 5 
 6 # 插数字
 7 class InsertNumberCommand(sublime_plugin.TextCommand):
 8     def run(self, edit):
 9         self.view.window().show_input_panel("input start_num and step:", "start:1, step:1", lambda text: self.view.run_command('insert_number_cb', {"text": text}), None, None)
10 
11 
12 class InsertNumberCbCommand(sublime_plugin.TextCommand):
13     def run(self, edit, text):
14         # sublime.message_dialog(text)
15         text =  re.sub(r"[^d+-]*([+-]?d+)[,s	]+[^d+-]*([+-]?d+)", r"1 2", text)
16         numbers = text.split(" ")
17         start_num   = int(numbers[0])
18         diff_num    = int(numbers[1])
19         for region in self.view.sel():
20             #(row,col) = self.view.rowcol(region.begin())
21             self.view.insert(edit, region.end(), "%d" %start_num)
22             start_num += diff_num

求和:

 1 #coding=utf-8
 2 import datetime, getpass
 3 import sublime, sublime_plugin
 4 
 5 # 求和
 6 class SumCommand(sublime_plugin.TextCommand):
 7     def run(self, edit):
 8         sum_all = 0
 9         for region in self.view.sel():
10             add = 0
11             str_region = self.view.substr(region)
12             try:
13                 add = int(str_region)
14             except ValueError:
15                 sublime.error_message(u"含有非数字的字符串")
16                 return
17             sum_all = sum_all + add
18 
19         sublime.message_dialog(str(sum_all))
20 
21 class SelectWordCommand(sublime_plugin.TextCommand):
22     def run(self, edit):
23         for region in self.view.sel():
24             reg = self.view.word(region)
25             self.view.sel().add(reg)

写配置文件时,可以根据数据的规律,编写适当的函数。根据 count 计算对应的数据。

 

测试:

  ctrl + ` 打开 command window

  输入 view.run_command('insert_number') 回车

  

http://sublime-text-unofficial-documentation.readthedocs.org/en/latest/extensibility/plugins.html

步骤:

Tools -> New Plugin...

粘贴以上代码,两份代码可以放在同一个文件里

command + s 保存文件为 insertNumber.py   文件名可以随便取~

快捷键设置:

Sublime Text 2 -> Preferences -> Key Bindings - User

如果打开的文件为空,可以参考 Key Bindings - Default 文件中的格式,添加一条新的配置

{ "keys": ["super+shift+f5"], "command": "insert_number" }

"insert_number" 对应类名 InsertNumberCommand

+V d2h5X251bGw= 请备注:from博客园
原文地址:https://www.cnblogs.com/hangj/p/3954175.html