Skip to main content

工具函数

启动debug模式

sublime.log_commands(True)

获取当前配置

options = SETTINGS.get(syntax, {})

获取当前项目文件夹


获取当前文件名


获取当前光标位置

def get_currt_coords(view) -> int:
currt_cursor = view.sel()[0].a
return currt_cursor

获取当前行内容

def get_currt_coords(view) -> str:
currt_cursor = view.sel()[0].a
curt_line: sublime.Region = view.full_line(currt_cursor)
line_content: str = view.substr(curt_line)
return line_content

获取当前语法 syntax

def get_syntax(view):
return view.settings().get('syntax')

更改当前视区

postition = self.view.sel()[0].a

# 滚动位置到视区中心
self.view.show_at_center(postition)

判断选区是否空的

def has_selection() -> bool:
if self.view.sel()[0].empty() :
print('当前没有任何文件')
return False
return True

获取选取内容

region = self.view.sel()[0]
buffer_str = self.view.substr(region)

def substr_to_str(region):
return view.substr(region)

获取当前有多少字符串数量

size:int = self.view.size()

清空当前视图数据

def clean(view:sublime.View, edit:sublime.Edit):
view.replace(edit, sublime.Region(0, view.size()), "")

获取数据

def get_view_context(view) -> str:
region = sublime.Region(0, view.size())
# 将region的内容转换为字符串
return view.substr(region)

插入数据

  • 通过 view.insert
context = '''
@ author:cps
'''

def insert_data(
view:sublime.View,
edit:sublime.Edit,
context:str,
insert_poststion:int=0):

view.insert(edit, insert_poststion, context)
  • 通过 view.run_command
context = '''
@ author:cps
'''

def insert_data_async(
view:sublime.View,
edit:sublime.Edit,
context:str,
insert_poststion:int=0):

view.run_command(
'append',
{'characters':characters, 'force':True, 'scroll_to_end': True}
)

替换数据

def replace_context(
view:sublime.View,
edit:sublime.Edit,
data:str,
region:sublime.Region,
)
view.replace(edit, region, data)

点转行列 point:int -> rowcol:(int,int)

# 通过 Ponit 获取行
rowcol = sublime.View.rowcol(point)

信息窗口 panel

# 获取所有消息窗口
panels:list[str] = sublime.active_window().panels()

# 获取当前已弹出的窗口
res:str = sublime.active_window().active_panel()

# 弹出显示指定的panel
def show_panel(panel_name:str):
sublime.active_window().run_cammand(
'show_panel',{
'panel':panel_name
})

# 创建panel
def create_panel(name:str) -> Optional[sublime.View]
window = sublime.active_window()

panel = window.find_output_panel(name) or
window.find_output_panel(f'output.{name}') or
None

if not panel:
panel = window.create_output_panel(name)

return panel

监听当前使用了什么内置命令

class BintinCommandListener(sublime_plugin.EventListener):
def on_window_command(self, window: sublime.Window, command_name: str, args: Dict) -> None:
listen_command = ('move', 'show_panel', 'hide_panel')
if command_name in listen_command:
sublime.set_timeout(lambda: self.method(argv))

def method(self):
pass