sublime text 给选中项插入编号
2024-08-21 15:06:19
#coding=utf-8
import datetime, getpass
import sublime, sublime_plugin
import re # 插数字
class InsertNumberCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.window().show_input_panel("input start_num and step:", "start:1, step:1", lambda text: self.accumulate(text, edit), None, None)
def accumulate(self, text, edit):
text = re.sub(r"[^\d\+-]*([\+-]?\d+)[,\s\t]+[^\d\+-]*([\+-]?\d+)", r"\1 \2", text)
numbers = text.split(" ")
start_num = int(numbers[0])
diff_num = int(numbers[1])
for region in self.view.sel():
#(row,col) = self.view.rowcol(region.begin())
self.view.insert(edit, region.end(), "%d" %start_num)
start_num += diff_num
Sublime Text 3 版本运行会报错 ValueError: Edit objects may not be used after the TextCommand's run method has returned ,需要把 callback 改成一个独立的 command
import sublime
import sublime_plugin
import datetime, getpass
import re # 插数字
class InsertNumberCommand(sublime_plugin.TextCommand):
def run(self, edit):
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) class InsertNumberCbCommand(sublime_plugin.TextCommand):
def run(self, edit, text):
# sublime.message_dialog(text)
text = re.sub(r"[^\d\+-]*([\+-]?\d+)[,\s\t]+[^\d\+-]*([\+-]?\d+)", r"\1 \2", text)
numbers = text.split(" ")
start_num = int(numbers[0])
diff_num = int(numbers[1])
for region in self.view.sel():
#(row,col) = self.view.rowcol(region.begin())
self.view.insert(edit, region.end(), "%d" %start_num)
start_num += diff_num
求和:
#coding=utf-8
import datetime, getpass
import sublime, sublime_plugin # 求和
class SumCommand(sublime_plugin.TextCommand):
def run(self, edit):
sum_all = 0
for region in self.view.sel():
add = 0
str_region = self.view.substr(region)
try:
add = int(str_region)
except ValueError:
sublime.error_message(u"含有非数字的字符串")
return
sum_all = sum_all + add sublime.message_dialog(str(sum_all)) class SelectWordCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
reg = self.view.word(region)
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
最新文章
- background-position (转)
- Js内存回收
- 在JavaScript中,arguments是对象的一个特殊属性。
- ajax返回数据类型为XML数据的处理
- 李洪强iOS开发之宏定义方法来初始化一个单例对象
- iOS应用程序安全
- Windows服务定时运行,判断当前时间是否在配置时间段内
- POJ 1564 Sum It Up(DFS)
- Insert into a Cyclic Sorted List
- 《Windows驱动开发技术详解》之驱动程序调用驱动程序——通过设备指针调用其他驱动程序
- HDU 5616 Jam's balance
- 重温Javascript(四)-函数
- Linux使用系统光盘作为YUM源
- Linux 下编写服务器程序时关于Address already in use 的小错误
- [NOIP2018]普及组初赛题解
- Thirft框架介绍
- vs 编译库文件
- arm学习——有关位操作的总结
- NSURLRequest with UserAgent
- VUE 生命周期 详解
热门文章
- linux下nginx tomcat集群
- LINUX 笔记5
- POJ 3709 K-Anonymous Sequence - 斜率优化dp
- Eclipse下配置TinyOS开发环境
- 数学整合 新(LUOGU)
- C变参数函数demo
- IntelliJ IDEA 2017版 spring-boot使用Spring Data JPA使用Repository<;T, T>;编程
- UVa 10382 Watering Grass (区间覆盖贪心问题+数学)
- Yarn application has already exited with state FINISHED
- IDEA14添加SVN