-
Notifications
You must be signed in to change notification settings - Fork 1
/
SideBar.py
1461 lines (1243 loc) · 48.8 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, sublime_plugin
import os
import threading, time
from sidebar.SideBarItem import SideBarItem
from sidebar.SideBarSelection import SideBarSelection
from sidebar.SideBarProject import SideBarProject
from send2trash import send2trash
# needed for getting local app data path on windows
if sublime.platform() == 'windows':
import _winreg
def expand_vars(path):
for k, v in os.environ.iteritems():
# dirty hack, this should be autofixed in python3
try:
k = unicode(k.encode('utf8'))
v = unicode(v.encode('utf8'))
path = path.replace('%'+k+'%', v).replace('%'+k.lower()+'%', v)
except:
pass
return path
#NOTES
# A "directory" for this plugin is a "directory"
# A "directory" for a user is a "folder"
s = sublime.load_settings('Side Bar.sublime-settings')
def check_version():
version = '11.13.2012.1305.0';
if s.get('version') != version:
SideBarItem(sublime.packages_path()+'/SideBarEnhancements/messages/'+version+'.txt', False).edit();
s.set('version', version);
sublime.save_settings('Side Bar.sublime-settings')
sublime.set_timeout(lambda:check_version(), 3000);
class SideBarNewFile2Command(sublime_plugin.WindowCommand):
def run(self, paths = [], name = ""):
import functools
self.window.run_command('hide_panel');
self.window.show_input_panel("File Name:", name, functools.partial(SideBarNewFileCommand(sublime_plugin.WindowCommand).on_done, paths, True), None, None)
class SideBarNewFileCommand(sublime_plugin.WindowCommand):
def run(self, paths = [], name = ""):
import functools
self.window.run_command('hide_panel');
self.window.show_input_panel("File Name:", name, functools.partial(self.on_done, paths, False), None, None)
def on_done(self, paths, relative_to_project, name):
if relative_to_project and s.get('new_files_relative_to_project_root'):
paths = SideBarProject().getDirectories()
if paths:
paths = [SideBarItem(paths[0], False)]
if not paths:
paths = SideBarSelection(paths).getSelectedDirectoriesOrDirnames()
else:
paths = SideBarSelection(paths).getSelectedDirectoriesOrDirnames()
if not paths:
paths = SideBarProject().getDirectories()
if paths:
paths = [SideBarItem(paths[0], False)]
if not paths:
sublime.active_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 SideBarNewDirectoryCommand(sublime_plugin.WindowCommand):
def run(self, paths = [], name = ""):
import functools
self.window.run_command('hide_panel');
self.window.show_input_panel("Folder Name:", name, functools.partial(self.on_done, paths), None, None)
def on_done(self, paths, name):
for item in SideBarSelection(paths).getSelectedDirectoriesOrDirnames():
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 SideBarSelection(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 SideBarSelection(paths).hasFiles()
def is_visible(self, paths =[]):
return not s.get('disabled_menuitem_edit')
class SideBarOpenCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
for item in SideBarSelection(paths).getSelectedFiles():
item.open()
def is_enabled(self, paths = []):
return SideBarSelection(paths).hasFiles()
def is_visible(self, paths =[]):
return not s.get('disabled_menuitem_open_run')
class SideBarFilesOpenWithEditApplicationsCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
item = SideBarItem(os.path.join(sublime.packages_path(), 'User', 'SideBarEnhancements', 'Open With', 'Side Bar.sublime-menu'), False)
if not item.exists():
item.create()
item.write("""[
{"id": "side-bar-files-open-with",
"children":
[
//application 1
{
"caption": "Photoshop",
"id": "side-bar-files-open-with-photoshop",
"command": "side_bar_files_open_with",
"args": {
"paths": [],
"application": "Adobe Photoshop CS5.app", // OSX
"extensions":"psd|png|jpg|jpeg" //any file with these extensions
}
},
{"caption":"-"}
]
}
]""");
item.edit()
def is_enabled(self, paths = []):
return True
class SideBarFilesOpenWithCommand(sublime_plugin.WindowCommand):
def run(self, paths = [], application = "", extensions = ""):
import sys
application_dir, application_name = os.path.split(application)
application_dir = application_dir.encode(sys.getfilesystemencoding())
application_name = application_name.encode(sys.getfilesystemencoding())
application = application.encode(sys.getfilesystemencoding())
if extensions == '*':
extensions = '.*'
if extensions == '':
items = SideBarSelection(paths).getSelectedItems()
else:
items = SideBarSelection(paths).getSelectedFilesWithExtension(extensions)
import subprocess
for item in items:
if sublime.platform() == 'osx':
subprocess.Popen(['open', '-a', application, item.nameSystem()], cwd=item.dirnameSystem())
elif sublime.platform() == 'windows':
subprocess.Popen([application_name, item.pathSystem()], cwd=expand_vars(application_dir), shell=True)
else:
subprocess.Popen([application_name, item.nameSystem()], cwd=item.dirnameSystem())
def is_enabled(self, paths = [], application = "", extensions = ""):
if extensions == '*':
extensions = '.*'
if extensions == '':
return SideBarSelection(paths).len() > 0
else:
return SideBarSelection(paths).hasFilesWithExtension(extensions)
def is_visible(self, paths = [], application = "", extensions = ""):
if extensions == '*':
extensions = '.*'
if extensions == '':
return SideBarSelection(paths).len() > 0
else:
has = SideBarSelection(paths).hasFilesWithExtension(extensions)
return has or (not has and not s.get("hide_open_with_entries_when_there_are_no_applicable"))
class SideBarFindInSelectedCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedItemsWithoutChildItems():
items.append(item.path())
self.window.run_command('hide_panel');
if int(sublime.version()) >= 2134:
self.window.run_command("show_panel", {"panel": "find_in_files", "where":",".join(items) })
else:
self.window.run_command("show_panel", {"panel": "find_in_files", "location":",".join(items) })
def is_enabled(self, paths = []):
return SideBarSelection(paths).len() > 0
class SideBarFindInParentCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedItems():
items.append(item.dirname())
items = list(set(items))
self.window.run_command('hide_panel');
if int(sublime.version()) >= 2134:
self.window.run_command("show_panel", {"panel": "find_in_files", "where":",".join(items) })
else:
self.window.run_command("show_panel", {"panel": "find_in_files", "location":",".join(items) })
def is_enabled(self, paths = []):
return SideBarSelection(paths).len() > 0
class SideBarFindInProjectFoldersCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command('hide_panel');
if int(sublime.version()) >= 2137:
self.window.run_command("show_panel", {"panel": "find_in_files", "where":"<project>"})
elif int(sublime.version()) >= 2136:
self.window.run_command("show_panel", {"panel": "find_in_files", "where":"<open folders>"})
elif int(sublime.version()) >= 2134:
self.window.run_command("show_panel", {"panel": "find_in_files", "where":""})
else:
self.window.run_command("show_panel", {"panel": "find_in_files", "location":"<open folders>"})
class SideBarFindInProjectCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
self.window.run_command('hide_panel');
if int(sublime.version()) >= 2137:
self.window.run_command("show_panel", {"panel": "find_in_files", "where":"<project>"})
elif int(sublime.version()) >= 2136:
self.window.run_command("show_panel", {"panel": "find_in_files", "where":"<open folders>"})
elif int(sublime.version()) >= 2134:
self.window.run_command("show_panel", {"panel": "find_in_files", "where":""})
else:
self.window.run_command("show_panel", {"panel": "find_in_files", "location":"<open folders>"})
def is_visible(self, paths = []):
return not s.get('disabled_menuitem_find_in_project')
class SideBarFindInProjectFolderCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedItemsWithoutChildItems():
items.append(SideBarProject().getDirectoryFromPath(item.path()))
items = list(set(items))
if items:
self.window.run_command('hide_panel');
self.window.run_command("show_panel", {"panel": "find_in_files", "where":",".join(items)})
class SideBarFindInFilesWithExtensionCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedItems():
items.append('*'+item.extension())
items = list(set(items))
self.window.run_command('hide_panel');
if int(sublime.version()) >= 2134:
self.window.run_command("show_panel", {"panel": "find_in_files", "where":",".join(items) })
else:
self.window.run_command("show_panel", {"panel": "find_in_files", "location":",".join(items) })
def is_enabled(self, paths = []):
return SideBarSelection(paths).hasFiles()
def description(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedFiles():
items.append('*'+item.extension())
items = list(set(items))
if len(items) > 1:
return 'In Files With Extensions '+(",".join(items))+u'…'
elif len(items) > 0:
return 'In Files With Extension '+(",".join(items))+u'…'
else:
return u'In Files With Extension…'
sidebar_instant_search = 0
class SideBarFindFilesPathContainingCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
global sidebar_instant_search
if paths == [] and SideBarProject().getDirectories():
paths = SideBarProject().getDirectories()
else:
paths = [item.path() for item in SideBarSelection(paths).getSelectedDirectoriesOrDirnames()]
if paths == []:
return
view = self.window.new_file()
view.settings().set('word_wrap', False)
view.set_name('Instant File Search')
view.set_syntax_file('Packages/SideBarEnhancements/SideBar Results.hidden-tmLanguage')
view.set_scratch(True)
edit = view.begin_edit()
view.settings().set('sidebar_instant_search_paths', paths)
view.replace(edit, sublime.Region(0, view.size()), "Type to search: ")
view.end_edit(edit)
view.sel().clear()
view.sel().add(sublime.Region(16))
sidebar_instant_search += 1
def is_enabled(self, paths=[]):
return True
class SideBarFindResultsViewListener(sublime_plugin.EventListener):
def on_modified(self, view):
global sidebar_instant_search
if sidebar_instant_search > 0 and view.settings().has('sidebar_instant_search_paths'):
row, col = view.rowcol(view.sel()[0].begin())
if row != 0 or not view.sel()[0].empty():
return
paths = view.settings().get('sidebar_instant_search_paths')
searchTerm = view.substr(view.line(0)).replace("Type to search:", "").strip()
start_time = time.time()
view.settings().set('sidebar_search_paths_start_time', start_time)
if searchTerm:
sublime.set_timeout(lambda:SideBarFindFilesPathContainingSearchThread(paths, searchTerm, view, start_time).start(), 300)
def on_close(self, view):
if view.settings().has('sidebar_instant_search_paths'):
global sidebar_instant_search
sidebar_instant_search -= 1
class SideBarFindFilesPathContainingSearchThread(threading.Thread):
def __init__(self, paths, searchTerm, view, start_time):
if view.settings().get('sidebar_search_paths_start_time') != start_time:
self.should_run = False
else:
self.should_run = True
self.view = view
self.searchTerm = searchTerm
self.paths = paths
self.start_time = start_time
threading.Thread.__init__(self)
def run(self):
if not self.should_run:
return
# print 'run forrest run'
self.total = 0
self.highlight_from = 0
self.match_result = u''
self.match_result += 'Type to search: '+self.searchTerm+'\n'
for item in SideBarSelection(self.paths).getSelectedDirectoriesOrDirnames():
self.files = []
self.num_files = 0
self.find(item.path())
self.match_result += '\n'
length = len(self.files)
if length > 1:
self.match_result += str(length)+' matches'
elif length > 0:
self.match_result += '1 match'
else:
self.match_result += 'No match'
self.match_result += ' in '+str(self.num_files)+' files for term "'+self.searchTerm+'" under \n"'+item.path()+'"\n\n'
if self.highlight_from == 0:
self.highlight_from = len(self.match_result)
self.match_result += ('\n'.join(self.files))
self.total = self.total + length
self.match_result += '\n'
sublime.set_timeout(lambda:self.on_done(), 0)
def on_done(self):
if self.start_time == self.view.settings().get('sidebar_search_paths_start_time'):
view = self.view;
edit = view.begin_edit()
sel = sublime.Region(view.sel()[0].begin(), view.sel()[0].end())
view.replace(edit, sublime.Region(0, view.size()), self.match_result);
view.end_edit(edit)
view.erase_regions("sidebar_search_instant_highlight")
if self.total < 30000 and len(self.searchTerm) > 1:
regions = [item for item in view.find_all(self.searchTerm, sublime.LITERAL|sublime.IGNORECASE) if item.begin() >= self.highlight_from]
view.add_regions("sidebar_search_instant_highlight", regions, 'string', sublime.DRAW_EMPTY|sublime.DRAW_OUTLINED|sublime.DRAW_EMPTY_AS_OVERWRITE)
view.sel().clear()
view.sel().add(sel)
def find(self, path):
if os.path.isfile(path) or os.path.islink(path):
self.num_files = self.num_files+1
if self.match(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(file):
self.files.append(file)
else:
self.find(file)
def match(self, path):
return False if path.lower().find(self.searchTerm.lower()) == -1 else True
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 SideBarSelection(paths).len() > 0 and SideBarSelection(paths).hasProjectDirectories() == 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 SideBarSelection(paths).len() > 0
class SideBarPasteCommand(sublime_plugin.WindowCommand):
def run(self, paths = [], in_parent = 'False', test = 'True', replace = 'False'):
s = sublime.load_settings("SideBarEnhancements/Clipboard.sublime-settings")
cut = s.get('cut', '')
copy = s.get('copy', '')
already_exists_paths = []
if SideBarSelection(paths).len() > 0:
if in_parent == 'False':
location = SideBarSelection(paths).getSelectedItems()[0].path()
else:
location = SideBarSelection(paths).getSelectedDirectoriesOrDirnames()[0].dirname()
if os.path.isdir(location) == 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'):
sublime.error_message("Unable to cut and paste, destination exists.")
return
except:
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'):
sublime.error_message("Unable to copy and paste, destination exists.")
return
except:
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, in_parent, already_exists_paths)
elif test == 'True' and not len(already_exists_paths):
self.run(paths, in_parent, 'False', 'False')
elif test == 'False':
cut = s.set('cut', '')
SideBarProject().refresh();
def confirm(self, paths, in_parent, data):
import functools
window = sublime.active_window()
# window.show_input_panel("BUG!", '', '', 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');
window.show_quick_panel([yes, no], functools.partial(self.on_done, paths, in_parent))
def on_done(self, paths, in_parent, result):
if result != -1:
if result == 0:
self.run(paths, in_parent, 'False', 'True')
else:
self.run(paths, in_parent, 'False', 'False')
def is_enabled(self, paths = [], in_parent = False):
s = sublime.load_settings("SideBarEnhancements/Clipboard.sublime-settings")
return s.get('cut', '') + s.get('copy', '') != '' and len(SideBarSelection(paths).getSelectedDirectoriesOrDirnames()) == 1
def is_visible(self, paths = [], in_parent = False):
if in_parent == 'True':
return not s.get('disabled_menuitem_paste_in_parent')
else:
return True
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 SideBarSelection(paths).len() > 0
def is_visible(self, paths =[]):
return not s.get('disabled_menuitem_copy_name')
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 SideBarSelection(paths).len() > 0
class SideBarCopyPathCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedItems():
items.append(item.path())
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 SideBarSelection(paths).len() > 0
class SideBarCopyDirPathCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedDirectoriesOrDirnames():
items.append(item.path())
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 SideBarSelection(paths).len() > 0
def is_visible(self, paths =[]):
return not s.get('disabled_menuitem_copy_dir_path')
class SideBarCopyPathEncodedCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedItems():
items.append(item.uri())
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 SideBarSelection(paths).len() > 0
class SideBarCopyPathRelativeFromProjectCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedItems():
items.append(item.pathRelativeFromProject())
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 SideBarSelection(paths).len() > 0 and SideBarSelection(paths).hasItemsUnderProject()
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 SideBarSelection(paths).len() > 0 and SideBarSelection(paths).hasItemsUnderProject()
class SideBarCopyPathRelativeFromViewCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedItems():
items.append(item.pathRelativeFromView())
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 SideBarSelection(paths).len() > 0
class SideBarCopyPathRelativeFromViewEncodedCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedItems():
items.append(item.pathRelativeFromViewEncoded())
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 SideBarSelection(paths).len() > 0
class SideBarCopyPathAbsoluteFromProjectCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedItems():
items.append(item.pathAbsoluteFromProject())
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 SideBarSelection(paths).len() > 0 and SideBarSelection(paths).hasItemsUnderProject()
class SideBarCopyPathAbsoluteFromProjectEncodedCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedItems():
items.append(item.pathAbsoluteFromProjectEncoded())
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 SideBarSelection(paths).len() > 0 and SideBarSelection(paths).hasItemsUnderProject()
def is_visible(self, paths =[]):
return not s.get('disabled_menuitem_copy_path')
class SideBarCopyTagAhrefCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedItems():
items.append('<a href="'+item.pathAbsoluteFromProjectEncoded()+'">'+item.namePretty()+'</a>')
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 SideBarSelection(paths).len() > 0 and SideBarSelection(paths).hasItemsUnderProject()
class SideBarCopyTagImgCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedImages():
try:
image_type, width, height = self.getImageInfo(item.contentBinary())
items.append('<img src="'+item.pathAbsoluteFromProjectEncoded()+'" width="'+str(width)+'" height="'+str(height)+'">')
except:
items.append('<img src="'+item.pathAbsoluteFromProjectEncoded()+'">')
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")
#Project:http://code.google.com/p/bfg-pages/
#License:http://www.opensource.org/licenses/bsd-license.php
def getImageInfo(self, data):
import StringIO
import struct
data = str(data)
size = len(data)
height = -1
width = -1
content_type = ''
# handle GIFs
if (size >= 10) and data[:6] in ('GIF87a', 'GIF89a'):
# Check to see if content_type is correct
content_type = 'image/gif'
w, h = struct.unpack("<HH", data[6:10])
width = int(w)
height = int(h)
# See PNG 2. Edition spec (http://www.w3.org/TR/PNG/)
# Bytes 0-7 are below, 4-byte chunk length, then 'IHDR'
# and finally the 4-byte width, height
elif ((size >= 24) and data.startswith('\211PNG\r\n\032\n')
and (data[12:16] == 'IHDR')):
content_type = 'image/png'
w, h = struct.unpack(">LL", data[16:24])
width = int(w)
height = int(h)
# Maybe this is for an older PNG version.
elif (size >= 16) and data.startswith('\211PNG\r\n\032\n'):
# Check to see if we have the right content type
content_type = 'image/png'
w, h = struct.unpack(">LL", data[8:16])
width = int(w)
height = int(h)
# handle JPEGs
elif (size >= 2) and data.startswith('\377\330'):
content_type = 'image/jpeg'
jpeg = StringIO.StringIO(data)
jpeg.read(2)
b = jpeg.read(1)
try:
while (b and ord(b) != 0xDA):
while (ord(b) != 0xFF): b = jpeg.read(1)
while (ord(b) == 0xFF): b = jpeg.read(1)
if (ord(b) >= 0xC0 and ord(b) <= 0xC3):
jpeg.read(3)
h, w = struct.unpack(">HH", jpeg.read(4))
break
else:
jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2)
b = jpeg.read(1)
width = int(w)
height = int(h)
except struct.error:
pass
except ValueError:
pass
return content_type, width, height
def is_enabled(self, paths = []):
return SideBarSelection(paths).hasImages() and SideBarSelection(paths).hasItemsUnderProject()
class SideBarCopyTagStyleCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedFilesWithExtension('css'):
items.append('<link rel="stylesheet" type="text/css" href="'+item.pathAbsoluteFromProjectEncoded()+'"/>')
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 SideBarSelection(paths).hasFilesWithExtension('css') and SideBarSelection(paths).hasItemsUnderProject()
class SideBarCopyTagScriptCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedFilesWithExtension('js'):
items.append('<script type="text/javascript" src="'+item.pathAbsoluteFromProjectEncoded()+'"></script>')
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 SideBarSelection(paths).hasFilesWithExtension('js') and SideBarSelection(paths).hasItemsUnderProject()
class SideBarCopyProjectDirectoriesCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for directory in SideBarProject().getDirectories():
items.append(directory)
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 True
class SideBarCopyContentUtf8Command(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
for item in SideBarSelection(paths).getSelectedFiles():
items.append(item.contentUTF8())
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 SideBarSelection(paths).hasFiles()
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 SideBarSelection(paths).hasFiles()
class SideBarCopyUrlCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
items = []
project = SideBarProject()
url = project.getPreference('url_production')
if url:
if url[-1:] != '/':
url = url+'/'
for item in SideBarSelection(paths).getSelectedItems():
if item.isUnderCurrentProject():
items.append(url + item.pathRelativeFromProjectEncoded())
if len(items) > 0:
sublime.set_clipboard("\n".join(items));
if len(items) > 1 :
sublime.status_message("Items URL copied")
else :
sublime.status_message("Item URL copied")
def is_enabled(self, paths = []):
return SideBarSelection(paths).hasItemsUnderProject()
class SideBarDuplicateCommand(sublime_plugin.WindowCommand):
def run(self, paths = [], new = False):
import functools
self.window.run_command('hide_panel');
self.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)
def on_done(self, old, new):
item = SideBarItem(old, os.path.isdir(old))
try:
if not item.copy(new):
sublime.error_message("Unable to duplicate, destination exists.")
self.run([old], new)
return
except:
sublime.error_message("Unable to copy:\n\n"+old+"\n\nto\n\n"+new)
self.run([old], new)
return
item = SideBarItem(new, os.path.isdir(new))
if item.isFile():
item.edit();
SideBarProject().refresh();
def is_enabled(self, paths = []):
return SideBarSelection(paths).len() == 1 and SideBarSelection(paths).hasProjectDirectories() == False
class SideBarRenameCommand(sublime_plugin.WindowCommand):
def run(self, paths = [], newLeaf = False):
import functools
branch, leaf = os.path.split(SideBarSelection(paths).getSelectedItems()[0].path())
self.window.run_command('hide_panel');
self.window.show_input_panel("New Name:", newLeaf or leaf, functools.partial(self.on_done, SideBarSelection(paths).getSelectedItems()[0].path(), branch), None, None)
def on_done(self, old, branch, leaf):
self.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):
sublime.error_message("Unable to rename, destination exists.")
self.run([old], leaf)
return
except:
sublime.error_message("Unable to rename:\n\n"+old+"\n\nto\n\n"+new)
self.run([old], leaf)
raise
return
SideBarProject().refresh();
def is_enabled(self, paths = []):
return SideBarSelection(paths).len() == 1 and SideBarSelection(paths).hasProjectDirectories() == False
class SideBarMoveCommand(sublime_plugin.WindowCommand):
def run(self, paths = [], new = False):
import functools
self.window.run_command('hide_panel');
self.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)