Sublime Forum

Getting Edit Instance Within on_load Event

#1

My plug in uses on_load to look for certain file types. When found, the code processes the file contents and ends up with a JSON-formatted string. The plan was to open a new file and add the JSON to this window. e.g:

class FileConverter(sublime_plugin.EventListener):
	def on_load(self, view):

json_results = ProcessFile()
#put results in new file/tab
new_file = view.window().new_file()
new_file.insert(edit, 0, json_results) <- this does not work as I do not have an instance of edit, nor can I create one in ST3

According to the latest(?) documentation, Edit objects can oly be obtained through a TextCommand; they cannot be created in code. So how can I add text to a new file/tab without going through a TextCommand. Or if I create a TextCommand, how would I associate its edit instance with my new file/tab?

0 Likes

#2
sublime.run_command('insert', { 'characters': json_results })

will work in place of your new_file.insert

note that executing the insert command requires the caret to be in the desired place already, as AFAIK, one can’t specify a location

0 Likes

#3

Thanks. That did the trick. For the record, I had to use the view I got back from the new window to get the text into the new tab/file:

new_file = view.window().new_file()
new_file.run_command('insert', { 'characters': project_file })

Thanks for the assist.

0 Likes