-
Notifications
You must be signed in to change notification settings - Fork 1
/
json-par-split-join.el
682 lines (607 loc) · 24.2 KB
/
json-par-split-join.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
;;; json-par-split-join.el --- Splitting/joining 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 splitting or joining strings, object, or arrays.
;;; Code:
(require 'json-par-lexer)
(require 'json-par-motion)
(require 'json-par-insert)
(require 'json-par-indent)
(defvar json-par--fixup-adviced-functions nil
"Functions to be adviced with `json-par--fixup-advice'.")
(defun json-par-split ()
"Split a string, object, or array at the point.
A comma and other required tokens are inserted.
- Splitting a string:
If the point is on the middle of an escape sequence, split at the end of the
sequence.
The string can be a value or a key.
Examples (`|' is the point):
{
\"aaa\": \"b|bb\"
}
↓
{
\"aaa\": \"b\",
\"\": \"|bb\"
}
{
\"a|aa\": \"bbb\"
}
↓
{
\"a\": ,
\"|aa\": \"bbb\"
}
- Splitting an object/array:
It is assumed the object/array is a member of an array.
If the point is at the middle of a member, go to the beginning of the member
before splitting."
(interactive)
(let ((current-atom (json-par--current-atom))
(string-like-beginning-position
(json-par--string-like-beginning-position))
(parent-token (json-par--parent-token)))
(cond
((and (json-par-token-inside-p current-atom)
(json-par-token-string-p current-atom))
(json-par--split-string current-atom))
(string-like-beginning-position
(json-par--split-comment string-like-beginning-position))
((or (json-par-token-open-square-bracket-p parent-token)
(json-par-token-open-curly-bracket-p parent-token))
(json-par--split-array-or-object parent-token)))))
(push #'json-par-split json-par--fixup-adviced-functions)
(defun json-par--split-string (string-token)
"Split a string STRING-TOKEN at the point.
See `json-par-split' for details."
(let ((escape-sequence (json-par--escape-sequence-under-point)))
(when escape-sequence
(goto-char (cdr escape-sequence))))
(let* ((is-key (json-par--object-key-p string-token))
(end-position (save-excursion
(json-par-end-of-member)
(point)))
(end-marker
(let ((marker (copy-marker end-position)))
(set-marker-insertion-type marker t)
marker))
;; String that contains a comma, spaces, and a empty key with a colon
;; (if the string is a object value) to be inserted between the two
;; split strings.
;; We build this before splitting the string to get better line break
;; behavior.
(comma-and-spaces
(save-excursion
(goto-char end-position)
(json-par-insert-comma)
(unless is-key
(json-par--insert-value "0" t)
(delete-char -1))
;; `json-par-insert-comma' may insert extra spaces after the point.
;; Delete it.
(delete-region (point) (json-par--free-marker end-marker))
(delete-and-extract-region end-position (point)))))
(insert-char ?\")
(when is-key
(insert ": "))
(insert comma-and-spaces)
(save-excursion
(insert-char ?\"))))
(defun json-par--escape-sequence-under-point ()
"Return region of an escape sequence under the point.
Return a cons of the start and end positions.
If the point is not on an escape sequence, return nil."
(cond
((and (json-par--escaped-p) (not (eq (char-after) ?u)))
(cons (1- (point))
(1+ (point))))
((save-excursion
(backward-char 5)
(looking-at ".\\{,4\\}\\(\\\\u[0-9a-fA-F]\\{,4\\}\\)"))
(cons (match-beginning 1)
(match-end 1)))))
(defun json-par--split-comment (beginning-position)
"Split a comment at BEGINNING-POSITION."
(let* ((pos (point))
(end-position
(save-excursion
(goto-char beginning-position)
(forward-comment 1)
(point)))
(single-line
(save-excursion
(goto-char beginning-position)
(looking-at "//")))
(prefix-and-suffix
(if single-line
(json-par--prefix-and-suffix-of-single-line-comment
beginning-position
end-position
pos)
(json-par--prefix-and-suffix-of-multiline-comment
beginning-position
end-position
pos)))
(prefix (car prefix-and-suffix))
(suffix (cdr prefix-and-suffix))
before-prefix-position)
(when (= (point) (1+ beginning-position))
(forward-char))
(when (and (not single-line)
(= (point) (1- end-position)))
(backward-char))
(insert suffix)
(newline)
(setq before-prefix-position (point))
(insert prefix)
(save-excursion
(goto-char before-prefix-position)
(json-par-indent-line))))
(defun json-par--prefix-and-suffix-of-multiline-comment
(beginning-position end-position &optional point)
"Return the prefix and suffix string of a multiline comment.
The comment starts at BEGINNING-POSITION and end at END-POSITION.
Return a cons of strings.
A prefix is the first slash, following asterisks, and spaces including line
breaks. A suffix is the last slash, preceding asterisks, and spaces including
line breaks.
If the comment contains only spaces, those spaces are part of the prefix rather
than the suffix. If the comment contains only asterisks, those asterisks are
part of the prefix except the last asterisk.
If POINT is given, neither the prefix nor the suffix go beyond POINT."
(let* ((prefix-end-position
(save-excursion
(goto-char beginning-position)
(looking-at "/[*]+[\s\t\n*]*")
(max
(+ beginning-position 2)
(min
(match-end 0)
(- end-position 2)
(or point (point-max))))))
(prefix (buffer-substring beginning-position prefix-end-position))
(suffix (buffer-substring
(save-excursion
(goto-char end-position)
(backward-char)
(skip-chars-backward "*\s\t\n")
(max
(point)
prefix-end-position
(or point (point-min))))
end-position)))
(cons prefix suffix)))
(defun json-par--prefix-and-suffix-of-single-line-comment
(beginning-position end-position &optional point)
"Return the prefix and suffix string of a single line comment.
The comment starts at BEGINNING-POSITION and end at END-POSITION.
Return a cons of strings.
A prefix is the starting slashes and following spaces. A suffix is always an
empty string.
If POINT is given, the prefix doesn't go beyond POINT."
(ignore end-position)
(let ((prefix
(buffer-substring
beginning-position
(save-excursion
(goto-char beginning-position)
(looking-at "/+[\s\t]*")
(max (+ beginning-position 2)
(min (match-end 0) (or point (point-max)))))))
(suffix ""))
(cons prefix suffix)))
(defun json-par--split-array-or-object (parent-token)
"Split an array/object at point.
The array/object start at PARENT-TOKEN.
See `json-par-split' for details."
(let* ((position-in-member (json-par--position-in-member))
(parent-is-array (json-par-token-open-square-bracket-p parent-token))
(open-bracket (if parent-is-array ?\[ ?\{))
(close-bracket (if parent-is-array ?\] ?\}))
(insert-newline-around-bracket
(save-excursion
(goto-char (json-par-token-end parent-token))
(<= (line-end-position)
(progn
(skip-chars-forward "\s\t\n")
(point)))))
(whole-list-token
(save-excursion
(goto-char (json-par-token-start parent-token))
(json-par-forward-token-or-list)))
(end-position
(if (json-par-token-matching-brackets-p whole-list-token)
(json-par-token-end whole-list-token)
(point-max)))
(end-marker
(let ((marker (copy-marker end-position)))
(set-marker-insertion-type marker t)
marker))
;; String that contains a comma, spaces, and a empty key with a colon
;; (if the array/object is a object value) to be inserted between the
;; two split array/object.
;; We build this before splitting the array/object to get better line
;; break behavior.
(comma-and-spaces
(save-excursion
(goto-char end-position)
(json-par-insert-comma)
(json-par--insert-value "0" t)
(delete-char -1)
;; `json-par-insert-comma' may insert extra spaces after the point.
;; Delete it.
(delete-region (point) (json-par--free-marker end-marker))
(delete-and-extract-region end-position (point)))))
(json-par--out-atom)
(unless (or (eq position-in-member 'after-member)
(eq position-in-member 'before-member))
(json-par-beginning-of-member))
(save-excursion
(json-par--backward-spaces)
(when (eq (char-before) ?\,)
(delete-char -1)))
(save-excursion
(json-par--forward-spaces)
(when (eq (char-after) ?\,)
(delete-char 1)))
(insert-char close-bracket)
(save-excursion
(backward-char)
(if insert-newline-around-bracket
(if (save-excursion
(skip-chars-backward "\s\t")
(bolp))
(json-par-indent-line)
(newline-and-indent))
(unless (memq (char-before) '(?\s ?\t ?\n ?\[ nil))
(insert-char ?\s))))
(save-excursion
(insert-char open-bracket)
(if insert-newline-around-bracket
(unless (save-excursion
(skip-chars-forward "\s\t")
(eolp))
(newline-and-indent))
(unless (memq (char-after) '(?\s ?\t ?\n ?\] nil))
(insert-char ?\s))))
(insert comma-and-spaces)))
(defun json-par-join ()
"Join two strings or arrays/objects.
A comma between them, if any, is ignored.
When joining a string and other atoms (numbers or constants), other atoms are
converted to a string first.
When joining an array/object and an atom, the atom is appended/prepended to the
array/object.
When joining an array and an object, the object is appended/prepended to the
array.
If neither of tokens around the point is a string, array, nor object, do
nothing."
(interactive)
(json-par--out-comment)
(json-par--out-atom)
(let ((previous-token
(save-excursion (json-par-backward-token-or-list-or-comment)))
(next-token
(save-excursion (json-par-forward-token-or-list-or-comment))))
(unless (and (json-par-token-comment-p previous-token)
(json-par-token-comment-p next-token))
(setq previous-token (save-excursion (json-par-backward-token)))
(setq next-token (save-excursion (json-par-forward-token))))
(if (json-par-token-comma-p previous-token)
(save-excursion
(goto-char (json-par-token-start previous-token))
(setq previous-token (json-par-backward-token)))
(when (json-par-token-comma-p next-token)
(save-excursion
(goto-char (json-par-token-end next-token))
(setq next-token (json-par-forward-token)))))
(cond
;; Beginning of buffer/end of buffer
((or (json-par-token-outside-of-buffer-p previous-token)
(json-par-token-outside-of-buffer-p next-token))
nil)
;; Between comments
((and (json-par-token-comment-p previous-token)
(json-par-token-comment-p next-token))
(json-par--join-comments previous-token next-token))
;; Between string and atom
((or (and (json-par-token-string-p previous-token)
(json-par-token-atom-p next-token))
(and (json-par-token-atom-p previous-token)
(json-par-token-string-p next-token)))
(json-par--join-strings previous-token next-token))
;; Between array and value
((or (json-par-token-close-square-bracket-p previous-token)
(json-par-token-open-square-bracket-p next-token))
(json-par--join-arrays previous-token next-token))
;; Between objects
((or (json-par-token-close-curly-bracket-p previous-token)
(json-par-token-open-curly-bracket-p next-token))
(json-par--join-objects previous-token next-token))
;; Otherwise
(t nil))))
(push #'json-par-join json-par--fixup-adviced-functions)
(defun json-par--join-comments (previous-token next-token)
"Join comments PREVIOUS-TOKEN and NEXT-TOKEN."
(if (and (json-par-token-single-line-comment-p previous-token)
(json-par-token-single-line-comment-p next-token))
(progn
(goto-char (json-par-token-start next-token))
(skip-chars-forward "/")
(skip-chars-forward "\s\t")
(delete-region (1- (json-par-token-end previous-token)) (point)))
(let ((end-of-previous-token (json-par-token-end previous-token))
beginning-of-next-comment-body)
(when (json-par-token-single-line-comment-p next-token)
(json-par--convert-single-line-comment-to-multiline-comment
next-token
(json-par--prefix-and-suffix-of-multiline-comment
(json-par-token-start previous-token)
(json-par-token-end previous-token))))
(goto-char (json-par-token-start next-token))
(forward-char)
(skip-chars-forward "*\s\t\n")
(when (and (eq (char-before) ?*) (eq (char-after) ?/))
(backward-char))
(setq beginning-of-next-comment-body (point-marker))
(when (json-par-token-single-line-comment-p previous-token)
(setq end-of-previous-token
(json-par--convert-single-line-comment-to-multiline-comment
previous-token
(json-par--prefix-and-suffix-of-multiline-comment
(json-par-token-start next-token)
(json-par-token-end next-token)))))
(goto-char end-of-previous-token)
(backward-char)
(skip-chars-backward "*")
(skip-chars-backward "\s\t\n")
(when (and (eq (char-before) ?/) (eq (char-after) ?*))
(skip-chars-forward "*")
(backward-char))
(delete-region
(point)
(json-par--free-marker beginning-of-next-comment-body)))))
(defun json-par--convert-single-line-comment-to-multiline-comment
(token prefix-and-suffix)
"Convert a single line comment TOKEN with multiline comment.
PREFIX-AND-SUFFIX is a pair of prefix string and suffix string, which is
returned from `json-par--prefix-and-suffix-of-multiline-comment'.
Return the end position of the converted comment."
(save-excursion
(goto-char (json-par-token-start token))
(looking-at "/*")
(replace-match (car prefix-and-suffix))
(end-of-line)
(insert (cdr prefix-and-suffix))
(point)))
(defun json-par--join-strings (previous-token next-token)
"Join strings PREVIOUS-TOKEN and NEXT-TOKEN.
See `json-par-join' for details."
(setq next-token
(json-par--delete-comma-between-tokens previous-token next-token))
(setq previous-token
(json-par--move-comments-between-tokens previous-token next-token))
(goto-char (json-par-token-end previous-token))
(unless (json-par-token-string-p previous-token)
(json-par-stringify-region (json-par-token-start previous-token)
(json-par-token-end previous-token))
(setq previous-token (save-excursion (json-par-backward-token)))
(setq next-token (save-excursion (json-par-forward-token))))
(unless (json-par-token-string-p next-token)
(json-par-stringify-region (json-par-token-start next-token)
(json-par-token-end next-token))
(setq next-token (save-excursion (json-par-forward-token))))
(delete-region
(1- (json-par-token-end previous-token))
(1+ (json-par-token-start next-token))))
(defun json-par--join-arrays (previous-token next-token)
"Join arrays PREVIOUS-TOKEN and NEXT-TOKEN.
See `json-par-join' for details."
(setq next-token
(json-par--delete-comma-between-tokens previous-token next-token))
(goto-char (json-par-token-end previous-token))
(setq previous-token
(save-excursion
(goto-char (json-par-token-end previous-token))
(json-par-backward-token-or-list)))
(setq next-token
(save-excursion
(goto-char (json-par-token-start next-token))
(json-par-forward-token-or-list)))
(setq previous-token
(json-par--move-comments-between-tokens previous-token next-token))
(unless (json-par-token-matching-square-brackets-p previous-token)
(json-par--wrap-region-with-brackets
"["
"]"
(json-par-token-start previous-token)
(json-par-token-end previous-token)
'one-line)
(setq previous-token (save-excursion (json-par-backward-token-or-list)))
(setq next-token (save-excursion (json-par-forward-token-or-list))))
(unless (json-par-token-matching-square-brackets-p next-token)
(json-par--wrap-region-with-brackets
"["
"]"
(json-par-token-start next-token)
(json-par-token-end next-token)
'one-line)
(setq next-token (save-excursion (json-par-forward-token-or-list))))
(goto-char (json-par-token-start next-token))
(forward-char)
(skip-chars-forward "\s\t\n")
(unless (or (json-par-token-close-square-bracket-p
(save-excursion (json-par-forward-token)))
(json-par-token-open-square-bracket-p
(save-excursion
(goto-char (json-par-token-end previous-token))
(backward-char)
(json-par-backward-token))))
(json-par-insert-comma))
(delete-region
(save-excursion
(goto-char (json-par-token-end previous-token))
(backward-char)
(skip-chars-backward "\s\t\n")
(point))
(point))
(save-excursion
(goto-char (json-par-token-start previous-token))
(setq previous-token (json-par-forward-token-or-list))
(json-par--fixup-spaces-at-start-and-end-of-list
(json-par-token-start previous-token)
(json-par-token-end previous-token)))
(skip-chars-forward "\s\t\n")
(when (eq (char-after) ?\])
(skip-chars-backward "\s\t\n")))
(defun json-par--fixup-spaces-at-start-and-end-of-list (start end)
"Insert spaces and/or line breaks at start/end of an array/object.
The array/object starts at START and ends at END.
If spaces follows the open bracket but not precedes the close bracket, the
spaces are copied before the close bracket. Likewise, if spaces precedes the
close bracket but not follows the open bracket, the spaces are copied after the
open bracket."
(save-excursion
(let ((preceding-spaces
(save-excursion
(goto-char start)
(forward-char)
(buffer-substring
(point)
(progn
(skip-chars-forward "\s\t\n")
(point)))))
(following-spaces
(save-excursion
(goto-char end)
(backward-char)
(buffer-substring
(save-excursion
(skip-chars-backward "\s\t\n")
(point))
(point)))))
(when (string= following-spaces "")
(goto-char end)
(backward-char)
(insert preceding-spaces)
(when (save-excursion
(skip-chars-backward "\s\t")
(bolp))
(json-par-indent-line)))
(when (string= preceding-spaces "")
(goto-char start)
(forward-char)
(insert-before-markers following-spaces)
(when (save-excursion
(skip-chars-backward "\s\t")
(bolp))
(json-par-indent-line))))))
(defun json-par--join-objects (previous-token next-token)
"Join objects PREVIOUS-TOKEN and NEXT-TOKEN.
See `json-par-join' for details."
;; FIXME: almost identical to `json-par--join-arrays'.
(setq next-token
(json-par--delete-comma-between-tokens previous-token next-token))
(goto-char (json-par-token-end previous-token))
(setq previous-token
(save-excursion
(goto-char (json-par-token-end previous-token))
(json-par-backward-token-or-list)))
(setq next-token
(save-excursion
(goto-char (json-par-token-start next-token))
(json-par-forward-token-or-list)))
(setq previous-token
(json-par--move-comments-between-tokens previous-token next-token))
(unless (json-par-token-matching-curly-brackets-p previous-token)
(json-par--wrap-region-with-brackets
"{"
"}"
(json-par-token-start previous-token)
(json-par-token-end previous-token)
'one-line)
(json-par--prepend-empty-keys-to-values-if-missing
(json-par-token-start previous-token))
(setq previous-token (save-excursion (json-par-backward-token-or-list)))
(setq next-token (save-excursion (json-par-forward-token-or-list))))
(unless (json-par-token-matching-curly-brackets-p next-token)
(json-par--wrap-region-with-brackets
"{"
"}"
(json-par-token-start next-token)
(json-par-token-end next-token)
'one-line)
(json-par--prepend-empty-keys-to-values-if-missing
(json-par-token-start next-token))
(setq next-token (save-excursion (json-par-forward-token-or-list))))
(goto-char (json-par-token-start next-token))
(forward-char)
(skip-chars-forward "\s\t\n")
(unless (or (json-par-token-close-curly-bracket-p
(save-excursion (json-par-forward-token)))
(json-par-token-open-curly-bracket-p
(save-excursion
(goto-char (json-par-token-end previous-token))
(backward-char)
(json-par-backward-token))))
(json-par-insert-comma))
(delete-region
(save-excursion
(goto-char (json-par-token-end previous-token))
(backward-char)
(skip-chars-backward "\s\t\n")
(point))
(point))
(save-excursion
(goto-char (json-par-token-start previous-token))
(setq previous-token (json-par-forward-token-or-list))
(json-par--fixup-spaces-at-start-and-end-of-list
(json-par-token-start previous-token)
(json-par-token-end previous-token)))
(skip-chars-forward "\s\t\n")
(when (eq (char-after) ?})
(skip-chars-backward "\s\t\n")))
(defun json-par--delete-comma-between-tokens (previous-token next-token)
"Delete a comma between PREVIOUS-TOKEN and NEXT-TOKEN if any.
Return NEXT-TOKEN with new position."
(save-excursion
(goto-char (json-par-token-end previous-token))
(let ((token (json-par-forward-token)))
(if (and (json-par-token-comma-p token)
(not (eq (json-par-token-start token)
(json-par-token-start next-token))))
(progn
(delete-region
(json-par-token-start token)
(json-par-token-end token))
(json-par-forward-token))
next-token))))
(defun json-par--move-comments-between-tokens (previous-token next-token)
"Move comments between PREVIOUS-TOKEN and NEXT-TOKEN before PREVIOUS-TOKEN."
(save-excursion
(transpose-regions
(save-excursion
(skip-chars-forward "\s\t\n")
(point))
(json-par-token-start next-token)
(json-par-token-start previous-token)
(json-par-token-start previous-token))
(goto-char (json-par-token-start next-token))
(json-par-backward-token-or-list)))
(provide 'json-par-split-join)
;;; json-par-split-join.el ends here