Home Download Buy Blog Forum Support

reload current file from a plugin

reload current file from a plugin

Postby vim on Sun Sep 21, 2008 8:44 am

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:
Code: Select all
f = view.fileName();
wnd = view.window()
wnd.reloadFile(f, 0, 0)

OR
Code: Select all
view.runCommand('revert')


but none seems to work, any suggestions?
vim
 
Posts: 298
Joined: Tue May 27, 2008 6:51 pm

Re: reload current file from a plugin

Postby vim on Sun Sep 21, 2008 8:58 am

i found a workaround:

Code: Select all
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!
vim
 
Posts: 298
Joined: Tue May 27, 2008 6:51 pm

Re: reload current file from a plugin

Postby jps on Thu Sep 25, 2008 3:29 pm

Try this:

Code: Select all
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.
jps
Site Admin
 
Posts: 3066
Joined: Wed Mar 19, 2008 12:33 pm

Re: reload current file from a plugin

Postby vim on Thu Sep 25, 2008 8:15 pm

great! thanks
vim
 
Posts: 298
Joined: Tue May 27, 2008 6:51 pm


Return to Plugin Announcements

Who is online

Users browsing this forum: No registered users and 10 guests