forked from bradsokol/VcsGutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview_collection.py
108 lines (91 loc) · 3.21 KB
/
view_collection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import tempfile
import time
import sublime
try:
from .vcs_helpers import GitHelper, HgHelper, SvnHelper
except ValueError:
from vcs_helpers import GitHelper, HgHelper, SvnHelper
class ViewCollection:
views = {}
vcs_times = {}
vcs_files = {}
buf_files = {}
@staticmethod
def add(view):
try:
from .gutter_handlers import GitGutterHandler, HgGutterHandler, SvnGutterHandler
except ValueError:
from gutter_handlers import GitGutterHandler, HgGutterHandler, SvnGutterHandler
settings = sublime.load_settings('VcsGutter.sublime-settings')
vcs_paths = settings.get('vcs_paths', {
'git': 'git',
'hg': 'hg',
'svn': 'svn'
})
key = None
if GitHelper.is_git_repository(view):
key = 'git'
klass = GitGutterHandler
elif HgHelper.is_hg_repository(view):
key = 'hg'
klass = HgGutterHandler
elif SvnHelper.is_svn_repository(view):
key = 'svn'
klass = SvnGutterHandler
handler = None
if key is not None:
try:
path = vcs_paths[key]
except (KeyError, TypeError):
print('Vcs Gutter: Invalid path for %s executable in settings. Using default.' % key)
path = key
handler = klass(view, path)
# If no handler found then either the view does not represent a
# file on disk (e.g. not yet saved) or the file is not in a supported
# VCS repository.
if handler is not None:
key = ViewCollection.get_key(view)
ViewCollection.views[key] = handler
ViewCollection.views[key].reset()
@staticmethod
def vcs_path(view):
key = ViewCollection.get_key(view)
if key in ViewCollection.views:
return ViewCollection.views[key].get_vcs_path()
else:
return False
@staticmethod
def get_key(view):
return view.file_name()
@staticmethod
def diff(view):
key = ViewCollection.get_key(view)
try:
result = ViewCollection.views[key].diff()
except KeyError:
result = ([], [], [])
return result
@staticmethod
def vcs_time(view):
key = ViewCollection.get_key(view)
if not key in ViewCollection.vcs_times:
ViewCollection.vcs_times[key] = 0
return time.time() - ViewCollection.vcs_times[key]
@staticmethod
def update_vcs_time(view):
key = ViewCollection.get_key(view)
ViewCollection.vcs_times[key] = time.time()
@staticmethod
def vcs_tmp_file(view):
key = ViewCollection.get_key(view)
if not key in ViewCollection.vcs_files:
ViewCollection.vcs_files[key] = tempfile.NamedTemporaryFile()
ViewCollection.vcs_files[key].close()
return ViewCollection.vcs_files[key]
@staticmethod
def buf_tmp_file(view):
key = ViewCollection.get_key(view)
if not key in ViewCollection.buf_files:
ViewCollection.buf_files[key] = tempfile.NamedTemporaryFile()
ViewCollection.buf_files[key].close()
return ViewCollection.buf_files[key]