-
-
Notifications
You must be signed in to change notification settings - Fork 382
/
SideBar.py
1677 lines (1373 loc) · 54.1 KB
/
SideBar.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding=utf8
import sublime
import sublime_plugin
import os
import shutil
import threading
import time
import re
import subprocess
import platform
from .edit.Edit import Edit
from .hurry.filesize import size as hurry_size
try:
from urllib import unquote as urlunquote
except ImportError:
from urllib.parse import unquote as urlunquote
from .SideBarAPI import SideBarItem, SideBarSelection, SideBarProject
Pref = {}
s = {}
Cache = {}
def cli(command):
info = subprocess.STARTUPINFO()
info.dwFlags = subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = 0
p = subprocess.Popen(
command,
startupinfo=info,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
shell=platform.system() == "Windows" or os.name == "nt",
)
stdout, stderr = p.communicate()
try:
p.kill()
except:
pass
p = {"stderr": stderr, "stdout": stdout, "returncode": p.returncode}
return p
def CACHED_SELECTION(paths=[]):
if Cache.cached:
return Cache.cached
else:
return SideBarSelection(paths)
def escapeCMDWindows(string):
return string.replace("^", "^^")
class Pref:
def load(self):
pass
def plugin_loaded():
global Pref, s
s = sublime.load_settings("Side Bar.sublime-settings")
Pref = Pref()
Pref.load()
s.clear_on_change("reload")
s.add_on_change("reload", lambda: Pref.load())
def Window(window=None):
return window if window else sublime.active_window()
def expandVars(path):
for k, v in list(os.environ.items()):
path = path.replace("%" + k + "%", v).replace("%" + k.lower() + "%", v)
return path
def window_set_status(key, name=""):
for window in sublime.windows():
for view in window.views():
view.set_status("SideBar-" + key, name)
class Object:
pass
class Cache:
pass
Cache = Cache()
Cache.cached = False
class aaaaaSideBarCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
pass
def is_visible(self, paths=[]): # <- WORKS AS AN ONPOPUPSHOWN
Cache.cached = SideBarSelection(paths)
return False
class SideBarNewFileCommand(sublime_plugin.WindowCommand):
def run(self, paths=[], name=""):
import functools
Window().run_command("hide_panel")
view = Window().show_input_panel(
"File Name:",
name,
functools.partial(self.on_done, paths, False),
None,
None,
)
Window().focus_view(view)
def on_done(self, paths, relative_to_project, name):
_paths = paths
_paths = SideBarSelection(_paths).getSelectedDirectoriesOrDirnames()
if not _paths:
_paths = SideBarProject().getDirectories()
if _paths:
_paths = [SideBarItem(_paths[0], False)]
if not _paths:
Window().new_file()
else:
for item in _paths:
item = SideBarItem(item.join(name), False)
if item.exists():
sublime.error_message(
"Unable to create file, file or folder exists."
)
self.run(paths, name)
return
else:
try:
item.create()
item.edit()
except:
sublime.error_message(
"Unable to create file:\n\n" + item.path()
)
self.run(paths, name)
return
SideBarProject().refresh()
class SideBarNewFile2Command(sublime_plugin.WindowCommand):
def run(self, paths=[], name=""):
import functools
Window().run_command("hide_panel")
view = Window().show_input_panel(
"File Name:",
name,
functools.partial(SideBarNewFileCommand(Window()).on_done, paths, True),
None,
None,
)
Window().focus_view(view)
class SideBarNewDirectory2Command(sublime_plugin.WindowCommand):
def run(self, paths=[], name=""):
import functools
Window().run_command("hide_panel")
view = Window().show_input_panel(
"Folder Name:",
name,
functools.partial(
SideBarNewDirectoryCommand(Window()).on_done, paths, True
),
None,
None,
)
Window().focus_view(view)
class SideBarNewDirectoryCommand(sublime_plugin.WindowCommand):
def run(self, paths=[], name=""):
import functools
Window().run_command("hide_panel")
view = Window().show_input_panel(
"Folder Name:",
name,
functools.partial(self.on_done, paths, False),
None,
None,
)
Window().focus_view(view)
def on_done(self, paths, relative_to_project, name):
_paths = paths
_paths = SideBarSelection(_paths).getSelectedDirectoriesOrDirnames()
for item in _paths:
item = SideBarItem(item.join(name), True)
if item.exists():
sublime.error_message("Unable to create folder, folder or file exists.")
self.run(paths, name)
return
else:
item.create()
if not item.exists():
sublime.error_message("Unable to create folder:\n\n" + item.path())
self.run(paths, name)
return
SideBarProject().refresh()
def is_enabled(self, paths=[]):
return CACHED_SELECTION(paths).len() > 0
class SideBarEditCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
for item in SideBarSelection(paths).getSelectedFiles():
item.edit()
def is_enabled(self, paths=[]):
return CACHED_SELECTION(paths).hasFiles()
class SideBarEditToRightCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
window = Window()
window.run_command(
"set_layout",
{
"cols": [0.0, 0.5, 1.0],
"rows": [0.0, 1.0],
"cells": [[0, 0, 1, 1], [1, 0, 2, 1]],
},
)
window.focus_group(1)
for item in SideBarSelection(paths).getSelectedFiles():
view = item.edit()
window.set_view_index(view, 1, 0)
def is_enabled(self, paths=[]):
return CACHED_SELECTION(paths).hasFiles()
class SideBarOpenCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
for item in SideBarSelection(paths).getSelectedItems():
item.open(s.get("use_powershell", True), s.get("use_command", ""))
def is_enabled(self, paths=[]):
return CACHED_SELECTION(paths).len() > 0
class SideBarFindInSelectedCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
window = Window()
views = []
for view in window.views():
if view.name() == "Find Results":
views.append(view)
for view in views:
view.close()
window = Window()
views = []
for view in window.views():
if view.name() == "Find Results":
Window().focus_view(view)
content = view.substr(sublime.Region(0, view.size()))
_view = Window().new_file()
_view.settings().set("auto_indent", False)
_view.run_command("insert", {"characters": content})
_view.settings().erase("auto_indent")
# the space at the end of the name prevents it from being reused by Sublime Text
# it looks like instead of keeping an internal refrence they just look at the view name -__-
_view.set_name("Find Results ")
_view.set_syntax_file("Packages/Default/Find Results.hidden-tmLanguage")
_view.sel().clear()
for sel in view.sel():
_view.sel().add(sel)
_view.set_scratch(True)
views.append(view)
for view in views:
view.close()
# fill form
items = []
for item in SideBarSelection(paths).getSelectedItemsWithoutChildItems():
items.append(item.path())
Window().run_command("hide_panel")
Window().run_command(
"show_panel", {"panel": "find_in_files", "where": ",".join(items)}
)
def is_enabled(self, paths=[]):
return CACHED_SELECTION(paths).len() > 0
Object.sidebar_instant_search_id = 0
class SideBarFindFilesPathContainingCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
if paths == [] and SideBarProject().getDirectories():
paths = SideBarProject().getDirectories()
else:
paths = [
item.path()
for item in SideBarSelection(paths).getSelectedDirectoriesOrDirnames()
]
if paths == []:
return
view = Window().new_file()
view.settings().set("word_wrap", False)
view.set_name("Instant File Search")
view.set_syntax_file(
"Packages/SideBarEnhancements/FindFilesResults.sublime-syntax"
)
view.set_scratch(True)
view.run_command("insert", {"characters": "Type to search: "})
view.sel().clear()
view.sel().add(sublime.Region(16, 16))
view.settings().set("sidebar_instant_search_paths", paths)
class SideBarFindFilesPathContainingViewListener(sublime_plugin.EventListener):
def on_modified(self, view):
view.settings().has(
"sidebar_instant_search_paths"
) # for some reason the first call in some conditions returns true
# but not the next one WTH xD
if view.settings().has("sidebar_instant_search_paths"):
searchTerm = (
view.substr(view.line(0)).replace("Type to search:", "").strip()
)
if searchTerm and Object.sidebar_instant_search_id != searchTerm:
SideBarFindFilesPathContainingSearchThread(view, searchTerm).start()
elif not searchTerm:
view.set_name("Instant File Search")
class SideBarFindFilesPathContainingSearchThread(threading.Thread):
def __init__(self, view, searchTerm):
self.view = view
self.searchTerm = searchTerm
threading.Thread.__init__(self)
def run(self):
if Object.sidebar_instant_search_id == self.searchTerm:
return
searchTerm = self.searchTerm
Object.sidebar_instant_search_id = searchTerm
view = self.view
paths = view.settings().get("sidebar_instant_search_paths")
self.ignore_paths = view.settings().get("file_exclude_patterns", [])
try:
self.searchTermRegExp = re.compile(searchTerm, re.I | re.U)
self.match_function = self.match_regexp
search_type = "REGEXP"
except:
self.match_function = self.match_string
search_type = "LITERAL"
if Object.sidebar_instant_search_id == searchTerm:
total = 0
highlight_from = 0
match_result = ""
match_result += "Type to search: " + searchTerm + "\n"
find = self.find
for item in SideBarSelection(paths).getSelectedDirectoriesOrDirnames():
self.files = []
self.num_files = 0
find(item.path())
match_result += "\n"
length = len(self.files)
if length > 1:
match_result += str(length) + " matches"
elif length > 0:
match_result += "1 match"
else:
match_result += "No match"
match_result += (
" in "
+ str(self.num_files)
+ ' files for term "'
+ searchTerm
+ '" using '
+ search_type
+ ' under \n"'
+ item.path()
+ '"\n\n'
)
if highlight_from == 0:
highlight_from = len(match_result)
match_result += "\n".join(self.files)
total += length
match_result += "\n"
if Object.sidebar_instant_search_id == searchTerm:
sel = view.sel()
position = sel[0].begin()
if position > 16 + len(searchTerm):
position = 16 + len(searchTerm)
view.run_command(
"side_bar_enhancements_write_to_view",
{
"content": match_result,
"position": position,
"searchTerm": searchTerm,
},
)
view.set_name(searchTerm + " - IFS")
if Object.sidebar_instant_search_id == searchTerm:
view.erase_regions("sidebar_search_instant_highlight")
if total < 5000 and len(searchTerm) > 1:
if search_type == "REGEXP":
regions = [
item
for item in view.find_all(
searchTerm, sublime.IGNORECASE
)
if item.begin() >= highlight_from
]
else:
regions = [
item
for item in view.find_all(
searchTerm, sublime.LITERAL | sublime.IGNORECASE
)
if item.begin() >= highlight_from
]
if Object.sidebar_instant_search_id == searchTerm:
view.add_regions(
"sidebar_search_instant_highlight",
regions,
"entity.name.function",
"",
sublime.PERSISTENT
| sublime.DRAW_SQUIGGLY_UNDERLINE
| sublime.DRAW_NO_FILL
| sublime.DRAW_NO_OUTLINE
| sublime.DRAW_EMPTY_AS_OVERWRITE,
)
def find(self, path):
if os.path.isfile(path) or os.path.islink(path):
self.num_files = self.num_files + 1
if self.match_function(path):
self.files.append(path)
elif os.path.isdir(path):
for content in os.listdir(path):
file = os.path.join(path, content)
if os.path.isfile(file) or os.path.islink(file):
self.num_files = self.num_files + 1
if self.match_function(file):
self.files.append(file)
else:
self.find(file)
def match_regexp(self, path):
return self.searchTermRegExp.search(path) and not [
1 for s in self.ignore_paths if s in path
]
def match_string(self, path):
return self.searchTerm in path and not [
1 for s in self.ignore_paths if s in path
]
class SideBarEnhancementsWriteToViewCommand(sublime_plugin.TextCommand):
def run(self, edit, content, position, searchTerm):
if Object.sidebar_instant_search_id == searchTerm:
view = self.view
view.replace(edit, sublime.Region(0, view.size()), content)
view.sel().clear()
view.sel().add(sublime.Region(position, position))
view.end_edit(edit)
class SideBarCutCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
s = sublime.load_settings("SideBarEnhancements/Clipboard.sublime-settings")
items = []
for item in SideBarSelection(paths).getSelectedItemsWithoutChildItems():
items.append(item.path())
if len(items) > 0:
s.set("cut", "\n".join(items))
s.set("copy", "")
if len(items) > 1:
sublime.status_message("Items cut")
else:
sublime.status_message("Item cut")
def is_enabled(self, paths=[]):
return (
CACHED_SELECTION(paths).len() > 0
and CACHED_SELECTION(paths).hasProjectDirectories() is False
)
class SideBarCopyCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
s = sublime.load_settings("SideBarEnhancements/Clipboard.sublime-settings")
items = []
for item in SideBarSelection(paths).getSelectedItemsWithoutChildItems():
items.append(item.path())
if len(items) > 0:
s.set("cut", "")
s.set("copy", "\n".join(items))
if len(items) > 1:
sublime.status_message("Items copied")
else:
sublime.status_message("Item copied")
def is_enabled(self, paths=[]):
return CACHED_SELECTION(paths).len() > 0
class SideBarPasteCommand(sublime_plugin.WindowCommand):
def run(self, paths=[], test="True", replace="False"):
key = "paste-" + str(time.time())
SideBarPasteThread(paths, test, replace, key).start()
def is_enabled(self, paths=[]):
s = sublime.load_settings("SideBarEnhancements/Clipboard.sublime-settings")
return (s.get("cut", "") + s.get("copy", "")) != "" and len(
CACHED_SELECTION(paths).getSelectedDirectoriesOrDirnames()
) == 1
class SideBarPasteThread(threading.Thread):
def __init__(self, paths=[], test="True", replace="False", key=""):
self.paths = paths
self.test = test
self.replace = replace
self.key = key
threading.Thread.__init__(self)
def run(self):
SideBarPasteCommand2(Window()).run(
self.paths, self.test, self.replace, self.key
)
class SideBarPasteCommand2(sublime_plugin.WindowCommand):
def run(self, paths=[], test="True", replace="False", key=""):
window_set_status(key, "Pasting…")
s = sublime.load_settings("SideBarEnhancements/Clipboard.sublime-settings")
cut = s.get("cut", "")
copy = s.get("copy", "")
already_exists_paths = []
if SideBarSelection(paths).len() > 0:
location = SideBarSelection(paths).getSelectedItems()[0].path()
if os.path.isdir(location) is False:
location = SideBarItem(os.path.dirname(location), True)
else:
location = SideBarItem(location, True)
if cut != "":
cut = cut.split("\n")
for path in cut:
path = SideBarItem(path, os.path.isdir(path))
new = os.path.join(location.path(), path.name())
if test == "True" and os.path.exists(new):
already_exists_paths.append(new)
elif test == "False":
if os.path.exists(new) and replace == "False":
pass
else:
try:
if not path.move(new, replace == "True"):
window_set_status(key, "")
sublime.error_message(
"Unable to cut and paste, destination exists."
)
return
except:
window_set_status(key, "")
sublime.error_message(
"Unable to move:\n\n"
+ path.path()
+ "\n\nto\n\n"
+ new
)
return
if copy != "":
copy = copy.split("\n")
for path in copy:
path = SideBarItem(path, os.path.isdir(path))
new = os.path.join(location.path(), path.name())
if test == "True" and os.path.exists(new):
already_exists_paths.append(new)
elif test == "False":
if os.path.exists(new) and replace == "False":
pass
else:
try:
if not path.copy(new, replace == "True"):
window_set_status(key, "")
sublime.error_message(
"Unable to copy and paste, destination exists."
)
return
except:
window_set_status(key, "")
sublime.error_message(
"Unable to copy:\n\n"
+ path.path()
+ "\n\nto\n\n"
+ new
)
return
if test == "True" and len(already_exists_paths):
self.confirm(paths, already_exists_paths, key)
elif test == "True" and not len(already_exists_paths):
SideBarPasteThread(paths, "False", "False", key).start()
elif test == "False":
cut = s.set("cut", "")
SideBarProject().refresh()
window_set_status(key, "")
else:
window_set_status(key, "")
def confirm(self, paths, data, key):
import functools
window = Window()
window.show_input_panel("BUG! xD", "", "", None, None)
window.run_command("hide_panel")
yes = []
yes.append("Yes, Replace the following items:")
for item in data:
yes.append(SideBarItem(item, os.path.isdir(item)).pathWithoutProject())
no = []
no.append("No")
no.append("Continue without replacing")
while len(no) != len(yes):
no.append("ST3 BUG xD")
window.show_quick_panel([yes, no], functools.partial(self.on_done, paths, key))
def on_done(self, paths, key, result):
window_set_status(key, "")
if result != -1:
if result == 0:
SideBarPasteThread(paths, "False", "True", key).start()
else:
SideBarPasteThread(paths, "False", "False", key).start()
class SideBarCopyNameCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
items = []
for item in SideBarSelection(paths).getSelectedItems():
items.append(item.name())
if len(items) > 0:
sublime.set_clipboard("\n".join(items))
if len(items) > 1:
sublime.status_message("Items copied")
else:
sublime.status_message("Item copied")
def is_enabled(self, paths=[]):
return CACHED_SELECTION(paths).len() > 0
class SideBarCopyNameEncodedCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
items = []
for item in SideBarSelection(paths).getSelectedItems():
items.append(item.nameEncoded())
if len(items) > 0:
sublime.set_clipboard("\n".join(items))
if len(items) > 1:
sublime.status_message("Items copied")
else:
sublime.status_message("Item copied")
def is_enabled(self, paths=[]):
return CACHED_SELECTION(paths).len() > 0
class SideBarCopyPathRelativeFromProjectEncodedCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
items = []
for item in SideBarSelection(paths).getSelectedItems():
items.append(item.pathRelativeFromProjectEncoded())
if len(items) > 0:
sublime.set_clipboard("\n".join(items))
if len(items) > 1:
sublime.status_message("Items copied")
else:
sublime.status_message("Item copied")
def is_enabled(self, paths=[]):
return (
CACHED_SELECTION(paths).len() > 0
and CACHED_SELECTION(paths).hasItemsUnderProject()
)
class SideBarCopyContentBase64Command(sublime_plugin.WindowCommand):
def run(self, paths=[]):
items = []
for item in SideBarSelection(paths).getSelectedFiles():
items.append(item.contentBase64())
if len(items) > 0:
sublime.set_clipboard("\n".join(items))
if len(items) > 1:
sublime.status_message("Items content copied")
else:
sublime.status_message("Item content copied")
def is_enabled(self, paths=[]):
return CACHED_SELECTION(paths).hasFiles()
class SideBarDuplicateCommand(sublime_plugin.WindowCommand):
def run(self, paths=[], new=False):
import functools
Window().run_command("hide_panel")
view = Window().show_input_panel(
"Duplicate As:",
new or SideBarSelection(paths).getSelectedItems()[0].path(),
functools.partial(
self.on_done, SideBarSelection(paths).getSelectedItems()[0].path()
),
None,
None,
)
Window().focus_view(view)
view.sel().clear()
view.sel().add(
sublime.Region(
view.size() - len(SideBarSelection(paths).getSelectedItems()[0].name()),
view.size()
- len(SideBarSelection(paths).getSelectedItems()[0].extension()),
)
)
def on_done(self, old, new):
key = "duplicate-" + str(time.time())
SideBarDuplicateThread(old, new, key).start()
def is_enabled(self, paths=[]):
return (
CACHED_SELECTION(paths).len() == 1
and CACHED_SELECTION(paths).hasProjectDirectories() is False
)
class SideBarDuplicateThread(threading.Thread):
def __init__(self, old, new, key):
self.old = old
self.new = new
self.key = key
threading.Thread.__init__(self)
def run(self):
old = self.old
new = self.new
key = self.key
window_set_status(key, "Duplicating…")
item = SideBarItem(old, os.path.isdir(old))
try:
if not item.copy(new):
window_set_status(key, "")
if SideBarItem(new, os.path.isdir(new)).overwrite():
self.run()
else:
SideBarDuplicateCommand(Window()).run([old], new)
return
except:
window_set_status(key, "")
sublime.error_message("Unable to copy:\n\n" + old + "\n\nto\n\n" + new)
SideBarDuplicateCommand(Window()).run([old], new)
return
item = SideBarItem(new, os.path.isdir(new))
if item.isFile():
item.edit()
SideBarProject().refresh()
window_set_status(key, "")
class SideBarRenameCommand(sublime_plugin.WindowCommand):
def run(self, paths=[], newLeaf=False):
import functools
branch, leaf = os.path.split(
SideBarSelection(paths).getSelectedItems()[0].path()
)
Window().run_command("hide_panel")
view = Window().show_input_panel(
"New Name:",
newLeaf or leaf,
functools.partial(
self.on_done,
SideBarSelection(paths).getSelectedItems()[0].path(),
branch,
),
None,
None,
)
Window().focus_view(view)
view.sel().clear()
view.sel().add(
sublime.Region(
view.size() - len(SideBarSelection(paths).getSelectedItems()[0].name()),
view.size()
- len(SideBarSelection(paths).getSelectedItems()[0].extension()),
)
)
def on_done(self, old, branch, leaf):
key = "rename-" + str(time.time())
SideBarRenameThread(old, branch, leaf, key).start()
def is_enabled(self, paths=[]):
return (
CACHED_SELECTION(paths).len() == 1
and CACHED_SELECTION(paths).hasProjectDirectories() is False
)
class SideBarRenameThread(threading.Thread):
def __init__(self, old, branch, leaf, key):
self.old = old
self.branch = branch
self.leaf = leaf
self.key = key
threading.Thread.__init__(self)
def run(self):
old = self.old
branch = self.branch
leaf = self.leaf
key = self.key
window_set_status(key, "Renaming…")
Window().run_command("hide_panel")
leaf = leaf.strip()
new = os.path.join(branch, leaf)
item = SideBarItem(old, os.path.isdir(old))
try:
if not item.move(new):
if SideBarItem(new, os.path.isdir(new)).overwrite():
self.run()
else:
window_set_status(key, "")
SideBarRenameCommand(Window()).run([old], leaf)
except:
window_set_status(key, "")
sublime.error_message("Unable to rename:\n\n" + old + "\n\nto\n\n" + new)
SideBarRenameCommand(Window()).run([old], leaf)
raise
return
SideBarProject().refresh()
window_set_status(key, "")
class SideBarMassRenameCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
import functools
Window().run_command("hide_panel")
view = Window().show_input_panel(
"Find:", "", functools.partial(self.on_find, paths), None, None
)
Window().focus_view(view)
def on_find(self, paths, find):
if not find:
return
import functools
Window().run_command("hide_panel")
view = Window().show_input_panel(
"Replace:", "", functools.partial(self.on_replace, paths, find), None, None
)
Window().focus_view(view)
def on_replace(self, paths, find, replace):
key = "mass-renaming-" + str(time.time())
SideBarMassRenameThread(paths, find, replace, key).start()
def is_enabled(self, paths=[]):
return CACHED_SELECTION(paths).len() > 0
class SideBarMassRenameThread(threading.Thread):
def __init__(self, paths, find, replace, key):
self.paths = paths
self.find = find
self.replace = replace
self.key = key
threading.Thread.__init__(self)
def run(self):
paths = self.paths
find = self.find
replace = self.replace
key = self.key
if find == "":
return None
else:
window_set_status(key, "Mass renaming…")
to_rename_or_move = []
for item in SideBarSelection(paths).getSelectedItemsWithoutChildItems():
self.recurse(item.path(), to_rename_or_move)
to_rename_or_move.sort()
to_rename_or_move.reverse()
for item in to_rename_or_move:
if find in item:
origin = SideBarItem(item, os.path.isdir(item))
destination = SideBarItem(
origin.pathProject()
+ ""
+ (origin.pathWithoutProject().replace(find, replace)),
os.path.isdir(item),
)
origin.move(destination.path())
SideBarProject().refresh()
window_set_status(key, "")
def recurse(self, path, paths):
if os.path.isfile(path) or os.path.islink(path):
paths.append(path)
else:
for content in os.listdir(path):
file = os.path.join(path, content)
if os.path.isfile(file) or os.path.islink(file):
paths.append(file)
else:
self.recurse(file, paths)
paths.append(path)
class SideBarMoveCommand(sublime_plugin.WindowCommand):
def run(self, paths=[], new=False):
import functools
Window().run_command("hide_panel")
view = Window().show_input_panel(
"New Location:",
new or SideBarSelection(paths).getSelectedItems()[0].path(),
functools.partial(
self.on_done, SideBarSelection(paths).getSelectedItems()[0].path()
),
None,
None,
)
Window().focus_view(view)
view.sel().clear()
view.sel().add(
sublime.Region(
view.size() - len(SideBarSelection(paths).getSelectedItems()[0].name()),
view.size()
- len(SideBarSelection(paths).getSelectedItems()[0].extension()),