Sublime Forum

Reload current file from a plugin

#1

Any of you happen to know how can i reload the content of the current file from a plugin? In my plugin i update the file contents using an external program, and at the end i want the view to reflect the updated file contents.

i tried adding the following to my plugin:

f = view.fileName();
wnd = view.window()
wnd.reloadFile(f, 0, 0)

OR

view.runCommand('revert')

but none seems to work, any suggestions?

0 Likes

#2

i found a workaround:

f = view.fileName();
s = open(file, 'r').read()
region = sublime.Region(0L, view.size())
view.replace(region, s)

If you think on a more elegant solution please let me know.

cheers!

0 Likes

#3

Try this:

import sublime, sublimeplugin
import functools

class ThingyCommand(sublimeplugin.TextCommand):
	def run(self, view, args):
		# ...code to write out file here
		sublime.setTimeout(functools.partial(view.runCommand, 'revert'), 0)

revert is somewhat byzantine, due mostly to file loading being async: it won’t work correctly unless it’s in the top level of an undo group, so you have to run it via setTimeout, rather than within a command.

0 Likes

#4

great! thanks

0 Likes