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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
|
;;; lsp-treemacs.el --- LSP treemacs -*- lexical-binding: t; -*-
;; Copyright (C) 2019-2024 emacs-lsp maintainers
;; Author: Ivan Yonchovski
;; Keywords: languages
;; Package-Requires: ((emacs "27.1") (dash "2.18.0") (f "0.20.0") (ht "2.0") (treemacs "2.5") (lsp-mode "6.0"))
;; Homepage: https://github.com/emacs-lsp/lsp-treemacs
;; Version: 0.5
;; 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:
;; `lsp-mode' and `treemacs' integration.
;;; Code:
(require 'treemacs)
(require 'treemacs-icons)
(require 'treemacs-treelib)
(require 'lsp-treemacs-generic)
(require 'lsp-treemacs-themes)
(require 'lsp-mode)
(defconst lsp-treemacs-deps-buffer-name "*Java Dependency List*")
(defconst lsp-treemacs-symbols-buffer-name "*LSP Symbols List*")
(defconst lsp-treemacs-errors-buffer-name "*LSP Error List*")
(defgroup lsp-treemacs nil
"Language Server Protocol client."
:group 'tools
:tag "Language Server")
(defvar lsp-treemacs-deps-position-params
`((side . ,treemacs-position)
(slot . 1)
(window-width . ,treemacs-width))
"The params which will be used by
`display-buffer-in-side-window' in
`lsp-treemacs-java-deps-list'.")
(defvar lsp-treemacs-symbols-position-params
`((side . ,treemacs-position)
(slot . 2)
(window-width . ,treemacs-width))
"The params which will be used by
`display-buffer-in-side-window' in `lsp-treemacs-symbols'.")
(defvar lsp-treemacs-errors-position-params
`((side . bottom))
"The params which will be used by
`display-buffer-in-side-window' in
`lsp-treemacs-errors-list'.")
(defface lsp-treemacs-project-root-error
'((t :inherit font-lock-keyword-face))
"Face used for highlighting symbols being read."
:group 'lsp-faces)
(defface lsp-treemacs-project-root-info
'((t :inherit font-lock-keyword-face))
"Face used for highlighting symbols being read."
:group 'lsp-faces)
(defface lsp-treemacs-project-root-warn
'((t :inherit font-lock-keyword-face))
"Face used for highlighting symbols being read."
:group 'lsp-faces)
(defface lsp-treemacs-file-error
'((t :inherit error))
"Face used for highlighting symbols being read."
:group 'lsp-faces)
(defface lsp-treemacs-file-info
'((t :inherit success))
"Face used for highlighting symbols being read."
:group 'lsp-faces)
(defface lsp-treemacs-file-hint
'((t :inherit success))
"Face used for highlighting symbols being read."
:group 'lsp-faces)
(defface lsp-treemacs-file-warn
'((t :inherit warning))
"Face used for highlighting symbols being read."
:group 'lsp-faces)
(defcustom lsp-treemacs-face-map
'((1 . lsp-treemacs-project-root-error)
(2 . lsp-treemacs-project-root-warn)
(3 . lsp-treemacs-project-root-info))
"Alist diagnostics to face."
:type 'alist)
(defcustom lsp-treemacs-file-face-map
'((1 . lsp-treemacs-file-error)
(2 . lsp-treemacs-file-warn)
(3 . lsp-treemacs-file-info)
(4 . lsp-treemacs-file-hint))
"Alist diagnostics to face."
:type 'alist)
(defcustom lsp-treemacs-error-list-severity 3
"Severity level for `lsp-treemacs-error-list-mode'. 1 (highest) to 3 (lowest)"
:type 'number)
(defcustom lsp-treemacs-error-list-current-project-only nil
"List the error list of the current project only if available.
Fallback to list all workspaces if no project root is found."
:type 'boolean
:group 'lsp-treemacs)
(defcustom lsp-treemacs-error-list-expand-depth nil
"Automatic expansion depth for `lsp-treemacs-error-list'."
:type 'number
:group 'lsp-treemacs)
(defcustom lsp-treemacs-call-hierarchy-expand-depth nil
"Automatic expansion depth for `lsp-treemacs-call-hierarchy'."
:type 'number
:group 'lsp-treemacs)
(defcustom lsp-treemacs-type-hierarchy-expand-depth nil
"Automatic expansion depth for `lsp-treemacs-type-hierarchy'."
:type 'number
:group 'lsp-treemacs)
(defcustom lsp-treemacs-java-deps-list-expand-depth nil
"Automatic expansion depth for `lsp-treemacs-java-deps-list'."
:type 'number
:group 'lsp-treemacs)
(defun lsp-treemacs--open-file-in-mru (file)
(select-window (get-mru-window (selected-frame) nil :not-selected))
(find-file file))
(defun lsp-treemacs-symbol-kind->icon (kind)
(cl-case kind
(1 'document)
(2 'namespace)
(3 'namespace)
(4 'namespace)
(5 'class)
(6 'method)
(7 'property)
(8 'field)
(9 'method)
(10 'enumerator)
(11 'interface)
(12 'method )
(13 'localvariable)
(14 'constant)
(15 'string)
(16 'numeric)
(17 'boolean-data)
(18 'boolean-data)
(19 'namespace)
(20 'indexer)
(21 'boolean-data)
(22 'enumitem)
(23 'structure)
(24 'event)
(25 'operator)
(26 'template)
(t 'misc)))
(defun lsp-treemacs-get-icon (icon-name)
"Get the treemacs ICON using current theme."
(treemacs-get-icon-value icon-name nil lsp-treemacs-theme))
(defun lsp-treemacs-symbol-icon (kind)
"Get icon for `kind'."
(treemacs-get-icon-value (lsp-treemacs-symbol-kind->icon kind)
nil
lsp-treemacs-theme))
(defun lsp-treemacs--symbol-icon (symbol expanded)
"Get the symbol for the the kind."
(-let [(&DocumentSymbol :kind :children?) symbol]
(concat
(if (seq-empty-p children?)
" "
(if expanded "▾ " "▸ "))
(lsp-treemacs-symbol-icon kind))))
(defvar-local lsp-treemacs--symbols nil)
(defvar-local lsp-treemacs--symbols-tick nil)
(defvar-local lsp-treemacs--empty nil)
(defvar-local lsp-treemacs--symbols-state-string nil)
(defvar-local lsp-treemacs--symbols-state-locals nil)
(defvar lsp-treemacs--symbols-current-buffer nil)
(defvar lsp-treemacs--symbols-last-buffer nil)
(defvar lsp-treemacs--symbols-timer nil)
(defun lsp-treemacs-sort-by-position (left right)
(-let (((&plist :position left-position) left)
((&plist :position right-position) right))
(and left-position right-position (lsp--position-compare left-position right-position))))
(defun lsp-treemacs-sort-by-kind (left right)
(-let (((&plist :kind left-kind) left)
((&plist :kind right-kind) right))
(and left-kind right-kind (> left-kind right-kind))))
(defun lsp-treemacs-sort-by-name (left right)
(-let* (((&plist :label left-name) left)
((&plist :label right-name) right))
(string> (downcase right-name) (downcase left-name))))
(defcustom lsp-treemacs-symbols-sort-functions '(lsp-treemacs-sort-by-position)
"Sort functions."
:type '(repeat
(choice
(const :tag "Name" lsp-treemacs-sort-by-name)
(const :tag "Kind" lsp-treemacs-sort-by-kind)
(const :tag "Position" lsp-treemacs-sort-by-position))))
(defcustom lsp-treemacs-detailed-outline t
"Whether `lsp-treemacs-symbols' should include signatures.
For this to work, the language server must support
DocumentSymbols."
:group 'lsp-treemacs
:type 'boolean)
(defcustom lsp-treemacs-after-jump-hook
(list (lambda () (run-hooks 'xref-after-jump-hook)))
"List of functions to call after jumping to a symbol.
When pressing RET on a symbol in the `lsp-treemacs-symbols' view,
this hook will be run after having jumped to the target."
:group 'lsp-treemacs
:type '(list function))
(defmacro lsp-treemacs-define-action (name keys &rest body)
(declare (doc-string 3) (indent 2) (debug (&define name sexp lambda-doc def-body)))
(let* ((docstring (car body)))
(when (stringp docstring)
(pop body))
`(defun ,name (&rest args)
,(if (stringp docstring) docstring (format "Code action %s." name))
(interactive)
(ignore args)
(-if-let (node (treemacs-node-at-point))
(-let [,(cons '&plist keys) (button-get node :item)]
,@body)
(treemacs-pulse-on-failure "No node at point")))))
(lsp-treemacs-define-action lsp-treemacs-symbols-goto-symbol (:location)
"Goto the symbol node at `point'."
(pop-to-buffer lsp-treemacs--symbols-last-buffer)
(goto-char (lsp--position-to-point location))
(run-hooks 'lsp-treemacs-after-jump-hook))
(lsp-treemacs-define-action lsp-treemacs-go-to (:uri :position)
"Goto POSITION in URL."
(lsp-treemacs--open-file-in-mru (lsp--uri-to-path uri))
(goto-char (lsp--position-to-point position))
(run-hooks 'xref-after-jump-hook))
(defun lsp-treemacs--symbols->tree (items parent-key)
"Convert ITEMS and PARENT-KEY to a treemacs tree."
(-sort (lambda (left right)
(-first (lambda (fn)
(funcall fn left right))
lsp-treemacs-symbols-sort-functions))
(if (-some->> items lsp-seq-first lsp-symbol-information?)
(-let [(current rest) (-separate (-lambda ((&SymbolInformation :container-name?))
(string= container-name? parent-key))
(append items nil))]
(seq-map (-lambda ((&SymbolInformation :name :container-name? :kind
:location (location &as &Location :range (&Range :start start-range))))
(when (string= parent-key container-name?)
`(:label ,name
:key ,name
:icon ,(lsp-treemacs-symbol-kind->icon kind)
,@(when (-first (-lambda ((&SymbolInformation :container-name? parent))
(string= name parent))
rest)
(list :children (lsp-treemacs--symbols->tree rest name)))
:kind ,kind
:location ,start-range
:ret-action lsp-treemacs-symbols-goto-symbol)))
current))
(seq-map
(-lambda ((sym &as &DocumentSymbol :name :kind :selection-range
(&Range :start start-range) :children?))
`(:label ,(lsp-render-symbol sym lsp-treemacs-detailed-outline)
:key ,name
:icon ,(lsp-treemacs-symbol-kind->icon kind)
:kind ,kind
:location ,start-range
,@(unless (seq-empty-p children?)
(list :children (lsp-treemacs--symbols->tree children? name)))
:ret-action lsp-treemacs-symbols-goto-symbol))
items))))
(defun lsp-treemacs--update-symbols ()
"After diagnostics handler."
(setq-local header-line-format
(unless lsp-treemacs--symbols
(propertize "No symbol information." 'face 'shadow)))
(lsp-treemacs-render
(lsp-treemacs--symbols->tree
lsp-treemacs--symbols
nil)
" LSP Symbols "
(and lsp-treemacs--symbols (> 30 (length lsp-treemacs--symbols)))
lsp-treemacs-symbols-buffer-name ))
(defun lsp-treemacs--update ()
(unless (eq (current-buffer) (get-buffer "*scratch*"))
(when (with-current-buffer lsp-treemacs-symbols-buffer-name (get-buffer-window))
(if (lsp--find-workspaces-for "textDocument/documentSymbol")
(when (or (not lsp-treemacs--symbols-tick)
(not (eq lsp-treemacs--symbols-tick (buffer-modified-tick)))
(not (eq (current-buffer) lsp-treemacs--symbols-current-buffer)))
(lsp-request-async "textDocument/documentSymbol"
(lsp-make-document-symbol-params :text-document (lsp--text-document-identifier))
(lambda (document-symbols)
(save-excursion
(with-current-buffer lsp-treemacs-symbols-buffer-name
(setq-local lsp-treemacs--symbols document-symbols)
(lsp-treemacs--update-symbols))))
:mode 'alive)
(setq-local lsp-treemacs--symbols-tick (buffer-modified-tick))
(setq lsp-treemacs--symbols-last-buffer (current-buffer)))
(when (buffer-file-name)
(with-current-buffer lsp-treemacs-symbols-buffer-name
(setq-local lsp-treemacs--symbols nil)
(lsp-treemacs--update-symbols)))))
(setq lsp-treemacs--symbols-current-buffer (current-buffer))))
(defun lsp-treemacs-goto-symbol (&rest _)
"Goto the symbol at point."
(interactive)
(if-let ((symbol-data (-some-> (treemacs-node-at-point)
(button-get :symbol))))
(with-current-buffer lsp-treemacs--symbols-last-buffer
(let ((p (lsp--position-to-point (or (-some->> symbol-data
lsp:document-symbol-selection-range
lsp:range-start)
(-some->> symbol-data
lsp:symbol-information-location
lsp:location-range
lsp:range-start)
(error "Unable to go to location")))))
(pop-to-buffer lsp-treemacs--symbols-last-buffer)
(goto-char p)
(run-hooks 'xref-after-jump-hook)))
(user-error "No symbol under point.")))
(with-eval-after-load 'winum
(when (boundp 'winum-ignored-buffers)
(add-to-list 'winum-ignored-buffers lsp-treemacs-symbols-buffer-name)
(add-to-list 'winum-ignored-buffers lsp-treemacs-errors-buffer-name)
(add-to-list 'winum-ignored-buffers lsp-treemacs-deps-buffer-name)))
(defun lsp-treemacs--expand (root-key depth)
(-when-let (root (treemacs-dom-node->position (treemacs-find-in-dom root-key)))
(treemacs-save-position
(lsp-treemacs--expand-recursively root depth))))
(defun lsp-treemacs--kill-symbols-buffer ()
(and lsp-treemacs--symbols-timer (cancel-timer lsp-treemacs--symbols-timer)))
(defcustom lsp-treemacs-symbols-space-between-root-nodes nil
"Whether there should be empty lines between symbols.
If this is set to t, top-level symbols in `lsp-treemacs-symbols'
will be rendered an empty line between them."
:group 'lsp-treemacs
:type 'boolean)
;;;###autoload
(defun lsp-treemacs-symbols ()
"Show symbols view."
(interactive)
(let ((original-buffer (current-buffer)))
(if-let (buf (get-buffer lsp-treemacs-symbols-buffer-name))
(select-window (display-buffer-in-side-window buf lsp-treemacs-symbols-position-params))
(let* ((buf (get-buffer-create lsp-treemacs-symbols-buffer-name))
(window (display-buffer-in-side-window buf lsp-treemacs-symbols-position-params)))
(select-window window)
(set-window-dedicated-p window t)
(unless lsp-treemacs--symbols-timer
(setq lsp-treemacs--symbols-timer (run-with-idle-timer 1 t #'lsp-treemacs--update)))
(add-hook 'kill-buffer-hook 'lsp-treemacs--kill-symbols-buffer nil t)))
(with-current-buffer original-buffer (lsp-treemacs--update))))
(defun lsp-treemacs--expand-recursively (root depth)
(when (if (booleanp depth) depth (not (zerop depth)))
(save-excursion
(-map
(lambda (btn)
(unless (treemacs-is-node-expanded? btn)
(goto-char (marker-position btn))
(funcall (alist-get (treemacs-button-get btn :state) treemacs-TAB-actions-config)))
(lsp-treemacs--expand-recursively btn (if (booleanp depth) depth (1- depth))))
(treemacs-collect-child-nodes root)))))
;; deps
(eval-and-compile
(lsp-interface
(java:Node (:projectUri :rootPath :path :kind :name :uri :entryKind))))
(defmacro lsp-treemacs-deps-with-jdtls (&rest body)
"Helper macro for invoking BODY against WORKSPACE context."
(declare (debug (form body))
(indent 0))
`(if-let (lsp--cur-workspace (lsp-find-workspace 'jdtls nil))
(progn ,@body)
(user-error "Java Language Server is not started.")))
(defun lsp-treemacs-deps--icon (dep)
"Get the symbol for the the kind."
(-let (((&java:Node :uri :kind :entry-kind) dep))
(if (or (= kind 8)
(= kind 6))
(f-ext uri)
(cond
((eq entry-kind 2) 'jar)
((eq kind 5) 'package)
((eq kind 7) 'folder)
((eq kind 4) 'packagefolder)
((eq kind 2) 'java-project)
((eq entry-kind 3) 'packagefolder)
((eq entry-kind 5) 'library)))))
(defun lsp-treemacs-deps--get-children (dep)
(lsp-treemacs-deps-with-jdtls
(-let* (((&java:Node :project-uri :root-path :path :kind :name :uri) dep)
(project-uri (if (eq kind 2) uri project-uri)))
(unless (or (= kind 6)
(= kind 8))
(->> (lsp-send-execute-command
"java.getPackageData"
(vector (ht ("kind" kind)
("path" (unless (eq kind 2)
(if (= 5 kind)
name
path)))
("rootPath" (unless (eq kind 2)
(or root-path path)))
("projectUri" project-uri))))
(-mapcat (lambda (inner-dep)
(lsp-put inner-dep :projectUri project-uri)
(when (= kind 4)
(lsp-put inner-dep :rootPath path))
(if (eq (lsp-get inner-dep :entryKind) 3)
(lsp-treemacs-deps--get-children inner-dep)
(list inner-dep)))))))))
(defun lsp-treemacs-deps--java-file? (dep)
(-let [(&java:Node :kind :entry-kind) dep]
(and (eq kind 6)
(or (eq entry-kind 1)
(eq entry-kind 2)))))
(defun lsp-treemacs-deps--root-folders ()
(lsp-treemacs-deps-with-jdtls
(-mapcat (lambda (root-path)
(let ((project-uri (lsp--path-to-uri root-path)))
(->> project-uri
(lsp-send-execute-command "java.project.list")
(--map (--doto it (lsp-put it :projectUri project-uri))))))
(lsp-session-folders (lsp-session)))))
(lsp-defun lsp-treemacs-deps--process-dep ((item &as &java:Node :name :uri :path :project-uri))
`(:label ,name
:icon ,(lsp-treemacs-deps--icon item)
:key ,(list name uri path)
:ret-action ,(lambda (&rest _)
(lsp-treemacs--open-file-in-mru
(or (when uri (lsp--uri-to-path uri))
(when (f-exists? path) path)
(concat (f-parent (lsp--uri-to-path project-uri))
path))))
,@(unless (lsp-treemacs-deps--java-file? item)
(list :children
(lambda (_)
(-map #'lsp-treemacs-deps--process-dep
(lsp-treemacs-deps--get-children item)))))))
;;;###autoload
(defun lsp-treemacs-java-deps-list ()
"Display java dependencies."
(interactive)
(select-window
(display-buffer-in-side-window
(lsp-treemacs-render
(-map 'lsp-treemacs-deps--process-dep (lsp-treemacs-deps--root-folders))
"*Java Deps*" lsp-treemacs-java-deps-list-expand-depth)
lsp-treemacs-deps-position-params)))
(defun lsp-treemacs--deps-find-children-for-key (node key)
(->> node
treemacs-collect-child-nodes
(-first (lambda (child)
(goto-char (marker-position child))
(equal (treemacs-button-get child :key) key)))))
(defvar lsp-treemacs-deps-list-mode-map
(let ((m (make-sparse-keymap)))
(define-key m (kbd "r") #'lsp-treemacs-generic-refresh)
m)
"Keymap for `lsp-treemacs-deps-list-mode'.")
(define-minor-mode lsp-treemacs-deps-list-mode "LSP Treemacs mode for listing dependencies."
:keymap lsp-treemacs-deps-list-mode-map
:group 'lsp-treemacs)
;;;###autoload
(defun lsp-treemacs-java-deps-follow ()
(interactive)
(lsp-treemacs-deps-with-jdtls
(let ((paths (lsp-send-execute-command "java.resolvePath"
(lsp--buffer-uri))))
(select-window
(with-current-buffer lsp-treemacs-deps-buffer-name
(set-window-point
(get-buffer-window)
(marker-position
(-reduce-from
(-lambda (node (&java:Node :path :name :uri))
(unless (treemacs-is-node-expanded? node)
(save-excursion
(goto-char (marker-position node))
(funcall (alist-get (treemacs-button-get node :state) treemacs-TAB-actions-config))))
(or (lsp-treemacs--deps-find-children-for-key node (list name uri path))
(user-error "Unable to find %s in the dependency tree." (buffer-name))))
(treemacs-dom-node->position (treemacs-find-in-dom '(:custom LSP-Java-Dependency)))
paths)))
(get-buffer-window)))
(recenter nil))))
;; treemacs synchronization
(defun lsp-treemacs--on-folder-remove (project)
(lsp-workspace-folders-remove (treemacs-project->path project)))
(defun lsp-treemacs--on-folder-added (project)
(lsp-workspace-folders-add (treemacs-project->path project)))
(defun lsp-treemacs--treemacs->lsp ()
(let ((lsp-folders (lsp-session-folders (lsp-session)))
(treemacs-folders (->> (treemacs-current-workspace)
(treemacs-workspace->projects)
(-map #'treemacs-project->path)
(-map #'lsp-canonical-file-name))))
(seq-do #'lsp-workspace-folders-remove (-difference lsp-folders treemacs-folders))
(seq-do #'lsp-workspace-folders-add (-difference treemacs-folders lsp-folders))))
(defun lsp-treemacs--sync-folders (added removed)
(when-let (treemacs-workspace (treemacs-current-workspace))
(let ((treemacs-create-project-functions (remove #'lsp-treemacs--on-folder-added
treemacs-create-project-functions))
(treemacs-delete-project-functions (remove #'lsp-treemacs--on-folder-remove
treemacs-delete-project-functions))
(added (seq-map #'treemacs--canonical-path added))
(removed (seq-map #'treemacs--canonical-path removed)))
(dolist (added-path added)
(unless (treemacs-is-path added-path :in-workspace treemacs-workspace)
(let* ((name (file-name-nondirectory (directory-file-name added-path)))
(result (treemacs-do-add-project-to-workspace added-path name)))
(unless (eq 'success result)
(lsp-log "Failed to add path '%s' to treemacs' workspace: %s" added-path result)))))
(dolist (removed-path removed)
(when (treemacs-is-path removed-path :in-workspace treemacs-workspace)
(let ((result (treemacs-do-remove-project-from-workspace removed-path)))
(unless (eq 'success result)
(lsp-log "Failed to remove path '%s' from treemacs' workspace: %s" removed-path result))))))))
;;;###autoload
(define-minor-mode lsp-treemacs-sync-mode
"Global minor mode for synchronizing lsp-mode workspace folders and
treemacs projects."
:init-value nil
:group 'lsp-treemacs
:global t
(cond
(lsp-treemacs-sync-mode
(add-hook 'treemacs-create-project-functions #'lsp-treemacs--on-folder-added)
(add-hook 'treemacs-delete-project-functions #'lsp-treemacs--on-folder-remove)
(add-hook 'lsp-workspace-folders-changed-functions #'lsp-treemacs--sync-folders)
(add-hook 'treemacs-workspace-edit-hook #'lsp-treemacs--treemacs->lsp)
(add-hook 'treemacs-switch-workspace-hook #'lsp-treemacs--treemacs->lsp))
(t
(remove-hook 'treemacs-create-project-functions #'lsp-treemacs--on-folder-added)
(remove-hook 'treemacs-delete-project-functions #'lsp-treemacs--on-folder-remove)
(remove-hook 'lsp-workspace-folders-changed-functions #'lsp-treemacs--sync-folders)
(remove-hook 'treemacs-workspace-edit-hook #'lsp-treemacs--treemacs->lsp)
(remove-hook 'treemacs-switch-workspace-hook #'lsp-treemacs--treemacs->lsp))))
(defun lsp-treemacs--java-get-class-file (file)
(-let (((_ _package _class jar-file) (s-match "jdt://contents/.*\/\\(.*\\)\/\\(.*\\).class\\?=.*?/\\(.*?\\)=\/" file)))
(symbol-name (read (url-unhex-string jar-file )))))
(defun lsp-treemacs--get-xrefs-in-file (file-locs location-link)
(-let (((filename . links) file-locs))
(list :key filename
:label (format (propertize "%s %s" 'face 'default)
(propertize (f-filename filename) 'face 'default)
(propertize (format "%s references" (length links)) 'face 'lsp-details-face))
:icon (f-ext filename)
:children (lambda (_item)
(condition-case err
(let ((buf (find-buffer-visiting filename))
(fn (lambda ()
(seq-map (lambda (loc)
(lsp-treemacs--make-ref-item
(if location-link
(or (lsp:location-link-target-selection-range loc)
(lsp:location-link-target-range loc))
(lsp:location-range loc))
filename))
links))))
(if buf
(with-current-buffer buf
(funcall fn))
(when (file-readable-p filename)
(with-temp-buffer
(insert-file-contents-literally filename)
(funcall fn)))))
(error (ignore (lsp-warn
"Failed to process xref entry for filename '%s': %s"
filename
(error-message-string err))))
(file-error (ignore (lsp-warn
"Failed to process xref entry, file-error, '%s': %s"
filename
(error-message-string err))))))
:ret-action (lambda (&rest _)
(interactive)
(lsp-treemacs--open-file-in-mru filename)))))
(defun lsp-treemacs--extract-line (point)
"Return the line pointed to by POS (a Position object) in the current buffer."
(let* ((inhibit-field-text-motion t))
(save-excursion
(goto-char point)
(buffer-substring (line-beginning-position)
(line-end-position)))))
(lsp-defun lsp-treemacs--make-ref-item ((&Range :start (start &as &Position :line start-line :character start-character)
:end (&Position :character end-character))
filename)
"Return a xref-item from a RANGE in FILENAME."
(-let* ((start-point (lsp--position-to-point start))
(line (lsp-treemacs--extract-line start-point))
(len (length line)))
(add-face-text-property (max (min start-character len) 0)
(max (min end-character len) 0)
'highlight t line)
;; LINE is nil when FILENAME is not being current visited by any buffer.
(list :label (s-trim (format "%s %s"
line
(propertize(format "%s line"
(1+ start-line))
'face 'lsp-details-face)))
:key line
:point start-point
:icon-literal ""
:ret-action (lambda (&rest _)
(interactive)
(lsp-treemacs--open-file-in-mru filename)
(goto-char start-point)
(run-hooks 'xref-after-jump-hook)))))
(defun lsp-treemacs--handle-references (refs)
(->> refs
(-group-by (-lambda ((&Location :uri))
(let ((type (url-type (url-generic-parse-url (url-unhex-string uri)))))
(if (string= type "jdt")
(lsp-treemacs--java-get-class-file uri)
(lsp-workspace-root (lsp--uri-to-path uri))))))
(-map (-lambda ((path . rst))
(list :key path
:label (format
"%s %s"
(f-filename path)
(propertize (format "%s references" (length rst)) 'face 'lsp-details-face))
:icon (if (f-file? path)
(f-ext path)
'dir-open)
:children (lambda (_item)
(-map (lambda (it)
(lsp-treemacs--get-xrefs-in-file it nil))
(-group-by (-lambda ((&Location :uri))
(lsp--uri-to-path uri))
rst)))
:ret-action (lambda (&rest _)
(interactive)
(lsp-treemacs--open-file-in-mru path)))))))
(defun lsp-treemacs--set-mode-line-format (buffer title)
"Set the mode line format of BUFFER to TITLE.
This function sets the `mode-name' or `mode-line-format'
depending on if a custom mode line is detected."
(with-current-buffer buffer
(cond ((or (fboundp 'spaceline-install)
(memq 'moody-mode-line-buffer-identification
(default-value 'mode-line-format))
(and (fboundp 'doom-modeline)
(fboundp 'doom-modeline-def-modeline)))
(setq mode-name title))
(t
(setq mode-line-format title)))))
(defun lsp-treemacs--do-search (method params title prefix-args)
(let ((search-buffer (get-buffer-create "*LSP Lookup*"))
(window (display-buffer (get-buffer-create "*LSP Lookup*"))))
(lsp-request-async
method
params
(lambda (refs)
(lsp-treemacs--set-mode-line-format search-buffer " Rendering results... ")
(lsp-with-cached-filetrue-name
(let ((lsp-file-truename-cache (ht)))
(lsp-treemacs-render (lsp-treemacs--handle-references refs)
(format title (length refs))
(and prefix-args (not (equal prefix-args 0))))))
(lsp--info "Refresh completed!"))
:mode 'detached
:cancel-token :treemacs-lookup)
(unless (equal prefix-args 0)
(select-window window)
;; (set-window-dedicated-p window t)
)
(with-current-buffer search-buffer
(lsp-treemacs--set-mode-line-format search-buffer " Loading... ")
(setq-local lsp-treemacs-tree nil)
(lsp-treemacs-generic-refresh))))
;;;###autoload
(defun lsp-treemacs-references (arg)
"Show the references for the symbol at point.
With a prefix argument, select the new window and expand the tree of
references automatically."
(interactive "P")
(lsp-treemacs--do-search
"textDocument/references"
`(:context (:includeDeclaration t) ,@(lsp--text-document-position-params))
" Found %s references "
arg))
;;;###autoload
(defun lsp-treemacs-implementations (arg)
"Show the implementations for the symbol at point.
With a prefix argument, select the new window expand the tree of
implementations automatically."
(interactive "P")
(lsp-treemacs--do-search "textDocument/implementation"
(lsp--text-document-position-params)
" Found %s implementations "
arg))
;; Call hierarchy.
(lsp-defun lsp-treemacs--call-hierarchy-ret-action ((&CallHierarchyItem :uri :selection-range (&Range :start)))
"Build the ret action for a call hierarchy item using URI and START range."
(lsp-treemacs--open-file-in-mru (lsp--uri-to-path uri))
(goto-char (lsp--position-to-point start))
(run-hooks 'xref-after-jump-hook))
(defun lsp-treemacs--call-hierarchy-children (buffer method outgoing node callback)
(-let [item (plist-get node :item)]
(with-current-buffer buffer
(lsp-request-async
method
(list :item item)
(lambda (result)
(funcall
callback
(seq-map
(-lambda (node)
(-let* (((child-item &as &CallHierarchyItem :_name :kind :_detail? :_uri :selection-range (&Range :_start))
(if outgoing
(lsp:call-hierarchy-outgoing-call-to node)
(lsp:call-hierarchy-incoming-call-from node)))
(label (lsp-render-symbol child-item t)))
(list :label label
:key label
:icon (lsp-treemacs-symbol-kind->icon kind)
:children-async (-partial #'lsp-treemacs--call-hierarchy-children buffer method outgoing)
:ret-action (lambda (&rest _)
(interactive)
(lsp-treemacs--call-hierarchy-ret-action child-item))
:item child-item)))
result)))
:mode 'detached))))
;;;###autoload
(defun lsp-treemacs-call-hierarchy (outgoing)
"Show the incoming call hierarchy for the symbol at point.
With a prefix argument, show the outgoing call hierarchy."
(interactive "P")
(unless (lsp-feature? "textDocument/prepareCallHierarchy")
(user-error "Call hierarchy not supported by the current servers: %s"
(-map #'lsp--workspace-print (lsp-workspaces))))
(let ((buffer (current-buffer)))
(select-window
(display-buffer-in-side-window
(lsp-treemacs-render
(seq-map
(-lambda ((item &as &CallHierarchyItem :kind :name))
(list :label (lsp-render-symbol item t)
:key name
:icon (lsp-treemacs-symbol-kind->icon kind)
:children-async (-partial
#'lsp-treemacs--call-hierarchy-children
buffer
(if outgoing
"callHierarchy/outgoingCalls"
"callHierarchy/incomingCalls")
outgoing)
:ret-action (lambda (&rest _)
(interactive)
(lsp-treemacs--call-hierarchy-ret-action item))
:item item))
(lsp-request "textDocument/prepareCallHierarchy"
(lsp--text-document-position-params)))
(concat (if outgoing "Outgoing" "Incoming") " Call Hierarchy")
lsp-treemacs-call-hierarchy-expand-depth "*Call Hierarchy*" nil t) nil))))
;; Type hierarchy.
(defconst lsp-treemacs--hierarchy-sub 0)
(defconst lsp-treemacs--hierarchy-super 1)
(defconst lsp-treemacs--hierarchy-both 2)
(defun lsp-treemacs--type-hierarchy-render-nodes (result loaded? &optional direction)
(-map (-lambda ((it &as &TypeHierarchyItem :name :kind :uri :range (&Range :start) :children? :parents?))
`(:label ,(concat name (cond
((eq lsp-treemacs--hierarchy-sub direction) (propertize " ↓" 'face 'shadow))
((eq lsp-treemacs--hierarchy-super direction) (propertize " ↑" 'face 'shadow))))
:key ,name
:icon ,(lsp-treemacs-symbol-kind->icon kind)
,@(if loaded?
(list :children (append
(lsp-treemacs--type-hierarchy-render-nodes children? nil lsp-treemacs--hierarchy-sub)
(lsp-treemacs--type-hierarchy-render-nodes parents? nil lsp-treemacs--hierarchy-super)))
(list :children-async (-partial #'lsp-treemacs--type-hierarchy-render
it
direction)))
:ret-action ,(lambda (&rest _)
(interactive)
(lsp-treemacs--open-file-in-mru (lsp--uri-to-path uri))
(goto-char (lsp--position-to-point start))
(run-hooks 'xref-after-jump-hook))))
result))
(lsp-defun lsp-treemacs--type-hierarchy-render ((&TypeHierarchyItem :uri :range (&Range :start)) direction _ callback)
(lsp-request-async
"textDocument/typeHierarchy"
(lsp-make-type-hierarchy-params :text-document (lsp-make-text-document-item :uri uri)
:position start
:direction direction
:resolve 1)
(-lambda ((&TypeHierarchyItem :children? :parents?))
(funcall callback (lsp-treemacs--type-hierarchy-render-nodes
(if (eq direction lsp-treemacs--hierarchy-sub)
children?
parents?)
nil direction)))))
;;;###autoload
(defun lsp-treemacs-type-hierarchy (direction)
"Show the type hierarchy for the symbol at point.
With prefix 0 show sub-types.
With prefix 1 show super-types.
With prefix 2 show both."
(interactive "P")
(unless (lsp--find-workspaces-for "textDocument/typeHierarchy")
(user-error "Type hierarchy not supported by the current servers: %s"
(-map #'lsp--workspace-print (lsp-workspaces))))
(setq direction (or direction 0))
(let ((workspaces (lsp-workspaces))
(result (lsp-request
"textDocument/typeHierarchy"
(-> (lsp--text-document-position-params)
(plist-put :direction direction)
(plist-put :resolve 1)))))
(if result
(pop-to-buffer
(lsp-treemacs-render
(lsp-treemacs--type-hierarchy-render-nodes (vector result) t)
(concat (cond
((eq lsp-treemacs--hierarchy-sub direction) "Sub")
((eq lsp-treemacs--hierarchy-super direction) "Super")
((eq lsp-treemacs--hierarchy-both direction) "Sub/Super"))
" Type Hierarchy")
lsp-treemacs-type-hierarchy-expand-depth
"*lsp-treemacs-call-hierarchy*"))
(user-error "No class under point."))
(setq lsp--buffer-workspaces workspaces)))
;; errors
(defun lsp-treemacs--error-list-diags (_folder file &rest _)
(->> (lsp-diagnostics)
(gethash file)
(-filter #'lsp-treemacs--match-diagnostic-severity)
(-sort (-lambda ((&Diagnostic :range (&Range :start (&Position :character char-a
:line line-a)))
(&Diagnostic :range (&Range :start (&Position :character char-b
:line line-b))))
(if (= line-a line-b)
(< char-a char-b)
(< line-a line-b))))
(-map (-lambda ((diag &as &Diagnostic
:severity?
:message
:range (&Range :start (start &as &Position :line :character))
:source?))
(list :id message
:label (format (propertize "%s %s %s" 'face 'default)
(if source?
(propertize (format "[%s]" source?)
'face 'shadow)
"")
message
(propertize (format "(%s:%s)" line character)
'face 'lsp-details-face))
:icon-literal (lsp-treemacs--diagnostic-icon severity?)
:ret-action (lambda (&rest _)
(lsp-treemacs--open-file-in-mru file)
(->> start
lsp--position-to-point
goto-char)
(run-hooks 'xref-after-jump-hook))
:file file
:diag diag
:actions `(["Quick fix..." lsp-treemacs-list-errors-quick-fix ]
["Go to..." lsp-treemacs-list-errors-quick-fix ]))))))
(defun lsp-treemacs-errors--list-files (folder &rest _)
(->> (lsp-diagnostics)
(ht-keys)
(-keep
(lambda (file)
(when (and (lsp-f-ancestor-of? folder file)
(lsp-treemacs-errors--diags? (lsp-diagnostics-stats-for file)))
(list :id file
:label (format "%s %s %s"
(f-filename file)
(->> (append (lsp-diagnostics-stats-for file) ())
(-map-indexed
(lambda (index count)
(unless (zerop count)
(propertize
(number-to-string count)
'face (alist-get index lsp-treemacs-file-face-map)))))
(-filter #'identity)
(s-join "/"))
(propertize (f-dirname (f-relative file folder))
'face 'lsp-details-face))
:icon (if (f-directory? file) 'dir-closed (f-ext file))
:children (-partial #'lsp-treemacs--error-list-diags folder file)
:ret-action (lambda (&rest _)
(interactive)
(lsp-treemacs--open-file-in-mru file))))))))
(lsp-treemacs-define-action lsp-treemacs-quick-fix (:file :diag)
"Select the element under cursor."
(lsp-treemacs--open-file-in-mru file)
(-let [(&Diagnostic :range (&RangeToPoint :start)) diag]
(goto-char start)
(call-interactively #'lsp-execute-code-action)))
(defun lsp-treemacs-cycle-severity ()
"Cycle through the severity levels shown in the errors list"
(interactive)
(setq lsp-treemacs-error-list-severity
(if (= lsp-treemacs-error-list-severity lsp/diagnostic-severity-error)
lsp/diagnostic-severity-hint
(1- lsp-treemacs-error-list-severity)))
(lsp-treemacs-errors-list--refresh))
(defun lsp-treemacs-errors--diags? (diags)
(->> diags
(seq-map-indexed (lambda (count index)
(when (and (not (zerop count))
(<= index lsp-treemacs-error-list-severity))
count)))
(seq-some #'identity)))
(defun lsp-treemacs--build-error-list (folder)
(when-let ((diags (append (lsp-diagnostics-stats-for folder) ())))
(when (lsp-treemacs-errors--diags? diags)
(list :label (format
(propertize "%s %s %s" 'face 'default)
(f-filename folder)
(->> diags
(-map-indexed
(lambda (index count)
(when (and (not (zerop count))
(<= index lsp-treemacs-error-list-severity))
(propertize
(number-to-string count)
'face (alist-get index lsp-treemacs-file-face-map)))))
(-filter #'identity)
(s-join "/"))
(propertize (f-dirname folder)
'face 'lsp-lens-face))
:id folder
:icon 'root
:children (-partial #'lsp-treemacs-errors--list-files folder)
:ret-action (lambda (&rest _)
(interactive)
(lsp-treemacs--open-file-in-mru folder))))))
(defvar lsp-treemacs--current-workspaces nil)
(defun lsp-treemacs-errors-list--refresh ()
(lsp-treemacs-render
(if (and lsp-treemacs-error-list-current-project-only
lsp-treemacs--current-workspaces)
(->> lsp-treemacs--current-workspaces
(-map #'lsp-workspace-folders)
(-flatten)
(-keep #'lsp-treemacs--build-error-list))
(->> (lsp-session)
(lsp-session-folders)
(-keep #'lsp-treemacs--build-error-list)))
"Errors List"
lsp-treemacs-error-list-expand-depth
lsp-treemacs-errors-buffer-name
`(["Cycle Severity" lsp-treemacs-cycle-severity])))
;;;###autoload
(defun lsp-treemacs-errors-list ()
(interactive)
(setq lsp-treemacs--current-workspaces (lsp-workspaces))
(-if-let (buffer (get-buffer lsp-treemacs-errors-buffer-name))
(progn
(select-window (display-buffer-in-side-window buffer lsp-treemacs-errors-position-params))
(lsp-treemacs-errors-list--refresh))
(let* ((buffer (lsp-treemacs-errors-list--refresh))
(window (display-buffer-in-side-window buffer lsp-treemacs-errors-position-params)))
(select-window window)
(set-window-dedicated-p window t)
(lsp-treemacs-error-list-mode 1)
(add-hook 'lsp-diagnostics-updated-hook #'lsp-treemacs-errors-list--refresh)
(add-hook 'kill-buffer-hook 'lsp-treemacs--kill-buffer nil t)))
(let ((buf (lsp-treemacs-errors-list--refresh)))
(pop-to-buffer buf)
(with-current-buffer buf
(lsp-treemacs-error-list-mode 1))))
(defun lsp-treemacs--diagnostic-icon (severity)
"Get the icon for DIAGNOSTIC."
(cl-case severity
(1 treemacs-icon-error)
(2 treemacs-icon-warning)
(t treemacs-icon-info)))
(defun lsp-treemacs--kill-buffer ()
"Kill buffer hook."
(remove-hook 'lsp-diagnostics-updated-hook #'lsp-treemacs-errors-list--refresh))
(defvar lsp-treemacs-error-list-mode-map
(let ((m (make-sparse-keymap)))
(define-key m (kbd "x") #'lsp-treemacs-quick-fix)
(define-key m (kbd "=") #'lsp-treemacs-cycle-severity)
m)
"Keymap for `lsp-treemacs-error-list-mode'.")
(define-minor-mode lsp-treemacs-error-list-mode "LSP Treemacs mode for listing errors."
:keymap lsp-treemacs-error-list-mode-map
:group 'lsp-treemacs)
(defun lsp-treemacs--match-diagnostic-severity (diagnostic)
(<= (lsp:diagnostic-severity? diagnostic)
(prefix-numeric-value lsp-treemacs-error-list-severity)))
(provide 'lsp-treemacs)
;;; lsp-treemacs.el ends here
;; Local Variables:
;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
;; End:
|