【发布时间】:2016-09-08 17:56:59
【问题描述】:
我一直在开发 Sublime Text 3 插件,该插件修复了我在工作中使用的一些编码标准(我有一个坏习惯)我目前正在使用控制台中运行的命令进行此操作。 Most of the code was originally from this thread.
import sublime, sublime_plugin
class ReplaceCommand(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)
然后你只需要运行:
view.run_command('replace')
它将应用编码标准(我计划实施更多但现在我会坚持这些)我希望它在保存时运行。
我尝试将 run(self, edit) 更改为 on_pre_save(self, edit) 但它不起作用。我没有收到任何语法错误,但它不起作用。
谁能告诉我如何让它在保存时运行而不是运行命令?
【问题讨论】:
标签: python plugins sublimetext3