Sublime Text 2 live multi-user edit detection

984 Views Asked by At

Me and a buddy of mine have been working on a website project using Sublime Text 2 as an editor. We're hosting everything locally, and we love Sublime but sometimes things get tangled when we're both trying to edit the same file, or when we both have the file open but don't realize we're not the only one in it.

So I've tried to come up with something that saves the file when you edit it, and then reloads all open tabs so that you can see the changes your colleagues have made to it. Ideally it won't interrupt work-flow, but it will highlight tabs that have been edited.

The files are all hosted locally, so we're literally changing the same files not duplicates on different machines.

I don't know python and I'm not that familiar with object oriented code, so naturally I've run into a bit of trouble when trying to find a way to accomplish this. I pieced together a plugin from various things I found online (http://www.sublimetext.com/docs/2/api_reference.html) with the following code:

import sublime
import sublime_plugin
import os

class AutoSaveCommand(sublime_plugin.EventListener):
def on_modified(self, view):
    if view.file_name() != None:
        view.run_command('save')

This succeeded, and it saves the file whenever it is modified. We have very fast machines and the files we're editing aren't usually that large (LESS/SASS files), but now the next step is to push this change to the open files so that if one of us edits it the other will see the change (or it will highlight the tab if we're not focused on it, like it does when we edit and then move view without saving).

I can think of two ways to do this, one would be to run a command on an interval to check for file changes and reload the contents of the files. The other would be to somehow detect when a file is changed and reload files then.

I tried this for the first option, but it just crashes Sublime, I'm guessing I just suck that much at Python lol.

import sublime
import sublime_plugin
import sched
import time

s = sched.scheduler(time.time, time.sleep)
def check_for_changes(sc): 
    sublime.view.run_command('livereload')
    sc.enter(60, 1, check_for_changes, (sc,))

s.enter(60, 1, check_for_changes, (s,))
s.run()


class LivereloadCommand(sublime_plugin.WindowCommand):
    def run(self):
        window = self.window
        current = window.active_view()
        groups = window.num_groups()
        for i in range(0, groups):
            window.focus_group(i)
            views_num = window.views_in_group(i)
            for inner_view in views_num:
                window.focus_view(inner_view)

        window.focus_view(current)

Any ideas on how to fix this code, or how to detect file changes and reload the file that way, or any other method to achieve this goal, would be greatly appreciated.

I'm certain that if somebody who actually knows Python tackled this and built a plugin to achieve this kind of functionality it would be liked by the ST2 community.

Thanks!

1

There are 1 best solutions below

3
On

As a completely different solution, why not use a version control system like Git? There are a number of Git plugins available via Package Control that should, at minimum, allow you to check out files, modify them, and check them back in again, as well as resolve varying (and perhaps conflicting) edits. All of this is also (generally easily) accomplished via the command line. Git includes two basic GUIs, git-gui and gitk, or if you don't like them you can find more.

If you don't mind your website being available publicly, or you're willing to pay for a private repository, then GitHub is the place to go. All of the git command line options and GUIs work with it (you just clone a copy of your repo locally to begin with), and you can let someone else worry about setting up and maintaining the server.