|
7 | 7 | ;; Description: Eldoc support for meta-net |
8 | 8 | ;; Keyword: eldoc c# dotnet sdk |
9 | 9 | ;; Version: 0.1.0 |
10 | | -;; Package-Requires: ((emacs "24.3") (meta-net "1.1.0")) |
| 10 | +;; Package-Requires: ((emacs "25.1") (meta-net "1.1.0")) |
11 | 11 | ;; URL: https://github.com/emacs-vs/eldoc-meta-net |
12 | 12 |
|
13 | 13 | ;; This file is NOT part of GNU Emacs. |
|
32 | 32 |
|
33 | 33 | ;;; Code: |
34 | 34 |
|
| 35 | +(require 'subr-x) |
| 36 | + |
35 | 37 | (require 'eldoc) |
36 | 38 | (require 'meta-net) |
37 | 39 |
|
|
41 | 43 | :group 'tool |
42 | 44 | :link '(url-link :tag "Repository" "https://github.com/emacs-vs/eldoc-meta-net")) |
43 | 45 |
|
| 46 | +;; |
| 47 | +;; (@* "Util" ) |
| 48 | +;; |
| 49 | + |
| 50 | +(defun eldoc-meta-net--inside-comment-p () |
| 51 | + "Return non-nil if it's inside comment." |
| 52 | + (nth 4 (syntax-ppss))) |
| 53 | + |
| 54 | +;; |
| 55 | +;; (@* "Core" ) |
| 56 | +;; |
| 57 | + |
| 58 | +(defun eldoc-meta-net--possible-function-point () |
| 59 | + "This function get called infront of the opening curly bracket. |
| 60 | +
|
| 61 | +For example, |
| 62 | +
|
| 63 | + SomeFunction<TypeA, TypeB>(a, b, c); |
| 64 | + ^ |
| 65 | +
|
| 66 | +This function also ignore generic type between < and >." |
| 67 | + (let ((start (point)) |
| 68 | + (normal (save-excursion (forward-symbol -1) (point))) |
| 69 | + (generic (save-excursion (search-backward ">" nil t) |
| 70 | + (forward-char 1) |
| 71 | + (forward-sexp -1) |
| 72 | + (forward-symbol -1) |
| 73 | + (point))) |
| 74 | + result search-pt) |
| 75 | + (when (<= generic (line-beginning-position)) |
| 76 | + (setq generic nil)) |
| 77 | + ;; Make sure the result is number to avoid error |
| 78 | + (setq normal (or normal (point)) |
| 79 | + generic (or generic (point)) |
| 80 | + result (min normal generic)) |
| 81 | + (goto-char result) ; here suppose to be the start of the function name |
| 82 | + ;; We check to see if there is comma right behind the symbol |
| 83 | + (save-excursion |
| 84 | + (forward-symbol 1) |
| 85 | + (setq search-pt (point)) |
| 86 | + (when (and (re-search-forward "[^,]*" nil t) |
| 87 | + (string-empty-p (string-trim (buffer-substring search-pt (point))))) |
| 88 | + (setq result start))) |
| 89 | + (goto-char result))) |
| 90 | + |
| 91 | +(defun eldoc-meta-net--function-at-point () |
| 92 | + "Return the function name at point." |
| 93 | + (let* ((right 0) (left 0)) |
| 94 | + (while (and (<= left right) (re-search-backward "\\((\\|)\\)" nil t)) |
| 95 | + (if (equal (buffer-substring-no-properties (point) (+ (point) 1)) "(") |
| 96 | + (setq left (+ left 1)) |
| 97 | + (setq right (+ right 1)))) |
| 98 | + (while (equal (buffer-substring-no-properties (- (point) 1) (point)) " ") |
| 99 | + (goto-char (- (point) 1)))) |
| 100 | + (eldoc-meta-net--possible-function-point) |
| 101 | + (unless (eldoc-meta-net--inside-comment-p) (thing-at-point 'symbol))) |
| 102 | + |
| 103 | +(defun eldoc-meta-net--arg-string () |
| 104 | + "Return argument string." |
| 105 | + (when (search-forward "(" nil t) |
| 106 | + (forward-char -1) |
| 107 | + (let ((beg (point)) arg-string) |
| 108 | + (forward-sexp 1) |
| 109 | + (setq arg-string (buffer-substring-no-properties beg (point))) |
| 110 | + ;; Here we remove everything inside nested arguments |
| 111 | + ;; |
| 112 | + ;; For example, |
| 113 | + ;; |
| 114 | + ;; Add(a, b, (x, y, z) => { }, c) |
| 115 | + ;; |
| 116 | + ;; should return, |
| 117 | + ;; |
| 118 | + ;; (a, b, => { }, c) |
| 119 | + ;; |
| 120 | + ;; Of course, if you are inside the x y z scope then it would just |
| 121 | + ;; return (x, y, z). This is fine since ElDoc should just only need |
| 122 | + ;; to know the first layer's function. |
| 123 | + (with-temp-buffer |
| 124 | + (insert arg-string) |
| 125 | + (goto-char (1+ (point-min))) |
| 126 | + (while (search-forward "(" nil t) |
| 127 | + (forward-char -1) |
| 128 | + (let ((start (point))) |
| 129 | + (forward-sexp 1) |
| 130 | + (delete-region start (point)))) |
| 131 | + (buffer-string))))) |
| 132 | + |
| 133 | +(defun eldoc-meta-net-function () |
| 134 | + "Main eldoc entry." |
| 135 | + (save-excursion |
| 136 | + (when-let ((function-name (eldoc-meta-net--function-at-point)) |
| 137 | + (arg-string (eldoc-meta-net--arg-string))) |
| 138 | + (jcs-print function-name) |
| 139 | + (jcs-print arg-string) |
| 140 | + ))) |
| 141 | + |
| 142 | +(defun eldoc-meta-net--turn-on () |
| 143 | + "Start the `eldoc-meta-net' worker." |
| 144 | + (unless meta-net-csproj-current (meta-net-read-project)) |
| 145 | + (add-function :before-until (local 'eldoc-documentation-function) #'eldoc-meta-net-function) |
| 146 | + (eldoc-mode 1)) |
| 147 | + |
| 148 | +;;;###autoload |
| 149 | +(defun eldoc-meta-net-enable () |
| 150 | + "Turn on `eldoc-meta-net'." |
| 151 | + (interactive) |
| 152 | + (add-hook 'csharp-mode-hook #'eldoc-meta-net--turn-on) |
| 153 | + (add-hook 'csharp-tree-sitter-mode #'eldoc-meta-net--turn-on)) |
| 154 | + |
| 155 | +;;;###autoload |
| 156 | +(defun eldoc-meta-net-disable () |
| 157 | + "Turn off `eldoc-meta-net'." |
| 158 | + (interactive) |
| 159 | + (remove-hook 'csharp-mode-hook #'eldoc-meta-net--turn-on) |
| 160 | + (remove-hook 'csharp-tree-sitter-mode #'eldoc-meta-net--turn-on)) |
| 161 | + |
44 | 162 | (provide 'eldoc-meta-net) |
45 | 163 | ;;; eldoc-meta-net.el ends here |
0 commit comments