Sublime Forum

On keymap changed event?

#1

Hi. What is the best way to recognize that any *.sublime-keymap resource file was modified?

0 Likes

#2

You’ll need to poll sublime.find_resources for additions and stat the resulting files, either directly on the file system or through/via the containing sublime-package archive. @ThomSmith is interested in implementing something similar into the sublime_lib dependency for packages to use.

0 Likes

#3

There’s no easy, reliable way to do this, but there are easy, mostly-reliable ways and hard, reliable ways.

The easiest approach is to read in a resource with sublime.load_binary_resource, hold on to the bytes, and then later check for changes by loading it again and comparing it to the saved version. (You can hash() the bytes to avoid keeping the whole file around.) The drawbacks to this approach are that:

  • You have to check every file ahead of time to get the initial value/hash.
  • You have to manually check to see whether a file has changed.
  • If the file has been modified, but the modification has been reverted, then you can’t observe this. In some cases, this is fine, but in others it may not be.
  • If you’re observing a lot of files, repeatedly reading the entire files could be bad for performance.

You can do something similar by checking file modification times. The drawbacks are mostly the same. You don’t have to read the entire file every time, and you may be able to skip the initial check (marking down an initial timestamp instead), but this is complicated to implement — you’d basically be reimplementing the resource system. A library could abstract away the implementation, but this would still leave subtle gotchas.

The proper solution is to use inotify or other heavyweight tools to monitor the filesystem. This comes with all the hassle of the previous solution, but you also have to deal with file monitoring on multiple platforms. (I don’t think that there’s a built-in library to do this, although it could be made into a dependency.) This solution annoys me: Sublime is already monitoring all of these files, so you’re redoing a lot of work that Sublime is doing anyway. (This could even lead to bugs — because your notifications and Sublime’s may happen in a nondeterministic order, so when you handle your notification, Sublime’s internals could see the old or the new version of the file essentially at random.)

The ideal solution is for Sublime to let you listen for resource changes via an EventListener. I should write that up and submit it.

0 Likes

#4

Yes, I already writed ViewEventListener.on_post_save_async() and I use hash for checking updates. Thanks, will wait for new events from sublime.

0 Likes

#5
0 Likes