forked from PaulJuliusMartinez/jless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewer.rs
2292 lines (2014 loc) · 78.6 KB
/
viewer.rs
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
use clap::ValueEnum;
use crate::flatjson::{FlatJson, Index, OptionIndex};
use crate::types::TTYDimensions;
#[derive(PartialEq, Eq, Copy, Clone, Debug, ValueEnum)]
pub enum Mode {
Line,
Data,
}
const DEFAULT_SCROLLOFF: u16 = 3;
pub struct JsonViewer {
pub flatjson: FlatJson,
pub top_row: Index,
pub focused_row: Index,
// Used for Focus{Prev,Next}Sibling actions.
desired_depth: usize,
// Used for JumpDown/JumpUp (ctrl-d/ctrl-u) actions.
jump_distance: Option<usize>,
pub dimensions: TTYDimensions,
// We call this scrolloff_setting, to differentiate between
// what it's set to, and what the scrolloff functionally is
// if it's set to value >= height / 2.
//
// Access the functional value via .scrolloff().
pub scrolloff_setting: u16,
pub mode: Mode,
}
impl JsonViewer {
pub fn new(flatjson: FlatJson, mode: Mode) -> JsonViewer {
JsonViewer {
flatjson,
top_row: 0,
focused_row: 0,
desired_depth: 0,
jump_distance: None,
dimensions: TTYDimensions::default(),
scrolloff_setting: DEFAULT_SCROLLOFF,
mode,
}
}
}
#[derive(Debug, Copy, Clone)]
pub enum Action {
// Does nothing, for debugging, shouldn't modify any state.
#[allow(dead_code)]
NoOp,
MoveUp(usize),
MoveDown(usize),
MoveLeft,
MoveRight,
// TODO: Come up with better names for these. Their behavior is
// a little subtle. When moving down it'll move forward until
// the depth changes. If the depth increases (because it got to
// an expanded container) it'll stop on the line of the opening
// of the container, but if the depth decreases (because we moved
// past the last child of the current container) it'll focus the
// line after.
MoveUpUntilDepthChange,
MoveDownUntilDepthChange,
FocusParent,
// The behavior of these is subtle and stateful. These move to the
// previous/next sibling of the focused element. If we are focused
// on the first/last child, we will move to the parent, but we
// will remember what depth we were at when we first performed
// this action, and move back to that depth the next time we can.
FocusPrevSibling(usize),
FocusNextSibling(usize),
FocusFirstSibling,
FocusLastSibling,
FocusTop,
FocusBottom,
FocusMatchingPair,
ScrollUp(usize),
ScrollDown(usize),
// By default, these move by half a screen, and move the focus by
// the same number of lines, so the focus doesn't appear to move
// on the screen. When jumping down, it will not show lines past
// the end of the file.
//
// When a count is provided, we'll move by that many *lines* (not
// N half screen sizes). This count is stored in
// JsonViewer.jump_distance and used for subsequent jumps, rather
// than half a screen size.
//
// vim always moves both the viewing window and the focused line
// by the appropriate lines, so the location of the focused line
// on the screen will move when jumping past the end of the file
// (or before the start).
//
// We'll implement a slight variation on this behavior. If the
// viewing window moves, we'll keep the focused line in the same
// vertical location, but once we're at the top of the file, and
// the viewing window doesn't change at all, then we will change
// the focused line by the expected count.
//
// These commands ignore the scrolloff option.
JumpUp(Option<usize>),
JumpDown(Option<usize>),
JumpTo {
line: Index,
make_visible: bool,
},
PageUp(usize),
PageDown(usize),
MoveFocusedLineToTop,
MoveFocusedLineToCenter,
MoveFocusedLineToBottom,
Click(u16),
ToggleCollapsed,
CollapseNodeAndSiblings,
DeepCollapseNodeAndSiblings,
ExpandNodeAndSiblings,
DeepExpandNodeAndSiblings,
ToggleMode,
ResizeViewerDimensions(TTYDimensions),
}
impl JsonViewer {
pub fn perform_action(&mut self, action: Action) {
// TODO: These two functions should really be refactored into a single function
// that returns something like:
// enum WindowTrackingBehavior {
// HandledByAction,
// EnsureFocusedRowIsVisible,
// KeepFocusedLineInSamePlaceOnScreen(u16)
// }
let track_window = JsonViewer::should_refocus_window(&action);
let prev_index_of_focused_row = self.should_keep_focused_row_at_same_screen_index(&action);
let reset_desired_depth = JsonViewer::should_reset_desired_depth(&action);
match action {
Action::NoOp => {}
Action::MoveUp(n) => self.move_up(n),
Action::MoveDown(n) => self.move_down(n),
Action::MoveLeft => self.move_left(),
Action::MoveRight => self.move_right(),
Action::MoveUpUntilDepthChange => self.move_up_until_depth_change(),
Action::MoveDownUntilDepthChange => self.move_down_until_depth_change(),
Action::FocusParent => self.focus_parent(),
Action::FocusPrevSibling(n) => self.focus_prev_sibling(n),
Action::FocusNextSibling(n) => self.focus_next_sibling(n),
Action::FocusFirstSibling => self.focus_first_sibling(),
Action::FocusLastSibling => self.focus_last_sibling(),
Action::FocusTop => self.focus_top(),
Action::FocusBottom => self.focus_bottom(),
Action::FocusMatchingPair => self.focus_matching_pair(),
Action::ScrollUp(n) => self.scroll_up(n),
Action::ScrollDown(n) => self.scroll_down(n),
Action::JumpUp(option_n) => self.jump_up(option_n),
Action::JumpDown(option_n) => self.jump_down(option_n),
Action::JumpTo { line, make_visible } => self.jump_to(line, make_visible),
Action::PageUp(n) => self.scroll_up(self.dimensions.height as usize * n),
Action::PageDown(n) => self.scroll_down(self.dimensions.height as usize * n),
Action::MoveFocusedLineToTop => self.move_focused_line_to_top(),
Action::MoveFocusedLineToCenter => self.move_focused_line_to_center(),
Action::MoveFocusedLineToBottom => self.move_focused_line_to_bottom(),
Action::Click(n) => self.click_row(n),
Action::ToggleCollapsed => self.toggle_collapsed(),
Action::CollapseNodeAndSiblings => self.collapse_node_and_siblings(),
Action::DeepCollapseNodeAndSiblings => self.deep_collapse_node_and_siblings(),
Action::ExpandNodeAndSiblings => self.expand_node_and_siblings(),
Action::DeepExpandNodeAndSiblings => self.deep_expand_node_and_siblings(),
Action::ToggleMode => self.toggle_mode(),
Action::ResizeViewerDimensions(dims) => self.dimensions = dims,
}
if reset_desired_depth {
self.desired_depth = self.flatjson[self.focused_row].depth;
}
if track_window {
self.ensure_focused_row_is_visible();
} else if let Some(screen_index) = prev_index_of_focused_row {
// Keep focused line in same place on the screen.
self.top_row =
self.count_n_lines_before(self.focused_row, screen_index as usize, self.mode);
}
}
fn should_refocus_window(action: &Action) -> bool {
match action {
Action::NoOp => false,
Action::MoveUp(_) => true,
Action::MoveDown(_) => true,
Action::MoveLeft => true,
Action::MoveRight => true,
Action::MoveUpUntilDepthChange => true,
Action::MoveDownUntilDepthChange => true,
Action::FocusParent => true,
Action::FocusPrevSibling(_) => true,
Action::FocusNextSibling(_) => true,
Action::FocusFirstSibling => true,
Action::FocusLastSibling => true,
Action::FocusTop => false, // Window refocusing is handled in focus_top.
Action::FocusBottom => true,
Action::FocusMatchingPair => true,
Action::ScrollUp(_) => false,
Action::ScrollDown(_) => false,
Action::JumpUp(_) => false,
Action::JumpDown(_) => false,
Action::JumpTo { .. } => true,
Action::PageUp(_) => false,
Action::PageDown(_) => false,
Action::MoveFocusedLineToTop => false,
Action::MoveFocusedLineToCenter => false,
Action::MoveFocusedLineToBottom => false,
Action::Click(_) => true,
Action::CollapseNodeAndSiblings => false,
Action::DeepCollapseNodeAndSiblings => false,
Action::ExpandNodeAndSiblings => false,
Action::DeepExpandNodeAndSiblings => false,
Action::ToggleMode => false,
Action::ResizeViewerDimensions(_) => true,
_ => false,
}
}
fn should_reset_desired_depth(action: &Action) -> bool {
!matches!(
action,
Action::NoOp
| Action::FocusPrevSibling(_)
| Action::FocusNextSibling(_)
| Action::ScrollUp(_)
| Action::ScrollDown(_)
| Action::MoveFocusedLineToTop
| Action::MoveFocusedLineToCenter
| Action::MoveFocusedLineToBottom
| Action::ToggleMode
| Action::ResizeViewerDimensions(_)
)
}
fn should_keep_focused_row_at_same_screen_index(&self, action: &Action) -> Option<u16> {
match action {
Action::ToggleMode
| Action::CollapseNodeAndSiblings
| Action::DeepCollapseNodeAndSiblings
| Action::ExpandNodeAndSiblings
| Action::DeepExpandNodeAndSiblings => Some(self.index_of_focused_row_on_screen()),
_ => None,
}
}
fn move_up(&mut self, rows: usize) {
let mut row = self.focused_row;
for _ in 0..rows {
let prev_row = match self.mode {
Mode::Line => self.flatjson.prev_visible_row(row),
Mode::Data => self.flatjson.prev_item(row),
};
match prev_row {
OptionIndex::Nil => break,
OptionIndex::Index(prev_row_index) => {
row = prev_row_index;
}
}
}
self.focused_row = row;
}
fn move_down(&mut self, rows: usize) {
let mut row = self.focused_row;
for _ in 0..rows {
let next_row = match self.mode {
Mode::Line => self.flatjson.next_visible_row(row),
Mode::Data => self.flatjson.next_item(row),
};
match next_row {
OptionIndex::Nil => break,
OptionIndex::Index(next_row_index) => {
row = next_row_index;
}
}
}
self.focused_row = row;
}
fn move_right(&mut self) {
let focused_row = &self.flatjson[self.focused_row];
if focused_row.is_primitive() {
return;
}
if focused_row.is_collapsed() {
self.flatjson.expand(self.focused_row);
return;
}
if focused_row.is_opening_of_container() {
self.focused_row = focused_row.first_child().unwrap();
} else {
debug_assert!(
self.mode == Mode::Line,
"Can't be focused on closing char in Data mode"
);
self.focused_row = self.flatjson.prev_visible_row(self.focused_row).unwrap();
}
}
fn move_left(&mut self) {
if self.flatjson[self.focused_row].is_container()
&& self.flatjson[self.focused_row].is_expanded()
{
self.flatjson.collapse(self.focused_row);
if self.flatjson[self.focused_row].is_closing_of_container() {
self.focused_row = self.flatjson[self.focused_row].pair_index().unwrap();
}
return;
}
self.focus_parent();
}
fn move_up_until_depth_change(&mut self) {
let mut row = self.focused_row;
let mut current_depth = self.flatjson[row].depth;
// We *will* change depths if the very next row is
// at a different depth.
let mut moved_yet = false;
loop {
let prev_row = match self.mode {
Mode::Line => self.flatjson.prev_visible_row(row),
Mode::Data => self.flatjson.prev_item(row),
};
match prev_row {
OptionIndex::Nil => break,
OptionIndex::Index(prev_row_index) => {
let prev_row_depth = self.flatjson[prev_row_index].depth;
if prev_row_depth != current_depth {
// If the line immediately above the starting one
// is at greater depth, then we won't stop immediately.
// We will keep moving along the new depth until it changes.
//
// This makes sure that this action acts like the inverse
// of move_down_until_depth_change, which will focus
// focus to a less indented line, but not a more indented one.
if !moved_yet && prev_row_depth > current_depth {
current_depth = prev_row_depth;
} else {
// If we're not in the above special case, then we do
// want to stop because the depth changed. The only
// question then is whether we keep the focus at the
// current depth or choose to focus the line with the
// different depth. We will only focus the line with
// the different depth if we haven't moved yet to ensure
// we move at least one row.
if !moved_yet {
row = prev_row_index;
}
break;
}
}
row = prev_row_index;
moved_yet = true;
}
}
}
self.focused_row = row;
}
fn move_down_until_depth_change(&mut self) {
let mut row = self.focused_row;
let current_depth = self.flatjson[row].depth;
// We *will* move down into a child if that's the
// very next line.
let mut moved_yet = false;
loop {
let next_row = match self.mode {
Mode::Line => self.flatjson.next_visible_row(row),
Mode::Data => self.flatjson.next_item(row),
};
match next_row {
OptionIndex::Nil => break,
OptionIndex::Index(next_row_index) => {
let next_row_depth = self.flatjson[next_row_index].depth;
if next_row_depth != current_depth {
// Do move to parent nodes, but don't move into
// child nodes (unless we haven't moved yet).
if next_row_depth < current_depth || !moved_yet {
row = next_row_index;
}
break;
}
row = next_row_index;
moved_yet = true;
}
}
}
self.focused_row = row;
}
fn focus_parent(&mut self) {
if let OptionIndex::Index(parent) = self.flatjson[self.focused_row].parent {
self.focused_row = parent;
}
}
fn focus_prev_sibling(&mut self, rows: usize) {
for _ in 0..rows {
// The user is trying to move up in the file, but stay at the desired depth, so we just
// move up once, and then, if we're focused on a node that's nested deeper than the
// desired depth, move up the node's parents until we get to the right depth.
self.move_up(1);
let mut focused_row = &self.flatjson[self.focused_row];
while focused_row.depth > self.desired_depth {
self.focused_row = focused_row.parent.unwrap();
focused_row = &self.flatjson[self.focused_row];
}
}
}
fn focus_next_sibling(&mut self, rows: usize) {
for _ in 0..rows {
// The user is trying to move down in the file, but stay at the desired depth.
// If we just move down once, this will accomplish what the user wants, unless
// they are already at the correct depth and currently focused on a opening
// of an expanded container. If this is the case, we just want to jump past the
// contents to the closing brace. If we're in Data mode, since the closing brace
// can't be focused, we'll still want to go past it to the next visible item.
let current_row = &self.flatjson[self.focused_row];
if current_row.depth == self.desired_depth
&& current_row.is_opening_of_container()
&& current_row.is_expanded()
{
let closing_brace = current_row.pair_index().unwrap();
self.focused_row = if self.mode == Mode::Data {
match self.flatjson.next_item(closing_brace) {
// If there's no item after the closing brace, then we don't actually
// want to move the focus at all.
OptionIndex::Nil => self.focused_row,
OptionIndex::Index(i) => i,
}
} else {
closing_brace
}
} else {
self.move_down(1);
}
}
}
fn focus_first_sibling(&mut self) {
match &self.flatjson[self.focused_row].parent {
OptionIndex::Index(parent_index) => {
self.focused_row = self.flatjson[*parent_index].first_child().unwrap();
}
// If node has no parent, then we're at the top level and want to focus
// the first element, which is the top of the file.
OptionIndex::Nil => self.focus_top(),
}
}
fn focus_last_sibling(&mut self) {
match &self.flatjson[self.focused_row].parent {
OptionIndex::Index(parent_index) => {
let closing_parent_index = self.flatjson[*parent_index].pair_index().unwrap();
self.focused_row = self.flatjson[closing_parent_index].last_child().unwrap();
}
// If node has no parent, then we're at the top level and want to focus
// the last element. If this last element is a container though, we want to
// make sure to focus on the _start_ of the container.
OptionIndex::Nil => {
let last_index = self.flatjson.last_visible_index();
if self.flatjson[last_index].is_container() {
self.focused_row = self.flatjson[last_index].pair_index().unwrap();
} else {
self.focused_row = last_index;
}
}
}
}
fn focus_top(&mut self) {
self.top_row = 0;
self.focused_row = 0;
}
fn focus_bottom(&mut self) {
self.focused_row = match self.mode {
Mode::Line => self.flatjson.last_visible_index(),
Mode::Data => self.flatjson.last_visible_item(),
};
}
fn focus_matching_pair(&mut self) {
if self.mode == Mode::Data {
return;
}
let current_row = &self.flatjson[self.focused_row];
if current_row.is_collapsed() {
return;
}
match current_row.pair_index() {
// Do nothing; focused element isn't a container
OptionIndex::Nil => {}
OptionIndex::Index(matching_pair_index) => {
self.focused_row = matching_pair_index;
}
}
}
fn scroll_up(&mut self, rows: usize) {
self.top_row = self.count_n_lines_before(self.top_row, rows, self.mode);
let max_focused_row = self.count_n_lines_past(
self.top_row,
(self.dimensions.height - self.scrolloff() - 1) as usize,
self.mode,
);
if self.focused_row > max_focused_row {
self.focused_row = max_focused_row;
}
}
fn scroll_down(&mut self, rows: usize) {
self.top_row = self.count_n_lines_past(self.top_row, rows, self.mode);
let first_focusable_row =
self.count_n_lines_past(self.top_row, self.scrolloff() as usize, self.mode);
if self.focused_row < first_focusable_row {
self.focused_row = first_focusable_row;
}
}
fn jump_up(&mut self, distance: Option<usize>) {
let lines = self.determine_jump_distance(distance);
let original_top_row = self.top_row;
let num_visible_before_focused = self.index_of_focused_row_on_screen();
self.top_row = self.count_n_lines_before(self.top_row, lines, self.mode);
// If the viewing window moved at all, then keep the focused line in the
// same place vertically. But if we're at the top of the file, then move
// the focused line by the expected amount. This prevents the viewing
// window and the focused line from both changing, but by different amounts.
if original_top_row != self.top_row {
self.focused_row = self.count_n_lines_past(
self.top_row,
num_visible_before_focused as usize,
self.mode,
);
} else {
self.focused_row = self.count_n_lines_before(self.focused_row, lines, self.mode);
}
}
fn jump_down(&mut self, distance: Option<usize>) {
let lines = self.determine_jump_distance(distance);
let original_top_row = self.top_row;
let num_visible_before_focused = self.index_of_focused_row_on_screen();
self.top_row = self.count_n_lines_past(self.top_row, lines, self.mode);
let last_line = match self.mode {
Mode::Line => self.flatjson.last_visible_index(),
Mode::Data => self.flatjson.last_visible_item(),
};
let top_row_if_last_row_is_at_bottom =
self.count_n_lines_before(last_line, self.dimensions.height as usize - 1, self.mode);
// When jumping, we won't show lines past EOF, unless we already
// are showing lines past EOF.
if self.top_row > top_row_if_last_row_is_at_bottom {
self.top_row = top_row_if_last_row_is_at_bottom.max(original_top_row);
}
// If the viewing window moved at all, then keep the focused line in the
// same place vertically. But if we're at the bottom of the file, then move
// the focused line by the expected amount. This prevents the viewing
// window and the focused line from both changing, but by different amounts.
if original_top_row != self.top_row {
self.focused_row = self.count_n_lines_past(
self.top_row,
num_visible_before_focused as usize,
self.mode,
);
} else {
self.focused_row = self.count_n_lines_past(self.focused_row, lines, self.mode);
}
}
// Focus on a specific line. If the line is the closing of a container (in Data mode),
// go to the last non-closing container line before it.
//
// If make_visible is true, this will ensure all of the parent containers are opened.
// If make_visible is false, then we'll focus the highest level closed parent.
fn jump_to(&mut self, line: Index, make_visible: bool) {
self.focused_row = line.min(self.flatjson.0.len() - 1);
match self.mode {
Mode::Data => {
// Back up to a non-closing of a container.
while self.flatjson[self.focused_row].is_closing_of_container() {
self.focused_row -= 1;
}
}
Mode::Line => {
// If the line is the closing of a container, and it's collapsed, either
// make it visible by expanding it, or go to the opening instead.
let row = &self.flatjson[self.focused_row];
if row.is_closing_of_container() && row.is_collapsed() {
if make_visible {
self.flatjson.expand(self.focused_row);
} else {
self.focused_row = row.pair_index().unwrap();
}
}
}
}
if make_visible {
let mut curr = self.focused_row;
while let OptionIndex::Index(parent) = self.flatjson[curr].parent {
self.flatjson.expand(parent);
curr = parent;
}
} else {
self.focused_row = self.flatjson.first_visible_ancestor(self.focused_row);
}
}
// If the user provided a count to a jump command, sets that as the new
// jump distance. Otherwise, use the stored jump distance, or if none has
// been set yet, use the default of half a window size.
fn determine_jump_distance(&mut self, distance: Option<usize>) -> usize {
self.jump_distance = distance.or(self.jump_distance);
match self.jump_distance {
Some(n) => n,
None => (self.dimensions.height as usize / 2).max(1),
}
}
fn move_focused_line_to_top(&mut self) {
let padding = self.scrolloff() as usize;
self.top_row = self.count_n_lines_before(self.focused_row, padding, self.mode);
}
fn move_focused_line_to_center(&mut self) {
let padding = (self.dimensions.height / 2) as usize;
self.top_row = self.count_n_lines_before(self.focused_row, padding, self.mode);
}
fn move_focused_line_to_bottom(&mut self) {
let padding = (self.dimensions.height - self.scrolloff() - 1) as usize;
self.top_row = self.count_n_lines_before(self.focused_row, padding, self.mode);
}
fn click_row(&mut self, row: u16) {
self.focused_row = self.count_n_lines_past(self.top_row, (row - 1) as usize, self.mode);
if self.flatjson[self.focused_row].is_opening_of_container() {
self.toggle_collapsed();
}
}
fn toggle_collapsed(&mut self) {
let focused_row = &mut self.flatjson[self.focused_row];
if focused_row.is_primitive() {
return;
}
if focused_row.is_closing_of_container() {
debug_assert!(
focused_row.is_expanded(),
"Focused on closing char when row is collapsed",
);
self.focused_row = self.flatjson[self.focused_row].pair_index().unwrap();
}
self.flatjson.toggle_collapsed(self.focused_row);
}
fn collapse_node_and_siblings(&mut self) {
// If we're collapsing a node, make sure we're focused on the open.
self.switch_focus_to_opening_of_container_if_on_closing();
self.set_collapse_state_on_node_and_siblings(true);
}
fn deep_collapse_node_and_siblings(&mut self) {
// If we're collapsing a node, make sure we're focused on the open.
self.switch_focus_to_opening_of_container_if_on_closing();
self.set_deep_collapse_state_on_node_and_siblings(true);
}
fn expand_node_and_siblings(&mut self) {
self.set_collapse_state_on_node_and_siblings(false);
}
fn deep_expand_node_and_siblings(&mut self) {
self.set_deep_collapse_state_on_node_and_siblings(false);
}
fn switch_focus_to_opening_of_container_if_on_closing(&mut self) {
let focused_row = &mut self.flatjson[self.focused_row];
if focused_row.is_closing_of_container() {
debug_assert!(
focused_row.is_expanded(),
"Focused on closing char when row is collapsed",
);
self.focused_row = self.flatjson[self.focused_row].pair_index().unwrap();
}
}
fn set_collapse_state_on_node_and_siblings(&mut self, collapsed: bool) {
let first_sibling =
if let OptionIndex::Index(parent) = self.flatjson[self.focused_row].parent {
self.flatjson[parent].first_child().unwrap()
} else {
// If we don't have parent, that means we're at the top level, so the first
// sibling is the very first element.
0
};
let mut next_sibling = OptionIndex::Index(first_sibling);
while let OptionIndex::Index(next) = next_sibling {
if collapsed {
self.flatjson.collapse(next);
} else {
self.flatjson.expand(next);
}
next_sibling = self.flatjson[next].next_sibling;
}
}
fn set_deep_collapse_state_on_node_and_siblings(&mut self, collapsed: bool) {
let (start, end) =
if let OptionIndex::Index(parent) = self.flatjson[self.focused_row].parent {
(parent + 1, self.flatjson[parent].pair_index().unwrap())
} else {
// If we don't have parent, that means we're at the top level, so the first
// sibling is the very first element.
(0, self.flatjson.0.len())
};
for i in start..end {
if self.flatjson[i].is_opening_of_container() {
if collapsed {
self.flatjson.collapse(i);
} else {
self.flatjson.expand(i);
}
}
}
}
fn toggle_mode(&mut self) {
// If we're transitioning from line mode to focused mode, and we're focused on
// the closing of a container, we need to move the focus.
if self.mode == Mode::Line && self.flatjson[self.focused_row].is_closing_of_container() {
// We'll move focus to the next item, unless we're at the end of
// the file and have to move focus backwards.
//
// By focusing the next item, and ensuring that the focus stays in the
// same place on the screen, it will look the surrounding data is getting
// "pulled" towards the focused line.
if let OptionIndex::Index(next) = self.flatjson.next_item(self.focused_row) {
self.focused_row = next;
} else {
self.focused_row = self.flatjson.prev_item(self.focused_row).unwrap();
}
}
// Toggle the mode.
self.mode = match self.mode {
Mode::Line => Mode::Data,
Mode::Data => Mode::Line,
};
}
fn scrolloff(&self) -> u16 {
self.scrolloff_setting.min((self.dimensions.height - 1) / 2)
}
// This is called after moving the cursor up or down (or other operations that
// change where the focused row is) and makes sure that it isn't within SCROLLOFF
// lines of the top or bottom of the screen.
fn ensure_focused_row_is_visible(&mut self) {
// First make sure that the top row is visible. It may no longer be visible
// after performing an action like CollapseNodeAndSiblings.
self.ensure_top_row_is_visible();
// height; scrolloff; actual scrolloff; max_padding
// 100 3 3 96
// 15 7 7 7
// 15 8 7 7
// 16 8 7 8
let scrolloff = self.scrolloff();
// Max padding is max number of rows that can be visible between the focused
// row and the top or bottom of the screen.
let max_padding = self.dimensions.height - scrolloff - 1;
// Normally as the user moves down the file we'll keep the focused line
// scrolloff lines from the bottom of the screen.
//
// But if the user jumps well past the end of the screen, rather than leaving
// the cursor scrolloff lines from the bottom, we'll put it closer to the
// middle, so they see more context, matching similar behavior in vim.
//
// In vim, the re-centering behavior occurs when you jump roughly half a screen
// past the bottom of the current visible screen. The exact point where it
// switches between leaving the cursor at the bottom of the screen vs.
// recentering works out so that there are no lines in common between the
// lines displayed before the jump and the lines displayed after the jump.
//
// We'll make the assumption that in JSON the context provided by previous lines
// is less helpful. When we refocus the screen we'll put the focused line 1/3
// of the way from the top, so we need to have moved 1 and 1/3 screen lengths
// past the top line for there to not be any overlap in the lines visible on the
// screen.
//
// We anticipate that users will also jump using FocusNextSibling frequently,
// which means that the focused line is a natural starting point of a large
// object, so showing more lines after the focused line than before makes
// sense.
//
// This might make less sense if they arrived there after a random jump or
// text search. Perhaps we could do something more intelligent where we try
// to make sure that the parent is visible, but this works for now.
//
// Because of the assumption that lines after the focused line are more relevant,
// we don't recenter the focused line when moving far up in the file.
let recenter_distance = self.dimensions.height + (self.dimensions.height / 3);
// Note that this will return 0 if focused_row < top_row.
let num_visible_before_focused = self.count_visible_rows_before(
self.top_row,
self.focused_row,
// Add 1 so we can differentiate between == recenter_distance and > recenter_distance
recenter_distance + 1,
self.mode,
);
// Handle focused line too close to or past the top of the screen.
if self.focused_row < self.top_row || num_visible_before_focused < scrolloff {
self.top_row =
self.count_n_lines_before(self.focused_row, scrolloff as usize, self.mode);
} else if num_visible_before_focused > max_padding {
// Handle focused line too close to or past the bottom of the screen.
// If the user moved well past the bottom of the screen, we will refocus
// the cursor in the middle of the screen, rather than at the bottom of
// the screen.
//
// Note this is padding from the _bottom_ of the screen.
let refocus_padding = if num_visible_before_focused > recenter_distance {
let bottom_padding = self.dimensions.height * 2 / 3;
// Make sure to still obey scrolloff on the top if scrolloff > 1/3 of height.
bottom_padding.min(max_padding)
} else {
scrolloff
};
// We need to figure out where the last line is because we won't
// show any empty lines past the end of the file (unless the
// user explicitly scrolls past the end of the file).
//
// This overrides the scrolloff setting.
let last_line = match self.mode {
Mode::Line => self.flatjson.last_visible_index(),
Mode::Data => self.flatjson.last_visible_item(),
};
let lines_visible_before_eof = self.count_visible_rows_before(
self.focused_row,
last_line,
refocus_padding + 1,
self.mode,
);
// Clamp the refocus padding at the number of lines visible before EOF
// so that we don't show anything past EOF.
let bottom_padding = refocus_padding.min(lines_visible_before_eof);
self.top_row = self.count_n_lines_before(
self.focused_row,
(self.dimensions.height - bottom_padding - 1) as usize,
self.mode,
);
}
}
// Makes sure that the top row is visible. If not, the top row will be updated
// to the first visible parent of the top row.
//
// We need to consider both the case that a parent of the top row has been
// collapsed, and the case that the top row is the closing brace of a container
// that has been collapsed.
//
// In this second (much less likely) case, we'll set the top row to the opening
// of the container (but then still make sure all of its parents are visible).
fn ensure_top_row_is_visible(&mut self) {
// Check rare case that top row is closing of container that is now collapsed.
if self.flatjson[self.top_row].is_closing_of_container() {
let opening = self.flatjson[self.top_row].pair_index().unwrap();
if self.flatjson[opening].is_collapsed() {
self.top_row = opening;
}
}
// Now make sure all ancestors are visible.
let mut ancestor = self.top_row;
while let OptionIndex::Index(ancestor_index) = self.flatjson[ancestor].parent {
if self.flatjson[ancestor_index].is_collapsed() {
self.top_row = ancestor_index;
}
ancestor = ancestor_index;
}
}
fn count_n_lines_before(&self, mut start: Index, mut lines: usize, mode: Mode) -> Index {
while lines != 0 && start != 0 {
start = match mode {
Mode::Line => self.flatjson.prev_visible_row(start).unwrap(),
Mode::Data => self.flatjson.prev_item(start).unwrap(),
};
lines -= 1;
}
start
}
fn count_n_lines_past(&self, mut start: Index, mut lines: usize, mode: Mode) -> Index {
while lines != 0 {
let next = match mode {
Mode::Line => self.flatjson.next_visible_row(start),
Mode::Data => self.flatjson.next_item(start),
};
match next {
OptionIndex::Nil => break,
OptionIndex::Index(n) => start = n,
};
lines -= 1;
}
start
}
// Counts how many visible lines/items (depending on mode) there are between start and end.
//
// start is counted as visible, and end is not counted as visible.
//
// If start == end, we return 0.
//
// We won't count more than max lines past start. If we still haven't gotten to end,
// we'll return max.
fn count_visible_rows_before(&self, mut start: Index, end: Index, max: u16, mode: Mode) -> u16 {
let mut num_visible: u16 = 0;
while start < end && num_visible < max {
num_visible += 1;
start = match mode {
Mode::Line => self.flatjson.next_visible_row(start).unwrap(),
Mode::Data => self.flatjson.next_item(start).unwrap(),
};
}
num_visible
}
// Returns the index of the focused row within the actual viewing window.