-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
141 lines (116 loc) · 4.36 KB
/
main.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from typing import Callable
from .core.diff_view import DIFF_VIEW_NAME
from .core.git_commands import Git
from .core.git_diff_view import GitDiffView
from .core.status_view import get_status_view, STATUS_VIEW_NAME
from .core.view_manager import SESSION_DIR, ViewsManager
from .utils import get_line, get_point
import sublime
import sublime_plugin
import os
STOP_INTERVAL = False
def set_interval(fn: Callable) -> None:
def interval() -> None:
fn()
if not STOP_INTERVAL:
sublime.set_timeout(interval, 2000)
sublime.set_timeout(interval, 2000)
def refresh_list() -> None:
''' Refresh git status view content'''
window = sublime.active_window()
view = get_status_view(window.views())
if view is None:
return
git_statuses = Git(window).git_statuses()
view.run_command('update_status_view', {
'git_statuses': git_statuses,
})
def plugin_loaded() -> None:
# create session dir where the session file will be stored
if not os.path.exists(SESSION_DIR):
os.makedirs(SESSION_DIR)
status_view = get_status_view(sublime.active_window().views())
# If status view is open when sublime starts,
if status_view is not None:
set_interval(refresh_list)
# command: close_git_diff_view
class CloseGitDiffViewCommand(sublime_plugin.TextCommand):
def run(self, _: sublime.Edit) -> None:
global STOP_INTERVAL
window = self.view.window()
if not window:
return
git_diff_view = GitDiffView(window)
git_diff_view.close()
git = Git(window)
views_manager = ViewsManager(window, git.git_root_dir or "")
views_manager.restore()
STOP_INTERVAL = True
# command: open_git_diff_view
class OpenGitDiffViewCommand(sublime_plugin.TextCommand):
def run(self, _: sublime.Edit) -> None:
global STOP_INTERVAL
window = self.view.window()
if not window:
return
git = Git(window)
# open GitView
# array of dict that holds information about
# the file, type of modification, and if the file is staged
git_statuses = git.git_statuses()
if not git_statuses:
window.status_message('No git changes to show.')
return
views_manager = ViewsManager(window, git.git_root_dir or "")
views_manager.prepare()
git_diff_view = GitDiffView(window)
git_diff_view.open()
STOP_INTERVAL = False
set_interval(refresh_list)
# command: toggle_git_diff_view
class ToggleGitDiffViewCommand(sublime_plugin.TextCommand):
def run(self, _: sublime.Edit) -> None:
global STOP_INTERVAL
window = self.view.window()
if not window:
return
if ViewsManager.is_git_view_open(window.views()):
self.view.run_command('close_git_diff_view')
else:
self.view.run_command('open_git_diff_view')
class SelectionChangedEvent(sublime_plugin.EventListener):
previous_line = None
def on_pre_close(self, view: sublime.View) -> None:
window = view.window()
if not window:
return
if not ViewsManager.is_git_view_open(window.views()):
return
if view.name() in [STATUS_VIEW_NAME, DIFF_VIEW_NAME]:
point = get_point(view)
w = view.window()
if view.name() == STATUS_VIEW_NAME and point and w:
ViewsManager.status_view_last_position[w.id()] = point
sublime.set_timeout(lambda: window.run_command('close_git_diff_view'))
def on_selection_modified(self, view: sublime.View) -> None:
if view.name() != STATUS_VIEW_NAME:
return
window = view.window()
if not window:
return
status_view = view
line = get_line(status_view)
on_same_line = line == self.previous_line
if on_same_line or line is None:
return
self.previous_line = line
_, y =status_view.viewport_position()
status_view.set_viewport_position((0, y)) # make sure that the labels are always visible
git_statuses = GitDiffView.git_statuses[window.id()]
try:
git_status = git_statuses[line]
view.run_command("update_diff_view", {
'git_status': git_status,
})
except:
status_view.run_command("clear_diff_view")