Sublime Forum

How to refresh a sublime-project programatically

#1

I’m trying to figure out how to reload a sublime-project programatically in a plugin but I don’t know how to do it. My last attempt was this one:

# 1) Close all sublmie-project views first
for view in self.window.views():
    if view.file_name()==dst_project_path:
        view.set_scratch(True)
        self.window.focus_view(view)
        self.window.run_command("close")

# 2) Parsing/Saving of the *.sublime_project (dst_project_path) GOES HERE
...
# End of parsing

# 3) Reload the project, the below one DOESN'T WORK
#
# I'd expect this line to reload the project magically on the window, unfortunately
# it doesn't work. But... why when i open the .sublime-project manually and press
# ctrl+s it does the reloading properly?
self.window.open_file(dst_project_path).run_command("save")

So, could anyone explain me how to reload a sublime-project in the same view after the plugin has done some parsing&saving?

Thanks in advance.

1 Like

#2

I’ve thought maybe my last attempt of reloading was because the view opened by open_file wouldn’t have a proper focus, so my next attempt was:

view = self.window.open_file(dst_project_path)
view.set_scratch(True)
self.window.focus_view(view)
self.window.run_command("save")

But no luck so far, It’s still not reloading the project :frowning: , hopefully somebody can bring some light here.

0 Likes

#3

I guess, if you use window.set_project_data() API, changes should be reloaded automatically?

1 Like

#4

Yay! Wonderful, thanks a lot :slight_smile: . My first tests indicates that one is exactly what i needed to use in the first place.

Now I need to think about which cases this command could not work. Right now the code looks like this:

# 1) Close all sublmie-project views first
for view in self.window.views():
    if view.file_name()==dst_project_path:
        view.set_scratch(True)
        self.window.focus_view(view)
        self.window.run_command("close")

# 2) Parsing/Saving of the *.sublime_project (dst_project_path) GOES HERE
...
# End of parsing

# 3) Reload the project
self.window.set_project_data(parsed_sublime_project_json)

Maybe @wbond can bring some light about this one? For instance, do you foresee any case where the project wouldn’t be reloaded succesfully? Asking this cos I’ve experienced some situations in the past where the project build system wouldn’t be loaded for some reason in the command palette, that’s why asking you in the first place :wink:

0 Likes

#5

Btw, as pointed out by @OdatNurd , the first step of closing all sublime-project views first is not necessary, that’s already handled wonderfully by set_project_data itself

0 Likes

#6

Unfortunately I haven’t tried to use set_project_data() to reload a project before, so I don’t have any experience to add.

0 Likes

#7

Alright, thanks anyway, so far so good, the reload works wonderfully. I guess if I find some issues in the future I’ll come back to this thread :wink: . Moving on

0 Likes