forked from titoBouzout/SideBarEnhancements
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SideBar.py
2834 lines (2380 loc) · 97.9 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, shutil
import threading, time
import re
import subprocess, 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
# How to change a module variable from another module?
# http://stackoverflow.com/questions/3536620/how-to-change-a-module-variable-from-another-module
try:
from AutoFileName.autofilename import enable_autocomplete, disable_autocomplete
class AutoFileNameListener(sublime_plugin.EventListener):
"""
Is possible to auto complete file names on panels?
https://forum.sublimetext.com/t/is-possible-to-auto-complete-file-names-on-panels/28415
"""
# Notice, when the input panel is closed by `enter` the `hide_panel` command is not called
# https://github.com/SublimeTextIssues/Core/issues/1827
def on_post_window_command(self, window, command, args):
if command == "hide_panel":
disable_autocomplete()
except:
def enable_autocomplete():
pass
def disable_autocomplete():
pass
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 OpenWithListener(sublime_plugin.EventListener):
def on_load_async(self, view):
if view and view.file_name() and not view.settings().get("open_with_edit"):
item = SideBarItem(
os.path.join(
sublime.packages_path(),
"User",
"SideBarEnhancements",
"Open With",
"Side Bar.sublime-menu",
),
False,
)
if item.exists():
settings = sublime.decode_value(item.contentUTF8())
selection = SideBarSelection([view.file_name()])
for item in settings[0]["children"]:
try:
if item[
"open_automatically"
] and selection.hasFilesWithExtension(
item["args"]["extensions"]
):
SideBarFilesOpenWithCommand(Window()).run(
[view.file_name()],
item["args"]["application"],
item["args"]["extensions"],
item["args"]["args"],
)
view.close()
break
except:
pass
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")
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):
_paths = paths
if relative_to_project or s.get("new_files_relative_to_project_root", False):
_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:
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")
Window().show_input_panel(
"File Name:",
name,
functools.partial(SideBarNewFileCommand(Window()).on_done, paths, True),
None,
None,
)
class SideBarNewDirectory2Command(sublime_plugin.WindowCommand):
def run(self, paths=[], name=""):
import functools
Window().run_command("hide_panel")
Window().show_input_panel(
"Folder Name:",
name,
functools.partial(
SideBarNewDirectoryCommand(Window()).on_done, paths, True
),
None,
None,
)
class SideBarNewDirectoryCommand(sublime_plugin.WindowCommand):
def run(self, paths=[], name=""):
import functools
Window().run_command("hide_panel")
Window().show_input_panel(
"Folder Name:",
name,
functools.partial(self.on_done, paths, False),
None,
None,
)
def on_done(self, paths, relative_to_project, name):
_paths = paths
if relative_to_project or s.get("new_folders_relative_to_project_root", False):
_paths = SideBarProject().getDirectories()
if _paths:
_paths = [SideBarItem(_paths[0], True)]
if not _paths:
_paths = SideBarSelection(_paths).getSelectedDirectoriesOrDirnames()
else:
_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 SideBarFolderSaveViewsCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
views = []
for item in SideBarSelection(paths).getSelectedDirectories():
views = views + item.views()
for view in views:
view.run_command("save")
def is_enabled(self, paths=[]):
_views = []
has_dirty_view = False
for item in SideBarSelection(paths).getSelectedDirectories():
views = item.views()
_views = _views + views
for view in views:
if view.is_dirty():
has_dirty_view = True
return (
CACHED_SELECTION(paths).hasDirectories()
and len(views) > 0
and has_dirty_view
)
def is_visible(self, paths=[]):
return not s.get("disabled_menuitem_folder_save", False)
class SideBarFolderCloseViewsCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
collapsed = False
for item in SideBarSelection(paths).getSelectedDirectories():
for view in item.views():
if not collapsed:
Window().focus_view(view)
self.collapse_sidebar_folder()
collapsed = True
view.close()
def collapse_sidebar_folder(self):
# Window().run_command("reveal_in_side_bar") the tree animation breaks the functionality
Window().run_command("focus_side_bar")
Window().run_command("move", {"by": "characters", "forward": False})
def is_enabled(self, paths=[]):
views = []
for item in SideBarSelection(paths).getSelectedDirectories():
views = views + item.views()
return CACHED_SELECTION(paths).hasDirectories() and len(views) > 0
def is_visible(self, paths=[]):
return not s.get("disabled_menuitem_folder_close", False)
class SideBarFolderCloseOtherViewsCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
to_close = self.others_views(paths)
for view in to_close:
view.close()
def others_views(self, paths=[]):
window = Window()
opened = []
selected = []
for view in window.views():
opened.append(view)
for item in SideBarSelection(paths).getSelectedDirectories():
for view in item.views():
selected.append(view)
return [view for view in opened if view not in selected]
def is_enabled(self, paths=[]):
views = self.others_views(paths)
return CACHED_SELECTION(paths).hasDirectories() and len(views) > 0
def is_visible(self, paths=[]):
return not s.get("disabled_menuitem_folder_close", False)
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()
def is_visible(self, paths=[]):
return not s.get("disabled_menuitem_edit", False)
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()
def is_visible(self, paths=[]):
return not s.get("disabled_menuitem_edit", False) and not s.get(
"disabled_menuitem_edit_to_right", False
)
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
def is_visible(self, paths=[]):
return not s.get("disabled_menuitem_open_run", False)
class SideBarFilesOpenWithEditApplicationsCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
platform = ""
if sublime.platform() == "osx":
platform = "OSX"
elif sublime.platform() == "windows":
platform = "Windows"
else:
platform = "Linux"
item = SideBarItem(
os.path.join(
sublime.packages_path(),
"User",
"SideBarEnhancements",
"Side Bar.sublime-menu",
),
False,
)
if not item.exists() and False:
item = SideBarItem(
os.path.join(
sublime.packages_path(),
"User",
"SideBarEnhancements",
"Side Bar (" + platform + ").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
"args":[]
},
"open_automatically" : false // will close the view/tab and launch the application
},
//separator
{"caption":"-"},
//application 2
{
"caption": "SeaMonkey",
"id": "side-bar-files-open-with-seamonkey",
"command": "side_bar_files_open_with",
"args": {
"paths": [],
"application": "C:\\\\Archivos de programa\\\\SeaMonkey\\\\seamonkey.exe", // WINNT
"extensions":"", //open all even folders
"args":[]
},
"open_automatically" : false // will close the view/tab and launch the application
},
//application n
{
"caption": "Chrome",
"id": "side-bar-files-open-with-chrome",
"command": "side_bar_files_open_with",
"args": {
"paths": [],
"application": "C:\\\\Documents and Settings\\\\tito\\\\local\\\\Datos de programa\\\\Google\\\\Chrome\\\\Application\\\\chrome.exe",
"extensions":".*", //any file with extension
"args":[]
},
"open_automatically" : false // will close the view/tab and launch the application
},
{"caption":"-"}
]
}
]"""
)
item.edit()
def is_enabled(self, paths=[]):
return True
class SideBarFilesOpenWithCommand(sublime_plugin.WindowCommand):
def run(self, paths=[], application="", extensions=""):
self.run(self, paths, application, extensions, args=[])
def run(self, paths=[], application="", extensions="", args=[]):
application_dir, application_name = os.path.split(application)
if extensions == "*":
extensions = ".*"
if extensions == "":
items = SideBarSelection(paths).getSelectedItems()
else:
items = SideBarSelection(paths).getSelectedFilesWithExtension(extensions)
import subprocess
try:
for item in items:
# $PATH - The full path to the current file, e. g., C:\Files\Chapter1.txt.
# $PROJECT - The root directory of the current project.
# $DIRNAME - The directory of the current file, e. g., C:\Files.
# $NAME - The name portion of the current file, e. g., Chapter1.txt.
# $EXTENSION - The extension portion of the current file, e. g., txt.
for k in range(len(args)):
args[k] = args[k].replace("$PATH", item.path())
args[k] = args[k].replace("$PROJECT", item.pathProject())
args[k] = args[k].replace(
"$DIRNAME",
item.path() if item.isDirectory() else item.dirname(),
)
args[k] = args[k].replace(
"$NAME_NO_EXTENSION",
item.name().replace("." + item.extension(), ""),
)
args[k] = args[k].replace("$NAME", item.name())
args[k] = args[k].replace("$EXTENSION", item.extension())
if sublime.platform() == "osx":
subprocess.Popen(
["open", "-a", application] + args + [item.name()],
cwd=item.dirname(),
)
elif sublime.platform() == "windows":
try:
subprocess.Popen(
[application_name] + args + [escapeCMDWindows(item.path())],
cwd=expandVars(application_dir),
shell=True,
)
except:
subprocess.Popen(
[application_name] + args + [escapeCMDWindows(item.path())],
shell=True,
)
else:
try:
subprocess.Popen(
[application_name] + args + [escapeCMDWindows(item.name())],
cwd=item.dirname(),
)
except:
subprocess.Popen(
[application_name] + args + [escapeCMDWindows(item.name())]
)
except:
sublime.error_message(
'Unable to "Open With..", probably incorrect path to application.'
)
def is_enabled(self, paths=[], application="", extensions=""):
self.is_enabled(self, paths, application, extensions, args=[])
def is_enabled(self, paths=[], application="", extensions="", args=[]):
if extensions == "*":
extensions = ".*"
if extensions == "":
return CACHED_SELECTION(paths).len() > 0
else:
return CACHED_SELECTION(paths).hasFilesWithExtension(extensions)
def is_visible(self, paths=[], application="", extensions=""):
self.is_visible(self, paths, application, extensions, args=[])
def is_visible(self, paths=[], application="", extensions="", args=[]):
if extensions == "*":
extensions = ".*"
if extensions == "":
return CACHED_SELECTION(paths).len() > 0
else:
has = CACHED_SELECTION(paths).hasFilesWithExtension(extensions)
return has or (
not has
and not s.get(
"hide_open_with_entries_when_there_are_no_applicable", False
)
)
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()
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
class SideBarFindInParentCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
items = []
for item in SideBarSelection(paths).getSelectedItems():
items.append(item.dirname())
items = list(set(items))
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
class SideBarFindInProjectFoldersCommand(sublime_plugin.WindowCommand):
def run(self):
Window().run_command("hide_panel")
Window().run_command(
"show_panel", {"panel": "find_in_files", "where": "<project>"}
)
class SideBarFindInProjectCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
Window().run_command("hide_panel")
Window().run_command(
"show_panel", {"panel": "find_in_files", "where": "<project>"}
)
def is_visible(self, paths=[]):
return not s.get("disabled_menuitem_find_in_project", False)
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:
Window().run_command("hide_panel")
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))
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).hasFiles()
def description(self, paths=[]):
items = []
for item in CACHED_SELECTION(paths).getSelectedFiles():
items.append("*" + item.extension())
items = list(set(items))
if len(items) > 1:
return "In Files With Extensions " + (",".join(items)) + "…"
elif len(items) > 0:
return "In Files With Extension " + (",".join(items)) + "…"
else:
return "In Files With Extension…"
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/SideBar Results.hidden-tmLanguage"
)
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)
def is_enabled(self, paths=[]):
return True
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
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=[], in_parent="False", test="True", replace="False"):
key = "paste-" + str(time.time())
SideBarPasteThread(paths, in_parent, test, replace, key).start()
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(
CACHED_SELECTION(paths).getSelectedDirectoriesOrDirnames()
) == 1
def is_visible(self, paths=[], in_parent=False):
if in_parent == "True":
return not s.get("disabled_menuitem_paste_in_parent", False)
else:
return True
class SideBarPasteThread(threading.Thread):
def __init__(
self, paths=[], in_parent="False", test="True", replace="False", key=""
):
self.paths = paths
self.in_parent = in_parent
self.test = test
self.replace = replace
self.key = key
threading.Thread.__init__(self)
def run(self):
SideBarPasteCommand2(Window()).run(
self.paths, self.in_parent, self.test, self.replace, self.key
)
class SideBarPasteCommand2(sublime_plugin.WindowCommand):
def run(self, paths=[], in_parent="False", 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:
if in_parent == "False":
location = SideBarSelection(paths).getSelectedItems()[0].path()
else:
location = (
SideBarSelection(paths)
.getSelectedDirectoriesOrDirnames()[0]
.dirname()
)