|
32 | 32 |
|
33 | 33 | ;;; Code: |
34 | 34 |
|
| 35 | +(require 'pcase) |
35 | 36 | (require 'subr-x) |
36 | 37 |
|
37 | 38 | (require 'eldoc) |
@@ -88,55 +89,86 @@ This function also ignore generic type between < and >." |
88 | 89 | (setq result start))) |
89 | 90 | (goto-char result))) |
90 | 91 |
|
91 | | -(defun eldoc-meta-net--function-at-point () |
| 92 | +(defun eldoc-meta-net--function-name () |
92 | 93 | "Return the function name at point." |
93 | 94 | (let* ((right 0) (left 0)) |
94 | 95 | (while (and (<= left right) (re-search-backward "\\((\\|)\\)" nil t)) |
95 | | - (if (equal (buffer-substring-no-properties (point) (+ (point) 1)) "(") |
| 96 | + (if (equal (buffer-substring (point) (+ (point) 1)) "(") |
96 | 97 | (setq left (+ left 1)) |
97 | 98 | (setq right (+ right 1)))) |
98 | | - (while (equal (buffer-substring-no-properties (- (point) 1) (point)) " ") |
| 99 | + (while (equal (buffer-substring (- (point) 1) (point)) " ") |
99 | 100 | (goto-char (- (point) 1)))) |
100 | | - (eldoc-meta-net--possible-function-point) |
101 | | - (unless (eldoc-meta-net--inside-comment-p) (thing-at-point 'symbol))) |
| 101 | + (save-excursion |
| 102 | + (eldoc-meta-net--possible-function-point) |
| 103 | + (unless (eldoc-meta-net--inside-comment-p) (thing-at-point 'symbol)))) |
102 | 104 |
|
103 | 105 | (defun eldoc-meta-net--arg-string () |
104 | 106 | "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))))) |
| 107 | + (save-excursion |
| 108 | + (when (search-forward "(" nil t) |
| 109 | + (forward-char -1) |
| 110 | + (let ((beg (point)) arg-string) |
| 111 | + (forward-sexp 1) |
| 112 | + (setq arg-string (buffer-substring beg (point))) |
| 113 | + ;; Here we remove everything inside nested arguments |
| 114 | + ;; |
| 115 | + ;; For example, |
| 116 | + ;; |
| 117 | + ;; Add(a, b, (x, y, z) => { }, c) |
| 118 | + ;; |
| 119 | + ;; should return, |
| 120 | + ;; |
| 121 | + ;; (a, b, => , c) |
| 122 | + ;; |
| 123 | + ;; Of course, if you are inside the x y z scope then it would just |
| 124 | + ;; return (x, y, z). This is fine since ElDoc should just only need |
| 125 | + ;; to know the first layer's function. |
| 126 | + (with-temp-buffer |
| 127 | + (insert arg-string) |
| 128 | + (goto-char (1+ (point-min))) |
| 129 | + (while (re-search-forward "[({]" nil t) |
| 130 | + (forward-char -1) |
| 131 | + (let ((start (point))) |
| 132 | + (forward-sexp 1) |
| 133 | + (delete-region start (point)))) |
| 134 | + (buffer-string)))))) |
| 135 | + |
| 136 | +(defun eldoc-meta-net--arg-boundaries () |
| 137 | + "Return a list of cons cell represent arguments' boundaries. |
| 138 | +
|
| 139 | +The start boundary should either behind of `(` or `,`; the end boundary should |
| 140 | +either infront of `,` or `)`. |
| 141 | +
|
| 142 | +For example, (^ is start; $ is end) |
| 143 | +
|
| 144 | + Add(var1, var2, var3) |
| 145 | + ^ $ ^ $ ^ $ |
| 146 | +
|
| 147 | +This function also get called infront of the opening curly bracket. See |
| 148 | +function `eldoc-meta-net--possible-function-point' for the graph." |
| 149 | + (let (boundaries start (max-pt (save-excursion (forward-sexp 1)))) |
| 150 | + (save-excursion |
| 151 | + (forward-char 1) |
| 152 | + (setq start (point)) |
| 153 | + (while (re-search-forward "[,{()]" max-pt t) |
| 154 | + (pcase (string (char-before)) |
| 155 | + ((or "," ")") |
| 156 | + (push (cons start (1- (point))) boundaries) |
| 157 | + (setq start (point))) |
| 158 | + ((or "{" "(") (forward-char -1) (forward-sexp 1))))) |
| 159 | + (reverse boundaries))) |
132 | 160 |
|
133 | 161 | (defun eldoc-meta-net-function () |
134 | 162 | "Main eldoc entry." |
135 | 163 | (save-excursion |
136 | | - (when-let ((function-name (eldoc-meta-net--function-at-point)) |
137 | | - (arg-string (eldoc-meta-net--arg-string))) |
| 164 | + (when-let* ((function-name (eldoc-meta-net--function-name)) |
| 165 | + (arg-string (eldoc-meta-net--arg-string)) |
| 166 | + (arg-bounds (eldoc-meta-net--arg-boundaries)) ; list of cons cell |
| 167 | + (arg-count (length arg-bounds))) |
138 | 168 | (jcs-print function-name) |
139 | 169 | (jcs-print arg-string) |
| 170 | + (jcs-print arg-bounds) |
| 171 | + (jcs-print arg-count) |
140 | 172 | ))) |
141 | 173 |
|
142 | 174 | (defun eldoc-meta-net--turn-on () |
|
0 commit comments