-
Notifications
You must be signed in to change notification settings - Fork 9
/
yaml-pro.el
1448 lines (1321 loc) · 56.8 KB
/
yaml-pro.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
;;; yaml-pro.el --- Parser-aided YAML editing features -*- lexical-binding: t -*-
;; Author: Zachary Romero
;; Maintainer: Zachary Romero
;; Version: 0.3.1
;; Package-Requires: ((emacs "26.1") (yaml "0.5.1"))
;; Homepage: https://github.com/zkry/yaml-pro
;; Keywords: tools
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; yaml-pro contains a new mode which provides conveniences when
;; editing YAML. Running `yaml-pro-mode' switches the mode and the
;; following commands are available:
;; - `yaml-pro-move-subtree-up' and `yaml-pro-move-subtree-down'
;; - `yaml-pro-next-subtree' and `yaml-pro-prev-subtree',
;; - `yaml-pro-kill-subtree'
;; - `yaml-pro-up-level'
;; - `yaml-pro-fold-at-point' and `yaml-pro-unfold-at-point'.
;; - `yaml-pro-edit-scalar'
;; - `yaml-pro-jump' or if consult exists `yaml-pro-consult-jump'
;;; Code:
(require 'yaml)
(require 'yaml-pro-edit)
(require 'yaml-pro-format)
(require 'treesit nil t)
(require 'consult nil t)
(defgroup yaml-pro nil
"YAML editing tools."
:prefix "yaml-pro-"
:group 'convenience)
;;; yaml-pro tree sitter
(defcustom yaml-pro-ts-yank-subtrees t
"Non-nil means when yanking subtrees, adjust the level."
:group 'yaml-pro
:type 'boolean)
(defcustom yaml-pro-ts-path-element-separator ?.
"Character separating path elements when generating node labels."
:group 'yaml-pro
:type 'character)
(defun yaml-pro-ts--until-mapping (node)
"Recursively look up from NODE returning first `block_mapping_pair'."
(treesit-parent-until
node
(lambda (node)
(equal (treesit-node-type node) "block_mapping_pair"))))
(defun yaml-pro-ts--until-mapping-or-list (node)
"Recursively look up from NODE returning first `block_mapping_pair'."
(treesit-parent-until
node
(lambda (node)
(or (equal (treesit-node-type node) "block_mapping_pair")
(equal (treesit-node-type node) "block_sequence_item")))))
(defun yaml-pro-ts--until-list (node)
"Recursively look up from NODE returning first `block_sequence_item'."
(treesit-parent-until
node
(lambda (node)
(equal (treesit-node-type node) "block_sequence_item"))))
(defun yaml-pro-ts--until-mapping-or-list (node)
"Recursively look up from NODE returning first mapping or sequence item."
(treesit-parent-until
node
(lambda (node)
(or
(equal (treesit-node-type node) "block_mapping_pair")
(equal (treesit-node-type node) "block_sequence_item")))))
(defun yaml-pro-ts-kill-subtree ()
"Kill the entire subtree located at the current-point."
(interactive)
(let* ((at-node (treesit-node-at (point) 'yaml))
(tree-top (yaml-pro-ts--until-mapping-or-list at-node)))
(when tree-top
(kill-region (treesit-node-start tree-top)
(treesit-node-end tree-top)))))
(defun yaml-pro-copy-node-path-at-point ()
"Copy node path at point to clipboard."
(interactive)
(let* ((at-node (treesit-node-at (point) 'yaml))
(path (yaml-pro-ts--imenu-node-label at-node)))
(kill-new path)
(message "%s was copied to clipboard" (propertize path 'face '(bold default)))))
(defun yaml-pro-ts-up-level ()
"Move the point to the parent tree."
(interactive)
(let* ((at-node (treesit-node-at (point) 'yaml))
(tree-top (yaml-pro-ts--until-mapping-or-list at-node))
(parent-tree-top (and tree-top (yaml-pro-ts--until-mapping-or-list tree-top))))
(when parent-tree-top
(goto-char (treesit-node-start parent-tree-top)))))
(defun yaml-pro-ts-down-level ()
"Move the point down to first child of current item."
(interactive)
(let* ((at-node (treesit-node-at (point) 'yaml))
(tree-top (yaml-pro-ts--until-mapping-or-list at-node))
(nodes (seq-map #'cdr
(treesit-query-capture tree-top '((block_sequence_item) @match
(block_mapping_pair) @match))))
(children (seq-sort-by
#'treesit-node-start
#'<
(seq-filter
(lambda (node)
(treesit-node-eq tree-top (yaml-pro-ts--until-mapping-or-list node)))
nodes))))
(if (car children)
(goto-char (treesit-node-start (car children)))
(beep))))
(defun yaml-pro-ts-first-sibling ()
"Move the point to the first sibling."
(interactive)
(let* ((at-node (treesit-node-at (point) 'yaml))
(at-sibling (yaml-pro-ts--until-mapping-or-list at-node))
(parent (treesit-node-parent at-sibling))
(all-siblings (treesit-node-children parent))
(first-sibling (car all-siblings)))
(if first-sibling
(goto-char (treesit-node-start first-sibling))
(beep))))
(defun yaml-pro-ts-last-sibling ()
"Move the point to the last sibling."
(interactive)
(let* ((at-node (treesit-node-at (point) 'yaml))
(at-sibling (yaml-pro-ts--until-mapping-or-list at-node))
(parent (treesit-node-parent at-sibling))
(all-siblings (treesit-node-children parent))
(last-sibling (car (last all-siblings))))
(if last-sibling
(goto-char (treesit-node-start last-sibling))
(beep))))
(defun yaml-pro-ts-prev-mapping-node (tree-top type)
"Return nearest previous sibling node of TREE-TOP of type TYPE."
(interactive)
(setq tree-top (treesit-node-prev-sibling tree-top))
(while (and tree-top
(not (equal (treesit-node-type tree-top) type)))
(setq tree-top (treesit-node-prev-sibling tree-top)))
tree-top)
(defun yaml-pro-ts-next-mapping-node (tree-top type)
"Return nearest next sibling node of TREE-TOP of type TYPE."
(interactive)
(setq tree-top (treesit-node-next-sibling tree-top))
(while (and tree-top
(not (equal (treesit-node-type tree-top) type)))
(setq tree-top (treesit-node-next-sibling tree-top)))
tree-top)
(defun yaml-pro-ts-prev-subtree ()
"Move the point to the previous subtree or list."
(interactive)
(let* ((at-node (treesit-node-at (point) 'yaml))
(prev-node
(if (and (equal (treesit-node-type at-node) "-")
(equal (treesit-node-type (treesit-node-parent at-node))
"block_sequence_item"))
(yaml-pro-ts-prev-mapping-node (yaml-pro-ts--until-list at-node) "block_sequence_item")
(yaml-pro-ts-prev-mapping-node (yaml-pro-ts--until-mapping at-node) "block_mapping_pair"))))
(if prev-node
(goto-char (treesit-node-start prev-node))
(ding))))
(defun yaml-pro-ts-next-subtree ()
"Move the point to the next subtree or list."
(interactive)
(let* ((at-node (treesit-node-at (point) 'yaml))
(next-node
(if (and (equal (treesit-node-type at-node) "-")
(equal (treesit-node-type (treesit-node-parent at-node))
"block_sequence_item"))
(yaml-pro-ts-next-mapping-node (yaml-pro-ts--until-list at-node) "block_sequence_item")
(yaml-pro-ts-next-mapping-node (yaml-pro-ts--until-mapping at-node) "block_mapping_pair"))))
(if next-node
(goto-char (treesit-node-start next-node))
(ding))))
(defun yaml-pro-ts-move-subtree (dir)
"Get the current and DIR node and swap the contents of the two."
(interactive)
(let* ((at-node (treesit-node-at (point) 'yaml))
(tree-top (yaml-pro-ts--until-mapping-or-list at-node))
(tree-top-type (treesit-node-type tree-top))
(sibling-node (if (eq dir 'down)
(yaml-pro-ts-next-mapping-node tree-top tree-top-type)
(yaml-pro-ts-prev-mapping-node tree-top tree-top-type)))
(at-start-marker (make-marker))
(at-end-marker (make-marker))
(sibling-start-marker (make-marker))
(sibling-end-marker (make-marker))
(sibling-end-trailing-newline nil))
(when (and tree-top sibling-node)
(set-marker at-start-marker (treesit-node-start tree-top))
(set-marker at-end-marker (treesit-node-end tree-top))
(set-marker sibling-start-marker (treesit-node-start sibling-node))
(set-marker sibling-end-marker (treesit-node-end sibling-node))
;; Check if there's a trailing newline after the last node. We
;; do this because we don't want to insert a trailing newline if
;; the user doesn't have a trailing newline.
(save-excursion
(goto-char sibling-end-marker)
(setq sibling-end-trailing-newline (equal (char-before) ?\n)))
(let* ((at-tree-text (buffer-substring-no-properties
at-start-marker at-end-marker))
(sibling-tree-text (buffer-substring-no-properties
sibling-start-marker sibling-end-marker)))
(delete-region at-start-marker at-end-marker)
(delete-region sibling-start-marker sibling-end-marker)
(goto-char sibling-start-marker)
;; Empty newlines that are after the last node are moved along
;; with the last node, so after text has been deleted from
;; at-node and sibling-node, there's no newline for
;; at-node. We only do this when the last item has a trailing
;; newline.
(when (and (eobp) sibling-end-trailing-newline)
(save-excursion
(insert "\n")))
(insert at-tree-text)
(goto-char at-start-marker)
;; We delete the last newline (if there's one) because when we
;; move to `at-start-marker', there's already an empty line.
(insert (replace-regexp-in-string "\n\\'" "" sibling-tree-text))
(goto-char sibling-start-marker)))))
(defun yaml-pro-ts-move-subtree-up ()
"Swap the current tree with that of the next sibling's."
(interactive)
(yaml-pro-ts-move-subtree 'up))
(defun yaml-pro-ts-move-subtree-down ()
"Swap the current tree with that of the previous sibling's."
(interactive)
(yaml-pro-ts-move-subtree 'down))
(defun yaml-pro-ts-meta-return ()
"Insert new list item after current item."
(interactive)
(let* ((at-node (treesit-node-at (point) 'yaml))
(list-item-top (yaml-pro-ts--until-list at-node))
(list-item-end-in-newline nil))
(when list-item-top
(let* ((indentation (save-excursion
(goto-char (treesit-node-start list-item-top))
(current-column))))
(goto-char (treesit-node-end list-item-top))
;; If the file didn't have a newline at the end of the item,
;; then we are looking at ^$
(setq list-item-end-in-newline (looking-at "^$"))
(cond
;; After visiting the end of the top item, we check if we are
;; in a newline or not. If we are not, we start a new line.
((not (looking-at "^$"))
(insert "\n" (make-string indentation ?\s) "- "))
;; When there are empty lines above the point after going to
;; the end of the element. When in the top last element of a
;; list, treesit-node-end considers empty lines as part of
;; the list element.
((save-excursion
(forward-line -1)
(looking-at "^$"))
(while (save-excursion
(forward-line -1)
(looking-at "^$"))
(forward-line -1))
(insert (make-string indentation ?\s) "- "))
;; If none of the cases are true, we are at the beginning of
;; a line just one line below the previous item, so we just
;; start another item.
(t
(insert (make-string indentation ?\s) "- ")))
;; If the the user inserted a newline at the end of the item,
;; we insert a newline. If the user didn't, we don't insert a
;; newline. We do this to conform with the POSIX definition of
;; a line and at the same time to act according the user's
;; preferences.
(when list-item-end-in-newline
(save-excursion
(insert "\n")))))))
(defun yaml-pro-convolute-tree ()
"Swap the keys of the parent of the item at point and the parent's parent."
(interactive)
(let* ((at-node (yaml-pro-ts--until-mapping (treesit-node-at (point) 'yaml)))
(parent-tree (yaml-pro-ts--until-mapping at-node))
(grandparent-tree (yaml-pro-ts--until-mapping
(treesit-node-parent parent-tree))))
(when (and parent-tree grandparent-tree)
(let* ((query '((block_mapping_pair key: (_) @key)))
(parent-key (alist-get 'key (treesit-query-capture parent-tree query)))
(parent-start (treesit-node-start parent-key))
(parent-end (treesit-node-end parent-key))
(parent-text (buffer-substring-no-properties parent-start parent-end))
(grandparent-key (alist-get 'key (treesit-query-capture grandparent-tree query)))
(grandparent-start (treesit-node-start grandparent-key))
(grandparent-end (treesit-node-end grandparent-key))
(grandparent-text (buffer-substring-no-properties
grandparent-start grandparent-end)))
(save-excursion
(delete-region parent-start parent-end)
(goto-char parent-start)
(insert grandparent-text)
(delete-region grandparent-start grandparent-end)
(goto-char grandparent-start)
(insert parent-text))))))
(defun yaml-pro-ts-indent-subtree (prefix)
"Indent the current subtree by one level.
This function uses tree-sitter. Indentation is controlled by the
variable `yaml-pro-indent'."
(interactive "p\n")
(let* ((at-subtree (yaml-pro-ts--until-mapping (treesit-node-at (point) 'yaml)))
(origin-marker (make-marker))
(end-marker (make-marker))
(indent-str (make-string yaml-pro-indent ?\s))
(original-content (concat
(buffer-substring-no-properties
(save-excursion (goto-char (treesit-node-start at-subtree)) (pos-bol))
(treesit-node-start at-subtree))
(treesit-node-text at-subtree t)))
(original-point (point))
(start-point (save-excursion (goto-char (treesit-node-start at-subtree)) (pos-bol))))
(set-marker origin-marker (point))
(set-marker end-marker (treesit-node-end at-subtree))
(goto-char start-point)
(forward-line 0)
(while (< (point) end-marker)
(when (not (looking-at " *$"))
(insert indent-str))
(forward-line 1))
(goto-char origin-marker)
(when (and
(= prefix 1)
(treesit-query-capture (treesit-buffer-root-node 'yaml) '((ERROR) @err)))
(delete-region start-point end-marker)
(goto-char start-point)
(insert original-content)
(goto-char original-point)
(beep)
(message "indent would result in broken tree. Prefix command with `C-u' to force indent."))
(set-marker origin-marker nil)
(set-marker end-marker nil)))
(defun yaml-pro-ts-unindent-subtree (prefix)
"Unindent the current subtree by one level.
This function uses tree-sitter. Indentation is controlled by the
variable `yaml-pro-indent'."
(interactive "p\n")
(let* ((at-subtree (yaml-pro-ts--until-mapping (treesit-node-at (point) 'yaml)))
(origin-marker (make-marker))
(end-marker (make-marker))
(indent-str (make-string yaml-pro-indent ?\s))
(original-content (concat
(buffer-substring-no-properties
(save-excursion (goto-char (treesit-node-start at-subtree)) (pos-bol))
(treesit-node-start at-subtree))
(treesit-node-text at-subtree t)))
(original-point (point))
(start-point (save-excursion (goto-char (treesit-node-start at-subtree))
(pos-bol))))
(set-marker origin-marker (point))
(set-marker end-marker (treesit-node-end at-subtree))
(goto-char start-point)
(forward-line 0)
(while (< (point) end-marker)
(when (looking-at (regexp-quote indent-str))
(delete-char yaml-pro-indent))
(forward-line 1))
(goto-char origin-marker)
(when (and
(= 1 prefix)
(treesit-query-capture (treesit-buffer-root-node 'yaml) '((ERROR) @err)))
(delete-region start-point end-marker)
(goto-char start-point)
(insert original-content)
(goto-char original-point)
(beep)
(message "unindent would result in broken tree. Prefix command with `C-u' to force indent."))
(set-marker origin-marker nil)
(set-marker end-marker nil)))
(defun yaml-pro-ts-mark-subtree (up)
"Mark the current subtree.
This puts point at the start of the current subtree, and mark at
the end. If a numeric prefix UP is given, move up into the
hierarchy of headlines by UP levels before marking the subtree."
(interactive "P")
(while (and up (> up 0))
(yaml-pro-ts-up-level)
(cl-decf up))
(let* ((at-subtree (yaml-pro-ts--until-mapping-or-list (treesit-node-at (point) 'yaml))))
(if (and (not up)
(or (and (eq last-command this-command) (mark t))
(and transient-mark-mode mark-active)))
(progn
(while (and at-subtree (<= (treesit-node-end at-subtree) (mark)))
(setq at-subtree
(yaml-pro-ts-next-mapping-node
at-subtree (treesit-node-type at-subtree))))
(when at-subtree
(set-mark (treesit-node-end at-subtree))))
(push-mark (treesit-node-end at-subtree))
(goto-char (treesit-node-start at-subtree))
(activate-mark))))
(defun yaml-pro-ts--kill-is-subtree (&optional tree)
"Return non-nil if TREE (or current kill) is a valid tree."
(unless tree
(setq tree (and kill-ring (current-kill 0))))
(when (and tree (> (length (string-split tree "\n")) 1))
(let ((node (treesit-parse-string tree 'yaml)))
(treesit-query-capture node '((block_mapping_pair) @key)))))
(defun yaml-pro-ts--kill-is-sequence (&optional tree)
"Return non-nil if TREE (or current kill) is a valid sequence."
(unless tree
(setq tree (and kill-ring (current-kill 0))))
(let ((first-line (car (string-split (string-trim-left tree) "\n"))))
(when (and tree (> (length (string-split tree "\n")) 1))
(let ((node (treesit-parse-string first-line 'yaml)))
(treesit-query-capture node '((block_sequence) @key))))))
(defun yaml-pro-ts-paste-sequence (&optional remove)
"Insert the current kill into the buffer, preserving sequence structure.
If REMOVE is non-nil, pop item off `kill-ring'."
(interactive)
(let ((seq (current-kill 0)))
(unless (yaml-pro-ts--kill-is-sequence seq)
(user-error
(substitute-command-keys
"The kill is not a YAML-sequence. Use `\\[yank]' to yank anyways")))
(let* ((base-indent (current-column))
(seq (string-trim seq))
(seq-lines (split-string seq "\n"))
(indent-lengths (save-match-data
(or (seq-map
(lambda (line)
(string-match "^\\( *\\)" line)
(length (match-string 1 line)))
(seq-filter
(lambda (line)
(string-match-p "^ *-" line))
(cdr seq-lines)))
(list (* yaml-pro-indent 2)))))
(smallest-indent (apply #'min indent-lengths))
(smallest-indent-string (make-string smallest-indent ?\s))
(seq-rest-lines-at-indent (string-join
(seq-map
(lambda (line)
(concat
(make-string base-indent ?\s)
(string-remove-prefix smallest-indent-string line)))
(cdr seq-lines))
"\n")))
(push-mark (point) 'nomsg)
(insert (car seq-lines) "\n")
(insert seq-rest-lines-at-indent))))
(defun yaml-pro-ts-paste-subtree (&optional remove)
"Insert the current kill into the buffer, preserving tree structure.
If REMOVE is non-nil, pop item off `kill-ring'."
(interactive)
(let ((tree (current-kill 0)))
(unless (yaml-pro-ts--kill-is-subtree tree)
(user-error
(substitute-command-keys
"The kill is not a YAML-tree. Use `\\[yank]' to yank anyways")))
(let* ((base-indent (current-column))
(indent-lengths (save-match-data
(seq-map
(lambda (line)
(string-match "^\\( *\\)" line)
(length (match-string 1 line)))
(seq-filter
(lambda (line)
(string-match-p "^ *[a-zA-Z0-9\"'_-]+:" line))
(cdr (split-string tree "\n")))))))
(if (not indent-lengths)
(progn
(push-mark (point) 'nomsg)
(insert tree))
(let* (;; does the killed text have more than one top level?
;; if so we need to calculate indentation differently.
(single-top-lvl-p
(seq-every-p
(lambda (len)
(<= (car indent-lengths) len))
(cdr indent-lengths)))
;; indent that the rest of kill text should have
(kill-indent (max (+ (apply #'min indent-lengths)
(if single-top-lvl-p -2 0))
0))
(kill-indent-string (make-string kill-indent ?\s))
(base-indent-string (make-string base-indent ?\s))
(insertion-string (with-temp-buffer
(insert tree)
(goto-char (point-min))
(forward-line 1)
(while (not (eobp))
(when (looking-at kill-indent-string)
(delete-char kill-indent)
(insert base-indent-string))
(forward-line 1))
(buffer-string))))
(push-mark (point) 'nomsg)
(insert insertion-string)))))
(when remove (pop kill-ring)))
(defun yaml-pro-ts-yank (&optional arg)
"Yank text on kill ring. If YAML-subtree, then indent it correctly.
This command will look at the current kill and check if it is a
subtree, or series of subtrees. If so, and ARG is nil, the
subtree is yanked with the appropriate amount of whitespace
inserted to make the tree retain its original structure."
(interactive "P")
(setq this-command 'yank)
(if arg
(call-interactively #'yank)
(let* ((subtreep (yaml-pro-ts--kill-is-subtree))
(seqp (yaml-pro-ts--kill-is-sequence))
(pos (point))
;; Is point on a placeholder for a block node?
(block-allowed-p
(and
;; The line must not have nothing but spaces after the point.
(looking-at "[ \t]*$")
(and (save-excursion
(skip-chars-backward " \t")
(or
;; The line is blank.
(bolp)
;; The point is preceded by a block struct indicator.
;; Note that compact collections cannot be preceded by
;; anchors or tags.
(and (memq (char-before) '(?- ?: ??))
;; Block struct indicators must be followed by spaces.
(memq (char-before pos) '(?\s ?\t)))))))))
(cond
((and seqp block-allowed-p yaml-pro-ts-yank-subtrees)
(yaml-pro-ts-paste-sequence))
((and subtreep block-allowed-p yaml-pro-ts-yank-subtrees)
(yaml-pro-ts-paste-subtree))
(t (call-interactively #'yank))))))
(defun yaml-pro-ts-newline (&optional arg interactive)
(interactive "*P\np")
(barf-if-buffer-read-only)
(cond
((or (looking-at-p " *[^ \n]")
(equal (treesit-node-type (treesit-node-at (point) 'yaml)) ":")
(equal (treesit-node-type (treesit-node-parent (treesit-node-at (point) 'yaml)))
"block_scalar"))
(let* ((indent (yaml-pro-format-ts--node-indent (treesit-node-at (point) 'yaml)))
(indent-column (* indent yaml-pro-indent)))
(call-interactively #'newline)
(when (< (current-column) indent-column)
(insert (make-string (- indent-column (current-column)) ?\s)))))
(t (call-interactively #'newline))))
(defun yaml-pro-ts--imenu-node-label (node)
"Return an imenu label for NODE."
(let ((top node)
(root (treesit-buffer-root-node 'yaml))
(label ""))
(while (not (treesit-node-eq node root))
(cond
((equal (treesit-node-type node) "block_mapping_pair")
(let ((key-node (treesit-node-child-by-field-name node "key")))
(when key-node
(setq label (concat (treesit-node-text key-node)
(if (equal label "") "" (string yaml-pro-ts-path-element-separator))
label)))))
((equal (treesit-node-type node) "block_sequence_item")
(setq label (format "[%d]%c%s" (treesit-node-index node) yaml-pro-ts-path-element-separator label))))
(setq node (treesit-node-parent node)))
label))
(defun yaml-pro-ts-create-index ()
"Create imenu index of YAML file using treesitter."
(let ((all-keys (treesit-query-capture (treesit-buffer-root-node 'yaml) '((block_mapping_pair key: (_) @key))))
(imenu-items '()))
(dolist (key-item all-keys)
(let* ((key-node (cdr key-item))
(key-label (yaml-pro-ts--imenu-node-label key-node)))
(push (cons key-label (treesit-node-start key-node)) imenu-items)))
(seq-reverse imenu-items)))
(defun yaml-pro-ts-eldoc (&rest _ignored)
"Return eldoc message of current point."
(let* ((at-tree (yaml-pro-ts--until-mapping-or-list (treesit-node-at (point) 'yaml))))
(when at-tree
(yaml-pro-ts--imenu-node-label at-tree))))
(defun yaml-pro-ts--path-to-imenu-key (path)
"Stringify path to the same format that imenu uses.
E.g. the path (\"one\" 0 \"two\") results in the key string \"one.[0].two\"."
(string-join
(seq-map
(lambda (elt) (if (numberp elt) (format "[%d]" elt) elt))
path)
(string yaml-pro-ts-path-element-separator)))
(defun yaml-pro-ts-jump-to-definition (&rest parts)
"Jump to a part of the YAML file based on PARTS.
If element is a sting, use it as block mapping key.
If element is a number, use it as a sequence index."
(imenu (yaml-pro-ts--path-to-imenu-key parts)))
(defun yaml-pro-ts-add-mapping (&rest keys)
"Add KEYS as levels beneath current node's block mapping.
KEYS are added as increasingly nested levels."
(let* ((paths (seq-map #'car (yaml-pro-ts-create-index)))
(to-go keys)
(to-add'()))
(while (and (not (member (yaml-pro-ts--path-to-imenu-key to-go) paths))
to-go)
(setq to-add (append (last to-go) to-add))
(setq to-go (butlast to-go)))
(when to-go
(apply #'yaml-pro-ts-jump-to-definition to-go))
(when to-add
(let* ((at-node (treesit-node-at (point) 'yaml))
(parent (treesit-parent-until
at-node
(lambda (node)
(equal (treesit-node-type node) "block_mapping_pair"))
t))
(parent-indent (if to-go
(save-excursion
(goto-char (treesit-node-start parent))
(current-column))
(- yaml-pro-indent))))
(if (or (not to-go)
(or (not (treesit-node-child-by-field-name parent "value"))
(equal (treesit-node-type (car (treesit-node-children (treesit-node-child-by-field-name parent "value"))))
"block_mapping")))
(progn
(if to-go
(goto-char (treesit-node-end parent))
(goto-char (point-max))
(unless (looking-back "\n" (- (point) 2))
(insert "\n")))
(while to-add
(insert "\n")
(insert (make-string (+ yaml-pro-indent parent-indent) ?\s))
(insert (car to-add) ":")
(setq to-add (cdr to-add))
(cl-incf parent-indent yaml-pro-indent)))
(user-error "unable to to insert element at position"))))))
(defconst yaml-pro-ts-mode-map
(let ((map (make-sparse-keymap)))
(prog1 map
(define-key map (kbd "C-c C-x C-w") #'yaml-pro-ts-kill-subtree)
(define-key map (kbd "C-c C-u") #'yaml-pro-ts-up-level)
(define-key map (kbd "C-c C-d") #'yaml-pro-ts-down-level)
(define-key map (kbd "C-c C-p") #'yaml-pro-ts-prev-subtree)
(define-key map (kbd "C-c C-n") #'yaml-pro-ts-next-subtree)
(define-key map (kbd "s-<up>") #'yaml-pro-ts-move-subtree-up)
(define-key map (kbd "s-<down>") #'yaml-pro-ts-move-subtree-down)
(define-key map (kbd "C-c >") #'yaml-pro-ts-indent-subtree)
(define-key map (kbd "C-c <") #'yaml-pro-ts-unindent-subtree)
(define-key map (kbd "M-<return>") #'yaml-pro-ts-meta-return)
(define-key map (kbd "M-?") #'yaml-pro-convolute-tree)
(define-key map (kbd "C-c @") #'yaml-pro-ts-mark-subtree)
(define-key map (kbd "C-c C-x C-y") #'yaml-pro-ts-paste-subtree)
(define-key map [remap yank] #'yaml-pro-ts-yank)
(define-key map (kbd "C-c '") #'yaml-pro-edit-ts-scalar)))
"Map for minor mode `yaml-pro-ts-mode'.")
;;;###autoload
(define-minor-mode yaml-pro-ts-mode
"Minor mode to enable yaml-pro treesitter keymap.
\\{yaml-pro-mode-map}"
:init-value nil
:group 'yaml-pro
:keymap yaml-pro-ts-mode-map
(when yaml-pro-ts-mode
(unless (featurep 'treesit)
(user-error "Tree-sitter not supported in current Emacs version"))
(unless (treesit-ready-p 'yaml)
(user-error "YAML tree-sitter not ready"))
(treesit-parser-create 'yaml)
(setq imenu-generic-expression nil)
(setq imenu-create-index-function #'yaml-pro-ts-create-index)
(if (boundp 'eldoc-documentation-functions)
(add-hook 'eldoc-documentation-functions #'yaml-pro-ts-eldoc nil t)
(setq-local eldoc-documentation-function #'yaml-pro-ts-eldoc))))
;;; yaml-pro parser
(defcustom yaml-pro-max-parse-size 5000
"Size of buffer for which any size greater than use heuristic to parse.
Note that this isn't fully compatable with every command."
:group 'yaml-pro
:type 'number
:package-version '(yaml-pro "0.2.0"))
(defface yaml-pro-fold-replacement-face
'((t :inherit 'font-lock-comment-face))
"Face for fold replacement text.")
(defcustom yaml-pro-indent (if (boundp 'yaml-indent)
yaml-indent
2)
"Default indentation to use for yaml-pro."
:group 'yaml-pro
:type 'integer)
(defvar-local yaml-pro-buffer-tree nil)
(defun yaml-pro--offset-parse-tree (tree offset)
"Offset all TREE values of `yaml-position' property by OFFSET."
(cond
((or (listp tree) (vectorp tree))
(seq-map
(lambda (elt)
(yaml-pro--offset-parse-tree elt offset))
tree))
((stringp tree)
(let* ((yaml-position (get-text-property 0 'yaml-position tree)))
(when yaml-position
(let ((offset-position (cons (+ (car yaml-position) offset)
(+ (cdr yaml-position) offset))))
(set-text-properties 0
(length tree)
(list 'yaml-position offset-position)
tree)))))))
(defun yaml-pro--use-fast-p ()
"Return non nil if buffer size is larger than `yaml-pro-max-parse-size'."
(>= (buffer-size) yaml-pro-max-parse-size))
(defun yaml-pro--fast-parse-bounds (&optional point)
"Return a list of the start and end point of subsection to parse.
Find subsection based off of POINT if provided."
(let* ((point-indent
(save-excursion
(beginning-of-line)
(skip-chars-forward " ")
(current-column)))
(indent-regexp (make-string point-indent ?\s))
(start-point)
(end-point))
(if (= point-indent 0)
(setq start-point (point-min)
end-point (point-max))
(save-excursion
(beginning-of-line)
(while (not (or (bobp)
(and (looking-at " *[a-zA-Z_0-9-]+:\\s-*$")
(not (looking-at indent-regexp)))))
(forward-line -1))
(setq start-point (point)))
(save-excursion
(beginning-of-line)
(while (not (or (eobp)
(and (not (looking-at indent-regexp))
(not (looking-at " *$")))))
(forward-line 1))
(setq end-point (point))))
(list start-point end-point)))
(defun yaml-pro--get-buffer-tree ()
"Return the cached buffer-tree if exists, else regenerate it."
(or yaml-pro-buffer-tree
(if (not (yaml-pro--use-fast-p))
(let ((tree (yaml-parse-tree (buffer-string))))
(setq yaml-pro-buffer-tree tree)
tree)
(seq-let (start-point end-point) (yaml-pro--fast-parse-bounds)
(let* ((subsection-string (buffer-substring start-point end-point))
(tree (yaml-parse-tree subsection-string)))
(yaml-pro--offset-parse-tree tree (1- start-point))
(when (and (= start-point (point-min))
(= end-point (point-max)))
(setq yaml-pro-buffer-tree tree))
tree)))))
(defun yaml-pro--after-change-hook (_ _ _)
"Delete cached tree on buffer change."
(setq yaml-pro-buffer-tree nil))
(defun yaml-pro--find-node (parse point)
"Recursively look through PARSE to find scalar at POINT."
(catch 'done
(cond
((vectorp parse)
(seq-do
(lambda (item)
(let ((res (yaml-pro--find-node item point)))
(when res
(throw 'done res))))
parse)
nil)
((listp parse)
(dolist (item parse)
(let ((res (yaml-pro--find-node (cdr item) point)))
(when res
(throw 'done res)))))
((stringp parse)
(let* ((bounds (get-text-property 0 'yaml-position parse))
(start (and bounds (car bounds)))
(end (and bounds (cdr bounds))))
(if (and start end (<= start point end))
parse
nil))))))
(defun yaml-pro--fast-value-at-point ()
"Return the scalar under the current point.
This function uses a heuristic to limit the amount of parsing
that has to be done."
(seq-let (start-point end-point) (yaml-pro--fast-parse-bounds)
(let* ((subsection-string (buffer-substring start-point end-point))
(parse (yaml-parse-string-with-pos subsection-string))
(val (yaml-pro--find-node parse (1+ (- (point) start-point)))))
(when val
(let* ((yaml-position (get-text-property 0 'yaml-position val))
(new-position (cons (1- (+ (car yaml-position) start-point))
(1- (+ (cdr yaml-position) start-point)))))
(set-text-properties 0 (length val) (list 'yaml-position new-position) val)
val)))))
(defun yaml-pro--value-at-point ()
"Return the scalar under the current point."
(let* ((parse (yaml-parse-string-with-pos (buffer-string))))
(yaml-pro--find-node parse (point))))
(defun yaml-pro--get-parent-block* (tree point)
"Return subtree from TREE that best contain POINT."
(if (not (listp tree))
nil
(let ((sub-blocks
(seq-filter #'identity
(seq-map (lambda (st)
(yaml-pro--get-parent-block* st point))
tree))))
(cond
((and sub-blocks
(stringp (car tree))
(let* ((bounds (get-text-property 0 'yaml-position (cadr tree)))
(start (car bounds))
(end (cdr bounds)))
(and (numberp start) (<= start point end))))
(let* ((bounds (get-text-property 0 'yaml-position (cadr tree)))
(start (car bounds))
(end (cdr bounds)))
(throw 'result (list start end))))
(sub-blocks
(car sub-blocks))
((stringp (car tree))
(let* ((bounds (get-text-property 0 'yaml-position (cadr tree)))
(start (and bounds (car bounds)))
(end (and bounds (cdr bounds))))
(if (and (numberp start) (<= start point end))
(list start end)
nil)))
(t nil)))))
(defun yaml-pro-get-parent-block (tree point)
"Return the nearest parent block in TREE to node in POINT."
(catch 'result
(yaml-pro--get-parent-block* tree point)))
(defun yaml-pro-get-block-bounds (tree point)
"Return subtree from TREE that best contain POINT."
(if (not (listp tree))
nil
(let ((sub-blocks
(seq-filter #'identity
(seq-map (lambda (st) (yaml-pro-get-block-bounds st point))
tree))))
(cond
(sub-blocks
;; TODO should find best match instead of firt (?)
(car sub-blocks))
((and (stringp (car tree)) (not (equal (car tree) "")))
(let* ((bounds (get-text-property 0 'yaml-position (cadr tree)))
(start (and bounds (car bounds)))
(end (and bounds (cdr bounds))))
(if (and
(numberp start)
(<= start point end)
(not (= (save-excursion
(goto-char start) (beginning-of-line) (point))
(save-excursion
(goto-char end) (beginning-of-line) (point)))))
(list start end)
nil)))
(t nil)))))
(defun yaml-pro-get-block (tree point)
"Return subtree from TREE that best contain POINT."
(if (not (listp tree))
nil
(let ((sub-blocks
(seq-filter #'identity
(seq-map (lambda (st) (yaml-pro-get-block st point))
tree))))
(cond
(sub-blocks
;; TODO should find best match instead of firt (?)
(car sub-blocks))
((stringp (car tree))
(let* ((bounds (get-text-property 0 'yaml-position (cadr tree)))
(start (and bounds (car bounds)))
(end (and bounds (cdr bounds))))
(if (and (numberp start)
(<= start point end)
;; hack to get small maps to not get selected
(yaml-pro--fix-bounds (list start end)))
(list start end)
nil)))
(t nil)))))
(defun yaml-pro--fix-bounds (bounds)
"Adjust BOUNDS to proper fold points."
(seq-let (beg end) bounds
(save-excursion
(goto-char beg)
(cond
((looking-at-p "[ \t]*-")
(setq beg (1- (point))))
((and (not (looking-at "{"))
(not (looking-at "\\[")))
(end-of-line)
(setq beg (point)))
((looking-at-p "\\(\\[\\|{\\)")
(setq beg (1+ (point))))))
(save-excursion
(goto-char end)
(cond
((looking-back "\n" (- (point) 2))
(setq end (1- end)))
((looking-back "\\(\\]\\|\\}\\)" (- (point) 2))
(setq end (1- end)))))
(if (>= beg end)
nil
(list beg end))))
(defun yaml-pro--search-location (tree point path)
"Return path up to POINT of TREE having visited at PATH."
(cond
((stringp tree)
(let ((pos (get-text-property 0 'yaml-position tree)))
(when (<= (car pos) point (cdr pos))
path)))
(t
(seq-find #'identity
(seq-map
(lambda (tuple)
(let* ((key (car tuple))