|
| 1 | +;;; lsp-treemacs-generic.el --- treemacs wrapper -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2022 Ivan Yonchovski |
| 4 | + |
| 5 | +;; Author: Ivan Yonchovski <yyoncho@gmail.com> |
| 6 | +;; Keywords: data |
| 7 | + |
| 8 | +;; This program is free software; you can redistribute it and/or modify |
| 9 | +;; it under the terms of the GNU General Public License as published by |
| 10 | +;; the Free Software Foundation, either version 3 of the License, or |
| 11 | +;; (at your option) any later version. |
| 12 | + |
| 13 | +;; This program is distributed in the hope that it will be useful, |
| 14 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +;; GNU General Public License for more details. |
| 17 | + |
| 18 | +;; You should have received a copy of the GNU General Public License |
| 19 | +;; along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + |
| 21 | +;;; Commentary: |
| 22 | + |
| 23 | +;; |
| 24 | + |
| 25 | +;;; Code: |
| 26 | + |
| 27 | +(require 'treemacs) |
| 28 | +(require 'treemacs-treelib) |
| 29 | +(require 'lsp-treemacs-themes) |
| 30 | + |
| 31 | +(defvar-local lsp-treemacs-tree nil) |
| 32 | +(defvar-local lsp-treemacs--right-click-actions nil) |
| 33 | +(defvar-local lsp-treemacs-generic-filter nil) |
| 34 | + |
| 35 | +(defun lsp-treemacs-generic-refresh (&optional _cache) |
| 36 | + (condition-case _err |
| 37 | + (let ((inhibit-read-only t)) |
| 38 | + (treemacs-update-node '(lsp-treemacs-generic-root))) |
| 39 | + (error))) |
| 40 | + |
| 41 | +(defun lsp-treemacs-generic-update (tree) |
| 42 | + (setq lsp-treemacs-tree tree) |
| 43 | + (lsp-treemacs-generic-refresh)) |
| 44 | + |
| 45 | +(defun lsp-treemacs-generic-right-click (event) |
| 46 | + (interactive "e") |
| 47 | + (let* ((ec (event-start event)) |
| 48 | + (p1 (posn-point ec)) |
| 49 | + (w1 (posn-window ec))) |
| 50 | + (select-window w1) |
| 51 | + (goto-char p1) |
| 52 | + (hl-line-highlight) |
| 53 | + (run-with-idle-timer |
| 54 | + 0.001 nil |
| 55 | + (lambda () |
| 56 | + (-when-let* ((actions (if-let (node (treemacs-node-at-point)) |
| 57 | + (lsp-resolve-value (plist-get (button-get node :item) :actions)) |
| 58 | + lsp-treemacs--right-click-actions)) |
| 59 | + (menu (easy-menu-create-menu nil actions)) |
| 60 | + (choice (x-popup-menu event menu))) |
| 61 | + (when choice (call-interactively (lookup-key menu (apply 'vector choice)))) |
| 62 | + (hl-line-highlight)))))) |
| 63 | + |
| 64 | +(defvar lsp-treemacs-generic-map |
| 65 | + (-doto (make-sparse-keymap) |
| 66 | + (define-key [mouse-1] #'treemacs-TAB-action) |
| 67 | + (define-key [double-mouse-1] #'treemacs-RET-action) |
| 68 | + (define-key [mouse-3] #'lsp-treemacs-generic-right-click)) |
| 69 | + "Keymap for `lsp-treemacs-generic-mode'") |
| 70 | + |
| 71 | +(define-minor-mode lsp-treemacs-generic-mode "Treemacs generic mode." |
| 72 | + :keymap lsp-treemacs-generic-map) |
| 73 | + |
| 74 | +(defun lsp-treemacs--generic-icon (item expanded?) |
| 75 | + "Get the symbol for the the kind." |
| 76 | + (concat |
| 77 | + (if (or (plist-get item :children) |
| 78 | + (plist-get item :children-async)) |
| 79 | + (if expanded? "▾ " "▸ ") |
| 80 | + " ") |
| 81 | + (or (plist-get item :icon-literal) |
| 82 | + (if-let ((icon (plist-get item :icon))) |
| 83 | + (treemacs-get-icon-value |
| 84 | + icon |
| 85 | + nil |
| 86 | + lsp-treemacs-theme) |
| 87 | + " ")))) |
| 88 | + |
| 89 | +(defun lsp-treemacs-filter-if-needed (result) |
| 90 | + (if lsp-treemacs-generic-filter |
| 91 | + (funcall lsp-treemacs-generic-filter result) |
| 92 | + result)) |
| 93 | + |
| 94 | +(defun lsp-treemacs-perform-ret-action (&rest _) |
| 95 | + (interactive) |
| 96 | + (if-let (action (-> (treemacs-node-at-point) |
| 97 | + (button-get :item) |
| 98 | + (plist-get :ret-action))) |
| 99 | + (funcall-interactively action) |
| 100 | + (treemacs-pulse-on-failure "No ret action defined."))) |
| 101 | + |
| 102 | +(treemacs-define-expandable-node-type lsp-treemacs-generic-node |
| 103 | + :closed-icon (lsp-treemacs--generic-icon item nil) |
| 104 | + :open-icon (lsp-treemacs--generic-icon item t) |
| 105 | + :label (plist-get item :label) |
| 106 | + :key (plist-get item :key) |
| 107 | + :ret-action #'lsp-treemacs-perform-ret-action |
| 108 | + :children (-let (((item &as &plist :children :children-async) item)) |
| 109 | + (cond |
| 110 | + ((functionp children) (->> (funcall children item) |
| 111 | + (lsp-treemacs-filter-if-needed) |
| 112 | + (funcall callback))) |
| 113 | + (children-async |
| 114 | + (-let [buffer (current-buffer)] |
| 115 | + (funcall children-async |
| 116 | + item |
| 117 | + (lambda (result) |
| 118 | + (lsp-treemacs-wcb-unless-killed buffer |
| 119 | + (funcall callback (lsp-treemacs-filter-if-needed result))))))) |
| 120 | + (t (funcall callback (lsp-treemacs-filter-if-needed children))))) |
| 121 | + :child-type 'lsp-treemacs-generic-node |
| 122 | + :async? t) |
| 123 | + |
| 124 | +(treemacs-define-variadic-entry-node-type lsp-treemacs-generic-root |
| 125 | + :key 'lsp-treemacs-generic-root |
| 126 | + :children lsp-treemacs-tree |
| 127 | + :child-type 'lsp-treemacs-generic-node) |
| 128 | + |
| 129 | +(defun lsp-treemacs-render (tree title expand-depth |
| 130 | + &optional buffer-name right-click-actions _clear-cache?) |
| 131 | + (let ((buffer (get-buffer-create (or buffer-name "*LSP Lookup*")))) |
| 132 | + (with-current-buffer buffer |
| 133 | + (setq-local treemacs-default-visit-action 'treemacs-RET-action) |
| 134 | + (setq-local lsp-treemacs--right-click-actions right-click-actions) |
| 135 | + (setq-local face-remapping-alist '((button . default))) |
| 136 | + (setq-local window-size-fixed nil) |
| 137 | + (setq-local treemacs--width-is-locked nil) |
| 138 | + (setq-local treemacs-space-between-root-nodes nil) |
| 139 | + (lsp-treemacs--set-mode-line-format buffer title) |
| 140 | + (treemacs-initialize 'lsp-treemacs-generic-root |
| 141 | + (setq-local lsp-treemacs-tree tree)) |
| 142 | + (when treemacs-text-scale |
| 143 | + (text-scale-increase treemacs-text-scale)) |
| 144 | + (lsp-treemacs-generic-mode t) |
| 145 | + (when expand-depth (lsp-treemacs--expand 'lsp-treemacs-generic-root expand-depth)) |
| 146 | + (current-buffer)))) |
| 147 | + |
| 148 | +(provide 'lsp-treemacs-generic) |
| 149 | +;;; lsp-treemacs-generic.el ends here |
0 commit comments