-
Notifications
You must be signed in to change notification settings - Fork 0
/
nice_transform.py
executable file
·35 lines (32 loc) · 1.41 KB
/
nice_transform.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import sublime
import sublime_plugin
import hashlib
import base64
from .nice_transform_funcs import MY_TRANSFORMATIONS as TRANSFORMATIONS
class NiceTransformSelectCommand(sublime_plugin.ApplicationCommand):
def run(self):
# 提取菜单项名称
options = [name for name, _, _ in TRANSFORMATIONS]
def on_done(index):
if index >= 0:
# 提取转换类型,并执行转换命令
transform_type = TRANSFORMATIONS[index][1]
sublime.active_window().run_command(
"nice_transform", {"transform_type": transform_type}
)
# 在快速选择面板中显示所有转换选项
sublime.active_window().show_quick_panel(options, on_done)
class NiceTransformCommand(sublime_plugin.TextCommand):
def run(self, edit, transform_type=""):
selected_texts = [self.view.substr(region) for region in self.view.sel()]
for i, region in enumerate(self.view.sel()):
text = self.view.substr(region)
try:
# 查找对应的转换函数并执行
transformed_text = next(
func(text, i, selected_texts) for _, t, func in TRANSFORMATIONS if t == transform_type
)
# 替换选中文本
self.view.replace(edit, region, transformed_text)
except StopIteration:
pass