-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPreventFakeClones.py
99 lines (87 loc) · 4.48 KB
/
PreventFakeClones.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
# coding=utf8
import sublime, sublime_plugin
import os.path as op
def normalize(path):
return op.normcase(op.normpath(op.realpath(path)))
class prevent_fake_clones_listener(sublime_plugin.EventListener):
def on_load(self, view):
self.prevent_fake_clone(view)
def on_activated(self, view):
transient = view.settings().get("FakeCloneTransient", False)
if transient:
view.settings().erase("FakeCloneTransient")
self.prevent_fake_clone(view)
def prevent_fake_clone(self, view):
if view.file_name():
path = normalize(view.file_name())
window = view.window()
transient = False
if window is None:
transient = True
view.settings().erase("FakeCloneTransient")
for _window in sublime.windows():
for _view in _window.views():
# check if the file is already opened
if (
# if the view has a file name
_view.file_name()
# if the view is different and not the same that we just opened
and _view.id() != view.id()
# if the buffer is not the same (if is not a real clone)
and _view.buffer_id() != view.buffer_id()
# if the path of the file matches exactly
and path == normalize(_view.file_name())
):
if transient:
view.settings().set("FakeCloneTransient", transient)
# elif not _view.is_dirty():
# self.focus_view(_view)
# _window.run_command("close")
# self.focus_view(view)
else:
self.focus_view(view)
window.run_command("close")
# If the other view is "dirty" and is in another window. Display the popup/statusbar message
if window != _window:
sublime.status_message(
'Preventing opening an already opened file (aka "fake clone") Trying to focus already opened file... Use: "File -> New File into View" which will open a real clone.'
)
self.focus_view(_view)
return
def on_post_save(self, view):
if view.file_name():
path = normalize(view.file_name())
window = view.window()
for _window in sublime.windows():
for _view in _window.views():
# check if the file is already opened
if (
_view.file_name()
and _view.id() != view.id() # if the view has a file name
and _view.buffer_id() # if the view is different and not the same that we just opened
!= view.buffer_id()
and path # if the buffer is not the same (if is not a real clone)
== normalize(
_view.file_name()
) # if the path of the file matches exactly
):
# You just have overwrite a file that is currently opened in another tab/view
# is that tab/view dirty? (if it has unsaved changes)
if not _view.is_dirty():
# then just close it
self.focus_view(_view)
_window.run_command("close")
self.focus_view(view)
else:
# the other view/tab is dirty (has unsaved changes)
# alert of the probably unwanted action
if sublime.ok_cancel_dialog(
"You just have overwrite a file that is currently opened in another view and has not been saved. Do you want to focus the view that has unsaved changes?"
):
self.focus_view(_view)
return
def focus_view(self, view):
window = view.window()
window.focus_view(view)
window.run_command("focus_neighboring_group")
window.focus_view(view)