Python 升华文本3插件将运行更改为on_pre_save
我一直在开发一个Sublime Text 3插件,该插件修复了我在工作中的一些编码标准(我有一个不好的习惯,就是缺少这些标准),目前我正在控制台中运行一个命令 然后,您只需运行:Python 升华文本3插件将运行更改为on_pre_save,python,plugins,sublimetext3,Python,Plugins,Sublimetext3,我一直在开发一个Sublime Text 3插件,该插件修复了我在工作中的一些编码标准(我有一个不好的习惯,就是缺少这些标准),目前我正在控制台中运行一个命令 然后,您只需运行: view.run_command('replace') 它将应用编码标准(我计划实现更多的标准,但目前我将坚持这些标准),我希望它在save上运行 我试着将run(self,edit)更改为on_pre_save(self,edit),但它不起作用。我没有得到任何语法错误,但它就是不工作 有谁能告诉我如何在保存时运行
view.run_command('replace')
它将应用编码标准(我计划实现更多的标准,但目前我将坚持这些标准),我希望它在save上运行
我试着将run(self,edit)更改为on_pre_save(self,edit),但它不起作用。我没有得到任何语法错误,但它就是不工作
有谁能告诉我如何在保存时运行此命令,而不必运行命令吗?在ST3上,获取
编辑
对象的唯一方法是运行TextCommand
。(它在地图上,但不是很清楚)。但是,幸运的是,您可以以与以前几乎相同的方式运行该命令
事件处理程序(如保存前的)只能在上定义。on_pre_save()
事件传递了一个视图对象,因此您只需添加类似的内容,这将启动您已经编写的命令
class ReplaceEventListener(sublime_plugin.EventListener):
def on_pre_save(self, view):
view.run_command('replace')
在ST3上,获取
编辑
对象的唯一方法是运行TextCommand
。(它在地图上,但不是很清楚)。但是,幸运的是,您可以以与以前几乎相同的方式运行该命令
事件处理程序(如保存前的)只能在上定义。on_pre_save()
事件传递了一个视图对象,因此您只需添加类似的内容,这将启动您已经编写的命令
class ReplaceEventListener(sublime_plugin.EventListener):
def on_pre_save(self, view):
view.run_command('replace')
我找到的解决方案是创建一个on_pre_save()函数,该函数运行我前面列出的命令:
import sublime, sublime_plugin
class ReplaceCodeStandardsCommand(sublime_plugin.TextCommand):
def run(self, edit):
#for each selected region
region = sublime.Region(0, self.view.size())
#if there is slected text
if not region.empty():
#get the selected region
s = self.view.substr(region)
#perform the replacements
s = s.replace('){', ') {')
s = s.replace('}else', '} else')
s = s.replace('else{', 'else {')
#send the updated string back to the selection
self.view.replace(edit, region, s)
class ReplaceCodeStandardsOnSave(sublime_plugin.EventListener):
# Run ST's 'unexpand_tabs' command when saving a file
def on_pre_save(self, view):
if view.settings().get('update_code_standard_on_save') == 1:
view.window().run_command('replace_code_standards')
希望这段代码能帮助一些人 我找到的解决方案是创建一个on_pre_save()函数,该函数运行我前面列出的命令:
import sublime, sublime_plugin
class ReplaceCodeStandardsCommand(sublime_plugin.TextCommand):
def run(self, edit):
#for each selected region
region = sublime.Region(0, self.view.size())
#if there is slected text
if not region.empty():
#get the selected region
s = self.view.substr(region)
#perform the replacements
s = s.replace('){', ') {')
s = s.replace('}else', '} else')
s = s.replace('else{', 'else {')
#send the updated string back to the selection
self.view.replace(edit, region, s)
class ReplaceCodeStandardsOnSave(sublime_plugin.EventListener):
# Run ST's 'unexpand_tabs' command when saving a file
def on_pre_save(self, view):
if view.settings().get('update_code_standard_on_save') == 1:
view.window().run_command('replace_code_standards')
希望这段代码能帮助一些人 在我看到这个lol之前,我就明白了这一点。我已经编写了一个运行命令的TabsToSpaces/SpacesToTabs插件。然后我把2和2放在一起。在我看到这个lol之前,我就明白了。我已经写了一个运行命令的TabsToSpaces/SpacesToTabs插件。然后我把2和2放在一起。