Can anyone explain to me how
View.set_reference_document() works. Thanks.
Can anyone explain to me how
def update_ref(): reference_content = "my new content to use as base for mini diff" view.set_reference_document(reference_content)
It simply passes a
string to be used as reference for the incremental diff.
string can be read from any file (e.g.: of an older revision from a vcs).
IMO the name
set_reference_document is misleading, I supposed it takes a filename (document) to compare the view against.
Keep in mind that any method in the API that accepts a filename include the word
file in the method name or parameter. Also,
document in development generally refers to the actual document, whereas a
filename refers to a reference to a document.
What if I want to make the reference of the incremental diff the contents of a file (that may be modified) that isn’t open in sublime ?
To do that, you would need to monitor the external file yourself to notice when it’s changing and then call the method again to update it.
Note that even Sublime doesn’t update the reference document when the disk file changes (except for files tracked by git). When you open an untracked file, that content remains as the reference document no matter how often you save the file; in order to reset it you need to either call the this API method manually or close and re-open the file.
You just open the file on disk, read its content and pass it to the API.
You should just make sure to read the file with the correct encoding. GitGutter does it by reading the view’s actual encoding and passing it to the
with open(the_filename_on_disc, mode='r', encoding=self.view_cache.python_friendly_encoding()) as file: self.view.set_reference_document(file.read())
I thought of that, but how to watch the file for changes ?
I was playing around with the
watchdog library in the past, but with all the edge cases and issues I’ve read about file change notifications, you should use it with care.
We’ve discussed it. The blocker on lots of issues is performance. Right now we sometimes run into issues where monitoring filesystem notifications in C++ can cause high CPU usage, especially if the user decides to “open” their whole home directory, or a directory that has constant churn.
Adding it to the API would require additionally:
- serializing all filesystem notifications
- sending the data over shared memory to both plugin hosts
- converting the data into Python data structures
- having each handler loop through and process each event
For users with reasonable size projects, this would be no problem. However, for users with large projects, it would cause severe performance issues.
I think for us to implement something like that, we’d have to find a way to flip it on its head and have plugins only listen to the bare minimum instead of getting a firehose of events.
BTW, I believe this is the canonical issue for the topic: https://github.com/sublimehq/sublime_text/issues/2669.