Sublime Forum

How to run command synchronously after set_project_data has finished

#1

I got a command which first uses set_project_data to update the project and then it tries to find a certain match in the file (text) and open the sublime-project moving the cursor on that particular line/column. Problem I’ve faced here is sometimes after doing set_project_data my command will open the view on a wrong line/column. Is it set_project_data a sync or async command? In fact, a more general question would be, if a function described in the API docs doesn’t have a callback argument, can we safely assume is a synchronous command by default?

In case set_project_data, would my only options be using set_timeout and set_timeout_async? As far as i know this command doesn’t accept a callback so I wouldn’t know any other way to know when the command has finished the execution.

0 Likes

#2

Actually, I think I was wrong assuming set_project_data is an async command, I think this one is synchronous… what is going on is this:

  • I got opened the sublime-project with the focus
  • I use set_project_data to update the project data
  • I move the cursor to the right position
  • But… after the cursor is in the right position, the view will be refreshed/reloaded and it’s at with refresh action when the cursor becomes misplaced.

So, any suggestion to parse the updated refreshed view after doing set_project_data? Maybe any listener to look at?

Thanks.

0 Likes

#3

Actually I’ve found a pretty simple solution to my problem, before using set_project_data, just close all sublime-project views (if any) and once you’ve used set_project_data opening the file in the right line/column, it seems it works now! :slight_smile:

0 Likes

#4

Updating the project data associated with the current window causes Sublime to also update the underlying file if the project data changed. As such, if the project file associated with the current window is already open when you call set_project_data, Sublime will notice the file is different and either reload it automatically or ask you if you want to reload it and then do so.

There is no event that I’m aware of that directly tells you that a file has been reloaded, but the reload operation does trigger the on_modified event handler when the reload happens:

>>> proj_data = window.project_data()
>>> proj_data["settings"]["word_wrap"] = False
>>> window.set_project_data(proj_data)
reloading /home/tmartin/local/sublime_text_3_sandbox/Data/Packages/User/test.sublime-project
on_modified: /home/tmartin/local/sublime_text_3_sandbox/Data/Packages/User/test.sublime-project
on_modified: /home/tmartin/local/sublime_text_3_sandbox/Data/Packages/User/test.sublime-project

There is some delay there which I assume is because the inotify listener that notices the file changing does it’s own thing asynchronously. This delay is increased if the user has configured Sublime to not reload without prompting, as in that case you need to wait until the user responds as well.

What happens if the user modified the open project file but didn’t save it?

Also I hope by “all sublime-project views” you mean “the sublime-project view for the project associated with the current window”, otherwise your code is going to probably upset someone some day when suddenly an unrelated file they’re editing (say to copy/paste some data from) is just mysteriously closed.

Just out of curiosity, what exactly were you trying to achieve here?

0 Likes

#5

Some commands I’m working on are editing the underlyig .sublime-project by adding/removing info into/from it without the user required to do it “manually”.

Thing is, it’s quite fast (at least to me) if the user has added a lot of info to the project at certain point and the project has become really unmanageable, that way he can just go-to directly to the relevant subproject section and inspecting their properties.

For instance, imagine you’re contributing to a large project like Chromium in ST where there are dozens of subprojects, it’s definitely handy to inspect the properties of these different subproject easily without examining manually the sublime-project, right? So that’s pretty much… :slight_smile: .

Even better, think about an IDE like Visual Studio, your workspace can have loaded many subprojects loaded and the user can edit easily the subproject properties by using the mouse without much effort without editing the .sln, .dsw, .vcproj or other project files… the idea I got in mind is pretty much the same here.

0 Likes