-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsp-symbol-outline.el
executable file
·1474 lines (1349 loc) · 62.7 KB
/
lsp-symbol-outline.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
;;; lsp-symbol-outline.el --- a symbol tree for lsp-mode -*- lexical-binding: t; -*-
;; Copyright (C) 2018 bizzyman
;; Version: 0.0.3
;; Homepage: https://github.com/bizzyman/LSP-Symbol-Outline
;; Keywords: languages, lsp, outline
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and-or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;
;;; Commentary:
;;
;; TODO:
;;
;; - Refactor
;; - Introduce configuration
;; - Support More languages: C++, Rust, Go, PHP, Ruby, Elixir, HTML
;; - Split file into language specific parts
;; - Debug tree sort function
;; - Make url based functions async and parallel
;; - Remove Ternjs and Anaconda as dependency and rely only on lsp for func
;; arguments and hierarchy/depth parsing
;; - Add 'context' view like in Nuclide -> shows definition, other info
;;
;;; Code:
;; Dependencies
; LSP-S-O
(require 'lsp-mode)
(require 'lsp-symbol-outline-custom)
(require 'lsp-symbol-outline-faces)
(require 'outline)
(require 'outline-magic)
(require 's)
(require 'dash)
;; Vars
(defconst lsp-symbol-outline-symbol-kind-alist
'((0 . "Unknown")
(1 . "File")
(2 . "Module")
(3 . "Namespace")
(4 . "Package")
(5 . "Class")
(6 . "Method")
(7 . "Property")
(8 . "Field")
(9 . "Constructor")
(10 . "Enum")
(11 . "Interface")
(12 . "Function")
(13 . "Variable")
(14 . "Constant")
(15 . "String")
(16 . "Number")
(17 . "Boolean")
(18 . "Array")
(19 . "Object")
(20 . "Key")
(21 . "Null")
(22 . "Enum Member")
(23 . "Struct")
(24 . "Event")
(25 . "Operator")
(26 . "Type Parameter")
;; extended
(252 . "TypeAlias")
(253 . "Parameter")
(254 . "StaticMethod")
(255 . "Macro"))
"Alist of symbol kind associations. Used to print correct symbol
kind names. Contains extensions from non core lsp projects like cquery.")
;; Major mode
;;;###autoload
(define-derived-mode lsp-symbol-outline-mode
special-mode
"S-outline"
"Major mode for the LSP Symbol Outline."
(read-only-mode 1))
;; Defuns
(defun lsp-symbol-outline--lsp-get-document-symbols ()
"Get hash table of symbols in current document, uses LSP.
Ensure that lsp-mode is on and enabled."
(lsp--send-request
(lsp--make-request "textDocument/documentSymbol"
`(:textDocument
,(lsp--text-document-identifier)))))
(defun lsp-symbol-outline--put-plist-name (plist-item hasht-item)
"Get name from hash table of symbol in HASHT-ITEM and set plist
:name property. Return plist."
(plist-put plist-item
:name
(pcase
(replace-regexp-in-string "\(.+\)"
""
(gethash "name" hasht-item))
("" "*anon*")
(SYMBOL SYMBOL))))
(defun lsp-symbol-outline--get-symbol-start-line (hasht-range)
"Get the symbol start line from hash table HASHT-RANGE.
Return line number."
(1+ (gethash "line"
(gethash "start"
hasht-range))))
(defun lsp-symbol-outline--get-symbol-end-point (hasht-range)
"Get the symbol end point from hash table HASHT-RANGE.
Return line number."
(lsp--position-to-point (gethash "end"
hasht-range)))
(defun lsp-symbol-outline--get-symbol-end-point-clike-generic (hasht-range)
"Get the symbol end point by moving point to end position in
HASHT-RANGE and jumping to matching } brace. Return line number.
For use with languages that have C/Java like syntax."
(save-excursion
(if
(progn
(goto-char
(lsp--position-to-point (gethash "start"
hasht-range)))
(beginning-of-line)
(move-to-column
(gethash "character"
(gethash "end"
hasht-range)))
;; TODO bound can fail if args on multiple lines
(search-forward "{" (line-end-position) t))
(progn
(backward-char)
(lsp-symbol-outline--jump-paren)
(1+ (point)))
(lsp-symbol-outline--get-symbol-end-point hasht-range))))
(defun lsp-symbol-outline--get-symbol-column (hasht-range)
"Get the symbol start column from hash table HASHT-RANGE.
Return column number."
(gethash "character" (gethash "start" hasht-range)))
(defun lsp-symbol-outline--set-placeholder-depth (plist-item)
"Set symbol depth to 0. Depth updated by `lsp-symbol-outline--tree-sort'.
Return PLIST-ITEM."
(plist-put plist-item :depth 0))
(defun lsp-symbol-outline--get-symbol-args-generic (plist-item hasht-range)
"Find symbol start line, move to opening param delimiting \"(\" and
return buffer contents between parens. Saves excursion so that next operation -
:docs lookup - can continue from :symbol-start-point.
Returns arg string based on whether it is empty or not."
(goto-char (plist-get plist-item :symbol-start-point))
(save-excursion
(search-forward "(" nil t)
(pcase (buffer-substring
(progn (forward-char -1) (point))
(progn (forward-sexp) (point)))
("()" nil)
(SYMBOL (s-replace-all '(("( " . "(") (" )" . ")"))
(s-collapse-whitespace SYMBOL))))))
(defun lsp-symbol-outline--create-symbols-list (sym-end-handler
depth-handler
args-handler
docs-handler)
"Create a list of plists corresponding to symbols in document.
Properties are inferred by transforming the hash-table returned by
`lsp-symbol-outline--lsp-get-document-symbols' and specific methods for
specific language needs. When such a method is required the function
passed in to the corresponding parameter is applied. For example
getting the lang symbol end position is done by funcalling SYM-END-HANDLER
passed to this function from lang specific file.
Each plist is generated by adding to the the plist-item local variable.
List of plists is returned by the local var agg-items."
(let ((agg-items)
(index 1))
(dolist (hasht-item (lsp-symbol-outline--lsp-get-document-symbols))
(let ((plist-item)
(hasht-kind (gethash "kind" hasht-item))
(hasht-range (gethash "range" (gethash "location" hasht-item))))
;; 0 - INDEX
(setq plist-item (plist-put plist-item :index index))
;; 1 - NAME
(setq plist-item
(lsp-symbol-outline--put-plist-name plist-item hasht-item))
;; 2 - KIND
(plist-put plist-item :kind hasht-kind)
;; 3 & 4 SYMBOL START AND END POSITION
(if (memq hasht-kind '(5 6 12)) ;is symbol function or class
(progn
;; 3 - func/class start range
(plist-put plist-item :symbol-start-point
(lsp--position-to-point
(gethash "start" hasht-range)))
;; 4 - func/class end range
(plist-put plist-item :symbol-end-point
(funcall sym-end-handler hasht-range)))
;; 3 - var start range
(plist-put plist-item :symbol-start-point
(lsp--position-to-point
(gethash "start" hasht-range)))
;; 4 - var end range
(plist-put plist-item :symbol-end-point
(lsp--position-to-point
(gethash "end" hasht-range))))
;; SYMBOL END LINE
(plist-put plist-item :symbol-end-line
(gethash "line"
(gethash "end" hasht-range)))
;; 5 - DEPTH
(setq plist-item (funcall depth-handler plist-item))
;; 6 - COLUMN
(plist-put plist-item :column
(lsp-symbol-outline--get-symbol-column hasht-range))
;; get arguments and docstring
(save-excursion
(if (or (memq hasht-kind '(6 12))
(and (equal major-mode 'python-mode)
;; ??? so all classes get args/docs?
(equal 5 (plist-get plist-item :kind))))
(progn
;; 7 - ARGS
(plist-put plist-item :args (funcall args-handler
plist-item
hasht-range))
;; 8 - DOCS
(plist-put plist-item :docs (funcall docs-handler
plist-item)))
;; 7 nil args for vars
(plist-put plist-item :args nil)
;; 8 nil docs for vars
(plist-put plist-item :docs nil)))
(setq index (1+ index))
(push plist-item agg-items)))
(reverse agg-items)))
(defun lsp-symbol-outline--sort-list-by-start (list)
"Sort list of plists by their :symbol-start-point property and update
indexes. Return list of plists in order they appear in document."
(let ((list_ list)
(index 1))
(setq list_
(--sort (cond
((= (plist-get it :symbol-start-point)
(plist-get other :symbol-start-point))
(< (plist-get it :symbol-end-point)
(plist-get other :symbol-end-point)))
(t (< (plist-get it :symbol-start-point)
(plist-get other :symbol-start-point))))
list_))
(dolist (item list_)
(plist-put item :index index)
(setq index (1+ index)))
list_))
(defun lsp-symbol-outline--tree-sort (list)
"Sort list of symbol plists into a hierarchical tree. This is done in two
stages. First compare :symbol-end-point of current symbol - the `global-counter'
local var - and find the next symbol with a higher :symbol-end-point - the
`local-counter' local var. Second, all symbol's depth properties between
`global-counter' and `local-counter' are incremented by one. Repeat for every
symbol.
Return tree sorted list of plists."
(let ((global-counter 0)
(local-counter 0)
(local-end 0)
(list-length (length list)))
(while (< global-counter list-length)
;; check if end-point of current symbol greater than next symbol
(if (if (and (memq (plist-get (nth global-counter list)
:kind)
'(7 13 14))
(memq (plist-get (nth (1+ global-counter) list)
:kind)
'(7 13 14)))
(ignore-errors (> (plist-get (nth global-counter list)
:symbol-end-line)
(plist-get (nth (1+ global-counter) list)
:symbol-end-line)))
(ignore-errors (> (plist-get (nth global-counter list)
:symbol-end-point)
(plist-get (nth (1+ global-counter) list)
:symbol-end-point))))
;; if it is > find the next symbol with end-point
;; > than symbol at index global-counter
(let ((local-counter (1+ global-counter)))
(while (ignore-errors
(> (plist-get (nth global-counter list)
:symbol-end-point)
(plist-get (nth local-counter list)
:symbol-end-point)))
(setq local-counter (1+ local-counter)))
(setq local-end local-counter)
(setq local-counter (1+ global-counter))
(while (< local-counter local-end)
(plist-put (nth local-counter list)
:depth
(1+ (plist-get (nth local-counter list)
:depth)))
(setq local-counter (1+ local-counter)))))
(setq global-counter (1+ global-counter))))
list)
(defun lsp-symbol-outline--remove-arg-indices (list)
"Remove symbols that are already covered by the :args plist prop."
(let ((indices))
(dolist (item list)
(if (plist-get item :args)
(let ((m (replace-regexp-in-string "\n" "" (plist-get item :args)))
(c))
(cond
((string-match "( *void *)" m) nil)
(m
(progn (setq c (s-count-matches "," m))
(push (number-sequence (plist-get item :index)
(+ (plist-get item :index)
(if (equal c 0) 0 c)) 1)
indices)))))))
(setq indices (-flatten indices))
(setf list (-remove-at-indices indices list))))
(defun lsp-symbol-outline--tree-sort-rem-args (list)
"Sort list of symbol plists into a hierarchical tree. This is done in two
stages. First remove function args from symbols list. Then compare
:symbol-end-point of current symbol - the `global-counter' local var - and find
the next symbol with a higher :symbol-end-point - the `local-counter' local var.
Third, all symbol's depth properties between `global-counter' and
`local-counter' are incremented by one. Repeat for every symbol.
Return tree sorted list of plists."
(setq list (lsp-symbol-outline--remove-arg-indices list))
(let ((global-counter 0)
(local-counter 0)
(local-end 0)
(list-length (length list)))
(while (< global-counter list-length)
;; check if end-point of current symbol greater than next symbol
(if (if (and (memq (plist-get (nth global-counter list)
:kind)
'(7 13 14))
(memq (plist-get (nth (1+ global-counter) list)
:kind)
'(7 13 14)))
(ignore-errors (> (plist-get (nth global-counter list)
:symbol-end-line)
(plist-get (nth (1+ global-counter) list)
:symbol-end-line)))
(ignore-errors (> (plist-get (nth global-counter list)
:symbol-end-point)
(plist-get (nth (1+ global-counter) list)
:symbol-end-point))))
;; if it is > find the next symbol with end-point
;; > than symbol at index global-counter
(let ((local-counter (1+ global-counter)))
(while (ignore-errors
(> (plist-get (nth global-counter list)
:symbol-end-point)
(plist-get (nth local-counter list)
:symbol-end-point)))
(setq local-counter (1+ local-counter)))
(setq local-end local-counter)
(setq local-counter (1+ global-counter))
(while (< local-counter local-end)
(plist-put (nth local-counter list)
:depth
(1+ (plist-get (nth local-counter list)
:depth)))
(setq local-counter (1+ local-counter)))))
(setq global-counter (1+ global-counter))))
list)
(defun lsp-symbol-outline-unhighlight-symbol ()
"Remove idle timer highlighting in symbol outline buffer."
(with-current-buffer
lsp-outline-buffer
(save-excursion
(remove-overlays (point-min) (point-max)
'face 'lsp-symbol-outline-inside-current-symbol))))
(defun lsp-symbol-outline-highlight-symbol ()
"Go to symbol outline buffer and highlight the symbol inside which point
currently resides."
(let* ((inhibit-message t)
(line
(ignore-errors (overlay-get
(-first (lambda (o)
(overlay-get
o 'lsp-symbol-outline-timer-goto-line))
(overlays-at (point) t))
'lsp-symbol-outline-timer-goto-line))))
(if (not line)
(ignore-errors (lsp-symbol-outline-unhighlight-symbol))
(ignore-errors
(with-current-buffer
lsp-outline-buffer
(save-excursion
(goto-char (point-min))
(forward-line (1- line))
(remove-overlays (point-min) (point-max)
'face 'lsp-symbol-outline-inside-current-symbol)
(let ((o (make-overlay (line-beginning-position)
(1+(line-beginning-position)) )))
(overlay-put o 'display
lsp-symbol-outline-position-indicator-char)
(overlay-put o 'face 'lsp-symbol-outline-inside-current-symbol)
(overlay-put o 'priority 99))))))))
(defun lsp-symbol-outline-add-idle-timer-highlight-props (start end line)
"Ceate an overlay corresponding to :symbol-start-point and
:symbol-end-point that stores the line on which the symbol is printed on in the
symbol outline buffer. Adds `lsp-symbol-outline-timer-goto-line'
properties between START and END."
(let ((o (make-overlay start end)))
(overlay-put o
'lsp-symbol-outline-timer-goto-line
line)))
(defun lsp-symbol-outline--delete-idle-timer-overlays ()
"Remove all overlays in buffer that have
`lsp-symbol-outline-timer-goto-line' properties."
(dolist (o (overlays-in (point-min) (point-max)))
(when (overlay-get o 'lsp-symbol-outline-timer-goto-line)
(delete-overlay o))))
(defun lsp-symbol-outline--create-buffer ()
"Create and return a buffer for inserting the LSP symbol outline."
(get-buffer-create
(format "*%s-outline*"
(file-name-sans-extension (buffer-name)))))
(defun lsp-symbol-outline--print-indentation (item)
"Loop that prints space :depth number of times. Indentation implies
hierarchy."
(let ((x 0))
(while (< x
(* (plist-get item :depth) 2))
(progn (insert " ") (setq x (1+ x))))))
(defun lsp-symbol-outline--insert-sym-kind-name (same-kind-list)
"Insert string based on plist's :kind property. Uses
`lsp-symbol-outline-symbol-kind-alist' for name associations."
(insert (if (memq (plist-get (car same-kind-list) :kind) '(252 5))
(propertize
(format "%ses\n"
(alist-get (plist-get (car same-kind-list) :kind)
lsp-symbol-outline-symbol-kind-alist))
'face 'default)
(propertize
(format "%ss\n"
(alist-get (plist-get (car same-kind-list) :kind)
lsp-symbol-outline-symbol-kind-alist))
'face 'default))))
(defun lsp-symbol-outline--print-symbol-icon-gui (item)
"Inserts the gui version of glyph icon for symbol. Glyphs use
atomicons.ttf font. May appear in source code as the wrong glyps or unicode
placeholders. Outputted correctly when face set."
(cond
((memq (plist-get item :kind) '(1 2 4)) ;Module
(insert (propertize " "
'face 'lsp-symbol-outline-atom-icons-face
'font-lock-ignore 't)))
((memq (plist-get item :kind) '(5 23)) ;Class
(insert (propertize " "
'face 'lsp-symbol-outline-atom-icons-face
'font-lock-ignore 't)))
((eq (plist-get item :kind) 6) ;Method
(insert (propertize " "
'face 'lsp-symbol-outline-atom-icons-face
'font-lock-ignore 't)))
((eq (plist-get item :kind) 12) ;Function
(insert (propertize " "
'face 'lsp-symbol-outline-atom-icons-face
'font-lock-ignore 't)))
((eq (plist-get item :kind) 13) ;Variable
(insert (propertize " "
'face 'lsp-symbol-outline-atom-icons-face
'font-lock-ignore 't)))
((memq (plist-get item :kind) '(14 21 255 252)) ;Constant
(insert (propertize " "
'face 'lsp-symbol-outline-atom-icons-face
'font-lock-ignore 't)))
((eq (plist-get item :kind) 15) ;string
(insert (propertize " "
'face 'lsp-symbol-outline-atom-icons-face
'font-lock-ignore 't)))
((eq (plist-get item :kind) 16) ;number
(insert (propertize " "
'face 'lsp-symbol-outline-atom-icons-face
'font-lock-ignore 't)))
((eq (plist-get item :kind) 20) ;key
(insert (propertize " "
'face 'lsp-symbol-outline-atom-icons-face
'font-lock-ignore 't)))
((memq (plist-get item :kind) '(10 18)) ;Array
(insert (propertize " "
'face 'lsp-symbol-outline-atom-icons-face
'font-lock-ignore 't)))
(t
(insert (propertize " " ;all other symbol types
'face 'lsp-symbol-outline-atom-icons-face
'font-lock-ignore 't)))))
(defun lsp-symbol-outline--print-symbol-icon-term (item)
"Inserts the terminal version of icon for symbol. Uses standard ascii
chars."
(cond
((memq (plist-get item :kind) '(1 2 4)) ;Module
(insert (propertize "M "
'face 'lsp-symbol-outline-term-symbol-type-name-face
'font-lock-ignore 't)))
((eq (plist-get item :kind) 5) ;Class
(insert (propertize "C "
'face 'lsp-symbol-outline-term-symbol-type-name-face
'font-lock-ignore 't)))
((eq (plist-get item :kind) 23) ;Struct
(insert (propertize "S "
'face 'lsp-symbol-outline-term-symbol-type-name-face
'font-lock-ignore 't)))
((eq (plist-get item :kind) 6) ;Method
(insert (propertize "m "
'face 'lsp-symbol-outline-term-symbol-type-name-face
'font-lock-ignore 't)))
((eq (plist-get item :kind) 12) ;Function
(insert (propertize "F "
'face 'lsp-symbol-outline-term-symbol-type-name-face
'font-lock-ignore 't)))
((eq (plist-get item :kind) 13) ;Variable
(insert (propertize "V "
'face 'lsp-symbol-outline-term-symbol-type-name-face
'font-lock-ignore 't)))
((memq (plist-get item :kind) '(14 21 255 252)) ;Constant
(insert (propertize "K "
'face 'lsp-symbol-outline-term-symbol-type-name-face
'font-lock-ignore 't)))
((memq (plist-get item :kind) '(10 18)) ;Array
(insert (propertize "A "
'face 'lsp-symbol-outline-term-symbol-type-name-face
'font-lock-ignore 't)))
(t
(insert (propertize "- " ;all other symbol types
'face 'lsp-symbol-outline-term-symbol-type-name-face
'font-lock-ignore 't)))))
(defun lsp-symbol-outline--move-point-to-symbol (item buf)
"Returns a function that moves the point and focus to symbol location in
original document buffer."
`(lambda (x)
(switch-to-buffer-other-window ,buf)
(goto-char ,(plist-get item :symbol-start-point))))
(defun lsp-symbol-outline--print-button (item buf)
"Print the button that handles the `lsp-symbol-outline-show-docstring-tip'
and `lsp-symbol-outline--move-point-to-symbol' functionality. Docstring
function is handled by letter 'd' - 100 in ascii."
(insert-button (plist-get item :name)
'action (lsp-symbol-outline--move-point-to-symbol item buf)
'keymap `(keymap (mouse-2 . push-button)
(100 . (lambda () (interactive)
(lsp-symbol-outline-show-docstring-tip
,(if (plist-get item :docs)
(plist-get item :docs)
nil)))))
'face (cond
((and (plist-get item :docs)
(ignore-errors (not (string-empty-p
(plist-get item :docs))))
(equal (plist-get item :kind) 12))
'lsp-symbol-outline-function-face-has-doc)
((equal (plist-get item :kind) 12)
'lsp-symbol-outline-function-face)
((and (plist-get item :docs)
(ignore-errors (not (string-empty-p
(plist-get item :docs))))
(equal (plist-get item :kind) 6))
'lsp-symbol-outline-function-face-has-doc)
((equal (plist-get item :kind) 6)
'lsp-symbol-outline-function-face)
((and (plist-get item :docs)
(ignore-errors (not (string-empty-p
(plist-get item :docs))))
(equal (plist-get item :kind) 5))
'lsp-symbol-outline-class-face-has-doc)
((equal (plist-get item :kind) 5)
'lsp-symbol-outline-class-face)
(t
'lsp-symbol-outline-var-face))))
(defun lsp-symbol-outline--print-outline-clike-generic (list buf)
"Insert indentation, icon, button and any args into symbol outline buffer.
Iterates over symbol list. For use with langs that resemeble C/Java in syntax."
(dolist (item list)
(plist-put item :line (string-to-number (format-mode-line "%l")))
(insert " ")
;; indentation
(lsp-symbol-outline--print-indentation item)
;; icon
(if (and window-system
lsp-symbol-outline-print-fancy-glyphs)
(lsp-symbol-outline--print-symbol-icon-gui item)
(lsp-symbol-outline--print-symbol-icon-term item))
;; button
(lsp-symbol-outline--print-button item buf)
;; args
(if (plist-get item :args)
(let ((arg-string
(ignore-errors
(replace-regexp-in-string
"\n" ""
(plist-get item :args)))))
(if arg-string
(progn
(insert arg-string)))))
(insert "\n")))
(defun lsp-symbol-outline--print-outline-sorted-clike-generic (list-sorted)
"Print a symbol outline grouped by symbol kind. Takes list of symbol
plists LIST-SORTED and prints the symbol icon, the kind name, symbol button and
any argument information.
LIST-SORTED is filtered to yield only distinct :kind values. LIST-SORTED is then
iterated over and filtered by each distinct :kind value. Symbols of that kind
are printed with no regard to indentation or hierarchy.
For use with langs that resemeble C/Java in syntax."
(let ((contains-types (-distinct (-map
(lambda (x) (plist-get x :kind))
list-sorted))))
(dolist (sym-kind contains-types)
(let ((same-kind-list
(-filter (lambda (i) (equal (plist-get i :kind) sym-kind))
list-sorted)))
;; icon
(insert " ")
(if (and window-system
lsp-symbol-outline-print-fancy-glyphs)
(lsp-symbol-outline--print-symbol-icon-gui (car same-kind-list))
(lsp-symbol-outline--print-symbol-icon-term (car same-kind-list)))
;; kind name
(lsp-symbol-outline--insert-sym-kind-name same-kind-list)
(dolist (item same-kind-list)
(plist-put item :line (string-to-number (format-mode-line "%l")))
;; spaces
(insert (make-string 5 32))
;; button
(lsp-symbol-outline--print-button item
lsp-symbol-outline-src-buffer)
;; args
(if (plist-get item :args)
(let ((arg-string
(ignore-errors
(replace-regexp-in-string
"\n" ""
(plist-get item :args)))))
(if arg-string
(progn
(insert arg-string)))))
(insert "\n"))
(insert " \t \n"))))
(save-excursion
(end-of-buffer)
(set-mark (point))
(backward-char 4)
(delete-region (point) (mark)))
(delete-trailing-whitespace))
(defun lsp-symbol-outline--find-closest-cell (list current-line)
"Find the closest line to line that main LSP sym outline function called
from. Return line number."
(cond ((ignore-errors
(+ 2 (progn
(-elem-index
(car
(last
(-filter
(lambda (x) (< (plist-get x :symbol-start-point)
current-line))
list)))
list)))))
(t 1)))
(defun lsp-symbol-outline--jump-paren ()
"Jump to the matching paren."
(cond ((eq 4 (car (syntax-after (point))))
(forward-sexp)
(forward-char -1))
((eq 5 (car (syntax-after (point))))
(forward-char 1)
(backward-sexp))))
(defun lsp-symbol-outline--set-arg-props-inv (beg end)
"Set text-properties to invisible."
(put-text-property beg end
'invisible t))
(defun lsp-symbol-outline--set-info-inv ()
"Search buffer for parens and set all found positions to invisible
text property."
(save-excursion
(goto-char (point-min))
(while (re-search-forward "(.*)" nil 'noerror 1)
(lsp-symbol-outline--set-arg-props-inv
(point)
(save-excursion
(forward-char -1)
(lsp-symbol-outline--jump-paren)
(point))))))
(defun lsp-symbol-outline--cycle-arg-visibility-clike-generic ()
"If `lsp-symbol-outline-args-inv' is 0, set only argument types invisible.
If `lsp-symbol-outline-args-inv' is 1, set arguments invisible.
If `lsp-symbol-outline-args-inv' is 2, set all to visible.
For use with langs that have C/Java like syntax."
(cond
;; args invisible
((equal lsp-symbol-outline-args-inv 0)
(read-only-mode 0)
(lsp-symbol-outline--set-info-inv)
(setq-local lsp-symbol-outline-args-inv 2)
(read-only-mode 1))
;; all visible
((memq lsp-symbol-outline-args-inv '(1 2))
(read-only-mode 0)
(progn
(remove-list-of-text-properties (point-min) (point-max) '(invisible))
;; (lsp-symbol-outline--set-info-vis)
;; (lsp-symbol-outline--finalize-arg-props-clike-generic)
)
(setq-local lsp-symbol-outline-args-inv 0)
(read-only-mode 1))))
(defun lsp-symbol-outline--find-end-of-arg-type-colon-generic ()
"Parse current line to find the end range of type information of current
arg. For use with langs that delimit arg types with colons."
(cond
((looking-at " fn")
(search-forward "(")
(backward-char 1)
(goto-char (plist-get (sp-get-sexp) :end)))
((looking-at " {")
(search-forward "{")
(backward-char 1)
(lsp-symbol-outline--jump-paren)
(if
(looking-at ".|")
(progn
(forward-char 2)
(cond ((lsp-symbol-outline--jump-paren)
(point))
((search-forward "," (line-end-position) t)
(1- (point)))
((search-forward ")" (line-end-position) t)
(1- (point)))))
(1+ (point))))
(t (progn
(backward-char 1)
(if (re-search-forward ": .+?," (line-end-position) t 1)
(1- (point))
(re-search-forward ": .+?$" (line-end-position) t 1)
(- (point) 1))))))
(defun lsp-symbol-outline--finalize-arg-props-colon-generic ()
"Parse buffer for colon char and find argument types. Position of types
is passed to `lsp-symbol-outline--set-arg-type-props' which sets different text
properties on argument type information.
Regex parsing is used to set invisible properties to toggle hiding type
information. For use with langs that delimit arg types with colons."
(save-excursion
(goto-char (point-min))
(while (re-search-forward ":" nil 'noerror 1)
(let ((ref (match-string-no-properties 1)))
(lsp-symbol-outline--set-arg-type-props
(1- (point))
(lsp-symbol-outline--find-end-of-arg-type-colon-generic))))))
(defun lsp-symbol-outline--set-arg-types-inv-colon-generic ()
"Parse buffer for colon char and find argument types. Call
`lsp-symbol-outline--set-arg-props-inv' on found positions to set argument
information invisible by setting text properties. For use with langs that delimit arg types with colons."
(save-excursion
(goto-char (point-min))
(while (re-search-forward ":" nil 'noerror 1)
(let ((ref (match-string-no-properties 1)))
(lsp-symbol-outline--set-arg-props-inv
(1- (point))
(lsp-symbol-outline--find-end-of-arg-type-colon-generic))))))
(defun lsp-symbol-outline--cycle-arg-visibility-colon-generic ()
"If `lsp-symbol-outline-args-inv' is 0, set only argument types invisible.
If `lsp-symbol-outline-args-inv' is 1, set arguments invisible.
If `lsp-symbol-outline-args-inv' is 2, set all to visible.
For use with langs that delimit arg types with colons."
(cond
;; arg types invisible
((equal lsp-symbol-outline-args-inv 0)
(read-only-mode 0)
(lsp-symbol-outline--set-arg-types-inv-colon-generic)
(setq-local lsp-symbol-outline-args-inv 1)
(read-only-mode 1))
;; args invisible
((equal lsp-symbol-outline-args-inv 1)
(read-only-mode 0)
(lsp-symbol-outline--set-info-inv)
(setq-local lsp-symbol-outline-args-inv 2)
(read-only-mode 1))
;; all visible
((equal lsp-symbol-outline-args-inv 2)
(read-only-mode 0)
(progn
(remove-list-of-text-properties (point-min) (point-max) '(invisible))
;; (lsp-symbol-outline--set-info-vis)
(lsp-symbol-outline--finalize-arg-props-colon-generic))
(setq-local lsp-symbol-outline-args-inv 0)
(read-only-mode 1))))
(defun lsp-symbol-outline--sort-by-category (list)
"Sort list of symbol plists by their :kind property.
Returns list of plists."
(--sort (< (plist-get it :kind) (plist-get other :kind)) list))
(defun lsp-symbol-outline--save-arg-level (arg-level)
"Set level of visibility for arguments and their types."
(cond
((eq arg-level 0) 2)
((eq arg-level 1) 0)
((eq arg-level 2) 1)))
(defun lsp-symbol-outline-find-plist-with-index (orig-index) ;; ??? slow? FIXME
"Find plist that matches `original-index' from `lsp-outline-list'."
(car (-filter (lambda (x) (eq (plist-get x :index) orig-index))
lsp-outline-list)))
(defun lsp-symbol-outline-get-current-line-plist-index () ;; ??? slow? FIXME
"Find the plist on line point is on from `lsp-outline-list'."
(let* ((current-line (string-to-number (format-mode-line "%l")))
(p (-filter (lambda (x) (eq (plist-get x :line) current-line))
lsp-outline-list)))
(plist-get (car p) :index)))
(defun lsp-symbol-outline-print-sorted ()
"Save current line data. Sort list of symbols by category. Call the
function contained by `lsp-symbol-outline-print-sorted-func' to print an
outline grouped by symbol kind. Call function that sets argument text properties.
Set buffer local variable `lsp-symbol-outline-is-sorted' to t. Find saved line
data."
(let ((index (lsp-symbol-outline-get-current-line-plist-index))
(list-sorted (lsp-symbol-outline--sort-by-category lsp-outline-list))
(inhibit-read-only t)
(inhibit-message t))
(erase-buffer)
(funcall lsp-symbol-outline-print-sorted-func list-sorted)
(goto-char (point-min))
(forward-whitespace 2)
(funcall lsp-symbol-outline-args-props-func)
;;save arg visibility level between calling sorted/sequential
(setq-local lsp-symbol-outline-args-inv
(lsp-symbol-outline--save-arg-level
lsp-symbol-outline-args-inv))
(if (not (eq 2 lsp-symbol-outline-args-inv))
(funcall lsp-symbol-outline-visibility-cycling-func)
(setq-local lsp-symbol-outline-args-inv 0))
(setq-local lsp-symbol-outline-is-sorted t)
(if index
(goto-line (plist-get (lsp-symbol-outline-find-plist-with-index index) ;; ??? goto-line speed?
:line)))
(beginning-of-line-text)
(with-current-buffer lsp-symbol-outline-src-buffer
(lsp-symbol-outline--delete-idle-timer-overlays)
(dolist (i buffer-orig-lsp-outline-list)
(lsp-symbol-outline-add-idle-timer-highlight-props
(plist-get i :symbol-start-point)
(plist-get i :symbol-end-point)
(plist-get i :line)))
(lsp-symbol-outline-highlight-symbol))))
(defun lsp-symbol-outline-print-sequential ()
"Reverse the grouping of symbols by kind and print a symbol outline in
order of symbol appearance in source document."
(let ((index (lsp-symbol-outline-get-current-line-plist-index))
(inhibit-read-only t)
;; (inhibit-message t)
)
(erase-buffer)
(funcall lsp-symbol-outline-print-func
lsp-outline-list
lsp-symbol-outline-src-buffer)
(funcall lsp-symbol-outline-args-props-func)
;;save arg visibility level between calling sorted/sequential
(setq-local lsp-symbol-outline-args-inv
(lsp-symbol-outline--save-arg-level
lsp-symbol-outline-args-inv))
(if (not (eq 2 lsp-symbol-outline-args-inv))
(funcall lsp-symbol-outline-visibility-cycling-func)
(setq-local lsp-symbol-outline-args-inv 0))
(setq-local lsp-symbol-outline-is-sorted nil)
(lsp-symbol-outline-go-top)
(if index
(goto-line (plist-get (lsp-symbol-outline-find-plist-with-index index) ;; ??? goto-line speed?
:line)))
(beginning-of-line-text)
(forward-to-word 1)
(with-current-buffer lsp-symbol-outline-src-buffer
(lsp-symbol-outline--delete-idle-timer-overlays)
(dolist (i buffer-orig-lsp-outline-list)
(lsp-symbol-outline-add-idle-timer-highlight-props
(plist-get i :symbol-start-point)
(plist-get i :symbol-end-point)
(plist-get i :line)))
(lsp-symbol-outline-highlight-symbol))))
;;; The below function is stolen from misc-cmds.el as emacswiki packages
;;; are no longer on melpa TODO implement own
;;;###autoload
(defun goto-longest-line (beg end)
"Go to the first of the longest lines in the region or buffer.
If the region is active, it is checked.
If not, the buffer (or its restriction) is checked.
Returns a list of three elements:
(LINE LINE-LENGTH OTHER-LINES LINES-CHECKED)
LINE is the first of the longest lines measured.
LINE-LENGTH is the length of LINE.
OTHER-LINES is a list of other lines checked that are as long as LINE.
LINES-CHECKED is the number of lines measured.
Interactively, a message displays this information.
If there is only one line in the active region, then the region is
deactivated after this command, and the message mentions only LINE and
LINE-LENGTH.
If this command is repeated, it checks for the longest line after the
cursor. That is *not* necessarily the longest line other than the
current line. That longest line could be before or after the current
line.
To search only from the current line forward, not throughout the
buffer, you can use `C-SPC' to set the mark, then use this
\(repeatedly)."
(interactive
(if (or (not mark-active) (not (< (region-beginning) (region-end))))
(list (point-min) (point-max))
(if (< (point) (mark))
(list (point) (mark))
(list (mark) (point)))))
(when (and (not mark-active) (= beg end)) (error "The buffer is empty"))
(when (and mark-active (> (point) (mark))) (exchange-point-and-mark))
(when (< end beg) (setq end (prog1 beg (setq beg end))))
(when (eq this-command last-command)
(forward-line 1) (setq beg (point)))
(goto-char beg)
(when (eobp) (error "End of buffer"))
(cond ((<= end (save-excursion (goto-char beg) (forward-line 1) (point)))
(let ((inhibit-field-text-motion t)) (beginning-of-line))
(when (and (> emacs-major-version 21) (require 'hl-line nil t))