I added one more even listener to the plugin.
I am using the TFS and if the read-only attribute was cleared for the file, later I’d have to find the file, properly check it out and commit. Of course, I have the Sublime TFS plugin installed. But it is hard to remember which files were already checked out.
In order to make it little easier, a new listener was added to the plugin. Now, if a read-only file is being edited, the plugin will prompt if user wants to check the file out first. After that check out will continue in the background (and in the Sublime status bar) and user can continue editing the file.
To avoid re-prompting on every keystroke, buffer_id will be saved and user will not be asked about the view again.
import sublime, sublime_plugin, os, stat, sublime_tfs
known_views = {}
class CustomFileCommands(sublime_plugin.EventListener):
def on_modified(self, view):
global known_views
id = view.buffer_id()
myFile = view.file_name()
fileAtt = os.stat(view.file_name())[0]
if (not (id in known_views) and (not fileAtt & stat.S_IWRITE)):
known_views[id] = True
if(sublime.ok_cancel_dialog('The file is Read-Only. Check out?', 'Go ahead!')):
tfs = sublime_tfs.TfsCheckoutCommand(view)
tfs.run(view)
def on_pre_save(self, view):
myFile = view.file_name()
fileAtt = os.stat(view.file_name())[0]
if view.is_dirty():
if (not fileAtt & stat.S_IWRITE):
if(sublime.ok_cancel_dialog('The file is Read-Only. Overwrite?', 'Overwrite!')):
os.chmod(myFile, stat.S_IWRITE)