Skip to content

Commit 047c2cb

Browse files
committed
Add doc
1 parent e2e6315 commit 047c2cb

File tree

1 file changed

+66
-34
lines changed

1 file changed

+66
-34
lines changed

eldoc-meta-net.el

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
;;; Code:
3434

35+
(require 'pcase)
3536
(require 'subr-x)
3637

3738
(require 'eldoc)
@@ -88,55 +89,86 @@ This function also ignore generic type between < and >."
8889
(setq result start)))
8990
(goto-char result)))
9091

91-
(defun eldoc-meta-net--function-at-point ()
92+
(defun eldoc-meta-net--function-name ()
9293
"Return the function name at point."
9394
(let* ((right 0) (left 0))
9495
(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)) "(")
9697
(setq left (+ left 1))
9798
(setq right (+ right 1))))
98-
(while (equal (buffer-substring-no-properties (- (point) 1) (point)) " ")
99+
(while (equal (buffer-substring (- (point) 1) (point)) " ")
99100
(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))))
102104

103105
(defun eldoc-meta-net--arg-string ()
104106
"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)))
132160

133161
(defun eldoc-meta-net-function ()
134162
"Main eldoc entry."
135163
(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)))
138168
(jcs-print function-name)
139169
(jcs-print arg-string)
170+
(jcs-print arg-bounds)
171+
(jcs-print arg-count)
140172
)))
141173

142174
(defun eldoc-meta-net--turn-on ()

0 commit comments

Comments
 (0)