I am working on an Sublime Text 3 plugin and I have so far a little script which copy all text from current file to another using three classes :
import sublime, sublime_plugin
# string and new file created
s = 0
newFile = 0
class CreateNewWindowCommand(sublime_plugin.WindowCommand):
def run(self):
global s, newFile
s = self.window.active_view().substr(sublime.Region(0, self.window.active_view().size()))
newFile = self.window.new_file()
class CopyTextCommand(sublime_plugin.TextCommand):
def printAChar(self,char,edit):
self.view.insert(edit, 0, char)
def run(self, edit):
global s
st = list(s)
for i in st[::-1]:
self.printAChar(i, edit)
class PrintCodeCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command("create_new_window")
newFile.run_command("copy_text")
the script is running via the PrintCodeCommand first.
I have multiple questions regarding this code :
- Is this the 'right' method to do ? Because passing stuff with global variables seems a little dirty.
- Is there a way to create a class that can use WindowCommand and TextCommand at the same time ?
- the insert command (in CopyTextCommand) insert in the first place, is there a way to append at the end of the file ?
And another one : How can I use sublime.set_timeout() ? Because like this :
# ...
class CopyTextCommand(sublime_plugin.TextCommand):
def printAChar(self,char,edit):
sublime.set_timeout(self.view.insert(edit, 0, char) , 1000)
# I want to print a char one per second
Or using the time.sleep() command but it doesn't seem to work...
Thanks in advance !