-
Notifications
You must be signed in to change notification settings - Fork 1
/
json-par-motion.el
2171 lines (1873 loc) · 67.5 KB
/
json-par-motion.el
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
;;; json-par-motion.el --- Moving the point in JSON Par mode -*- lexical-binding: t -*-
;; Copyright (C) 2021 taku0
;;
;; Author: taku0 (http://github.com/taku0)
;; URL: https://github.com/taku0/json-par
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Functions for moving the point in JSON Par mode.
;;; Code:
(require 'json-par-utils)
(require 'json-par-lexer)
(declare-function json-par-delete-head-of-member
"json-par-delete"
(&optional action))
(declare-function json-par-oneline
"json-par-oneline-multiline"
(&optional min-level))
(defvar-local json-par--dwim-function nil)
;;; Customizations
(defcustom json-par-collapse-when-exit-from-empty-brackets t
"If non-nil, collapse an empty array/object when exiting from it.
This affects `json-par-up-backward' and `json-par-up-forward'."
:type 'boolean
:group 'json-par
:safe #'booleanp)
(defcustom json-par-place-after-down-into-object 'value
"Target place after invoking `json-par-down' before an object.
- `value': before the value of the key-value pair.
- `member': before the whole key-value pair."
:type '(choice (const :tag "Before value" value)
(const :tag "Before member" member))
:group 'json-par
:safe #'symbolp)
;;; forward-sexp-function
(defun json-par-forward-sexp (&optional arg)
"Move forward a token or a list.
Inside a string or a comment, forward a word.
See `forward-sexp' for ARG.
Signal `scan-error' if it hits a unmatched parenthesis."
(interactive "p")
(unless arg
(setq arg 1))
(if (< 0 arg)
(dotimes (_ arg)
(json-par--forward-sexp-1))
(dotimes (_ (- arg))
(json-par--backward-sexp-1))))
(defun json-par--forward-sexp-1 ()
"Move forward a token or a list.
Inside a string or a comment, forward a word.
Signal `scan-error' if it hits a close parenthesis."
(let ((string-like-beginning-position
(json-par--string-like-beginning-position))
string-like-end-position
(pos (point))
token)
(if string-like-beginning-position
(progn
(setq string-like-end-position
(save-excursion
(goto-char string-like-beginning-position)
(json-par-token-end
(json-par-forward-token-or-list-or-comment))))
(forward-word)
(when (< string-like-end-position (point))
(goto-char string-like-end-position)
(when (bolp)
(backward-char)
(when (<= (point) pos)
(skip-chars-forward "\s\t\n")
(json-par--forward-sexp-1)))))
(setq token (json-par-forward-token-or-list-or-comment))
(when (bolp)
(backward-char)
(when (<= (point) pos)
(skip-chars-forward "\s\t\n")
(json-par--forward-sexp-1)))
(when (json-par-token-close-bracket-p token)
(goto-char pos)
(signal 'scan-error
(list "Unbalanced parentheses"
(json-par-token-start token)
(json-par-token-end token)))))))
(defun json-par--backward-sexp-1 ()
"Move backward a token or list.
Inside a string or a comment, forward a word.
Signal `scan-error' if it hits a open parenthesis."
(let ((string-like-beginning-position
(json-par--string-like-beginning-position))
(pos (point))
token)
(if string-like-beginning-position
(progn
(backward-word)
(when (< (point) string-like-beginning-position)
(goto-char string-like-beginning-position)))
(setq token (json-par-backward-token-or-list-or-comment))
(when (json-par-token-open-bracket-p token)
(goto-char pos)
(signal 'scan-error
(list "Unbalanced parentheses"
(json-par-token-start token)
(json-par-token-end token)))))))
;;; Object/array members
(defun json-par--parse-member-forward ()
"Parse the current member.
Assuming the point is at the beginning of the member.
Return a hash table with the following members:
- :start-of-member, the start position of the member
- :end-of-member, the end position of the member
- :key-token, the key token of a key-value pair, if any
- :colon-token, the colon token of a key-value pair, if any
- :value-token, the value token of member, if any"
(save-excursion
(json-par--forward-spaces)
(let ((done nil)
(result (make-hash-table :size 5))
(start-of-member (point))
end-of-member
key-token
colon-token
value-token
token
(json-par--already-out-of-comment t)
(json-par--already-out-of-atom t))
(while (progn
(json-par--forward-spaces)
(and (not (memq (char-after) '(nil ?\, ?\] ?\) ?})))
(not done)))
(setq token (json-par-forward-token-or-list))
(cond
;; Colon
((json-par-token-colon-p token)
(if (or colon-token value-token)
(progn
(goto-char (json-par-token-start token))
(setq done t))
(setq colon-token token)))
;; Object key
((json-par--object-key-p token)
(if (or key-token colon-token value-token)
(progn
(goto-char (json-par-token-start token))
(setq done t))
(setq key-token token)))
;; Value
((or (json-par-token-atom-p token)
(json-par-token-matching-brackets-p token))
(if value-token
(progn
(goto-char (json-par-token-start token))
(setq done t))
(setq value-token token)))))
(json-par--backward-spaces)
(setq end-of-member (point))
(puthash :start-of-member start-of-member result)
(puthash :end-of-member end-of-member result)
(puthash :key-token key-token result)
(puthash :colon-token colon-token result)
(puthash :value-token value-token result)
result)))
(defun json-par--parse-member-backward ()
"Parse the current member.
Assuming the point is at the end of the member.
Return a hash table with the following members:
- :start-of-member, the start position of the member
- :end-of-member, the end position of the member
- :key-token, the key token of a key-value pair, if any
- :colon-token, the colon token of a key-value pair, if any
- :value-token, the value token of member, if any"
(save-excursion
(json-par--backward-spaces)
(let ((done nil)
(result (make-hash-table :size 5))
start-of-member
(end-of-member (point))
key-token
colon-token
value-token
token
(json-par--already-out-of-comment t)
(json-par--already-out-of-atom t))
(while (progn
(json-par--backward-spaces)
(and (not (memq (char-before) '(nil ?\, ?\[ ?\( ?{)))
(not done)))
(setq token (json-par-backward-token-or-list))
(cond
;; Colon
((json-par-token-colon-p token)
(if (or colon-token key-token)
(progn
(goto-char (json-par-token-end token))
(setq done t))
(setq colon-token token)))
;; Object key
((json-par--object-key-p token)
(if key-token
(progn
(goto-char (json-par-token-end token))
(setq done t))
(setq key-token token)))
;; Value
((or (json-par-token-atom-p token)
(json-par-token-matching-brackets-p token))
(if (or value-token colon-token key-token)
(progn
(goto-char (json-par-token-end token))
(setq done t))
(setq value-token token)))))
(json-par--forward-spaces)
(setq start-of-member (point))
(puthash :start-of-member start-of-member result)
(puthash :end-of-member end-of-member result)
(puthash :key-token key-token result)
(puthash :colon-token colon-token result)
(puthash :value-token value-token result)
result)))
(defun json-par-end-of-member (&optional push-mark)
"Move the point to the end of the current member, not including a comma.
If PUSH-MARK is non-nil or called interactively, the function is not called
repeatedly, and the region is not active, push a mark first."
(interactive
(list
(not (eq last-command 'json-par-end-of-member))))
(when (and push-mark (not (region-active-p)))
(push-mark))
(json-par--out-comment)
(json-par--out-atom)
(goto-char (gethash :end-of-member (json-par--parse-member-forward))))
(defun json-par-beginning-of-member (&optional push-mark)
"Move the point to the start of the current member.
If PUSH-MARK is non-nil or called interactively, the function is not called
repeatedly, and the region is not active, push a mark first."
(interactive
(list
(not (eq last-command 'json-par-beginning-of-member))))
(when (and push-mark (not (region-active-p)))
(push-mark))
(json-par--out-comment)
(json-par--out-atom)
(goto-char (gethash :start-of-member (json-par--parse-member-backward))))
(defun json-par-beginning-of-object-value (&optional push-mark parsed)
"Move the point to the start of the object value of the current member.
If the point is not in a object, go to the beginning of the member.
If PUSH-MARK is non-nil or called interactively, the function is not called
repeatedly, and the region is not active, push a mark first.
If PARSED is given, it is used instead of calling
`json-par--parse-member-forward'."
(interactive
(list
(not (eq last-command 'json-par-beginning-of-object-value))))
(when (and push-mark (not (region-active-p)))
(push-mark))
(unless parsed
(json-par-beginning-of-member)
(setq parsed (json-par--parse-member-forward)))
(cond
((gethash :value-token parsed)
(goto-char (json-par-token-start (gethash :value-token parsed))))
((gethash :colon-token parsed)
(goto-char (json-par-token-end (gethash :colon-token parsed)))
(json-par--forward-spaces)
(when (memq (char-after) '(?\] ?\) ?}))
(goto-char (json-par-token-end (gethash :colon-token parsed)))
(skip-chars-forward "\s\t")
(when (memq (char-after) '(?\] ?\) ?}))
(goto-char (json-par-token-end (gethash :colon-token parsed)))
(when (memq (char-after) '(?\s ?\t))
(forward-char)))))
((gethash :key-token parsed)
(goto-char (json-par-token-end (gethash :key-token parsed)))
(json-par--forward-spaces)
(when (memq (char-after) '(?\] ?\) ?}))
(goto-char (json-par-token-end (gethash :key-token parsed)))
(skip-chars-forward "\s\t")
(when (memq (char-after) '(?\] ?\) ?}))
(goto-char (json-par-token-end (gethash :key-token parsed)))
(when (memq (char-after) '(?\s ?\t))
(forward-char)))))
(t
(goto-char (gethash :end-of-member parsed)))))
(defun json-par-beginning-of-list (&optional push-mark)
"Move the point before the first member of the current array/object.
If PUSH-MARK is non-nil or called interactively, the function is not called
repeatedly, and the region is not active, push a mark first."
(interactive
(list
(not (eq last-command 'json-par-beginning-of-list))))
(json-par-up-backward 1 push-mark)
(json-par-down nil 'member))
(defun json-par-end-of-list (&optional push-mark)
"Move the point after last the member of the current array/object.
If PUSH-MARK is non-nil or called interactively, the function is not called
repeatedly, and the region is not active, push a mark first."
(interactive
(list
(not (eq last-command 'json-par-end-of-list))))
(json-par-up-forward 1 push-mark)
(json-par-down nil 'member))
(defun json-par--find-member (p)
"Find a member satisfying a predicate P.
Move the point to the start of the first member, call P, and if it return nil,
move to the next member and call P until P returns non-nil.
If P returns non-nil, keep the point at the beginning of the member and return
the value returned from P.
Otherwise, move the point to the original position and return nil.
P is called with the index of the member, starting from zero."
(let ((point-marker (point-marker))
(parent-token (json-par--parent-token))
(found nil))
(goto-char (json-par-token-end parent-token))
(json-par--forward-spaces)
(setq found (json-par--find-member-forward p))
(unless found
(goto-char point-marker))
(json-par--free-marker point-marker)
found))
(defun json-par--find-member-forward (p &optional max-cousin-depth)
"Find a member satisfying a predicate P after the point.
Move the point to the start of the current member, call P, and if it return nil,
move to the next member and call P until P returns non-nil or all members are
inspected.
If P returns non-nil for some member, keep the point at the beginning of the
member and return the value returned from P.
Otherwise, move the point to the original position and return nil.
P is called with the index of the member relative the starting member, starting
from zero.
If MAX-COUSIN-DEPTH is non-nil, also search cousin members, upto
MAX-COUSIN-DEPTHth cousin. If MAX-COUSIN-DEPTH is t, it is infinite."
(unless max-cousin-depth
(setq max-cousin-depth 0))
(when (eq max-cousin-depth t)
(setq max-cousin-depth nil))
(json-par--find-member-from-here
p
(lambda ()
(json-par--goto-beginning-of-next-member-or-cousin nil max-cousin-depth))))
(defun json-par--find-member-backward (p &optional max-cousin-depth)
"Find a member satisfying a predicate P before the point.
Move the point to the start of the current member, call P, and if it return nil,
move to the previous member and call P until P returns non-nil or all members
are inspected.
If P returns non-nil for some member, keep the point at the beginning of the
member and return the value returned from P.
Otherwise, move the point to the original position and return nil.
P is called with the index of the member relative the starting member, starting
from zero and increasing.
If MAX-COUSIN-DEPTH is non-nil, also search cousin members, upto
MAX-COUSIN-DEPTHth cousin. If MAX-COUSIN-DEPTH is t, it is infinite."
(unless max-cousin-depth
(setq max-cousin-depth 0))
(when (eq max-cousin-depth t)
(setq max-cousin-depth nil))
(json-par--find-member-from-here
p
(lambda ()
(prog1 (json-par--goto-end-of-previous-member-or-cousin
nil
max-cousin-depth)
(json-par-beginning-of-member)))))
(defun json-par--goto-beginning-of-next-member-or-cousin
(&optional include-empty max-depth)
"Move the point to the beginning of the next member.
If the point is on the last member, move to the first member of the
sibling/cousin array/object.
If INCLUDE-EMPTY is non-nil, stop inside an empty brackets with the same depth.
If MAX-DEPTH is non-nil, search only up to MAX-DEPTHth cousin.
If a member is found, return t. Return nil otherwise."
(or (json-par--goto-beginning-of-next-member)
(let ((depth 1)
(token (json-par-forward-token)))
(while (and
(not (zerop depth))
(or (null max-depth) (<= depth max-depth))
(not (json-par-token-outside-of-buffer-p token)))
(setq token (json-par-forward-token))
(cond
((json-par-token-close-bracket-p token)
(setq depth (1+ depth)))
((json-par-token-open-bracket-p token)
(setq depth (1- depth))))
(json-par--forward-spaces)
(when (and (not include-empty)
(memq (char-after) '(?\] ?\) ?})))
(setq token (json-par-forward-token))
(setq depth (1+ depth))))
(zerop depth))))
(defun json-par--goto-end-of-previous-member-or-cousin
(&optional include-empty max-depth)
"Move the point to the end of the previous member.
If the point is on the first member, move to the last member of the
sibling/cousin array/object.
If INCLUDE-EMPTY is non-nil, stop inside an empty brackets with the same depth.
If MAX-DEPTH is non-nil, search only up to MAX-DEPTHth cousin.
If a member is found, return t. Return nil otherwise."
(or (json-par--goto-end-of-previous-member)
(let ((depth 1)
(token (json-par-backward-token)))
(while (and
(not (zerop depth))
(or (null max-depth) (<= depth max-depth))
(not (json-par-token-outside-of-buffer-p token)))
(setq token (json-par-backward-token))
(cond
((json-par-token-open-bracket-p token)
(setq depth (1+ depth)))
((json-par-token-close-bracket-p token)
(setq depth (1- depth))))
(json-par--backward-spaces)
(when (and (not include-empty)
(memq (char-before) '(?\[ ?\( ?{)))
(setq token (json-par-backward-token))
(setq depth (1+ depth))))
(zerop depth))))
(defun json-par--find-member-from-here (p move-next)
"Find a member satisfying a predicate P from the point.
Move the point to the start of the current member, call P, and if it return nil,
call MOVE-NEXT, and call P until P returns non-nil.
If P returns non-nil, keep the point at the beginning of the member and return
the value returned from P.
Otherwise, move the point to the original position and return nil.
If MOVE-NEXT return nil, it is considered as the end of the list.
P is called with the index of the member relative the starting member, starting
from zero and increasing."
(json-par-beginning-of-member)
(let ((point-marker (point-marker))
(done nil)
(found nil)
(i 0)
old-position
(json-par--already-out-of-comment t)
(json-par--already-out-of-atom t))
(while (not done)
(setq old-position (point-marker))
(setq found (funcall p i))
(setq done found)
(setq old-position (json-par--free-marker old-position))
(unless done
(goto-char old-position)
(if (funcall move-next)
(setq i (1+ i))
(setq done t))))
(unless found
(goto-char point-marker))
(json-par--free-marker point-marker)
found))
(defun json-par--position-in-member ()
"Return the position in a member.
Return one of:
- empty-member: the member is empty
- before-member: before a member
- after-member: after a member (before comma)
- before-value: between a colon and an object value
- after-key: between an object key and a colon"
(save-excursion
(json-par--out-comment)
(json-par--out-atom)
(let* ((beginning-position
(save-excursion
(json-par-beginning-of-member)
(point)))
(end-position
(save-excursion
(goto-char beginning-position)
(json-par-end-of-member)
(point)))
(point-before-spaces
(save-excursion
(json-par--backward-spaces)
(point))))
(cond
((= (save-excursion
(goto-char beginning-position)
(json-par--backward-spaces)
(point))
(save-excursion
(goto-char end-position)
(json-par--backward-spaces)
(point)))
'empty-member)
((= point-before-spaces
(save-excursion
(goto-char beginning-position)
(json-par--backward-spaces)
(point)))
'before-member)
((= point-before-spaces
(save-excursion
(goto-char end-position)
(json-par--backward-spaces)
(point)))
'after-member)
((= point-before-spaces
(save-excursion
(json-par-beginning-of-object-value)
(json-par--backward-spaces)
(point)))
'before-value)
(t 'after-key)))))
(defun json-par--goto-position-in-member (position-in-member)
"Go to POSITION-IN-MEMBER in the current member.
POSITION-IN-MEMBER is a symbol returned from `json-par--position-in-member'."
(cond
((eq position-in-member 'empty-member)
nil)
((eq position-in-member 'before-member)
(json-par-beginning-of-member))
((eq position-in-member 'after-member)
(json-par-end-of-member))
((eq position-in-member 'before-value)
(json-par-beginning-of-object-value))
((eq position-in-member 'after-key)
(json-par-beginning-of-object-value)
(let ((previous-token (save-excursion (json-par-backward-token))))
(when (json-par-token-colon-p previous-token)
(goto-char (json-par-token-start previous-token))))
(json-par--backward-spaces))))
(defun json-par--goto-position-in-parsed-member (position-in-member parsed)
"Go to POSITION-IN-MEMBER in the PARSED member.
POSITION-IN-MEMBER is a symbol returned from `json-par--position-in-member'.
PARSED is a parsed member returned from `json-par--parse-member-forward' or
`json-par--parse-member-backward'."
(cond
((eq position-in-member 'empty-member)
nil)
((eq position-in-member 'before-member)
(goto-char (gethash :start-of-member parsed)))
((eq position-in-member 'after-member)
(goto-char (gethash :end-of-member parsed)))
((eq position-in-member 'before-value)
(json-par-beginning-of-object-value nil parsed))
((eq position-in-member 'after-key)
(json-par-beginning-of-object-value nil parsed)
(let ((previous-token (save-excursion (json-par-backward-token))))
(when (json-par-token-colon-p previous-token)
(goto-char (json-par-token-start previous-token))))
(json-par--backward-spaces))))
(defun json-par-goto-key (key &optional push-mark)
"Move the point to the beginning of the member with KEY.
If PUSH-MARK is non-nil and the region is not active, push a mark first.
Return non-nil if KEY found. Otherwise, keep the original position and return
nil."
(interactive "MGoto key: \np")
(when (and push-mark (not (region-active-p)))
(push-mark))
(let ((pos (point))
found)
(json-par--out-comment)
(json-par--out-atom)
(setq found (json-par--find-member
(lambda (_)
(let ((key-token (save-excursion (json-par-forward-token))))
(and (json-par-token-string-p key-token)
(equal (json-par--read-token key-token) key))))))
(unless found
(goto-char pos)
(when (called-interactively-p 'interactive)
(message "Key not found")))
found))
(defun json-par-goto-index (index &optional push-mark)
"Move the point to the beginning of the member at INDEX.
If PUSH-MARK is non-nil and the region is not active, push a mark first.
Return non-nil if INDEX found. Otherwise, keep the original position and return
nil."
(interactive "nGoto index: \np")
(when (and push-mark (not (region-active-p)))
(push-mark))
(let ((pos (point))
found)
(json-par--out-comment)
(json-par--out-atom)
(setq found (json-par--find-member (lambda (i) (= i index))))
(unless found
(goto-char pos)
(when (called-interactively-p 'interactive)
(message "Index out of bound")))
found))
(defun json-par-goto-path (path &optional push-mark)
"Move the point to the beginning of the member at PATH.
PATH is a list of following elements:
- String: key of member in object
- Number: index of member in object/array
- Symbol `*': first member matching rest of the path
Example:
When the point is at (*1) below, (json-par-goto-path \\='(\"a\" * \"b\" 1))
move the point to (*2).
{
(*1) \"a\": [
{ \"a\": 1 },
{ \"b\": [ 2, (*2) 3, 4] },
{ \"c\": 3 }
]
}
If PUSH-MARK is non-nil and the region is not active, push a mark first.
Return non-nil if PATH found. Otherwise, keep the original position and return
nil."
(when (and push-mark (not (region-active-p)))
(push-mark))
(let ((pos (point))
(found t)
step)
(while (and path found)
(setq step (car path))
(setq path (cdr path))
(cond
;; Key
((stringp step)
(setq found (json-par-goto-key step)))
;; Index
((numberp step)
(setq found (json-par-goto-index step)))
;; Any
((eq step '*)
(setq found (json-par--find-member
(lambda (_)
(json-par-beginning-of-object-value)
(if (memq (char-after) '(?\[ ?{))
(progn
(forward-char)
(json-par-goto-path path))
nil))))
(setq path nil))
;; Invalid step
(t
(error "Invalid step: %S" step)))
(when path
(json-par-beginning-of-object-value)
(if (memq (char-after) '(?\[ ?{))
(forward-char)
(setq found nil))))
(unless found
(goto-char pos)
(when (called-interactively-p 'interactive)
(message "Not found")))
found))
(defun json-par--multiple-members-on-same-line-around-point-p (count)
"Return non-nil if lines around the point has multiple members.
Otherwise, return nil.
Check COUNT members around the point.
Examples:
// Has multiple members on a line:
[
1, 2, 3,|
4, 5, 6
]
// Has single member for each lines:
[
1,
2,
3,|
4,
5,
6
]
This function affects whether a line break is inserted or not when inserting a
comma or a value."
(or (json-par--multiple-members-on-same-line-before-point-p count)
(json-par--multiple-members-on-same-line-after-point-p count)))
(defun json-par--multiple-members-on-same-line-before-point-p (count)
"Return non-nil if lines before the point has multiple members.
Otherwise, return nil.
Check COUNT members around the point.
Examples:
// Has multiple members on a line:
[
1, 2, 3,|
4, 5, 6
]
// Has single member for each lines:
[
1,
2,
3,|
4,
5,
6
]
This function affects whether a line break is inserted or not when inserting a
comma or a value."
(let ((positions (list)))
(save-excursion
(json-par-beginning-of-member)
(push (point) positions)
(dotimes (_ count)
(when (zerop (json-par-backward-member))
(json-par-end-of-member)
(json-par-beginning-of-member)
(push (point) positions))))
(not (cl-every #'json-par--beginning-of-line-or-list-p positions))))
(defun json-par--multiple-members-on-same-line-after-point-p (count)
"Return non-nil if lines after the point has multiple members.
Otherwise, return nil.
Check COUNT members around the point.
Examples:
// Has multiple members on a line:
[
1, 2, 3,|
4, 5, 6
]
// Has single member for each lines:
[
1,
2,
3,|
4,
5,
6
]
This function affects whether a line break is inserted or not when inserting a
comma or a value."
(let ((positions (list)))
(save-excursion
(json-par--forward-spaces)
(when (eq (char-after) ?,)
(forward-char))
(json-par-end-of-member)
(json-par-beginning-of-member)
(push (point) positions)
(dotimes (_ count)
(when (zerop (json-par-forward-member))
(json-par-end-of-member)
(json-par-beginning-of-member)
(push (point) positions))))
(not (cl-every #'json-par--beginning-of-line-or-list-p positions))))
(defun json-par--multiple-members-on-same-line-p ()
"Return non-nil if containing object/array has multiple members on a line.
Return nil otherwise.
Examples:
// Has multiple members on a line:
[
1, 2, 3,
4, 5, 6
]
// Has single member for each lines:
[
1,
2,
3,
4,
5,
6
]
This function affects `json-par--join-line-backward' and
`json-par--join-line-forward'."
(let ((result nil)
(end-of-list nil))
(save-excursion
(json-par-up-backward)
(forward-char)
(while (and (not result)
(not end-of-list)
(progn
(json-par--forward-spaces)
(not (memq (char-after) '(nil ?\] ?\) ?})))))
(unless (json-par--beginning-of-line-or-list-p)
(setq result t))
(setq end-of-list (not (zerop (json-par-forward-member))))))
result))
(defun json-par--all-members-on-same-line-after-point-p ()
"Return non-nil if members after the point are on a line.
Examples (`|' is the point):
// Return non-nil
[
1,
|2, 3, 4, 5, 6
]
// Return nil
[
1,
|2, 3,
4, 5, 6
]
This function affects `json-par--post-newline'."
(let ((result t)
(end-of-list nil))
(save-excursion
(setq end-of-list (not (zerop (json-par-forward-member))))
(while (and result
(not end-of-list)
(progn
(json-par--forward-spaces)
(not (memq (char-after) '(nil ?\] ?\) ?})))))
(when (json-par--beginning-of-line-or-list-p)
(setq result nil))
(setq end-of-list (not (zerop (json-par-forward-member))))))
result))
(defun json-par--beginning-of-line-or-list-p (&optional pos)
"Return non-nil if POS is the beginning of a line or a list except spaces.
Return nil otherwise."
(unless pos
(setq pos (point)))
(save-excursion
(goto-char pos)
(json-par--backward-spaces t)
(or (bolp)
(memq (char-before) '(?\[ ?\( ?{)))))
(defun json-par--end-of-line-or-list-p (&optional pos)
"Return non-nil if POS is the end of a line or a list except spaces.
Return nil otherwise."
(unless pos
(setq pos (point)))
(save-excursion
(goto-char pos)
(json-par--forward-spaces t)
(or (eolp)
(memq (char-after) '(?\] ?\) ?}))
(looking-at "//"))))
(defun json-par--current-member-index ()
"Return the position of the current member in the object/array.
The index starts from zero."
(let ((index 0))
(save-excursion
(while (progn
(json-par-beginning-of-member)
(json-par--backward-spaces)
(not (memq (char-before) '(nil ?\[ ?\( ?{))))
(json-par-backward-member)
(setq index (1+ index))))
index))
(defun json-par--before-first-member-p (&optional previous-token)
"Return non-nil if the point is before the first member of an object/array.