-
Notifications
You must be signed in to change notification settings - Fork 0
/
window_management.py
1256 lines (981 loc) · 38.6 KB
/
window_management.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
from copy import deepcopy
from sublime_plugin import WindowCommand
MAX_NUM_GROUPS = 3
import re
import sys
import time
from enum import IntEnum
from typing import Optional, Union
import sublime
import sublime_plugin
from sublime import Selection, View, active_window
from sublime_api import view_cached_substr as substr # pyright: ignore
from sublime_api import view_full_line_from_point as full_line # pyright: ignore
from sublime_api import view_line_from_point as line # pyright: ignore
from sublime_api import view_selection_add_region as add_region # pyright: ignore
if sys.version_info[:2] == (3, 3):
import traceback
importer = "unknown"
for entry in reversed(traceback.extract_stack()[:-1]):
if entry[0].startswith("<frozen importlib"):
continue
importer = entry[0]
break
print('error: Default.history_list was imported on Python 3.3 by "%s"' % importer)
class Action(IntEnum):
DO_NOTHING = 0
CHANGE_TO_BOL = 1
EXTEND = 2
action: Action = Action.DO_NOTHING
first_view: Union[View, None] = None
old_pos: int = -1
revert_to_normal_mode = [
"next_character",
"repeat_next_character",
"store_character",
"revert_selection",
"find_next",
"find_prev",
"go_to_nth_match",
]
class JumpRecord:
__slots__ = ["key", "view", "sheets"]
def __init__(self, key, view, selections, sheets):
# The View.get_regions() key to retrieve the selections
self.key = key
# The sublime.View to focus
self.view = view
# A set of sublime.Sheet objects to be selected
self.sheets = sheets
view.add_regions(key, selections)
def __repr__(self):
return "JumpRecord(%r, %r, %r, %r)" % (
self.key,
self.view,
self.selections,
self.sheets,
)
def __del__(self):
self.view.erase_regions(self.key)
def update(self, view, selections, sheets):
"""
Update the record with new details
:param view:
The new sublime.View for the record
:param selections:
A list of sublime.Region objects to set the selections to
:param sheets:
A set of sublime.Sheet objects for the selected sheets
"""
view.add_regions(self.key, selections)
if self.view != view:
self.view.erase_regions(self.key)
self.view = view
self.sheets = sheets
@property
def selections(self):
"""
The selections are not stored in this object since modifications to
the view will cause the regions to be moved. By storing the regions in
the text buffer, it will deal with shifting them around.
:return:
A list of sublime.Region objects representing the selections
"""
return self.view.get_regions(self.key)
def _log(message):
"""
Prints a log to the console, prefixed by the plugin name
:param message:
A str of the message to print
"""
print("history_list: %s" % message)
class JumpHistory:
"""
Stores the current jump history
"""
LOG = False
LIST_LIMIT = 120
TIME_BETWEEN_RECORDING = 1
TIME_AFTER_ACTIVATE = 0.1
def __init__(self):
# A stack of JumpRecord objects
self.history_list = []
# The string name of the command the user most recently executed
self.current_command = ""
# If self.current_command was different from the preceeding command
self.different_command = False
# A float unix timestamp of when self.history_list was last modified
self.last_change_time = 0
# If the last modification to self.history_list was from on_activated()
self.last_was_activation = False
# A negative integer index in self.history_list of where the user is
# located. This allows them to jump forward and backward.
self.current_item = -1
# Used to generate the region names where the selection is stored
self.key_counter = 0
def push_selection(self, view, selection=None, is_activation=False):
"""
Possibly records the current selections in the view to the history.
Will skip recording if the current state of the view would result in
history entries the user would find confusing.
:param view:
A sublime.View object
:param selection:
None to use the view's current selection, otherwise a list of
sublime.Region objects to use as the selections to record
:param is_activation:
A bool - if the push event is triggered by on_activated()
"""
if (
self.current_command == "jump_back"
or self.current_command == "jump_forward"
or self.current_command == "soft_undo"
or self.current_command == "soft_redo"
or self.current_command == "undo"
or self.current_command == "redo_or_repeat"
or self.current_command == "redo"
or self.current_command == "smart_find_word"
or self.current_command == "show_panel" # find typing
):
self.current_command = ":empty"
return
if view.settings().get("multiplier") is not None:
return
if view.settings().get("search_in_selection") is not None:
return
# We need the view to be loaded in order to interact with regions
# and the selection
if view.is_loading():
kwargs = {"selection": selection, "is_activation": is_activation}
sublime.set_timeout(lambda: self.push_selection(view, **kwargs), 100)
return
if selection is not None:
cur_sel = selection
else:
cur_sel = list(view.sel())
to_ignore = view.get_regions("jump_ignore_selection")
if to_ignore:
view.erase_regions("jump_ignore_selection")
if to_ignore == cur_sel:
if self.LOG:
_log("ignoring selection %r" % cur_sel)
return
sheets = set()
window = view.window()
if window:
sheets = set(window.selected_sheets())
temp_item = self.current_item
if self.history_list != []:
while True:
record = self.history_list[temp_item]
prev_sel = record.selections
if prev_sel or temp_item <= -len(self.history_list):
break
temp_item -= 1
# Don't record duplicate history records
if prev_sel and record.view == view and prev_sel == cur_sel:
return
# There are two situations in which we overwrite the previous
# record:
# 1. When a command is repeated in quick succession. This
# prevents lots of records when editing.
# 2. When the last item was from on_activate, we don't want to
# mark that as a real record, otherwise things like
# Goto Definition result in two records the user has to jump
# back through.
change = time.time() - self.last_change_time
just_activated = (
change <= self.TIME_AFTER_ACTIVATE and self.last_was_activation
)
duplicate_command = (
change <= self.TIME_BETWEEN_RECORDING
and not self.different_command
and record.view == view
)
if self.current_command in [
"smarter_find_under_expand",
"find_under_expand",
]:
record.update(view, cur_sel, sheets)
elif just_activated or duplicate_command:
record.update(view, cur_sel, sheets)
if self.LOG:
_log("updated record %d to %r" % (temp_item, record))
self.last_change_time = time.time()
self.last_was_activation = False if just_activated else is_activation
return
# we are searching, don't record every selection change
if (
len(prev_sel) == 1 == len(cur_sel)
and cur_sel[0].b - 1 == prev_sel[0].b
and cur_sel[0].a == prev_sel[0].a
):
del self.history_list[temp_item]
return
if self.current_item != -1:
delete_index = self.current_item + 1
del self.history_list[delete_index:]
self.current_item = -1
if self.LOG:
_log("removed newest %d records, pointer = -1" % abs(delete_index))
key = self.generate_key()
self.history_list.append(JumpRecord(key, view, cur_sel, sheets))
if self.LOG:
_log("adding %r" % self.history_list[-1])
if len(self.history_list) > self.LIST_LIMIT:
# We remove more than one at a time so we don't call this every time
old_len = len(self.history_list)
new_len = old_len - int(self.LIST_LIMIT / 3)
if self.LOG:
_log(
"removed oldest %d records, pointer = %d"
% (old_len - new_len, self.current_item)
)
del self.history_list[:new_len]
self.last_change_time = time.time()
self.last_was_activation = is_activation
# This ensures the start of a drag_select gets a unique entry, but
# then all subsequent selections get merged into a single entry
if self.current_command == "post:drag_select":
self.different_command = False
elif self.current_command == "drag_select":
self.current_command = "post:drag_select"
self.different_command = True
def jump_back(self, in_widget):
"""
Returns info about where the user should jump back to, modifying the
index of the current item.
:param in_widget:
A bool indicating if the focus is currently on a widget. In this
case we don't move the current_item, just jump to it.
:return:
A 3-element tuple:
0: sublime.View - the view to focus on
1: a list of sublime.Region objects to use as the selection
2: a set of sublime.Sheet objects that should be selected
"""
temp_item = self.current_item
cur_record = self.history_list[temp_item]
cur_sel = cur_record.selections
while True:
if temp_item == -len(self.history_list):
return None, [], []
if not in_widget:
temp_item -= 1
record = self.history_list[temp_item]
record_sel = record.selections
if in_widget:
break
if record_sel and (cur_record.view != record.view or cur_sel != record_sel):
if not cur_sel:
cur_sel = record_sel
else:
break
self.current_item = temp_item
if self.LOG:
_log("setting pointer = %d" % self.current_item)
return record.view, record_sel, record.sheets
def jump_forward(self, in_widget):
"""
Returns info about where the user should jump forward to, modifying
the index of the current item.
:param in_widget:
A bool indicating if the focus is currently on a widget. In this
case we don't move the current_item, just jump to it.
:return:
A 3-element tuple:
0: sublime.View - the view to focus on
1: a list of sublime.Region objects to use as the selection
2: a set of sublime.Sheet objects that should be selected
"""
temp_item = self.current_item
cur_record = self.history_list[temp_item]
cur_sel = cur_record.selections
while True:
if temp_item == -1:
return None, [], []
if not in_widget:
temp_item += 1
record = self.history_list[temp_item]
record_sel = record.selections
if in_widget:
break
if record_sel and (cur_record.view != record.view or cur_sel != record_sel):
if not cur_sel:
cur_sel = record_sel
else:
break
self.current_item = temp_item
if self.LOG:
_log("setting pointer = %d" % self.current_item)
return record.view, record_sel, record.sheets
def set_current_item(self, index):
"""
Modifies the index of the current item in the history list
:param index:
A negative integer, with -1 being the last item
"""
self.current_item = index
if self.LOG:
_log("setting pointer = %d" % self.current_item)
def record_command(self, command):
"""
Records a command being run, used to determine when changes to the
selection should be recorded
:param command:
A string of the command that was run. The string ":text_modified"
should be passed when the buffer is modified. This is used in
combination with the last command to ignore recording undo/redo
changes.
"""
self.different_command = self.current_command != command
# We don't track text modifications when they occur due to
# the undo/redo stack. Otherwise we'd end up pusing new
# selections, and undo/redo is handled by
# JumpHistoryUpdater.on_post_text_command().
if command == ":text_modified" and (
self.current_command == "soft_undo"
or self.current_command == "soft_redo"
or self.current_command == "undo"
or self.current_command == "redo_or_repeat"
or self.current_command == "redo"
):
return
self.current_command = command
def reorient_current_item(self, view):
"""
Find the index of the item in the history list that matches the
current view state, and update the current_item with that
:param view:
The sublime.View object to use when finding the correct current
item in the history list
"""
cur_sel = list(view.sel())
for i in range(-1, -len(self.history_list) - 1, -1):
while True:
record = self.history_list[i]
record_sel = record.selections
if record_sel or i <= -len(self.history_list):
break
i -= 1
if record_sel and record.view == view and record_sel == cur_sel:
self.current_item = i
if self.LOG:
_log("set pointer = %d" % self.current_item)
return
def remove_view(self, view):
"""
Purges all history list items referring to a specific view
:param view:
The sublime.View being removed
"""
sheet = view.sheet()
removed = 0
for i in range(-len(self.history_list), 0):
record = self.history_list[i]
if record.view == view:
del self.history_list[i]
removed += 1
if self.current_item < i:
self.current_item += 1
elif sheet in record.sheets:
record.sheets.remove(sheet)
if self.LOG:
_log(
"removed %r including %d records, pointer = %d"
% (view, removed, self.current_item)
)
def generate_key(self):
"""
Creates a key to be used with sublime.View.add_regions()
:return:
A string key to use when storing and retrieving regions
"""
# generate enough keys for 5 times the jump history limit
# this can still cause clashes as new history can be erased when we jump
# back several steps and jump again.
self.key_counter += 1
self.key_counter %= self.LIST_LIMIT * 5
return "jump_key_" + hex(self.key_counter)
# dict from window id to JumpHistory
jump_history_dict = {}
def _history_for_view(view: Optional[View]):
"""
Fetches the JumpHistory object for the view
:param view:
A sublime.Window object
:return:
A JumpHistory object
"""
global jump_history_dict
if not view:
return JumpHistory()
else:
return jump_history_dict.setdefault(view.id(), JumpHistory())
# Compatibility shim to not raise ImportError with Anaconda and other plugins
# that manipulated the JumpHistory in ST3
get_jump_history_for_window = _history_for_view
class JumpHistoryUpdater(sublime_plugin.EventListener):
"""
Listens on the sublime text events and push the navigation history into the
JumpHistory object
"""
def _valid_view(self, view):
"""
Determines if we want to track the history for a view
:param view:
A sublime.View object
:return:
A bool if we should track the view
"""
return view is not None and not view.settings().get("is_widget")
def on_modified(self, view):
if not self._valid_view(view):
return
history = _history_for_view(view)
if history.LOG:
_log("%r was modified" % view)
history.record_command(":text_modified")
def on_selection_modified(self, view):
if not self._valid_view(view):
return
history = _history_for_view(view)
if history.LOG:
_log("%r selection was changed" % view)
history.push_selection(view)
def on_activated(self, view):
if not self._valid_view(view):
return
history = _history_for_view(view)
if history.LOG:
_log("%r was activated" % view)
history.push_selection(view, is_activation=True)
def on_text_command(self, view, name, args):
if not self._valid_view(view):
return
history = _history_for_view(view)
if history.LOG:
_log("%r is about to run text command %r" % (view, name))
history.record_command(name)
def on_post_text_command(self, view, name, args):
if not self._valid_view(view):
return
if name == "undo" or name == "redo_or_repeat" or name == "redo":
_history_for_view(view).reorient_current_item(view)
if name == "soft_redo":
_history_for_view(view).set_current_item(-1)
def on_window_command(self, window, name, args):
view = window.active_view()
if not self._valid_view(view):
return
history = _history_for_view(view)
if history.LOG:
_log("%r is about to run window command %r" % (view, name))
history.record_command(name)
# TODO: We need an on_pre_closed_sheet in the future since we currently
# leave stale ImageSheet() and HtmlSheet() references in the JumpHistory.
def on_pre_close(self, view):
if not self._valid_view(view):
return
_history_for_view(view).remove_view(view)
class _JumpCommand(sublime_plugin.TextCommand):
VALID_WIDGETS = {
"find:input",
"incremental_find:input",
"replace:input:find",
"replace:input:replace",
"find_in_files:input:find",
"find_in_files:input:location",
"find_in_files:input:replace",
"find_in_files:output",
}
def _get_window(self):
"""
Returns the (non-widget) view to get the history for
:return:
None or a sublime.Window to get the history from
"""
if (
not self.view.settings().get("is_widget")
or self.view.element() in self.VALID_WIDGETS
):
return self.view.window()
return None
def _perform_jump(self, window, view, selections, sheets, clear):
"""
Restores the window to the state where the view has the selections
:param window:
The sublime.Window containing the view
:param view:
The sublime.View to focus
:param selections:
A list of sublime.Region objects to set the selection to
:param sheets:
A list of sublime.Sheet objects that should be (minimally)
selected. If the currently selected sheets is a superset of these,
then no sheet selection changes will be made.
"""
# Reduce churn by only selecting sheets when one is not visible
if set(sheets) - set(window.selected_sheets()):
window.select_sheets(sheets)
window.focus_view(view)
if clear:
view.sel().clear()
view.sel().add_all(selections)
view.show(selections[0], True)
sublime.status_message("")
def is_enabled(self):
return self._get_window() is not None
class JumpBackCommand(_JumpCommand):
def run(self, edit, clear: bool = True):
window = self._get_window()
view = self.view
jump_history = _history_for_view(view)
is_widget = self.view.settings().get("is_widget")
_, selections, sheets = jump_history.jump_back(is_widget)
if not selections:
sublime.status_message("Already at the earliest position")
return
if jump_history.LOG:
_log("jumping back to %r, %r, %r" % (view, selections, sheets))
self._perform_jump(window, view, selections, sheets, clear)
class JumpForwardCommand(_JumpCommand):
def run(self, edit, clear: bool = True):
window = self._get_window()
view = self.view
jump_history = _history_for_view(view)
is_widget = self.view.settings().get("is_widget")
view, selections, sheets = jump_history.jump_forward(is_widget)
if not selections:
sublime.status_message("Already at the newest position")
return
if jump_history.LOG:
_log("jumping forward to %r, %r, %r" % (view, selections, sheets))
self._perform_jump(window, view, selections, sheets, clear)
def _2_int_list(value):
"""
:param value:
The value to check
:return:
A bool is the value is a list with 2 ints
"""
if not isinstance(value, list):
return False
if len(value) != 2:
return False
if not isinstance(value[0], int):
return False
return isinstance(value[1], int)
class AddJumpRecordCommand(sublime_plugin.TextCommand):
"""
Allows packages to add a jump point without changing the selection
"""
def run(self, edit, selection):
if not self.view.settings().get("is_widget"):
view = self.view
else:
view = self.view.window().active_view()
regions = []
type_error = False
if isinstance(selection, int):
regions.append(sublime.Region(selection, selection))
elif isinstance(selection, list):
if _2_int_list(selection):
regions.append(sublime.Region(selection[0], selection[1]))
else:
for s in selection:
if _2_int_list(s):
regions.append(sublime.Region(s[0], s[1]))
elif isinstance(s, int):
regions.append(sublime.Region(s, s))
else:
type_error = True
break
else:
type_error = True
if type_error:
raise TypeError(
"selection must be an int, 2-int list, " "or list of 2-int lists"
)
jump_history = _history_for_view(view)
jump_history.push_selection(view, selection=regions)
def plugin_unloaded():
# Clean up the View region side-effects of the JumpRecord objects
jump_history_dict.clear()
class FancyClonePaneCommand(WindowCommand):
def run(self) -> None:
w = self.window
num_groups = w.num_groups()
next_group = w.active_group() + 1
view = w.active_view()
if view is None:
return
old_id = view.id()
v_bid = view.buffer_id()
carets = list(view.sel()) or [0]
global jump_history_dict
if num_groups > next_group:
for v in w.views_in_group(next_group):
if v_bid == v.buffer_id():
return # only unique buffers per group
w.run_command("clone_file")
new_view = w.active_view()
if new_view is None:
return
w.move_sheets_to_group(sheets=[new_view.sheet()], group=next_group)
sels = new_view.sel()
sels.clear()
[sels.add(c) for c in carets]
new_view.show_at_center(carets[0])
jump_history_dict[new_view.id()] = deepcopy(jump_history_dict[old_id])
elif num_groups < MAX_NUM_GROUPS:
w.run_command("clone_file")
w.run_command("new_pane")
new_view = w.active_view()
if new_view is None:
return
sels = new_view.sel()
sels.clear()
[sels.add(c) for c in carets]
new_view.show_at_center(carets[0])
jump_history_dict[new_view.id()] = deepcopy(jump_history_dict[old_id])
class FancyMoveBufferToNextPaneCommand(WindowCommand):
def run(self) -> None:
w = self.window
if len(w.views_in_group(w.active_group())) < 2:
return
num_groups = w.num_groups()
next_group = w.active_group() + 1
if (view := w.active_view()) is None:
return
v_bid = view.buffer_id()
change_scratch_status = False
if num_groups > next_group:
for v in w.views_in_group(next_group):
if v_bid == v.buffer_id():
if v.is_dirty():
change_scratch_status = True
v.set_scratch(True)
v.close()
break
w.move_sheets_to_group(sheets=[view.sheet()], group=next_group)
if change_scratch_status:
view.set_scratch(False)
elif num_groups < MAX_NUM_GROUPS:
w.run_command("new_pane")
class FancyMoveBufferToPrevPaneCommand(WindowCommand):
def run(self) -> None:
w = self.window
if w.num_groups() < 2:
return
view = w.active_view()
if view is None:
return
global jump_history_dict
active_group = w.active_group()
if len(w.views_in_group(active_group)) < 2 or active_group == 0:
w.run_command("close_pane")
else:
prev_group = w.active_group() - 1
w.move_sheets_to_group(sheets=[view.sheet()], group=prev_group)
view = w.active_view()
if view is None:
return
v_id = view.id()
buffers = {view.buffer_id()}
scratch_buffers = set()
for v in w.views_in_group(w.active_group()):
if v_id != v.id() and v.buffer_id() in buffers:
if not v.is_scratch() and v.is_dirty():
scratch_buffers.add(v.buffer())
v.set_scratch(True)
mit_id = v.id()
v.close()
jump_history_dict.pop(mit_id)
else:
buffers.add(v.buffer_id())
for b in scratch_buffers:
b.primary_view().set_scratch(False)
class FocusViewCommand(WindowCommand):
"""
The missing command for switching focus from side bar to view
"""
def run(self) -> None:
w = self.window
active_group = w.active_group()
if (sheet := w.active_sheet_in_group(active_group)) is None:
return
w.focus_sheet(sheet)
class NewPaneCommand(sublime_plugin.WindowCommand):
def new_pane(self, window, move_sheet, max_columns):
cur_sheet = window.active_sheet()
layout = window.get_layout()
num_panes = len(layout["cells"])
cur_index = window.active_group()
rows = layout["rows"]
cols = layout["cols"]
cells = layout["cells"]
if cells != assign_cells(num_panes, max_columns):
# Current layout doesn't follow the automatic method, reset everyting
num_rows, num_cols = rows_cols_for_panes(num_panes + 1, max_columns)
rows = create_splits(num_rows)
cols = create_splits(num_cols)
cells = assign_cells(num_panes + 1, max_columns)
else:
# Adjust the current layout, keeping the user selected column widths
# where possible
num_cols = len(cols) - 1
num_rows = len(rows) - 1
# determine row or coloumn from screen width:
view = self.window.active_view()
if num_panes == 1 and view is not None:
width, height = view.viewport_extent()
if height > width:
num_rows += 1
rows = create_splits(num_rows)
max_columns = 1
else:
num_cols += 1
cols = create_splits(num_cols)
# insert a new row or a new col
elif num_cols < max_columns:
num_cols += 1
cols = create_splits(num_cols)
else:
num_rows += 1
rows = create_splits(num_rows)
cells = assign_cells(num_panes + 1, max_columns)
window.set_layout({"cells": cells, "rows": rows, "cols": cols})
window.settings().set("last_automatic_layout", cells)
# Move all the sheets so the new pane is created in the correct location
for i in reversed(range(0, num_panes - cur_index - 1)):
current_selection = window.selected_sheets_in_group(cur_index + i + 1)
window.move_sheets_to_group(
window.sheets_in_group(cur_index + i + 1),
cur_index + i + 2,
select=False,
)
window.select_sheets(current_selection)
if move_sheet:
transient = window.transient_sheet_in_group(cur_index)
if transient is not None and cur_sheet.sheet_id == transient.sheet_id:
# transient sheets may only be moved to index -1
window.set_sheet_index(cur_sheet, cur_index + 1, -1)
else:
selected_sheets = window.selected_sheets_in_group(cur_index)
window.move_sheets_to_group(selected_sheets, cur_index + 1)
window.focus_sheet(cur_sheet)
else:
window.focus_group(cur_index)
def run(self, move=True):
max_columns = self.window.template_settings().get("max_columns", MAX_COLUMNS)
self.new_pane(self.window, move, max_columns)
"""
Listeners start here
"""
def pre_command(v: Optional[View], command_name):
if v is None:
return
if v.element() is not None:
return